H3 · H30021

Check access before H3 validates the request

Flags a defineValidatedHandler or h3-route-tools defineEndpoint route that validates query, headers, or params and awaits an auth guard as the first statement of its handler, with no route middleware.
H30021vite-doctor/h3securitywarnSuggestion

Run this rule

pnpm vite-doctor . --rules h3/security/no-guard-after-validation

Why it matters

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.

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)
  },
})
Copyright © 2026