Repository navigation
fix: authenticate API keys before skipping Origin checks - #14352
Conversation
Refuse invalid, unknown-prefix, and project-mismatched X-Appwrite-Key values instead of decoding them as guests. Skip Origin validation only when a key authenticates as ROLE_KEYS so a junk header cannot ride a browser session. Co-authored-by: Jake Barnby <[email protected]>
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Keep invalid-key refusal on API routes, but do not fail the request while resolving X-Appwrite-Key for Origin validation. GraphQL is not in the api group and can send a key without a project header. Co-authored-by: Jake Barnby <[email protected]>
Main added Key::find with constant-time secret comparison and a unit test asserting an empty key decodes as a guest. This branch refuses every key that does not authenticate, so the empty-key case moves into the invalid-key provider and expects user_unauthorized; the guarantee that an empty key never matches a stored empty secret still holds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Key::decode still returns expired stored keys so api routes can answer with project_key_expired, which meant an expired key also satisfied the Origin bypass. Only a key that is both a project key and unexpired now skips the CSRF check. The regression test also moves to the Utopia\Database Id, Permission and Role classes, since the Helpers namespace no longer exists after the database migration on main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: Jake Barnby <[email protected]>
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
This change makes invalid API key headers fail loudly rather than fall back to guest privileges, and adds test coverage for the new contract.
Reviewed commit: 703f8a9 |
|
@hansi-codes review |
🟢 Tier S · Ready to merge
This change makes Origin-check bypass depend on a decoded, unexpired API key and rejects non-empty invalid keys on API routes. Invalid key decoding now raises an authorization error, while the request resource treats that error as an absent key so Origin validation can proceed. Unit and E2E coverage exercises invalid, expired, mismatched, and valid key scenarios.
📂 Walkthrough · 6
Reviewed |
There was a problem hiding this comment.
PR hardens API key validation: Key::decode now throws USER_UNAUTHORIZED on invalid/expired/mismatched keys, the apiKey resource maps that to null, and requests carrying a bad x-appwrite-key header are rejected with 401 instead of silently degrading to guest access; new unit and e2e tests cover key-origin behavior.
Key findings
- 🟡 Invalid API key headers now hard-fail 401, no guest fallback — api.php:144
Co-authored-by: Jake Barnby <[email protected]>
|
@hansi-codes review |
…h-a7ce fix: authenticate API keys before skipping Origin checks (cherry picked from commit 5fbaf75)
…ort appwrite#14352) (cherry picked from commit 5fbaf75)
What does this PR do?
X-Appwrite-Keyno longer short-circuits Origin validation just because the header is non-empty, and invalid keys are no longer decoded as guests.ROLE_KEYSkey (valid standard, ephemeral, dynamic, or organization key).Key::decode()refuses invalid, unknown-prefix, expired-JWT, and project-mismatched secrets withuser_unauthorizedinstead of returning a guest key that left the session user in place.apiKeyresource treats a refused decode as "no key" so Origin validation can still run. API routes (groups(['api'])) then refuse a non-emptyX-Appwrite-Keythat did not authenticate. GraphQL is not in theapigroup and may send a key withoutx-appwrite-project.Authorization::setDefaultStatus(false)stays gated on authenticatedROLE_KEYSwith a matching project (unchanged).Test Plan
tests/unit/Auth/KeyTest.phpuser_unauthorized.tests/e2e/General/KeyOriginTest.phpgeneral_unknown_origin).X-Appwrite-Keyis rejected (general_unknown_origin) and does not persist a document.general_unknown_origin).X-Appwrite-Keywithout Origin is rejected (user_unauthorized).Related PRs and Issues
Checklist