Skip to content

fix(presigned-url): a bucket cache hit re-reads the bucket id under the caller's RLS - #2007

Open
theothersideofgod wants to merge 2 commits into
mainfrom
feat/presigned-bucket-cache-rls
Open

theothersideofgod wants to merge 2 commits into
mainfrom
feat/presigned-bucket-cache-rls

Conversation

@theothersideofgod

Copy link
Copy Markdown
Contributor

Why

A user who may not see a bucket must get the same answer from uploadAppFile whether or not someone else used that bucket a moment ago.

getBucketConfig caches a bucket's config per database, storage module, key and owner, not per caller. Only a cache miss read the row under the request role's RLS. A hit returned the cached row to anyone. The cache's comment gave the reason: "since AuthzEntityMembership grants all org members access to all org buckets ... bypassing RLS on cache hits is safe". That holds only for callers who are members. A signed-up user who is not approved sees no bucket at all. If such a user names a bucket that another user already loaded into the cache, they get its config. The files INSERT is then refused by RLS with an error that carries the bucket's id.

Entry point E1 (GraphQL API), role authenticated. Anyone who can sign up on the tenant can do it. The request is uploadAppFile(input: { bucketKey: "default", ... }). Impact is low: the caller learns that the bucket exists and its id. Nothing is written and no file data is exposed.

Rules touched: R9, R13

Before / after

Live, on a local fun up (GraphQL image constructive:9b764d6, whose graphile-presigned-url-plugin source is identical to main). The user is erin, freshly signed up and not approved (appMemberships.isApproved = false, appBuckets returns 0 rows). The owner is alice. The cache was cold because the server had just restarted.

