Repository navigation
fix(security): transaction ACL, OTP secret masking, nested relationship permissions - #14045
Conversation
TransactionState loaded the transaction under Authorization::skip, so any caller with a leaked transactionId could read another user's staged rows through document get/list/count. Load it under the caller's ACL unless the caller is an API key or privileged user, matching the write paths. Co-authored-by: Jake Barnby <[email protected]>
…ions.write keys API keys holding only sessions.write could create email OTP, magic URL, phone and recovery tokens for any address and read the secret straight from the response, taking over the account without access to the inbox or phone. Only reveal the secret to keys that already hold users.write, which can mint tokens for any user anyway. Co-authored-by: Jake Barnby <[email protected]>
Document create, update and upsert only checked the root $permissions against the caller's roles, so a related document written through its parent could carry roles the caller does not hold. Transaction staging did not look at nested documents at all. Apply the same role check to every nested related document, ignoring permissions it already has. Co-authored-by: Jake Barnby <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
🟢 Tier S · Ready to mergeAdds caller-aware ACL checks for transaction overlays, masks account token and recovery secrets for API keys without users.write, and validates nested relationship permission grants. Transaction validation now considers staged child state and rechecks permissions during commit after preceding operations have applied. E2E coverage includes transaction isolation, unauthorized nested grants and rollback, and restricted-key email and phone token flows. Latest changes: The newest commits validate nested permissions against staged state and again during commit, add delete/recreate rollback coverage, and test phone secret masking with a sessions.write-only key.
📂 Walkthrough · 13
Reviewed the commits since |
…ansaction state Staging compared nested related-document permissions against the committed document, so a related document deleted earlier in the transaction could be recreated on commit with permissions the user does not hold. Staging now reads the related document through the transaction state, and commit re-checks the nested permissions against the document as it exists inside the commit transaction, after earlier operations (including same-batch and cascade deletes) have been applied. Co-authored-by: Jake Barnby <[email protected]>
Co-authored-by: Jake Barnby <[email protected]>
…nested-perms-8991' into cursor/security-transaction-otp-nested-perms-8991 Co-authored-by: Jake Barnby <[email protected]>
|
Both review findings are addressed. Details are in the inline replies.
All transaction suites pass for Legacy, DocumentsDB and TablesDB, as do |
Main moved 16 pull requests ahead (2aa55a4) and the PR could not merge. Conflicts keep both sides. Realtime's permission rebuild stays in Realtime::rebuildConnection, which now keeps every key the connection handler records (session, impersonator, expiry, JWT expiry, presences) instead of only authorization, and the session expiry is re-read after it, as main does. The realtime user factory main split from the store keeps the uncached, authorization-skipping user load. Main's nested related-permission check (appwrite#14045) is ported onto RelationshipValues: for users and guests every nested document's $permissions are checked against the stored related document at any depth, through a RelatedPermissions rule that Action::validateRelatedPermissions shares with the transaction staging and commit checks. Its Database::VAR_RELATIONSHIP uses become ColumnType::Relationship, which database 8 replaced it with. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What does this PR do?
Three security fixes for three Help Scout High reports. There is one commit per report, and each was checked against current
main. Review follow-ups are in separate commits on top.HS 1005147: transaction read ACL bypass
Confirmed on main.
TransactionState::getTransactionState()loaded thetransactionsdocument underAuthorization::skipand only checkedstatus === 'pending'. Every transaction-aware read goes through it: document get, list and count, plus the internal read used by update, delete and upsert. That covers Legacy, TablesDB, DocumentsDB and VectorsDB. So any caller with a leakedtransactionIdcould see another user's staged, uncommitted rows overlaid on the committed data.Fix:
TransactionStatenow receives the caller (userresource). It loads the transaction under the caller's ACL, unless the caller is an API key or a privileged user. This is the same ruleTransactions/UpdateandTransactions/Operations/Createalready use. A foreign transaction now resolves to an empty state: no staged overlay, and the caller only sees committed data their own ACL allows. Because this is the single choke point, every database flavour is covered without changing each read endpoint.HS 1296591: OTP/token secret returned to
sessions.writeAPI keysConfirmed on main.
Response::output()revealssensitivefields to anyisKey()caller. These endpoints all use thesessions.writescope:POST /v1/account/tokens/emailPOST /v1/account/tokens/magic-urlPOST /v1/account/tokens/phoneTheir SDK auth is admin, session or JWT only; API keys are not listed. Even so, a key holding just
sessions.write(the scope the SSR guide recommends) could mint a token for any email or phone and read the secret from the response. That is account takeover without access to the inbox or phone.POST /v1/account/recoveryandPOST /v1/account/recovery/otphave the same root cause (same scope, same reveal), and a recovery secret is also a takeover. They are included here as part of the same report, not as new scope.Product intent: server-side minting of user tokens is meant to go through
POST /v1/users/:userId/tokens, which requiresusers.write. SSR login flows never need the token secret back: the user brings the secret from their inbox or phone.Fix: these five endpoints return an empty
secretwhen the API key lacksusers.write. A key withusers.writecan already mint a token for any user, so it still gets the secret; the existingtestCreateMagicUrlexpectation is unchanged. Privileged console roles already holdusers.write. Event and webhook payloads are unchanged.HS 1381840: nested relationship child
$permissionsself-grant (finding 1)Confirmed on main. Document create, update and upsert check the root
$permissionsagainst$authorization->hasRole(). The relationship walkers never look at the$permissionsof a nested related document, so a client could create or update a related document granting roles it does not hold (for exampleteam:xoruser:other). TablesDB rows inherit these same actions. Transaction staging (POST /v1/databases/transactions/:id/operations, and the TablesDB subclass) did not walk relationships at all, and commit applies the staged data under the caller's ACL.Fix: a new
validateRelatedPermissions()on the sharedDatabases\Actionbase. It validates the format, aggregateswriteinto its sub-types, then requireshasRole()for every role the related document does not already have. Leaving out roles it already has means a client can send a fetched relationship back unchanged without a false 401. It is called from:create,updateandupsert. Here the related document is resolved throughTransactionState, so a delete staged in an earlier request already counts;Transactions/Update), which is the authoritative check. Just before each stagedcreate,updateorupsertis applied, it walks the nested related documents. It compares their permissions against the related document as it exists inside the commit transaction, after earlier operations have been applied. This closes stage-delete-then-recreate within a single request, across requests, and through cascade deletes. The error is rethrown as a databaseAuthorizationexception, sowithTransaction()rolls back without retrying and the transaction endsfailedwith a 401.Bulk paths are already restricted to keys and admins, and reject relationship collections.
Finding 2 (chunked upload takeover) is already fixed on main by #14014 (
assertResumeAllowed), so it is not touched here.Test Plan
New e2e tests:
TransactionPermissionsBase::testUserCannotReadAnotherUsersStagedDocuments(Legacy, DocumentsDB and TablesDB client). User 1 stages a create and can read it with their owntransactionId. User 2 using the leakedtransactionIdgets 404 on get, and a list without the staged row.AccountCustomServerTest::testCreateTokenWithSessionsKey. A key with onlysessions.writegets an emptysecretfrom email OTP, magic URL, recovery and recovery OTP, and the email is still delivered. The full test key (withusers.write) still gets the secret, and the empty secret cannot be turned into a session.MockPhonesSessionIntegrationTest::testMockPhoneSessionWithSessionsKey. Asessions.writekey gets an empty secret from the phone token endpoint, the user still signs in with the mock OTP, and the full key still gets the secret.DatabasesBase::testCreateRelatedPermissions(Legacy and TablesDB on client, server and console sides). On the client, a nested child with the caller's own roles is accepted, and sending existing child permissions back on update is accepted. A nested child with foreign team or user roles is rejected with 401 on create, update and upsert. Servers and consoles still succeed.TransactionPermissionsBase::testCannotSetUnauthorizedRelatedPermissions: staging a create whose nested child has a foreign role returns 401.TransactionPermissionsBase::testCannotRecreateRelatedWithUnauthorizedPermissions. A child holds a foreign role. A delete staged in an earlier request, followed by a nested recreate carrying that role, returns 401 at staging. A delete and recreate in the same request is staged, then the commit returns 401, the transaction isfailed, and the child is unchanged. Recreating it with the user's own role commits.Run locally against a full
docker composestack, with_APP_DOCUMENTSDB=enabledas in CI:200, expected404.sessions.writekey gets a non-empty secret, including from the phone endpoint.201, expected401. This happens on Legacy, on TablesDB, and through transaction staging.201. Without the commit check, the same-request commit returns200and recreates the child with the foreign role.AccountCustomServerTest,AccountCustomClientTest,AccountConsoleClientTestandMockPhonesSessionIntegrationTest.testCreateRelatedPermissionson Legacy and TablesDB for client, server and console, plus the existing relationship tests on the client side.php -l, Pint (--test), PHPStan (project config) on every changed source file, and the Rector dry-run on the changed tests all pass.Related PRs and Issues
Checklist