Repository navigation
fix(migrations): cap migration authority to the creating key's scopes - #15145
Conversation
A key holding only migrations.write could import users, memberships, project auth settings and API keys, because the worker wrote with an all-scope key and direct database writers. Requests now need the write scopes of every imported resource, the grant is recorded on the migration, the worker key is intersected with it, and imported API keys may not exceed it.
|
Review complete. 🟡 2 medium 💬 Inline comments (2)
🧹 Nitpicks (1) — 🟢 1 low
This PR restricts Appwrite-to-Appwrite migrations so the ephemeral key used by the migration worker can only grant scopes the creating API key actually holds. Endpoints now pre-validate the key's grant against per-resource write scopes, the persisted migration records
Reviewed commit: 9cfecd7 |
🟢 Tier S · Ready to merge
The PR validates migration requests against the caller's resource scopes, records the creating key's grant, and limits worker authority and imported API keys to that grant. Retry requests also apply the retrying key's scope limits, including for migrations without a recorded key grant. Latest changes: The latest commits authorize API-key retries of migrations without a recorded grant against the migration resources, intersect retries of granted migrations with the retrying key's scopes, and add e2e coverage for the retry behavior.
📂 Walkthrough · 16
✅ Fixed since the last review · 1
Reviewed the commits since |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
There was a problem hiding this comment.
Adds per-resource scope attenuation to the migrations service: create endpoints validate API-key grants, and the worker caps the ephemeral migration key to the grant.
Key findings
- 🟡 Scope cap breaks function imports mid-migration — Migrations.php:582
- 🟡 Unit tests stub
generateAPIKey, attenuation untested — MigrationsTest.php:340
| if (\is_array($grant)) { | ||
| $scopes = \array_values(\array_intersect($scopes, $grant)); | ||
| } |
There was a problem hiding this comment.
🟡 bug · medium
Scope cap breaks function imports mid-migration
The worker's ephemeral key is now intersected down to the creating key's grant (src/Appwrite/Platform/Workers/Migrations.php:582-584), but the destination still imports functions over the HTTP API passing the function's stored scopes (packages/migration/src/Destinations/Appwrite.php:2742-2760). The API rejects a function whose scopes the key cannot grant with An API key cannot grant scopes it does not hold (src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php:205-206). A source function with exec scopes broader than the migration key's grant therefore fails mid-transfer with a bare 401, failing the whole migration — a regression from the previous all-scopes ephemeral key, and the over-scope case is never surfaced as a clear per-resource error like the analogous createApiKey check does. The same applies to sites with broader scopes.
📋 Prompt for AI Agents
In src/Appwrite/Platform/Workers/Migrations.php around lines 577-584 the ephemeral JWT scopes are capped to the creating key's grant via array_intersect, which makes destination function/site imports fail mid-migration: packages/migration/src/Destinations/Appwrite.php createFunction (lines 2742-2760) passes resource->getScopes() to the HTTP API, and src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php:205 rejects functions whose scopes the key cannot grant. Fix so an over-scoped resource does not hard-fail the migration: either intersect the resource's stored scopes with the grant before import, or catch the GENERAL_UNAUTHORIZED_SCOPE failure for TYPE_FUNCTION/TYPE_SITE in the destination and record it as a skipped-resource error instead of a migration-fatal error. Keep the destination-side cap in createApiKey consistent with the chosen policy.
| #[\Override] | ||
| protected function generateAPIKey(Document $project): string | ||
| protected function generateAPIKey(Document $project, Document $migration): string | ||
| { | ||
| return 'key'; | ||
| } |
There was a problem hiding this comment.
🟡 bug · medium
Unit tests stub generateAPIKey, attenuation untested
Both MigrationsWorkerExportTest and MigrationsTest override generateAPIKey(...) to return a hardcoded 'key', so the new attenuation logic (array_intersect($scopes, $grant)) at src/Appwrite/Platform/Workers/Migrations.php:582-584 is never executed by a unit test. The only checks are indirect: the e2e reject paths and a success-path assertion on the persisted options.scopes — no test decodes the emitted ephemeral JWT and asserts its scope set equals the grant intersection, nor runs the migration to completion with the capped key. A regression in the intersection (e.g. dropping migrations.write, or ignoring the grant on the success path) would pass the whole suite.
📋 Prompt for AI Agents
Add unit coverage for the attenuation in src/Appwrite/Platform/Workers/Migrations.php lines 577-606: call the real (non-overridden) generateAPIKey with a migration whose options.scopes is a strict subset of getAPIKeyScopes(), decode the returned API_KEY_EPHEMERAL_... JWT, and assert the payload scopes array equals the intersection; add a case with options.scopes absent expecting the full getAPIKeyScopes() set. Currently tests/unit/Platform/Workers/MigrationsTest.php (lines 340-344) and tests/unit/Platform/Workers/MigrationsWorkerExportTest.php override the method to return 'key', leaving the logic untested. Additionally, extend the success branch of testAppwriteMigrationRejectsKeyWithoutResourceScopes in tests/e2e/Services/Migrations/MigrationsBase.php (around lines 946-969) to poll GET /migrations/:id until stage 'finished' and assert status 'completed', proving the capped ephemeral key can actually perform the export.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…thout a grant Shared names like collection and document are written straight to the database, so a TablesDB scope must not authorize a DocumentsDB or VectorsDB import. A migration with no recorded grant now gets none instead of every scope; retries stamp the retrying caller's grant, and exports record one too.
The findings that requested changes are resolved. See the summary comment for what is still open.
A key retrying a Console-created migration kept its null grant and reran it with every scope. A key retry now intersects the recorded grant with its own scopes, and checks the resources when the recorded grant was never checked against them.
The findings that requested changes are resolved. See the summary comment for what is still open.
What does this PR do?
Migrations ran every destination write with the worker's own authority: an ephemeral key holding every project scope, plus direct database writers. The only check on
POST /v1/migrations/*wasmigrations.write, so a key restricted to that scope could import users, memberships, project auth settings, webhooks and API keys with arbitrary scopes.This caps a migration's authority to the key that created it:
general_unauthorized_scope. The map lives on the sharedMigrations/Http/Migrations/Actionand uses the same scopes as the real endpoints. Project settings needproject.write. Imports need the row/document write scope for the database type, plusfiles.readfor non-internal files.options.scopes. Console sessions record nothing and keep full authority.generateAPIKey()intersects its scopes with the recorded grant.getAPIKeyScopes()is unchanged, so Cloud's override still applies.scopesgrant, andcreateApiKey()rejects imported keys holding scopes outside it, since that writer bypassesKey::mayCreate().Behaviour change: keys running full migrations now need the write scopes of what they import. The e2e project key gains
keys.write,platforms.write,policies.write,project.oauth2.writeandtemplates.writefortestCreateAppwriteMigration.Test Plan
testAppwriteMigrationRejectsKeyWithoutResourceScopes(401 without the needed scopes, 202 with them and the grant recorded) andtestAppwriteMigrationRejectsApiKeyBroaderThanGrant.tests/unit/Platform/Workers/MigrationsTest.phpandtests/unit/Migration/MigrationsWorkerExportTest.phppass.Not in scope: imported key expiry vs the creator's, read-scope checks on CSV/JSON exports, and reinstating the destination
report()probe.Related: appwrite-labs/cloud#6666