Skip to content

fix(keys): gate key secrets on the caller's effective scopes - #14603

Merged
loks0n merged 1 commit into
mainfrom
fix/key-secret-write-scope
Oct 16, 2026
Merged

loks0n merged 1 commit into
mainfrom
fix/key-secret-write-scope

Conversation

@loks0n

@loks0n loks0n commented Oct 16, 2026

Copy link
Copy Markdown
Member

What

Key secret on GET /v1/project/keys[/:keyId] and GET /v1/organization/projects/:projectId/keys[/:keyId] is now blanked unless the request was authorized with the key write scope, whatever the caller type.

#14600 only checked API keys. A console member whose role grants keys.read without keys.write (CE roles always hold both, but other role sets don't) still got secrets.

How

  • The api init hook records the scopes it authorized the request with via Request::setScopes() (right after the scope check).
  • The four key actions read $request->getScopes() in place of $apiKey->getScopes(). The project routes no longer inject apiKey.
  • A request that never ran the scope check has no scopes, so it gets no secret.

Tests

  • Organization/KeysBase::testGetKeySecretRequiresWriteScope: the suite's caller (console session or server key) still gets secrets from get and list, and the read-only key still gets ''.
  • Project/KeysBase::testKeysLifecycle: the writer gets the secret on get and list.

CE has no console role with keys.read but not keys.write, so the read-only console case gets its test in the Cloud PR.

🤖 Generated with Claude Code

The secret was only blanked for API keys without keys.write, so a
console member whose role grants keys.read but not keys.write still
received every key secret. Record the scopes the request was authorized
with on the request and check those instead, which covers API keys and
console sessions alike.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n loks0n self-assigned this Oct 16, 2026
@luxor-code-review

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

Copy link
Copy Markdown

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

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 No e2e coverage for organization.projects.keys.write secret visibility (KeysBase.php:797) — The organization endpoints grant secret visibility to callers holding either keys.write or organization.projects.keys.write, but the e2e redaction test only creates keys with the legacy scopes (tests/e2e/Services/Organization/KeysBase.php:797-800).

The change centralizes secret-redaction decisions into the request's granted scope set instead of inspecting each key document per endpoint, with matching e2e coverage updates for both the Project and Organization key suites.

Files Change
src/Appwrite/Utopia/Request.php Adds a mutable scopes property with setScopes()/getScopes() accessors for carrying granted scopes per request.
app/controllers/shared/api.php Populates the request's scopes during the shared API init hook so downstream actions can gate secret visibility.
src/Appwrite/Platform/Modules/Project/Http/Project/Keys/{Get,XList}.php Redact the key secret unless the granted scopes include keys.write.
src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/{Get,XList}.php Same redaction using array_intersect over the granted scope set for organization-scoped key endpoints.
tests/e2e/Services/{Project,Organization}/KeysBase.php Updates e2e assertions to reflect the new secret-visibility semantics.

Reviewed commit: d533315

@ghost

ghost commented Oct 16, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The scope capture and secret filtering are consistent with the request authorization flow, and the changed behavior is covered by the available E2E cases.

The API initialization hook now records the scopes that passed authorization on the request. Project and organization key get/list actions use those effective scopes to decide whether to return key secrets, and the E2E tests add checks for writer-visible and read-only responses.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 8
File Change
app/controllers/shared/api.php Stores the authorized scope set on the request after route-scope validation.
src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/Get.php Uses effective request scopes to filter the returned key secret.
src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/XList.php Uses effective request scopes to filter secrets in the key list.
src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Get.php Uses effective request scopes to filter the returned key secret.
src/Appwrite/Platform/Modules/Project/Http/Project/Keys/XList.php Uses effective request scopes to filter secrets in the key list.
src/Appwrite/Utopia/Request.php Adds storage and accessors for authorized request scopes.
tests/e2e/Services/Organization/KeysBase.php Checks secret visibility for the suite caller and read-only and writer API keys.
tests/e2e/Services/Project/KeysBase.php Checks that the writer can retrieve key secrets on get and list.

Reviewed d533315 · 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

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

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/key-secret-write-scope (after).

Metric Before After Change
🚀 Requests/sec 217.87 223.9 ⚪ +2.8%
⏱️ Latency P50 78.24 ms 78.89 ms ⚪ +0.8%
⏱️ Latency P95 186.8 ms 174.69 ms 🟢 -6.5%
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.89 174.69 13,794 223.9 -12.12
Account 142.57 255.09 726 12.06 -13.84
TablesDB 77.68 140.51 7,502 123.27 +6.07
Storage 70.24 162.81 3,630 61.92 -9.1
Functions 105.81 201.57 1,936 33.85 -30.78

Top API waits (after)

API request Max wait (ms)
functions.create 434.53
account.name.update 398.28
account.prefs.update 363.04
storage.buckets.create 345.45
account.get 294.48

@loks0n
loks0n merged commit cbcb14b into main Oct 16, 2026
66 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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