fix(api): CORS credential leak + empty-scopes privilege escalation#376
Conversation
…ials in CORS ALLOWED_ORIGINS hardcoded localhost/127.0.0.1 entries that were also served in production (single Worker, no per-env origin list), letting any page open on those loopback ports make credentialed cross-origin calls to the production API. The handler also answered "*" for a missing Origin and reflected ALLOWED_ORIGINS[0] for a disallowed Origin — both invalid/misleading when combined with Access-Control-Allow-Credentials: true. resolveAllowedOrigin() (lib/cors.ts) now only allows localhost origins when the Worker's ENVIRONMENT binding is not "production", and returns null (no header) for a missing or disallowed origin instead of "*" or a fallback origin. ENVIRONMENT=production is stamped only into the generated deploy config (prepare-wrangler-deploy-config.sh), so local `wrangler dev` and tests are unaffected. Closes #345 Co-Authored-By: Duyet Le <me@duyet.net> Co-Authored-By: duyetbot <bot@duyet.net>
buildApiKey collapsed scopes: [] to null in the DB column, and
effectiveKeyScopes(null) resolves to full access ("*") — the opposite of "no
permissions". createApiKey's response shaping had the same collapse. The
delegation guards in routes/keys.ts and routes/v1-keys.ts also used a truthy
check (if (childScopes)), which is fine for `[]` itself (arrays are always
truthy) but didn't make the undefined-vs-empty-array distinction explicit at
the call site.
Only `scopes === undefined` (omitted entirely) now maps to null/full access;
an explicit [] is persisted and reported as an empty scope set. This is not
currently reachable over HTTP (CreateApiKeySchema.scopes has .min(1)), but the
lib/service layer is the security-relevant unit per the audit — any future
caller (internal job, new route, MCP tool) that reaches createApiKey/
buildApiKey with [] must get a zero-permission key, not a god key. Legacy/
unscoped keys (scopes column null or absent) keep full access unchanged.
Closes #346
Co-Authored-By: Duyet Le <me@duyet.net>
Co-Authored-By: duyetbot <bot@duyet.net>
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
Warning Review limit reached
Next review available in: 43 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (10)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Two security fixes, each in its own commit:
#345 — CORS reflects credentials for localhost origins in production, invalid wildcard-with-credentials
ALLOWED_ORIGINShardcoded localhost/127.0.0.1 entries that were also served in production (single Worker, no per-env origin list). Any page open on those loopback ports could make credentialed cross-origin calls to the production API.Access-Control-Allow-Origin: *for a missingOrigin, which is invalid alongsideAccess-Control-Allow-Credentials: trueand rejected by browsers.ALLOWED_ORIGINS[0](https://agentstate.app) instead of omitting the header — misleading, since the response advertised an allow-origin that didn't match the request.resolveAllowedOrigin()(packages/api/src/lib/cors.ts) returnsnull(no header) for a missing/disallowed origin, and only allows localhost origins when the Worker'sENVIRONMENTbinding isn't"production".ENVIRONMENT=productionis stamped only into the generated deploy config (prepare-wrangler-deploy-config.sh), so localwrangler devand tests are unaffected.#346 — Empty scopes array mints a full-access API key (privilege-escalation landmine)
buildApiKeystoredscopes: scopes && scopes.length > 0 ? JSON.stringify(scopes) : null. An explicit empty array ([]) collapsed tonull, andeffectiveKeyScopes(null)resolves to full access (*) — the opposite of "no permissions".services/keys.ts's response shaping had the identical bug.routes/keys.tsandroutes/v1-keys.tsusedif (childScopes); changed toif (childScopes !== undefined)to make the undefined-vs-explicit-[]distinction explicit at the call site (functionally a no-op today since arrays are always truthy, but removes the ambiguity flagged by the audit).scopes === undefined(the field omitted entirely) now maps tonull/full access. Legacy/unscoped keys (columnnull/absent) are unaffected and keep full access.CreateApiKeySchema.scopeshas.min(1), unchanged), but the lib/service layer is the security-relevant unit — any future caller (internal job, new route, MCP tool) that reachescreateApiKey/buildApiKeywith[]now gets a zero-permission key instead of a god key.Closes #345
Closes #346
Test plan
packages/api/test/cors.test.ts(unit tests forresolveAllowedOrigincovering missing/disallowed/production/dev-localhost cases, plus liveSELF.fetchassertions that disallowed/missing origins get noAccess-Control-Allow-Originheader) — all fail against the pre-fix code.packages/api/test/scopes.test.ts(buildApiKey/createApiKeywithundefinedvs. explicit[]scopes) — fail against the pre-fix code (previously resolved tonull/full access).bunx biome check packages/api/src/— cleanbunx tsc --noEmit -p packages/api/tsconfig.json— cleancd packages/api && bunx vitest run— 366/366 passed (26 test files)