Skip to content

fix: authenticate API keys before skipping Origin checks - #14352

Merged
abnegate merged 8 commits into
mainfrom
cursor/key-origin-auth-a7ce
Oct 22, 2026
Merged

abnegate merged 8 commits into
mainfrom
cursor/key-origin-auth-a7ce

Conversation

@abnegate

@abnegate abnegate commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

X-Appwrite-Key no longer short-circuits Origin validation just because the header is non-empty, and invalid keys are no longer decoded as guests.

  • Origin validation is skipped only when the request has no Origin (server-side) or when the key authenticates as an unexpired ROLE_KEYS key (valid standard, ephemeral, dynamic, or organization key).
  • Key::decode() refuses invalid, unknown-prefix, expired-JWT, and project-mismatched secrets with user_unauthorized instead of returning a guest key that left the session user in place.
  • The apiKey resource treats a refused decode as "no key" so Origin validation can still run. API routes (groups(['api'])) then refuse a non-empty X-Appwrite-Key that did not authenticate. GraphQL is not in the api group and may send a key without x-appwrite-project.
  • Authorization::setDefaultStatus(false) stays gated on authenticated ROLE_KEYS with a matching project (unchanged).

Test Plan

  • Unit: tests/unit/Auth/KeyTest.php
    • Valid standard / ephemeral / account / organization keys still decode.
    • Invalid ephemeral, dynamic, standard, account, organization, unknown-prefix, random, and project-mismatched keys throw user_unauthorized.
  • E2E: tests/e2e/General/KeyOriginTest.php
    • Session + allowed Origin can create a document.
    • Valid API key can create a document without Origin.
    • Session + disallowed Origin is rejected (general_unknown_origin).
    • Session + disallowed Origin + junk X-Appwrite-Key is rejected (general_unknown_origin) and does not persist a document.
    • Session + disallowed Origin + expired API key is rejected (general_unknown_origin).
    • Session + junk X-Appwrite-Key without Origin is rejected (user_unauthorized).

Related PRs and Issues

  • N/A

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?
Open in Web Open in Cursor 

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]>
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → cursor/key-origin-auth-a7ce (after).

Metric Before After Change
🚀 Requests/sec 219.32 220.17 ⚪ +0.4%
⏱️ Latency P50 77.12 ms 78.91 ms ⚪ +2.3%
⏱️ Latency P95 193.15 ms 180.47 ms 🟢 -6.6%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 78.91 180.47 13,737 220.17 -12.68
Account 150.41 278.38 723 12.04 -11.6
TablesDB 77.36 148.03 7,471 121.17 -3.55
Storage 70.54 163.7 3,615 60.56 +2.63
Functions 106.55 200.52 1,928 32.96 -23.9

Top API waits (after)

API request Max wait (ms)
account.name.update 384.15
account.prefs.update 379.09
tablesdb.rows.create 362.11
functions.variables.create 336.19
tablesdb.rows.update 312.15

cursoragent and others added 6 commits October 12, 2026 05:37
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]>
@abnegate
abnegate marked this pull request as ready for review October 22, 2026 05:23
@luxor-code-review

luxor-code-review Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Invalid API key headers now hard-fail 401, no guest fallback — api.php:144

This change makes invalid API key headers fail loudly rather than fall back to guest privileges, and adds test coverage for the new contract.

Files Change
src/Appwrite/Auth/Key.php decode throws USER_UNAUTHORIZED for invalid, expired-JWT, unknown-prefix, or project-mismatched keys instead of returning a guest key; expiry flagging refined.
app/init/resources/request.php apiKey resource catches USER_UNAUTHORIZED from decode and resolves to null, leaving other exceptions to propagate.
app/controllers/shared/api.php Auth hook rejects requests whose x-appwrite-key header yields a null key with 401, removing the silent guest fallback.
app/controllers/general.php CSRF/origin handling tightened around key-authenticated requests.
tests/unit/Auth/KeyTest.php, tests/e2e/General/KeyOriginTest.php Unit tests for the new decode throw paths and an e2e test asserting 401-vs-403 behavior across client modes.

Reviewed commit: 703f8a9

@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The changed authentication and Origin-check paths appear consistent, and the added tests cover the key rejection and bypass cases.

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.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 6
File Change
app/controllers/general.php Resolves the API key for Origin validation and skips the check only for an unexpired key with the key role.
app/controllers/shared/api.php Rejects API requests with a non-empty key header that did not authenticate.
app/init/resources/request.php Treats unauthorized key decoding as an absent key so downstream Origin and API authentication checks can run.
src/Appwrite/Auth/Key.php Refuses invalid, expired-JWT, unknown-prefix, and project-mismatched secrets instead of producing a guest key.
tests/e2e/General/KeyOriginTest.php Covers Origin validation and document persistence for session, valid-key, invalid-key, and expired-key requests.
tests/unit/Auth/KeyTest.php Covers unauthorized decoding for invalid key types, project mismatch, empty input, and expired ephemeral keys.

Reviewed 9b23be7 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost 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.

🟢 Tier S · Looks good to merge. Summary

@luxor-code-review luxor-code-review 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.

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

Comment thread app/controllers/shared/api.php Resolved

@ghost ghost 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.

🟢 Tier S · Looks good to merge. Summary

Co-authored-by: Jake Barnby <[email protected]>
@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@ghost ghost 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.

🟢 Tier S · Looks good to merge. Summary

@abnegate
abnegate merged commit 5fbaf75 into main Oct 22, 2026
68 checks passed
@abnegate
abnegate deleted the cursor/key-origin-auth-a7ce branch October 22, 2026 06:02
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…h-a7ce

fix: authenticate API keys before skipping Origin checks
(cherry picked from commit 5fbaf75)
sarbojitrana pushed a commit to sarbojitrana/appwrite that referenced this pull request Nov 2, 2026
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.

2 participants