Repository navigation
fix(databases): appwrite:appwrite:alert#4628 scope cached list fields to the version they were read at - #15187
Conversation
…d at A list read took the collection's list version before it queried and saved its rows only while that version was unchanged, but the comparison and the write were two separate cache operations. A purge landing between them left the rows in the hash under a field readers still look up, so a caller whose document read permission had just been revoked kept being served the revoked rows for its chosen ttl, up to 24 hours. Name the field after the version instead. A read now looks up and saves a field of the version it queried at, and a purge both changes the version and drops the hash, so a fill that lands after the purge that should have stopped it is written where no later read looks. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Review complete. 🟡 1 medium 📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — Failed post-write purge leaves revoked rows servable · ListCache.php:122–128 · // src/Appwrite/Databases/ListCache.php
122 foreach ($caches as $cache) {
123 try {
124 $cache->purge();
125 } catch (\Throwable $error) {
126 Console::warning('Failed to purge the cached list responses of ' . $cache->key . ' after a permission change: ' . $error->getMessage());
127 }
128 }In 🧹 Nitpicks (2) — 🟢 2 low
This PR introduces a versioned list cache layer for listing documents, keyed by collection, roles, and queries, with version-purge invalidation wired into the documents list endpoint.
Reviewed commit: 772c8bf |
🟢 Tier S · Ready to mergeList-cache fields are now read and written under the collection version captured before the list query, preventing fills from superseded versions from being served after a purge. The documents list handler takes one version for document, operation, and total cache access, and the unit tests cover the purge interleaving.
📂 Walkthrough · 4
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…-fields fix(databases): appwrite:appwrite:alert#4628 scope cached list fields to the version they were read at (cherry picked from commit 33031c7)
What
Closes code-scanning alert appwrite:appwrite:alert#4628.
ListCachedocuments the invariant "a read that ran while a purge happened is not cached", but did not enforce it:save()compared the collection's list version with the version captured before the query, and then wrote the rows with a separate, unconditionalsaveMany.ListCache::purged()wraps a permission-changing write with a purge before and after it, and each purge rotates the version and drops the hash as two more commands.A list read on a
documentSecuritycollection could therefore:HMSETafter that purge had dropped the hash.The rows survived under the same hash field, because the field is derived from the collection schema, collection permissions, caller roles and query set — none of which change when one document's
$permissionschange. Later list requests from the same roles were served those rows with no permission check, for the caller-chosenttlof up to 24 hours. The TablesDB rows list inherits the handler.How
Name the hash field after the version the read was taken at, so the version is part of the lookup rather than a check preceding an unconditional write:
ListCache::save()writes its fields under<version>:<field>, and a newListCache::load()reads a field under a given version. A purge changes the version and drops the hash, so a field of a superseded version is both empty and unreachable: a fill that lands after the purge that should have stopped it ages out instead of being served.Documents/XListtakes the version once, before every query in the cached branch, and uses it for every lookup and save. A version it could not read now caches nothing and serves nothing, where before it would still read the hash.This also moves the three
try/catchblocks around the cache loads intoListCache::load(), and drops the secondgetVersion()the total path used to take.The cached-hit path now spends one extra
HGET(the version key), which the miss path already spent.Alternatives considered:
Versioned::compareAndSwap()applies the predicate and the write in one Lua script, but writes a single field and rotates the version, so the second and third field of one list response could never land;setLeaseGraceWindow()tombstones are ignored bysaveManyand are sized to a worst-case staleness nobody configures.Test plan
tests/unit/Databases/ListCacheTest.php— newtestReadSavedAfterAPurgeOvertookItsVersionIsNotServeddrives the exact interleaving through a named fake (Interleaved) that runs a permission write right after the version read answers, i.e. between the comparison and the write of the rows. It fails onmain('row read before the revocation'is served) and passes here. The existing assertions now read throughListCache::load(), the reader's view.tests/e2e/Services/Databases--filter=Cache: 59 passing, 4 pre-existingtestGetDocumentCacheEmptyfailures that reproduce identically on the base commit in this environment (database creation returns an empty body).testListDocumentsCacheBustedByDocumentPermissionChange(all six revocation flavors — update, bulk update, transaction, upsert, bulk upsert, transaction bulk upsert) passes, as dotestListDocumentsWithCache,testListDocumentsCacheBustedByPermissionChange,testListDocumentsCachePurgedByUpdateandtestListDocumentsCachedWithoutSelectQuery.composer lint,composer analyzeon the changed files, andcomposer refactor:checkare clean.🤖 Generated with Claude Code