Skip to content

perf: skip the proxy session-refresh getUser for API routes (round 2, phase 3 — auth)#481

Merged
BigSimmo merged 3 commits into
claude/perf-r2-hot-pathfrom
claude/perf-r2-auth-roundtrip
Jul 11, 2026
Merged

perf: skip the proxy session-refresh getUser for API routes (round 2, phase 3 — auth)#481
BigSimmo merged 3 commits into
claude/perf-r2-hot-pathfrom
claude/perf-r2-auth-roundtrip

Conversation

@BigSimmo

Copy link
Copy Markdown
Owner

Summary

Security-relevant change — please review with the tenancy defense-in-depth lens (docs/tenancy-defense-in-depth-review.md). Isolated in its own PR for that reason (stacked on #480).

Every authenticated request paid two serial auth-server (GoTrue) round trips: src/proxy.ts ran supabase.auth.getUser() for every matched path including /api/*, then the route handler independently validated the caller via getOptionalAuthenticatedUser. On API routes nothing consumed the proxy's result — it existed only to refresh session cookies.

The proxy now skips only the session-refresh getUser for /api/* paths.

Why this does not weaken auth

  • No authorization decision moves. API handlers keep validating the caller themselves (bearer token and/or @supabase/ssr cookie), fail-closed, exactly as before. The proxy's getUser result was never consulted for access on API routes.
  • CSP unchanged. The matcher still covers /api/*; every response still carries the per-request nonce CSP.
  • Session freshness preserved. Cookies still refresh on every page navigation; the browser Supabase client auto-refreshes its own token for bearer-based API calls. (Handler-side cookie validation has always been read-only — its setAll is a no-op — so API-only traffic never persisted refreshed cookies even before this change.)

Rejected alternative

A trusted-internal-header design (proxy validates once, forwards the user id, handlers trust it) was considered and rejected: independent handler validation is the stronger defense-in-depth posture, and header-trust introduces spoofing surface.

Explicitly untouched

The legacy cookie-token extraction in supabase/auth.ts (sb-access-token, plain-JSON auth cookies) was flagged as possibly dead during exploration, but private-access-routes tests define it as supported behavior (including stale-bearer → valid-cookie recovery), so it stays.

Impact

Removes one serial GoTrue network round trip from every authenticated API request — answers, typeahead, documents, registry. This is the highest-frequency single win in the round; it also reduces pressure on the documented auth connection-cap failure mode (docs/capacity-review.md).

Verification

  • New tests/proxy-session-refresh.test.ts pins the contract: getUser skipped for /api/* (CSP still stamped), called once on page navigations with an sb- cookie, never called without one
  • Existing proxy CSP nonce tests unchanged and green; public-access-deep + private-access-routes suites green (106 tests)
  • tsc, eslint, prettier clean

🤖 Generated with Claude Code

Round-2 phase 3, auth part (isolated for security review). Every
authenticated request previously paid two serial auth-server round trips:
src/proxy.ts ran supabase.auth.getUser() for every matched path including
/api/*, and the route handler then independently validated the caller via
getOptionalAuthenticatedUser. Nothing consumed the proxy's result on API
routes — the call existed only to keep session cookies fresh.

The proxy now skips only the session-refresh getUser for /api/* paths:

- API handlers keep validating the caller themselves (bearer token and/or
  @supabase/ssr cookie) — no authorization decision moves or weakens, and the
  handlers remain fail-closed exactly as before.
- The CSP nonce header still applies to every matched response including
  /api/* (matcher unchanged).
- Session cookies still refresh on every page navigation, and the browser
  Supabase client refreshes its own token (autoRefreshToken) for bearer-based
  API calls, so long-lived sessions behave as before.

Deliberately NOT done: the trusted-internal-header design (proxy forwards a
validated user id for handlers to trust) was rejected — handlers validating
tokens independently is the stronger defense-in-depth posture. The legacy
cookie-token extraction in supabase/auth.ts was left untouched: tests define
legacy sb-access-token / plain-JSON cookie support as intended behavior.

New tests pin the contract: getUser is skipped for /api/* (CSP still
stamped), runs on page navigations with an sb- cookie, and never runs without
one. Existing CSP nonce tests unchanged and green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@supabase

supabase Bot commented Jul 11, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project sjrfecxgysukkwxsowpy because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 72817b95-7de3-4623-ada6-d589a7211fa1

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/perf-r2-auth-roundtrip
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch claude/perf-r2-auth-roundtrip

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 82376e73f1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/proxy.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a21a2a9690

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/lib/answer-client-payload.ts Outdated
@BigSimmo
BigSimmo merged commit 843dcf8 into claude/perf-r2-hot-path Jul 11, 2026
3 checks passed
@BigSimmo
BigSimmo deleted the claude/perf-r2-auth-roundtrip branch July 13, 2026 16:35
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