Skip to content

fix(migrations): authenticate the source key before reading another project's database - #14003

Merged
abnegate merged 1 commit into
mainfrom
fix/migration-source-key
Sep 30, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix/migration-source-key

Conversation

@abnegate

Copy link
Copy Markdown
Member

What does this PR do?

An Appwrite → Appwrite migration whose endpoint points at this instance uses the database fast path. The worker opens the source project's database directly (SourceAppwrite::SOURCE_DATABASE), and the submitted apiKey never reaches the source API. So any project admin could copy every database, table and row of another project on the same instance: name its projectId and 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 have databases.read.
  • tablesDB.listTables('migration-scope-probe') when tables, columns, indexes or rows are requested, and listRows(...) when rows are requested. A database_not_found reply 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 only databases.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

  • Security audit: "Appwrite → Appwrite migration copies another project's database by naming its ID"
  • Cloud equivalent: appwrite-labs/cloud#5261

🤖 Generated with Claude Code

…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]>
@ghost

ghost commented Sep 30, 2026

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The guard closes the reported hole and the tests cover it. The only issue is that non-auth probe failures are also reported as 401.

Before the migrations worker reads another project's database directly, it now probes the source API with the submitted key. It calls tablesDB.list(), then listTables and listRows on a non-existent database, depending on the requested resources. Any failure other than database_not_found fails the migration with the new migration_source_unauthorized (401) error. Two E2E tests cover a foreign key and a key without table scope.

Verdict New comments Fixed Still open
✅ Approved 1 0 0
Finding Where
🟡 Every probe failure is reported as an authorization error src/Appwrite/Platform/Workers/Migrations.php:352
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
File Change
app/config/errors.php Adds the migration_source_unauthorized error definition (401).
src/Appwrite/Extend/Exception.php Adds the MIGRATION_SOURCE_UNAUTHORIZED constant.
src/Appwrite/Platform/Workers/Migrations.php Adds authenticateSource and probeScope. They check the source key against the source API before the direct database fast path is used for a different project.
tests/e2e/Services/Migrations/MigrationsBase.php Adds a failure-expecting migration helper and two E2E tests: one with a foreign key, one with a key missing table scope.

Reviewed 8fcbe17 · 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 A · Looks good to merge. Summary

Comment on lines +352 to +354
} catch (AppwriteException $error) {
throw new Exception(Exception::MIGRATION_SOURCE_UNAUTHORIZED, previous: $error);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Critical risk] Adds authentication check before reading another project's database.

The error classification is non-blocking, but the explicit repository testing requirement should be satisfied before merging.

Fix All in Claude CodeFindings

  1. P2 Tests mirror error wording ▶
  2. P2 Source API failures appear unauthorized ▶
Fix with agent prompt
### Issue 1
tests/e2e/Services/Migrations/MigrationsBase.php:630-633
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.

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!

### Issue 2
src/Appwrite/Platform/Workers/Migrations.php:352-353
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.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR checks a submitted source key through the internal API before a cross-project migration takes the direct database-read path, and adds an unauthorized error and regression tests.

  • The new tests cover a foreign-project key and a source key without table-read scope.
  • The review found configuration-coupled assertions and an error path that misclassifies source API failures.

Reviews (1) · Last reviewed commit: "fix(migrations): authenticate the source..."

Comment on lines +630 to +633
$this->assertStringContainsString(
'The source API key cannot read the requested resources of the source project.',
implode("\n", $result['errors']),
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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!

Fix in Claude Code Fix in Codex

Comment on lines +352 to +353
} catch (AppwriteException $error) {
throw new Exception(Exception::MIGRATION_SOURCE_UNAUTHORIZED, previous: $error);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Fix in Claude Code Fix in Codex

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/migration-source-key (after).

Metric Before After Change
🚀 Requests/sec 207 210.09 ⚪ +1.5%
⏱️ Latency P50 83.23 ms 82.06 ms ⚪ -1.4%
⏱️ Latency P95 195.11 ms 191 ms ⚪ -2.1%
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 82.06 191 13,395 210.09 -4.12
Account 161.49 295 705 11.69 -7.03
TablesDB 79.23 148.63 7,285 117.69 -15.18
Storage 75.57 165.26 3,525 58.32 +13.12
Functions 118.18 229.12 1,880 31.6 +8.28

Top API waits (after)

API request Max wait (ms)
tablesdb.rows.list 493.64
functions.create 412.49
tablesdb.rows.delete 408.49
account.name.update 399.24
account.prefs.update 384.24

@abnegate
abnegate merged commit 77def26 into main Sep 30, 2026
55 checks passed
@abnegate
abnegate deleted the fix/migration-source-key branch September 30, 2026 03:22
abnegate added a commit that referenced this pull request Sep 30, 2026
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]>
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