Skip to content

fix(databases): require an API key or privileged user for bulk writes - #15235

Merged
abnegate merged 1 commit into
mainfrom
fix/bulk-writes-require-key
Nov 3, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix/bulk-writes-require-key

Conversation

@abnegate

@abnegate abnegate commented Nov 3, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Bulk update, upsert and delete (Legacy, TablesDB, DocumentsDB and VectorsDB) are documented as admin/API-key only (auth: [AuthType::ADMIN, AuthType::KEY]), but that label only feeds the SDK spec. They share documents.write / rows.write with the single-record routes, and guests and members hold those scopes. Bulk create is the only bulk route that refused sessions at runtime.

This adds the same up-front check bulk create uses (isKey || isPrivileged, otherwise general_unauthorized_scope) to the three base actions, and injects user + authorization into all twelve route classes.

Exposure today

Checked against a running stack: on main a session calling bulk update/upsert/delete gets 404 database_not_found. The actions read the database metadata without authorization->skip(), so the session can't see it and the request stops before any write. Nothing is changed (title, $permissions, $createdAt and the row count stay the same). So this isn't exploitable right now. It only holds because that lookup happens to fail. Past that lookup, the bulk paths skip the three checks the single-record routes make for sessions:

  • $permissions are only format-validated, not checked against the caller's roles
  • removeReadonlyAttributes(..., privileged: true) lets $createdAt/$updatedAt through
  • disabled databases/collections aren't checked

Adding skip() to that lookup, as the single-record routes have, would expose all three. With this PR the contract is enforced explicitly.

Test Plan

New testBulkWritesRequireKeyOrPrivilegedUser in DatabasesBase, run locally on Legacy, TablesDB and DocumentsDB × custom client / custom server / console:

  • client (session): bulk create/update/upsert/delete each return 401 general_unauthorized_scope; the record's title, permissions and $createdAt and the row count are unchanged
  • server (key) and console (privileged): all four succeed

With the src/ change reverted, the client suites fail (404 is identical to 401 on bulk update). Also re-ran locally: VectorsDB testBulk*, GraphQL Legacy/TablesDB client+server testBulk*, testBulkOperators, testListDocumentsCacheBustedByDocumentPermissionChange. Pint and PHPStan (phpstan.neon) clean.

Related PRs and Issues

Reported internally (Discord, bulk writes with permissions).

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?

🤖 Generated with Claude Code

Bulk update, upsert and delete are documented as admin/API-key only, but
only bulk create enforced it. The other three shared the write scope with
the single-record routes, so a client session reached the action and was
stopped only because the unskipped database lookup happened to return 404.
Those actions also skip the checks the single-record routes rely on for
sessions: granted roles, read-only timestamps and the disabled switch.

Refuse non-key, non-privileged callers up front with the same
general_unauthorized_scope bulk create returns, so the contract no longer
rests on an incidental lookup failure.

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

luxor-code-review Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Align transaction bulk gate error code with bulk endpoints (Create.php:135) — The new gate on direct bulk endpoints throws Exception::GENERAL_UNAUTHORIZED_SCOPE when the caller is neither an API key nor a privileged user (src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php:104).

This PR restricts bulk write operations to API-key or privileged users: all twelve bulk endpoints (Delete/Update/Upsert across the four database service variants) now refuse session-authenticated callers with a 401 before any data mutation, and a new e2e test pins the refusal contract on the client side while verifying success on server and console sides. The gate is consistently placed as the first statement in each action, and the e2e assertions detect partial mutation for each verb. One minor inconsistency remains: the transaction bulk-operations gate uses a different error code than the bulk endpoints.

Reviewed commit: 1454c02

@appwrite

appwrite Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The authorization guard is consistent across all three base actions, and the route wrappers supply the required dependencies; the end-to-end test covers the expected client and privileged/server behavior.

Adds an explicit API-key-or-privileged-user check to the Legacy bulk update, upsert, and delete actions, and injects the required auth context into their DocumentsDB, TablesDB, and VectorsDB routes. Adds an end-to-end test covering rejected session writes and successful server/console writes across the database API variants.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 13
File Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php Rejects bulk deletes unless the caller has an API key or privileged role.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php Rejects bulk updates unless the caller has an API key or privileged role.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php Rejects bulk upserts unless the caller has an API key or privileged role.
src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Delete.php Injects user and authorization into the DocumentsDB bulk delete route.
src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Update.php Injects user and authorization into the DocumentsDB bulk update route.
src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Bulk/Upsert.php Injects user and authorization into the DocumentsDB bulk upsert route.
src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Bulk/Delete.php Injects user and authorization into the TablesDB bulk delete route.
src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Bulk/Update.php Injects user and authorization into the TablesDB bulk update route.
src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Bulk/Upsert.php Injects user and authorization into the TablesDB bulk upsert route.
src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Bulk/Delete.php Injects user and authorization into the VectorsDB bulk delete route.
src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Bulk/Update.php Injects user and authorization into the VectorsDB bulk update route.
src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Bulk/Upsert.php Injects user and authorization into the VectorsDB bulk upsert route.
tests/e2e/Services/Databases/DatabasesBase.php Tests that session bulk writes fail without changing data and that privileged/server callers succeed.

Reviewed 1454c02 · Details · Comment @appwrite review to re-run, or mention @appwrite with a question.

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

🟢 Tier S · Looks good to merge. Summary

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Security rules

WARNING (2 new) — review signal only; does not fail the job.

php.appwrite.weak-secret-env-default (2)

Secret-like env var (SECRET, KEY, PASS, PASSWORD, TOKEN) read with a non-empty literal default. If the variable is unset the process silently runs with a shared, guessable value. Default to '' and fail or disable the feature when it is empty.

  • src/Appwrite/Auth/EncryptionKey.php:40 — EncryptionKey::version()
    • What's wrong: _APP_OPENSSL_KEY_CURRENT falls back to '1' when unset, so every install that does not set it shares that value.
    • How to fix: Read System::getEnv('_APP_OPENSSL_KEY_CURRENT', '') and fail or disable the dependent connection when it is empty; keep the example value in .env / app/config/variables.php.
  • src/Appwrite/Auth/EncryptionKey.php:64 — EncryptionKey::accepted()
    • What's wrong: _APP_OPENSSL_KEY_OLDEST falls back to '1' when unset, so every install that does not set it shares that value.
    • How to fix: Read System::getEnv('_APP_OPENSSL_KEY_OLDEST', '') and fail or disable the dependent connection when it is empty; keep the example value in .env / app/config/variables.php.
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.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/bulk-writes-require-key (after).

Metric Before After Change
🚀 Requests/sec 199.62 203.62 ⚪ +2%
⏱️ Latency P50 84.81 ms 85.13 ms ⚪ +0.4%
⏱️ Latency P95 210.49 ms 198.22 ms 🟢 -5.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 85.13 198.22 12,597 203.62 -12.27
Account 162.53 289.83 663 10.98 -19.21
TablesDB 81.1 149.77 6,851 111.91 -21.92
Storage 82.67 187.42 3,315 56.31 +3.73
Functions 118.05 233.92 1,768 30.76 -1.16

Top API waits (after)

API request Max wait (ms)
account.name.update 416.52
functions.variables.update 412.42
tablesdb.rows.create 394.81
storage.buckets.create 390.56
functions.create 358.43

@abnegate
abnegate merged commit b824c81 into main Nov 3, 2026
39 checks passed
@abnegate
abnegate deleted the fix/bulk-writes-require-key branch November 3, 2026 10:02
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