Skip to content

fix(databases): resolve joins by public table id only - #14535

Merged
abnegate merged 4 commits into
mainfrom
qfix-join-ids
Oct 15, 2026
Merged

abnegate merged 4 commits into
mainfrom
qfix-join-ids

Conversation

@abnegate

Copy link
Copy Markdown
Member

Release blocker for the query-lib migration (#11649), found by staging QA of appwrite-labs/cloud#5410.

Problem (security)

Joins accepted the internal table name database_{seq}_collection_{N} as well as the public ID. A guest could count up through N and tell existing, readable and missing tables apart from the 200, 401 and 404 responses.

Fix

src/Appwrite/Utopia/Database/Hook/Joins.php now resolves the join target only by public ID, in the current database's registry. The internal-name fallback is gone for every caller.

  • For non-privileged callers, a missing, disabled or unreadable table (no table read and no rowSecurity) all return the same 404 table_not_found / collection_not_found.
  • rowSecurity, the library join permission check, and API key/privileged access are unchanged.
  • Every entry point goes through getDatabasesDB: list, POST /query, single-row GET, transactions, the legacy API and GraphQL.
  • Columns qualified by a table name instead of an alias were already rejected.

Behaviour change

Joining a table with no table-level read and no rowSecurity used to return 401 user_unauthorized. It now returns 404, so the response can't be used as an oracle. A direct list of such a table still returns 401.

Two existing tests were updated: testDocumentSecurityOffCollectionDeny in tests/e2e/Services/Databases/Queries/JoinPermissions.php and its GraphQL twin.

Tests

  • Unit: tests/unit/Utopia/Database/Hook/JoinsTest.php has 40 tests. 10 of them fail on the old hook.
  • E2E in JoinPermissions.php:
    • testJoinByInternalTableNameIsNotFound
    • testJoinByInternalTableNameInATransactionReadIsNotFound
    • testJoinOfAnUnreadableCollectionIsIndistinguishableFromAMissingOne
    • testColumnQualifiedByInternalTableNameIsInvalid
  • The E2E tests were not run locally (no Docker), so CI is their first run.
  • Pint, PHPStan and Rector are clean.

🤖 Generated with Claude Code

A join named its table either by public ID or by the internal name
database_{dbSeq}_collection_{N}, which the hook resolved through a sequence
lookup. Internal names are never exposed by the API, so accepting them let a
guest count N upwards and tell existing, readable and missing tables apart by
the 200, 401 and 404 responses without knowing a single table ID.

The hook now looks the join up by public ID in the current database's
registry and nothing else, for every caller, API keys included. A table the
caller may not join (disabled, or neither table-level read nor row security)
is refused with the same not-found error as a table that does not exist, so a
non-privileged caller can no longer distinguish "exists but forbidden" from
"missing" through a join. Row-security tables stay joinable without table
read, the database still filters their rows, and privileged callers and
reads that skip authorization keep joining every table.

Every read path that accepts joins (list, POST query, get, transactions, the
legacy documents API and GraphQL) builds its database through
getDatabasesDB, which registers this hook, so the fix covers all of them.
Column references qualified by a table name instead of a join alias were
already rejected by the query validator; E2E now pins that down.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 2 nitpicks below.

🧹 Nitpicks (2) — 🟢 2 low
  • 🟢 Fix orphaned docblock above joinCallers (JoinPermissions.php:1471) — The @return array{databaseId: string, ...} docblock belonging to createCustomerWithOrder (tests/e2e/Services/Databases/Queries/JoinPermissions.php:1471) was left stranded above the newly inserted joinCallers docblock, so it now documents the wrong method and misleads IDEs and PHPStan.
  • 🟢 Avoid regex in new join test helpers (JoinPermissions.php:1271) — New test code uses regex where the repo convention bans it: \preg_replace('/\d+$/', '999999999', $readable) builds the unknown table name in testJoinByInternalTableNameIsNotFound (tests/e2e/Services/Databases/Queries/JoinPermissions.php:1271).

The PR reworks src/Appwrite/Utopia/Database/Hook/Joins.php to resolve joined collections by public ID instead of internal sequence-based table names, adds a WeakMap-keyed per-request resolved-cache, and makes refusals for unreadable collections indistinguishable from missing ones (throwing the shared not-found exception with Table/Collection wording derived from the request URI). Privileged callers and documentSecurity handling were verified to gate correctly. The rest of the change is test coverage asserting permission behavior across surfaces.

Files Change
src/Appwrite/Utopia/Database/Hook/Joins.php Rewrites join resolution to public-ID lookups with a per-request WeakMap cache and uniform not-found refusals
tests/unit/Utopia/Database/Hook/JoinsTest.php Adds unit coverage for the hook's resolution and refusal paths
tests/e2e/Services/Databases/Queries/JoinPermissions.php Adds REST e2e cases covering unreadable/missing join indistinguishability and privileged bypass
tests/e2e/Services/GraphQL/QueryJoinPermissions.php Adds GraphQL parity tests mirroring the REST join-permission contract

Reviewed commit: 1cfc736

@ghost

ghost commented Oct 15, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The incremental test adjustment excludes echoed request data while continuing to inspect the rest of the response body.

Join targets now resolve only through public table or collection IDs in the current database registry, and inaccessible targets are returned as not found for non-privileged callers. Unit and API tests cover public-ID resolution, permissions, internal-name rejection, and the REST, transaction, and GraphQL paths. The qualified-column test now excludes the development error trace, which echoes the submitted query, when checking for returned private data.

Latest changes: The newest commit excludes the echoed development trace from the qualified-column test's private-data response assertion.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 5
File Change
src/Appwrite/Utopia/Database/Hook/Joins.php Resolves join targets by public ID and normalizes inaccessible targets to not-found for non-privileged callers.
tests/e2e/Services/Databases/Queries/JoinPermissions.php Covers REST join resolution and permissions, and excludes the echoed development trace when checking qualified-column refusals for private data.
tests/e2e/Services/GraphQL/QueryJoinPermissions.php Adds GraphQL coverage for join resolution and permissions across guest, signed-in, and API-key callers.
tests/e2e/Traits/InternalTableName.php Provides shared helpers for deriving existing and missing internal table names in E2E tests.
tests/unit/Utopia/Database/Hook/JoinsTest.php Tests public-ID-only resolution and join permission behavior.

Merged main (2c72744); no changes to this PR's code since d3f0875 · 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

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

…egex

GraphQL resolves joins through the same hook as REST, so it needs the same
regression: a join by an internal table name must be refused exactly as a
missing table, for guests, users and API keys alike.

The internal-name lookup moves to a shared InternalTableName trait so both
suites build the name the same way, and it checks the sequences with
ctype_digit and explode instead of regular expressions, which AGENTS.md bans.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → qfix-join-ids (after).

Metric Before After Change
🚀 Requests/sec 213.37 204.86 ⚪ -4%
⏱️ Latency P50 79.7 ms 85.65 ms 🔴 +7.5%
⏱️ Latency P95 190.73 ms 194.29 ms ⚪ +1.9%
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 85.65 194.29 12,597 204.86 +3.55
Account 150.49 278.75 663 11.1 +4.77
TablesDB 84.23 161.86 6,851 112.79 +17.11
Storage 75.53 170.32 3,315 57.03 -3.4
Functions 116.66 223.24 1,768 31.09 -19.92

Top API waits (after)

API request Max wait (ms)
account.name.update 464.45
tablesdb.rows.delete 412.59
functions.variables.update 401.52
functions.create 393.17
functions.variables.create 392.87

@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

…lumn refusals

The development error response carries a trace whose Swoole request args
echo the caller's own query string. The filter attempts put the secret
marker in their own filter value, so the leak assertion matched the
caller's input rather than leaked row data, failing every Legacy and
TablesDB variant on the Postgres (dedicated) lane even though each
refusal was the expected 400.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@abnegate
abnegate merged commit 885e9c7 into main Oct 15, 2026
63 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