Repository navigation
fix(migrations): authenticate the source key before reading another project's database - #14003
Conversation
…roject's database An Appwrite to Appwrite migration whose endpoint points at this instance takes the database fast path: the worker opens the source project's database directly and never presents the submitted API key to the source API. Any project admin could therefore copy every database, table and row of another project on the same instance by naming its project ID with an arbitrary key. Only non-database resources were protected, because those are still exported through the API with the key. Before opening another project's database, probe the source API over the internal migration host with the submitted key: list databases, and list tables/rows of a non-existent database when those resources are requested. A database_not_found reply proves the key passed the scope check; anything else fails the migration with migration_source_unauthorized. This mirrors the guard Cloud added in appwrite-labs/cloud#5261. Co-Authored-By: Claude Opus 5.5 <[email protected]>
🔵 Tier A · Mergeable after minor fixes
Before the migrations worker reads another project's database directly, it now probes the source API with the submitted key. It calls
Fix with agent prompt### Issue 1
src/Appwrite/Platform/Workers/Migrations.php:352-354
**Every probe failure is reported as an authorization error**
The `catch` maps every `AppwriteException` to `MIGRATION_SOURCE_UNAUTHORIZED` (401). That includes connection failures, timeouts and 5xx replies from the internal host. A transient outage would tell the user their key is wrong. Consider mapping only 401/403 (and the expected not-found types) to this error and letting other failures surface as-is. The original exception is already kept as `previous`.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 4
Reviewed |
| } catch (AppwriteException $error) { | ||
| throw new Exception(Exception::MIGRATION_SOURCE_UNAUTHORIZED, previous: $error); | ||
| } |
There was a problem hiding this comment.
Every probe failure is reported as an authorization error
The catch maps every AppwriteException to MIGRATION_SOURCE_UNAUTHORIZED (401). That includes connection failures, timeouts and 5xx replies from the internal host. A transient outage would tell the user their key is wrong. Consider mapping only 401/403 (and the expected not-found types) to this error and letting other failures surface as-is. The original exception is already kept as previous.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Workers/Migrations.php
Line: 352-354
Comment:
**Every probe failure is reported as an authorization error**
The `catch` maps every `AppwriteException` to `MIGRATION_SOURCE_UNAUTHORIZED` (401). That includes connection failures, timeouts and 5xx replies from the internal host. A transient outage would tell the user their key is wrong. Consider mapping only 401/403 (and the expected not-found types) to this error and letting other failures surface as-is. The original exception is already kept as `previous`.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · error-handling · Reply if this doesn't apply.
|
| $this->assertStringContainsString( | ||
| 'The source API key cannot read the requested resources of the source project.', | ||
| implode("\n", $result['errors']), | ||
| ); |
There was a problem hiding this comment.
Tests mirror error wording Both new rejection tests assert a sentence copied from the error configuration. Changing that wording would fail the tests even if authorization still works. The repository requires tests to check observable behavior rather than mirror configuration; this requirement should be satisfied before merging. Assert the failed migration, the absence of copied data, and a stable error contract instead. The same assertion appears in the test for a key without table scope.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Migrations/MigrationsBase.php
Line: 630-633
Comment:
**Tests mirror error wording** Both new rejection tests assert a sentence copied from the error configuration. Changing that wording would fail the tests even if authorization still works. The repository requires tests to check observable behavior rather than mirror configuration; this requirement should be satisfied before merging. Assert the failed migration, the absence of copied data, and a stable error contract instead. The same assertion appears in the test for a key without table scope.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| } catch (AppwriteException $error) { | ||
| throw new Exception(Exception::MIGRATION_SOURCE_UNAUTHORIZED, previous: $error); |
There was a problem hiding this comment.
Source API failures appear unauthorized If the internal source API returns an unrelated error, such as a service failure or rate limit, this catch reports it as
migration_source_unauthorized. The migration then tells the user the key lacks access, and the 401-classified error is not published as a platform warning. Distinguish rejected credentials from source API failures so operators can diagnose the failed migration.
Knowledge Base Used: Worker job families
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Workers/Migrations.php
Line: 352-353
Comment:
**Source API failures appear unauthorized** If the internal source API returns an unrelated error, such as a service failure or rate limit, this catch reports it as `migration_source_unauthorized`. The migration then tells the user the key lacks access, and the 401-classified error is not published as a platform warning. Distinguish rejected credentials from source API failures so operators can diagnose the failed migration.
**Knowledge Base Used:** [Worker job families](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/appwrite/appwrite/-/docs/worker-job-families.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Brings in main's #14003, which authenticates the submitted source key against the source API before a same-instance migration reads another project's database directly. Conflict in src/Appwrite/Platform/Workers/Migrations.php: - imports: kept this branch's Claim/Report/Superseded and main's Appwrite\Services\TablesDB; - local source path: main's authenticateSource() guard now runs before the branch's explicit getProjectDB resolver, which replaced main's call_user_func and throws when the resolver is missing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What does this PR do?
An Appwrite → Appwrite migration whose
endpointpoints at this instance uses the database fast path. The worker opens the source project's database directly (SourceAppwrite::SOURCE_DATABASE), and the submittedapiKeynever reaches the source API. So any project admin could copy every database, table and row of another project on the same instance: name itsprojectIdand pass any key. Non-database resources were not exposed, because they are still exported through the API with the key.Before the worker opens another project's database, it now checks the submitted key against the source API, over the internal migration host:
tablesDB.list(): the key must belong to the source project and havedatabases.read.tablesDB.listTables('migration-scope-probe')when tables, columns, indexes or rows are requested, andlistRows(...)when rows are requested. Adatabase_not_foundreply shows the key got past the scope check.Any other response fails the migration with the new
migration_source_unauthorized(401) error. Migrations from the current project into itself, and CSV/JSON exports, skip the check because they only read the current project.This is the same guard Cloud already ships (appwrite-labs/cloud#5261).
Test Plan
New E2E tests in
MigrationsBase. Both fail without the fix, because the source database gets copied:testAppwriteMigrationRejectsForeignSourceApiKey: migrating from the source project with the destination project's key fails, and the database does not appear in the destination.testAppwriteMigrationRejectsSourceApiKeyWithoutTableScope: a source key with onlydatabases.read, asked for databases and tables, fails.The existing Appwrite migration E2E tests use a valid source key, so they cover the success path through the probe.
Checks run locally: Pint and PHPStan are clean on the changed files. I couldn't run E2E locally because the Docker daemon wasn't responding, so this relies on CI.
Related PRs and Issues
🤖 Generated with Claude Code