Skip to content

docs(auth): order key-based modes before 'user' in all examples#76

Draft
tomaspozo wants to merge 2 commits into
mainfrom
docs/auth-mode-ordering
Draft

docs(auth): order key-based modes before 'user' in all examples#76
tomaspozo wants to merge 2 commits into
mainfrom
docs/auth-mode-ordering

Conversation

@tomaspozo
Copy link
Copy Markdown
Member

Documents a non-obvious auth-mode ordering rule and flips every combined-auth example to the safe order.

The problem

Behind Supabase's API gateway, an apikey request that omits the Authorization header does not reach the function header-less — the gateway injects an Authorization: Bearer <token> derived from the key (an anon token for a publishable key, a service_role token for a secret key). If a 'user' mode is listed before the key-based mode, it tries to verify that injected token, fails, and rejects with InvalidCredentialsError (401) before 'secret'/'publishable' is ever reached.

Confirmed by capturing the forwarded headers from a live local stack: apikey=secret + no Authorization → the function receives Authorization: Bearer <legacy HS256 service_role JWT>. A dummy non-Bearer header makes it pass; the absence is what triggers the injection.

This is a gateway behavior, not an SDK bug — no code change here. The rule: list key-based modes (secret/publishable) first, keep 'user' last (e.g. ['secret', 'user']).

What changed (docs + JSDoc only)

  • docs/auth-modes.md — new [!IMPORTANT] callout explaining the gateway injection + ordering rule; flipped examples.
  • docs/security.mdcorrected the claim that an absent Authorization header always falls through (true at the SDK level, not behind the gateway) and added the ordering guidance.
  • docs/api-reference.md, docs/core-primitives.md, and the h3 / hono / nestjs adapter guides — flipped examples + pointers.
  • README.md — flipped the dual-auth example and expanded the ordering note.
  • src/types.ts (WithSupabaseConfig.auth, AuthMode, AuthModeWithKey) and src/core/verify-credentials.ts JSDoc — added the rule so it surfaces in IntelliSense.

Unit tests are intentionally left on ['user', …] ordering — they exercise SDK-level logic where no gateway is involved. One deliberate anti-pattern example remains in the callout.

🤖 Generated with Claude Code

Behind Supabase's API gateway, an apikey request that omits the Authorization
header still reaches the function with a gateway-injected 'Bearer <token>'
(anon for a publishable key, service_role for a secret key). A 'user' mode
placed before the key-based mode then tries to verify that injected token,
fails, and rejects with InvalidCredentialsError before the apikey mode is
reached.

Document the rule — list 'secret'/'publishable' before 'user', keep 'user'
last — across auth-modes, security, api-reference, core-primitives, the adapter
guides, the README, and the withSupabase/verifyCredentials JSDoc, and flip every
combined-auth example to the safe ordering. Corrects the security.md claim that
an absent Authorization header always falls through (true at the SDK level, not
behind the gateway).
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jun 7, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@supabase/server@76

commit: 14ae41d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant