Skip to content

fix(compute): keep function and site scopes within the calling key - #15026

Merged
ChiragAgg5k merged 3 commits into
mainfrom
fix/key-scope-subset
Oct 28, 2026
Merged

ChiragAgg5k merged 3 commits into
mainfrom
fix/key-scope-subset

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Functions and sites sign their scopes into the keys their builds and executions receive. An API key configuring them may now only add scopes it holds itself; scopes already on the resource are kept.

Lint, PHPStan and the touched unit tests passed locally before rebasing on main; e2e runs in CI.

Appwrite to Appwrite migrations recreate functions and sites through the API with the migration worker's temporary key, which only held a hardcoded list of scopes. That key now holds every project scope, so a function or site holding databases.*, tables.*, execution.* or similar still migrates; testAppwriteMigrationSite now migrates a site with databases.read to cover it.

Part of appwrite-labs/cloud#6857

Functions and sites sign their scopes into the keys their builds and
executions receive. An API key configuring them may now only add scopes it
holds itself; scopes already on the resource are kept.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR prevents API keys from escalating privileges by creating or updating Functions and Sites with scopes the key does not itself hold.

Files Change
src/Appwrite/Auth/Key.php Adds mayGrant scope-delegation logic that mayCreate now delegates to.
src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php, Update.php Throw GENERAL_UNAUTHORIZED_SCOPE when the authenticating key cannot grant the requested scopes.
src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php, Update.php Same scope-delegation guard applied to Sites create/update.
tests/e2e/Services/Functions/FunctionsCustomServerTest.php, tests/e2e/Services/Sites/SitesCustomServerTest.php, tests/unit/Auth/KeyTest.php Coverage for grant-allowed and grant-denied paths across both services and the Key library.

Reviewed commit: f588dd0

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 27, 2026
@ghost

ghost commented Oct 27, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The migration key now draws from the same project-scope catalog used to validate function and site scopes, and the added migration assertion covers a scope omitted by the previous hard-coded list.

Adds API-key scope containment for function and site creation and updates, preserving existing resource scopes while restricting newly granted scopes. Migration-generated keys now receive the full project-scope catalog so migrated functions and sites can retain valid scopes.

Latest changes: The new commits derive migration API-key scopes from the project scope catalog and extend the migration site test to verify preservation of databases.read.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 10
File Change
src/Appwrite/Auth/Key.php Adds a reusable scope-grant check and uses it when determining whether a key may create another key.
src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php Rejects function creation when an API key does not hold the requested function scopes.
src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php Checks only newly added function scopes against the API key, retaining scopes already on the function.
src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php Rejects site creation when an API key does not hold the requested site scopes.
src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php Checks only newly added site scopes against the API key, retaining scopes already on the site.
src/Appwrite/Platform/Workers/Migrations.php Builds migration API-key scopes from all keys in the project scope catalog.
tests/e2e/Services/Functions/FunctionsCustomServerTest.php Covers function scope creation, retaining existing scopes, and rejecting unauthorized scope additions.
tests/e2e/Services/Migrations/MigrationsBase.php Extends the site migration scenario to verify preservation of an additional project scope.
tests/e2e/Services/Sites/SitesCustomServerTest.php Covers site scope creation, retaining existing scopes, and rejecting unauthorized scope additions.
tests/unit/Auth/KeyTest.php Tests the scope-containment behavior of mayGrant.

Reviewed the commits since f588dd0 · 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.

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 Oct 27, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

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

Metric Before After Change
🚀 Requests/sec 212.82 221.95 ⚪ +4.3%
⏱️ Latency P50 80.94 ms 78.79 ms ⚪ -2.7%
⏱️ Latency P95 191.75 ms 179.87 ms 🟢 -6.2%
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.79 179.87 13,680 221.95 -11.88
Account 151.85 285.56 720 12.44 +20.31
TablesDB 77.53 145.83 7,440 123.96 -9.18
Storage 70.52 164.05 3,600 61.51 -18.83
Functions 108.52 197.43 1,920 33.53 -12.25

Top API waits (after)

API request Max wait (ms)
account.name.update 460.1
functions.create 396.36
account.prefs.update 376.91
functions.variables.update 373.36
functions.delete 360.38

Functions and sites are recreated in the destination through the API
with the worker's temporary key. Now that a key may only grant scopes
it holds, a function or site holding databases.*, tables.*, rows.*,
execution.* or any other scope missing from the hardcoded list failed
to migrate with general_unauthorized_scope. Give the key every
project scope so migrated resources keep theirs.
@ChiragAgg5k
ChiragAgg5k merged commit dd51fc9 into main Oct 28, 2026
67 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/key-scope-subset branch October 28, 2026 11:43
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