H3 · Security

H30021Check 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.

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
Copyright © 2026