fix: enforce scoped subkey context on middlewareKey routes#1947
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
📝 WalkthroughWalkthroughThe changes implement parent API key and subkey context management in the Hono middleware layer. A new Changes
Sequence DiagramsequenceDiagram
participant Client
participant Middleware as Hono Middleware
participant Auth as Auth Context
participant WebhookCheck as checkWebhookPermissionV2
participant Policy as RLS Policy
Client->>Middleware: Request with apikey + x-limited-key-id header
activate Middleware
Middleware->>Auth: Authenticate parent API key
Auth-->>Middleware: Parent key row
Middleware->>Middleware: Store parent in c.parentApikey
Middleware->>Auth: Lookup subkey by x-limited-key-id
Auth-->>Middleware: Subkey row
Middleware->>Middleware: Write subkey to c.apikey<br/>Update c.capgkey with subkey secret
deactivate Middleware
Client->>WebhookCheck: Request webhook permission check
activate WebhookCheck
WebhookCheck->>WebhookCheck: Read parentApikey from context
WebhookCheck->>Policy: Check org access using parentApikey
Policy-->>WebhookCheck: Permission result
WebhookCheck-->>Client: Permission granted/denied
deactivate WebhookCheck
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly Related PRs
Suggested Labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
|
@CodeRabbit review |
✅ Actions performedReview triggered.
|



Summary (AI generated)
middlewareKeyexpose the effective subkey as the request API key context whenx-limited-key-idis used/organizationand/organization/membersMotivation (AI generated)
middlewareKeyauthenticated the parent key correctly, but downstream handlers still read the parent key from context after a scoped subkey was attached. That let scoped subkey requests behave like the unrestricted parent key on middlewareKey-protected routes.Business Impact (AI generated)
This closes a high-severity authorization bypass in the public API surface. Scoped keys used in CI/CD, partner integrations, and delegated automation now respect their intended org limits, which reduces tenant-isolation risk and protects customer trust.
Test Plan (AI generated)
bunx eslint supabase/functions/_backend/utils/hono.ts supabase/functions/_backend/utils/hono_middleware.ts supabase/functions/_backend/public/webhooks/index.ts tests/organization-api.test.tsbun run supabase:db:resetbun run supabase:with-env -- bunx vitest run tests/organization-api.test.ts tests/webhooks-apikey-policy.test.tsGenerated with AI
Summary by CodeRabbit
Bug Fixes
Tests