Skip to content

Commit a66f535

Browse files
committed
chore: store return URLs in session
Stores the return URL in the session rather than using cookies.
1 parent bd4b408 commit a66f535

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"nuxt": "4.3.0",
8484
"nuxt-og-image": "5.1.13",
8585
"ofetch": "1.5.1",
86+
"ohash": "2.0.11",
8687
"perfect-debounce": "2.1.0",
8788
"sanitize-html": "2.17.0",
8889
"semver": "7.7.3",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/api/auth/atproto.get.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default defineEventHandler(async event => {
6969

7070
if (!query.code) {
7171
// Validate returnTo is a safe relative path (prevent open redirect)
72-
// Only set cookie on initial auth request, not the callback
72+
// Store in session on initial auth request, not the callback
7373
let redirectPath = '/'
7474
try {
7575
const clientOrigin = new URL(clientUri).origin
@@ -81,12 +81,7 @@ export default defineEventHandler(async event => {
8181
// Invalid URL, fall back to root
8282
}
8383

84-
setCookie(event, 'auth_return_to', redirectPath, {
85-
maxAge: 60 * 5,
86-
httpOnly: true,
87-
// secure only if NOT in dev mode
88-
secure: !import.meta.dev,
89-
})
84+
await session.update({ returnTo: redirectPath })
9085
try {
9186
const handle = query.handle?.toString()
9287
const create = query.create?.toString()
@@ -148,8 +143,8 @@ export default defineEventHandler(async event => {
148143
})
149144
}
150145

151-
const returnToURL = getCookie(event, 'auth_return_to') || '/'
152-
deleteCookie(event, 'auth_return_to')
146+
const returnToURL = session.data.returnTo || '/'
147+
await session.update({ returnTo: undefined })
153148

154149
return sendRedirect(event, returnToURL)
155150
})

server/api/registry/badge/[type]/[...pkg].get.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as v from 'valibot'
2+
import { hash } from 'ohash'
23
import { createError, getRouterParam, getQuery, setHeader } from 'h3'
34
import { PackageRouteParamsSchema } from '#shared/schemas/package'
45
import { CACHE_MAX_AGE_ONE_HOUR, ERROR_NPM_FETCH_FAILED } from '#shared/utils/constants'
@@ -11,11 +12,13 @@ const OSV_QUERY_API = 'https://api.osv.dev/v1/query'
1112
const BUNDLEPHOBIA_API = 'https://bundlephobia.com/api/size'
1213
const NPMS_API = 'https://api.npms.io/v2/package'
1314

15+
const SafeStringSchema = v.pipe(v.string(), v.regex(/^[^<>"&]*$/, 'Invalid characters'))
16+
1417
const QUERY_SCHEMA = v.object({
15-
color: v.optional(v.string()),
18+
color: v.optional(SafeStringSchema),
1619
name: v.optional(v.string()),
17-
labelColor: v.optional(v.string()),
18-
label: v.optional(v.string()),
20+
labelColor: v.optional(SafeStringSchema),
21+
label: v.optional(SafeStringSchema),
1922
})
2023

2124
const COLORS = {
@@ -338,7 +341,7 @@ export default defineCachedEventHandler(
338341
const type = getRouterParam(event, 'type') ?? 'version'
339342
const pkg = getRouterParam(event, 'pkg') ?? ''
340343
const query = getQuery(event)
341-
return `badge:${type}:${pkg}:${JSON.stringify(query)}`
344+
return `badge:${type}:${pkg}:${hash(query)}`
342345
},
343346
},
344347
)

shared/types/userSession.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ export interface UserServerSession {
1212
// multiple did logins per server session
1313
oauthSession: NodeSavedSession | undefined
1414
oauthState: NodeSavedState | undefined
15+
// Temporary storage for post-auth redirect path during OAuth flow
16+
returnTo?: string
1517
}

0 commit comments

Comments
 (0)