H3 · Security
vite-doctor/h3securitywarnSuggestion
Why it happens
H3 validates query and headers, and h3-route-tools also validates params, before it calls the handler. Validation can reject a guest's request (400 by default) and disclose schema details before the guard runs.
Fix
Run the guard in middleware before validation. For method-key endpoints, use the method's own middleware. Preserve the guard's arguments and await it in a block-bodied onRequest callback without returning the session: middleware: onRequest(async (event) => { await requireUserSession(event); }). If the guard needs validated data, split out the access check that can run before validation.
Example
Check access in route middleware
Before
export default defineValidatedHandler({
validate: { query: InboxQuery },
handler: async (event) => {
const { user } = await requireUserSession(event)
return listInbox(user.id)
},
})
After
export default defineValidatedHandler({
middleware: [onRequest(async (event) => { await requireUserSession(event) })],
validate: { query: InboxQuery },
handler: async (event) => {
const { user } = await requireUserSession(event)
return listInbox(user.id)
},
})
Verify the fix
Run only this rule after editing so the report stays focused on the diagnostic you are closing:
pnpm vite-doctor . --rules h3/security/no-guard-after-validation