Repository navigation
fix(ai): use the shared requireUser auth check - #88
Conversation
The ai Edge Function parsed the Bearer token and looked up the user with its own copy of the logic in _shared/auth.ts. The copy had drifted: it did not trim the token, sent an empty token to Auth, and returned different error strings from account, announcements and workspace-invites. ai now calls requireUser and returns its HttpError through jsonError. Missing or empty tokens get 401 "Authentication required" without an Auth round trip; rejected tokens get 401 "Invalid authentication token". The client maps 401 by status, so its behavior is unchanged. A new handler test covers the missing, empty and rejected cases. Tests that import ai/index.ts now set SUPABASE_ANON_KEY, which _shared/auth.ts requires at load. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HyWuBiDEMvVnjmguH9CwCZ
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Free Run ID: 📒 Files selected for processing (5)
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe AI handler now uses shared authentication helpers to obtain the user ID and format authentication errors. Tests configure ChangesAI request authentication
Estimated code review effort: 2 (Simple) | ~10 minutes Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change centralizes authentication without an observed bypass or broader access. Deployment configuration remains unverified, so the assessment retains some uncertainty. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Note 🎁 Summarized by CodeRabbit FreeYour organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Essentials by visiting https://app.coderabbit.ai/settings/billing. Comment |
What changed
The
aiEdge Function had its own copy of the Bearer parsing and user lookup that_shared/auth.tsalready provides. It now callsrequireUserand returns itsHttpErrorthroughjsonError, the same wayaccount,announcementsandworkspace-invitesdo. An ultrareview of the logic slice flagged the duplicate (supabase/functions/ai/index.ts:277-294).The copy had already drifted:
Authorization: Bearerwent to Supabase Auth instead of being rejected locally."Unauthorized"and"Invalid token". They are now"Authentication required"and"Invalid authentication token", matching the other functions.The client (
mapAIError) maps auth failures by HTTP status (401), and the status is unchanged, so users see the same sign-in-again message._shared/auth.tsreadsSUPABASE_ANON_KEYwhen it loads. Hosted Edge Functions get that variable by default, andaccountalready depends on it in production. The four test files that importai/index.tsnow set it.Verification (local, at 8f489a8)
ai/handler-research.test.tscovers a missing header, an empty bearer token and a rejected token. It failed before the fix ("Unauthorized") and passes after.npm run test:edge: 326 passed.npm test: 1208 passed.npm run lint,npm run typecheck,npm run docs:checkandnpm run buildall pass.verify-debatelab):recipes/ai-toolspassed 12/12 on desktop-chromium and mobile-chromium.aifunction returned 401 withAuthentication required(no header),Authentication required(empty token) andInvalid authentication token(bogus token).Deploy note
The merge ships no frontend change. After merge,
aimust be redeployed (supabase functions deploy ai) for the change to reach production.Follow-up, not in this PR
admin,ai-stream,create-checkout,create-portal,export-gdoc,import-source,redeem-promo,shareandvoicestill have their own auth copies. Moving them torequireUserchanges nine functions' error contracts, so it should be a separate PR.🤖 Generated with Claude Code
https://claude.ai/code/session_01HyWuBiDEMvVnjmguH9CwCZ
Summary by CodeRabbit