Skip to content

Bind user JWTs to the project that issued them - #13992

Merged
loks0n merged 2 commits into
mainfrom
fix/jwt-project-binding
Sep 29, 2026
Merged

loks0n merged 2 commits into
mainfrom
fix/jwt-project-binding

Conversation

@loks0n

@loks0n loks0n commented Sep 29, 2026

Copy link
Copy Markdown
Member

What

User JWTs now carry a projectId claim. They are minted by account.createJWT, users.createJWT, and function executions (HTTP and worker). The HTTP and realtime user resources only accept a JWT in the project it was issued for. In admin mode, that's console.

JWTs minted before this change have no projectId. They're still accepted when they name a session, since a session ID is server-generated and exists only in the minting project. Old sessionless JWTs stop working. They last at most 3600s.

Test

UsersCustomServerTest::testUserJWTIsBoundToItsProject mints a sessionless JWT in one project and replays it against another project and against console. Both must return 401, and the JWT must still work where it was minted.

The first commit pushes the test alone so CI shows it failing. The fix follows.

🤖 Generated with Claude Code

loks0n and others added 2 commits September 29, 2026 17:47
User JWTs now carry a projectId claim, and the HTTP and realtime user
resources reject a JWT minted for another project. Tokens minted before
the claim existed are still accepted when they name a session, since the
session ID is server-generated and exists only in the minting project.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n loks0n self-assigned this Sep 29, 2026
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/jwt-project-binding (after).

Metric Before After Change
🚀 Requests/sec 193.61 196.98 ⚪ +1.7%
⏱️ Latency P50 88.74 ms 88.48 ms ⚪ -0.3%
⏱️ Latency P95 213.77 ms 203.5 ms ⚪ -4.8%
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 88.48 203.5 12,540 196.98 -10.27
Account 165.93 324.81 660 11.14 -9.89
TablesDB 85.34 164.03 6,820 109.73 -7.04
Storage 82 178.85 3,300 54.67 +3.11
Functions 129.24 239.15 1,760 29.7 -17.61

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 493.9
account.name.update 468.28
account.get 393.33
account.prefs.update 391.08
functions.create 386.86

@loks0n
loks0n marked this pull request as ready for review September 29, 2026 17:17
@loks0n
loks0n merged commit 96da5a3 into main Sep 29, 2026
48 of 49 checks passed
@ghost

ghost commented Sep 29, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

User JWTs now carry a projectId claim. They are minted by account.createJWT, users.createJWT, and HTTP and worker function executions. The HTTP and realtime user resources check that claim against the current project, or against console in admin mode. Legacy JWTs with no claim are still accepted only if they name a session. An e2e test covers replaying a sessionless JWT in another project and in console.

Verdict New comments Fixed Still open
🛑 Changes requested 0 0 0
📂 Walkthrough · 7
File Change
app/controllers/api/account.php Adds a projectId claim to JWTs minted by account.createJWT.
src/Appwrite/Platform/Modules/Users/Http/Users/JWTs/Create.php Injects project and adds a projectId claim to JWTs minted by users.createJWT.
src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php Adds a projectId claim to the user JWT passed to function executions.
src/Appwrite/Platform/Workers/Functions.php Adds a projectId claim to the user JWT the worker passes to functions.
app/init/resources/request.php The HTTP user resource rejects JWTs issued for another project. Legacy JWTs without the claim are accepted only if they carry a session.
app/init/realtime/connection.php The same project-binding check in the realtime user resource.
tests/e2e/Services/Users/UsersCustomServerTest.php E2E test that replays a sessionless JWT against another project and against console, and expects 401 in both.

Reviewed fb39f39 · 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 B · 1 blocking finding to address. Summary

Comment thread app/init/resources/request.php Resolved
@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 2/5

[Critical risk] Binds user JWTs to the project that issued them.

This PR is not safe to merge until admin-mode JWTs work and MQTT enforces the same project boundary.

Fix All in Claude CodeFindings

  1. P1 Admin JWTs are rejected ▶
  2. P1 Security MQTT accepts cross-project JWTs ▶
  3. P2 Legacy JWT fallback lacks coverage ▶
Fix with agent prompt
### Issue 1
app/controllers/api/account.php:3421
When a console user creates a JWT in admin mode for a non-console project, this line stamps the managed project's ID on a token backed by the console session. HTTP and realtime authentication require `console` as the token's project ID in admin mode, so the newly issued JWT is rejected when used for that same admin-mode project request.

### Issue 2
src/Appwrite/Platform/Modules/Users/Http/Users/JWTs/Create.php:89
A sessionless JWT minted for a user ID in project A can still authenticate over MQTT in project B if B has a user with the same ID. Unlike HTTP and realtime, MQTT loads the user from the connecting project without checking this new `projectId` claim, leaving the cross-project replay possible. **How this was verified:** MQTT selects the connecting project's user database and authenticates the JWT's `userId` without comparing its `projectId` claim or requiring a session.

