Skip to content

Hold keys and JWTs to their original grant - #14888

Merged
Meldiron merged 4 commits into
mainfrom
fix/keys-within-their-grant
Oct 23, 2026
Merged

Meldiron merged 4 commits into
mainfrom
fix/keys-within-their-grant

Conversation

@Meldiron

Copy link
Copy Markdown
Contributor

Summary

Keys and JWTs can no longer hand out more than they hold. A credential stays limited to its original setup: anything it creates or changes gets no scope it doesn't hold and no expiry later than its own, and a JWT can't create keys or the next JWT.

One commit per change:

  1. JWT callers can't create or change keys. Project and organization key create, update and ephemeral routes answer key_creation_denied to a request authorized with x-appwrite-jwt, as the account JWT route already does for JWTs.
  2. Keys stay within the caller's grant. Key now carries its expiry: stored keys take it from their document, and ephemeral and dynamic keys from the token's exp claim. Key::mayCreate() refuses a key with a scope the caller doesn't hold, or with an expiry later than the caller's own; a caller that expires can't create a key that never does. Key update and ephemeral key creation now check their caller (they didn't before), and create keeps refusing API keys outright. Organization keys still give project keys project scopes through organization.projects.keys.write, but the keys they create can't outlive them.
  3. Key secrets go only to callers that could have created the key. Get and list return secret to a session with the write scope, or to a key whose grant covers the key being read. JWT callers never see secrets.
  4. A JWT can't mint a user JWT. POST /v1/users/:userId/jwts refuses JWT callers with user_jwt_creation_denied.

Behaviour changes

  • A key with keys.write can no longer give a key, itself included, scopes it doesn't hold, and can't remove or extend a key's expiry beyond its own.
  • Ephemeral keys minted by a key get only scopes that key holds and expire no later than it does. An ephemeral key can't renew itself through a new one.
  • Organization keys with an expiry create project keys that expire no later than they do.
  • Keys no longer see the secrets of broader or longer-lived keys; JWTs see no secrets.
  • Console JWTs (admin mode) can't create, change or mint keys, or mint user JWTs.
  • Sessions are unchanged.
  • The key_creation_denied description now covers these cases.

Notes for review

  • Out of scope on purpose: user sessions and tokens created with users.write. They act as the user, not as the key, and server-side login flows create them with short-lived function keys.
  • In Cloud, OAuth2 bearer keys get the scope check now and the expiry check once Cloud passes the token expiry into Key (expire: is a new trailing constructor argument).

Tests

  • Unit: KeyTest covers expiry decoding for each key type, and a mayCreate matrix (delegation, self-renewal, expiry across timezones, the organization key exception). The new cases fail without the change.
  • E2E:
    • Organization\KeysBase::testKeysStayWithinTheirCallersGrant
    • testKeysRefuseJWTCallers
    • the updated testGetKeySecretRequiresWriteScope
    • Project\KeysBase::testKeysStayWithinTheirCallersGrant
    • UsersCustomServerTest::testCreateUserJWTRefusesJWTCallers
  • pint, PHPStan and Rector pass on every touched file.

🤖 Generated with Claude Code

Meldiron and others added 4 commits October 23, 2026 13:16
A JWT stands in for its session for at most an hour, and the account JWT
route already refuses to mint a JWT from one. Creating, changing or
minting an ephemeral key from a JWT left behind a credential that
outlived it, so the project and organization key routes now refuse JWT
callers with key_creation_denied.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A key with keys.write could rewrite any key, itself included, to every
project scope and no expiry, and could mint ephemeral keys with any
scopes, renewing itself without end. An organization key could provision
project keys that outlived it.