step before (9b764d6) after (9b764d6 + this file)
erin, bucketKey: "default", cold cache BUCKET_NOT_FOUND BUCKET_NOT_FOUND
alice, bucketKey: "default" ok ok
erin, bucketKey: "default", after alice Parent not found: 01a1d5ba-b597-… (the bucket's real id) BUCKET_NOT_FOUND
erin, a key nobody loaded / a key that does not exist BUCKET_NOT_FOUND BUCKET_NOT_FOUND
alice again, twice (cache warm) ok ok

For "after", only the built dist/storage-module-cache.js (CJS and ESM) was layered onto the pinned image. graphql-public was pointed at that image and then set back to the pinned one.

Unit:

cd graphile/graphile-presigned-url-plugin
npx jest __tests__/bucket-config-cache.test.ts      # test commit only
  ✕ does not serve a cached bucket to a caller whose RLS hides it   (Received: the cached bucket)
  ✕ reloads when the visible bucket under the key is not the cached one
  Tests: 2 failed, 3 passed
npx jest --testPathIgnorePatterns integration       # this branch
  Tests: 80 passed, 80 total   (rebased on 1dceb60e5)

s3-signer.integration.test.ts was not run here. It needs a RustFS on its default endpoint. This diff does not touch it.

The change

On a hit, getBucketConfig first runs SELECT id FROM <buckets> WHERE <the same key / owner / database filter> LIMIT 1 on the caller's client, so the request role's RLS applies. It then:

  • returns null when the caller sees no row (BUCKET_NOT_FOUND / DATABASE_ACCESS_DENIED, as on a cold cache);
  • returns the cached config when the visible id equals the cached id;
  • otherwise reloads, as on a miss. The bucket under that key was replaced, and the reload goes through the existing invalidation fence (readAndCache).

The miss query is unchanged. Its two SELECT variants now share the where / values the hit check uses. The column list is identical in every case, because hasOwner implies isEntityScoped.

This is the approach of the cache-hit block in draft #1655 (exact id re-read on every hit; "cached data never substitutes for row authorization"). This PR is that one piece on current main. It leaves out #1655's build-scoped caches and ambiguity checks.

Is this how it was meant to work

No. Three sources:

  • The upload path without bucketKey already refuses this caller before any bucket is read. On 2026-11-13, an unapproved user's uploadAppFile with no bucketKey returned STORAGE_SCOPE_MEMBERSHIP_REQUIRED (resolve_default_bucket requires the scope membership first).
  • The cache's author flagged the assumption for review when it was added. feat: add LRU bucket metadata cache to presigned-url-plugin #946 (2026-04-01): "RLS bypass on cache hits ... Verify that all org members should always have identical bucket visibility". The assumption was never tested.
  • Isolate Graphile plugin and storage data paths #1655 (draft, 2026-09-06) drops it.

How it came about:

The existing tests (bucket-config-cache.test.ts) use one fake client for every call. No test asked as a second caller.

Behavior changes

was now why
uploadAppFile / bulk upload, caller cannot see the bucket, bucket cached by someone else proceeds to the files INSERT, fails Parent not found: <bucket id> BUCKET_NOT_FOUND (or DATABASE_ACCESS_DENIED when databaseId is given) the same answer as a cold cache
cost of a cache hit no query one indexed SELECT id under RLS the visibility check

No error text, GraphQL description or README changes. The doc comments on the cache and getBucketConfig now describe the hit check.

Rule point: storage-module-invalidation.test.ts ("clears bucket metadata and fences an in-flight bucket read", 770f892) counted 3 queries, which pinned "a hit makes no query". It now counts 4 and asserts that the call after the clear is a full read. Its fence and toBe(fresh) assertions are unchanged. The serves a reconciled bucket from the cache test asserted one read. It now asserts the observable part: config changed in the table is not seen until the TTL ends.

Considered

  • Remove the bucket cache. With the id check, a hit and a miss each cost one indexed query. The cache now saves only the wider column list. Deleting it would remove the LRU, its TTL and reconcile handling, and the exported clearBucketCache.
  • Key the cache by caller. This does not fix the problem: RLS decides visibility per row, not per caller key, and it multiplies entries per user.
  • Return BUCKET_NOT_FOUND at the INSERT failure. This hides the message and still hands the caller config it may not read.

Recommended: this PR's id check, because it is the design in #1655 (so that stack rebases onto it unchanged) and it keeps the exported API. Removing the cache is the smaller code. It is the better choice if #1655 is not going to land.

Not done here: on entity-scoped planes (org buckets), a non-member naming ownerId of an org whose bucket is cached takes the same hit path. The fix covers it, because the hit check carries owner_id. I did not drive it live: the test tenant has only an app-scope storage module.

Generated with Devin

theothersideofgods and others added 2 commits November 16, 2026 18:16
An unapproved user who asks for a bucket another user already loaded gets
the cached row, and the file INSERT then fails with the bucket's id in the
error. These tests fail until the cache hit re-reads the id under RLS.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
The bucket cache returned a row loaded by one caller to every caller of the
database, on the assumption that all members see all buckets. An unapproved
user sees none, so a hit leaked the bucket's existence and id. A hit now
reads the id with the caller's RLS and returns the cached config only when
it matches.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@luxor-code-review

luxor-code-review Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR tightens the shared bucket-config cache in the presigned-URL plugin: even on a cache hit, the storage module now re-reads the bucket id under the caller's RLS settings and only serves the cached config when the freshly visible row id matches the cached entry, evicting and reloading otherwise.

Files Change
graphile/graphile-presigned-url-plugin/src/storage-module-cache.ts Adds an RLS visibility probe on the cache-hit path with mismatch-triggered reload.
graphile/graphile-presigned-url-plugin/__tests__/bucket-config-cache.test.ts Extends bucket cache tests for the new hit-path visibility semantics.
graphile/graphile-presigned-url-plugin/__tests__/storage-module-invalidation.test.ts Extends invalidation tests to cover the tightened recheck behavior.

Reviewer note: two low-severity observations (cache-hit path bypasses pendingReads fencing; unordered LIMIT 1 probe is nondeterministic when multiple rows match) were verified but fall below the reporting confidence threshold, so they are noted here rather than as inline comments.

Reviewed commit: df536ec

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.

2 participants