### Issue 3
tests/e2e/Services/Users/UsersCustomServerTest.php:50-57
This test only uses newly minted, sessionless JWTs. The validators also accept older JWTs without `projectId` when they name an active session, but the test does not cover that acceptance or rejection after the session is invalidated. An unintended change to that compatibility behavior could therefore go unnoticed.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR adds an issuing-project claim to user JWTs, checks it in HTTP and realtime authentication, and adds an HTTP cross-project replay test.

  • Admin-mode JWT minting and validation disagree on which project owns the token.
  • MQTT authentication does not enforce the new binding.
  • The session-bearing legacy-token fallback remains untested.

Reviews (1) · Last reviewed commit: "fix(auth): bind user JWTs to their proje..."

->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic(new Document([
'jwt' => $jwt->encode([
'projectId' => $project->getId(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Admin JWTs are rejected When a console user creates a JWT in admin mode for a non-console project, this line stamps the managed project's ID on a token backed by the console session. HTTP and realtime authentication require console as the token's project ID in admin mode, so the newly issued JWT is rejected when used for that same admin-mode project request.

Prompt To Fix With AI
This is a comment left during a code review.
Path: app/controllers/api/account.php
Line: 3421

Comment:
**Admin JWTs are rejected** When a console user creates a JWT in admin mode for a non-console project, this line stamps the managed project's ID on a token backed by the console session. HTTP and realtime authentication require `console` as the token's project ID in admin mode, so the newly issued JWT is rejected when used for that same admin-mode project request.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. Fixed in #13997: admin-mode account.createJWT now stamps console, matching what the admin-mode verifier expects.

$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic(new Document(['jwt' => $jwt->encode([
'projectId' => $project->getId(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security MQTT accepts cross-project JWTs A sessionless JWT minted for a user ID in project A can still authenticate over MQTT in project B if B has a user with the same ID. Unlike HTTP and realtime, MQTT loads the user from the connecting project without checking this new projectId claim, leaving the cross-project replay possible. How this was verified: MQTT selects the connecting project's user database and authenticates the JWT's userId without comparing its projectId claim or requiring a session.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Users/Http/Users/JWTs/Create.php
Line: 89

Comment:
**MQTT accepts cross-project JWTs** A sessionless JWT minted for a user ID in project A can still authenticate over MQTT in project B if B has a user with the same ID. Unlike HTTP and realtime, MQTT loads the user from the connecting project without checking this new `projectId` claim, leaving the cross-project replay possible. **How this was verified:** MQTT selects the connecting project's user database and authenticates the JWT's `userId` without comparing its `projectId` claim or requiring a session.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. Fixed in #13997: MQTT's appwrite-jwt auth now applies the same binding, with an E2E case in MqttServerTest.

Comment on lines +50 to +57
// No sessions, so the JWT carries no session to check.
$jwt = $this->client->call(Client::METHOD_POST, '/users/' . $userId . '/jwts', [
'content-type' => 'application/json',
'x-appwrite-project' => $attackerProject['$id'],
'x-appwrite-key' => $attackerProject['apiKey'],
]);
$this->assertSame(201, $jwt['headers']['status-code']);
$jwts[$userId] = $jwt['body']['jwt'];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Legacy JWT fallback lacks coverage This test only uses newly minted, sessionless JWTs. The validators also accept older JWTs without projectId when they name an active session, but the test does not cover that acceptance or rejection after the session is invalidated. An unintended change to that compatibility behavior could therefore go unnoticed.

Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Users/UsersCustomServerTest.php
Line: 50-57

Comment:
**Legacy JWT fallback lacks coverage** This test only uses newly minted, sessionless JWTs. The validators also accept older JWTs without `projectId` when they name an active session, but the test does not cover that acceptance or rejection after the session is invalidated. An unintended change to that compatibility behavior could therefore go unnoticed.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in #13997: testLegacyJWTWithoutProjectClaimIsBoundBySession signs the pre-#13992 payload and checks own-project acceptance, cross-project rejection, sessionless rejection, and rejection after the session is deleted.

loks0n added a commit that referenced this pull request Sep 29, 2026
…in-mode JWTs

MQTT's appwrite-jwt auth decoded the same user JWT and loaded the user
from the connecting project without the binding added to HTTP and
realtime in #13992, so a sessionless JWT from another project still
authenticated there.

account.createJWT in admin mode stamped the managed project's ID on a
token backed by a console session, which the admin-mode verifier then
rejected; it now stamps console.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
pull Bot pushed a commit to erickirt/appwrite that referenced this pull request Sep 29, 2026
appwrite#13992 threw user_jwt_invalid when a JWT was not issued for the request's
project. A function domain resolves to the console, and clients send
their own project's JWT there for the function to read, so every such
request got 401 before reaching the function. The token still grants
nothing: the request continues as a guest, as it did before appwrite#13992.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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