Key now carries its expiry (stored keys from their document, ephemeral
and dynamic keys from the token's exp claim) and mayCreate() bounds what a
key hands out: no scope it does not hold, and no expiry later than its own
(none at all when it expires). Organization keys keep provisioning project
scopes through organization.projects.keys.write, within their lifetime.
Key create, update and ephemeral routes apply it; an API key still cannot
create a stored key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d it

Holding keys.write was enough to read every key's secret, so a narrow or
expiring key could read a broader or longer-lived one and act with it,
and a JWT could read secrets that outlive it. Get and list now return a
secret only to a session with the write scope or to a key whose grant
covers that key; JWT callers never see secrets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The account JWT route refuses a JWT caller so a JWT cannot outlive itself
through the next one; the users JWT route did not, so an admin-mode
console JWT could mint project user JWTs. It now refuses JWT callers with
user_jwt_creation_denied.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 4 nitpicks below.

🧹 Nitpicks (4) — 🟢 4 low
  • 🟢 Hoist per-key scope and JWT checks out of loop (XList.php:135) — The secret-redaction loop re-evaluates caller-invariant checks on every key: $request->getScopes() and $request->getHeaderLine('x-appwrite-jwt', '') are constant for the request but recomputed per item (src/Appwrite/Platform/Modules/Project/Http/Project/Keys/XList.php:135-143).
  • 🟢 Unparseable stored expire crashes mayCreate with a 500 (Key.php:99) — mayCreate constructs new \DateTimeImmutable($expire) without guarding against unparseable values (src/Appwrite/Auth/Key.php:99), and the secret gates pass the raw stored attribute $key->getAttribute('expire') (src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Get.php:87, Organization Get.php:84, both XLists).
  • 🟢 Add a positive account-key row to creations() (KeyTest.php:493) — Both API_KEY_ACCOUNT rows in the creations() provider are negative expectations (tests/unit/Auth/KeyTest.php:493-494); no case asserts an account key CAN create a key within its grant.
  • 🟢 Grant-based secret masking untested on legacy /project/keys (KeysBase.php:179) — The legacy /v1/project/keys endpoints duplicate the grant-based secret masking independently of the organization routes (src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Get.php:87 and XList.php:139 both call mayCreate), but the new Project e2e test only covers ephemeral creation, self-widening, and JWT refusal — it never lists or gets keys with the writer to verify masking (tests/e2e/Services/Project/KeysBase.php:179-251).

This PR prevents privilege escalation via key delegation: API keys can no longer create, update, or read secrets of other keys beyond their own grant. Each endpoint now refuses x-appwrite-jwt callers, blocks ephemeral/dynamic caller types where relevant, and delegates to a new Key::mayCreate() scope-and-expiry check, with a documented exception for organization keys holding organization.projects.keys.write.

Files Change
src/Appwrite/Auth/Key.php, app/config/errors.php Adds mayCreate() grant logic, decode() expiry normalization, and the KEY_CREATION_DENIED error type.
src/Appwrite/Platform/Modules/Organization/.../Keys/*, src/Appwrite/Platform/Modules/Project/Http/Project/Keys/* Applies JWT refusal, caller-type blocks, and mayCreate() enforcement to Create, Update, Ephemeral/Create, Get, and XList in both modules.
src/Appwrite/Platform/Modules/Users/Http/Users/JWTs/Create.php Denies JWT-authenticated callers from minting new JWTs, mirroring the account JWT route.
tests/unit/Auth/KeyTest.php, tests/e2e/Services/Organization/KeysBase.php, tests/e2e/Services/Project/KeysBase.php, tests/e2e/Services/Users/UsersCustomServerTest.php Unit data providers and e2e cases pinning the 403 grant-denial and secret-masking behavior.

Reviewed commit: 045ef16

@ghost

ghost commented Oct 23, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

This change carries key expiry through authentication and constrains key creation and updates to the caller’s scopes and lifetime. It also blocks JWT-authenticated key and user-JWT creation, and redacts key secrets from callers that could not have created those keys.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 17
File Change
app/config/errors.php Expands the key-creation denial description to cover the new restrictions.
src/Appwrite/Auth/Key.php Tracks decoded expiry and adds grant/lifetime checks for key delegation.
src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/Create.php Blocks JWT callers and checks delegated scopes and expiry for stored project keys.
src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/Ephemeral/Create.php Applies JWT and grant/lifetime checks to ephemeral project keys.
src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/Get.php Redacts project-key secrets unless the caller could have created the key.
src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/Update.php Blocks JWT callers and checks delegated scopes and expiry before updating keys.
src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/XList.php Redacts each listed key’s secret based on the caller’s grant.
src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php Blocks JWT callers and checks delegated scopes and expiry for stored keys.
src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php Applies JWT and grant/lifetime checks to ephemeral keys.
src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Get.php Redacts key secrets unless the caller could have created the key.
src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Update.php Blocks JWT callers and checks delegated scopes and expiry before updating keys.
src/Appwrite/Platform/Modules/Project/Http/Project/Keys/XList.php Redacts each listed key’s secret based on the caller’s grant.
src/Appwrite/Platform/Modules/Users/Http/Users/JWTs/Create.php Prevents JWT-authenticated requests from minting user JWTs.
tests/e2e/Services/Organization/KeysBase.php Covers delegation limits, JWT restrictions, and secret redaction for organization project-key routes.
tests/e2e/Services/Project/KeysBase.php Covers grant checks and JWT restrictions for project-key routes.
tests/e2e/Services/Users/UsersCustomServerTest.php Covers refusal of user-JWT creation by JWT callers.
tests/unit/Auth/KeyTest.php Tests expiry decoding and scope/lifetime delegation decisions.

Reviewed 045ef16 · 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

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/keys-within-their-grant (after).

Metric Before After Change
🚀 Requests/sec 200.35 205.66 ⚪ +2.6%
⏱️ Latency P50 85.47 ms 84.49 ms ⚪ -1.2%
⏱️ Latency P95 205.81 ms 198.48 ms ⚪ -3.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 84.49 198.48 12,768 205.66 -7.34
Account 146.18 289.58 672 11.19 -10.64
TablesDB 82.1 155.33 6,944 113.99 -20.18
Storage 78.15 196.43 3,360 57.06 +11.21
Functions 111.09 226.18 1,792 31.17 +0.5

Top API waits (after)

API request Max wait (ms)
account.name.update 456.6
storage.buckets.create 403.24
functions.create 393.68
functions.variables.create 389.86
account.prefs.update 372.44

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • 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.

@Meldiron
Meldiron merged commit 7cae6fe into main Oct 23, 2026
69 checks passed
ChiragAgg5k pushed a commit that referenced this pull request Oct 24, 2026
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…-grant

Hold keys and JWTs to their original grant

(cherry picked from commit 7cae6fe)
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.

1 participant