diff --git a/graphile/graphile-presigned-url-plugin/__tests__/bucket-config-cache.test.ts b/graphile/graphile-presigned-url-plugin/__tests__/bucket-config-cache.test.ts index ad9c5be459..ae75bab8a1 100644 --- a/graphile/graphile-presigned-url-plugin/__tests__/bucket-config-cache.test.ts +++ b/graphile/graphile-presigned-url-plugin/__tests__/bucket-config-cache.test.ts @@ -72,12 +72,36 @@ it('sees the physical name the reconciler records after a pre-reconcile read', a it('serves a reconciled bucket from the cache', async () => { const { clearBucketCache, getBucketConfig } = await import('../src/storage-module-cache'); clearBucketCache(); - const table = bucketTable(bucketRow({ physical_name: 'cnst-app-documents-0001' })); + const table = bucketTable(bucketRow({ physical_name: 'cnst-app-documents-0001', max_file_size: 100 })); await getBucketConfig(table, storageConfig(), DATABASE_ID, 'documents'); + table.row = bucketRow({ physical_name: 'cnst-app-documents-0001', max_file_size: 200 }); + const second = await getBucketConfig(table, storageConfig(), DATABASE_ID, 'documents'); + + expect(second?.max_file_size).toBe(100); +}); + +it('does not serve a cached bucket to a caller whose RLS hides it', async () => { + const { clearBucketCache, getBucketConfig } = await import('../src/storage-module-cache'); + clearBucketCache(); + const member = bucketTable(bucketRow({ physical_name: 'cnst-app-documents-0001' })); + const stranger = { async query() { return { rows: [] as unknown[] }; } }; + + await getBucketConfig(member, storageConfig(), DATABASE_ID, 'documents'); + + expect(await getBucketConfig(stranger, storageConfig(), DATABASE_ID, 'documents')).toBeNull(); +}); + +it('reloads when the visible bucket under the key is not the cached one', async () => { + const { clearBucketCache, getBucketConfig } = await import('../src/storage-module-cache'); + clearBucketCache(); + const table = bucketTable(bucketRow({ physical_name: 'cnst-app-documents-0001' })); + await getBucketConfig(table, storageConfig(), DATABASE_ID, 'documents'); + table.row = bucketRow({ id: '44444444-4444-4444-4444-444444444444', physical_name: 'cnst-app-documents-0002' }); + const after = await getBucketConfig(table, storageConfig(), DATABASE_ID, 'documents'); - expect(table.reads).toBe(1); + expect(after).toMatchObject({ id: '44444444-4444-4444-4444-444444444444', physical_name: 'cnst-app-documents-0002' }); }); it('re-reads a changed bucket row once its TTL passes, even under steady traffic', async () => { diff --git a/graphile/graphile-presigned-url-plugin/__tests__/storage-module-invalidation.test.ts b/graphile/graphile-presigned-url-plugin/__tests__/storage-module-invalidation.test.ts index 399373aa2f..667bfaaf8f 100644 --- a/graphile/graphile-presigned-url-plugin/__tests__/storage-module-invalidation.test.ts +++ b/graphile/graphile-presigned-url-plugin/__tests__/storage-module-invalidation.test.ts @@ -132,5 +132,6 @@ it('clears bucket metadata and fences an in-flight bucket read with its schema', clearStorageModuleCache(DATABASE); await getBucketConfig(client, config, DATABASE, 'documents'); - expect(client.query).toHaveBeenCalledTimes(3); + expect(client.query).toHaveBeenCalledTimes(4); + expect(client.query.mock.calls[3][0].text).toContain('physical_name'); }); diff --git a/graphile/graphile-presigned-url-plugin/src/storage-module-cache.ts b/graphile/graphile-presigned-url-plugin/src/storage-module-cache.ts index 5217e2f3ca..52756e692a 100644 --- a/graphile/graphile-presigned-url-plugin/src/storage-module-cache.ts +++ b/graphile/graphile-presigned-url-plugin/src/storage-module-cache.ts @@ -248,12 +248,9 @@ export function resolveStorageConfigFromCodec( * LRU cache for per-database bucket metadata. * * Buckets are essentially static config — created once and rarely changed. - * Caching avoids a DB query on every requestUploadUrl call. The bucket - * lookup in the plugin runs under RLS, but since AuthzEntityMembership - * grants all org members access to all org buckets, and the cached data - * is just config (mime types, size limits), bypassing RLS on cache hits - * is safe. The important RLS is on the files table (INSERT/UPDATE), - * which is never cached. + * A cache hit still reads the bucket's id under the caller's RLS: callers of + * the same database do not all see the same buckets (an unapproved user sees + * none), so the cached config is returned only when that visible id matches. * * Only reconciled rows are cached: a row without a recorded `physical_name` * is still waiting on the reconciler, so it is re-read until the name lands @@ -310,7 +307,8 @@ export function storedPhysicalName(row: { physical_name?: string | null }): stri * Resolve bucket metadata for a given database + bucket key, using the LRU cache. * * On cache miss, queries the bucket table (RLS-enforced via pgSettings on - * the pgClient). On cache hit, returns the cached metadata directly. + * the pgClient). On cache hit, returns the cached metadata only if the caller + * can see a bucket with the cached id under the same key. * * @param pgClient - A pg client from the Graphile context * @param storageConfig - The resolved StorageModuleConfig for this database/scope @@ -329,31 +327,35 @@ export async function getBucketConfig( byDatabase = false, ): Promise { const cacheKey = `bucket:${databaseId}:${storageConfig.id}:${bucketKey}${ownerId ? `:${ownerId}` : ''}`; + // Entity-keyed planes (the module records an entity table) use the + // (owner_id, key) composite lookup; app- and database-scope planes just use key. + // Database-keyed planes hold every database's buckets under the same keys. + const isEntityScoped = storageConfig.entityTableId !== null; + const hasOwner = ownerId && isEntityScoped; + const scopeFilter = byDatabase ? ` AND database_id = $${hasOwner ? 3 : 2}` : ''; + const where = `${hasOwner ? 'key = $1 AND owner_id = $2' : 'key = $1'}${scopeFilter}`; + const values = [bucketKey, ...(hasOwner ? [ownerId] : []), ...(byDatabase ? [databaseId] : [])]; + const cached = bucketCache.get(cacheKey); if (cached) { - return cached; + const visible = await pgClient.query({ + text: `SELECT id FROM ${storageConfig.bucketsQualifiedName} WHERE ${where} LIMIT 1`, + values, + }); + const visibleId = (visible.rows[0] as { id?: string } | undefined)?.id; + if (!visibleId) return null; + if (visibleId === cached.id) return cached; } log.debug(`Bucket cache miss for ${databaseId}:${bucketKey}${ownerId ? ` (owner=${ownerId})` : ''}, querying DB...`); return readAndCache(databaseId, 'bucket', async () => { - // Entity-keyed planes (the module records an entity table) use the - // (owner_id, key) composite lookup; app- and database-scope planes just use key. - // Database-keyed planes hold every database's buckets under the same keys. - const isEntityScoped = storageConfig.entityTableId !== null; - const hasOwner = ownerId && isEntityScoped; - const scopeFilter = byDatabase ? ` AND database_id = $${hasOwner ? 3 : 2}` : ''; const result = await pgClient.query({ - text: hasOwner - ? `SELECT id, key, type, is_public, owner_id, allowed_mime_types, max_file_size, allow_custom_keys, physical_name - FROM ${storageConfig.bucketsQualifiedName} - WHERE key = $1 AND owner_id = $2${scopeFilter} - LIMIT 1` - : `SELECT id, key, type, is_public, ${isEntityScoped ? 'owner_id,' : ''} allowed_mime_types, max_file_size, allow_custom_keys, physical_name + text: `SELECT id, key, type, is_public, ${isEntityScoped ? 'owner_id,' : ''} allowed_mime_types, max_file_size, allow_custom_keys, physical_name FROM ${storageConfig.bucketsQualifiedName} - WHERE key = $1${scopeFilter} + WHERE ${where} LIMIT 1`, - values: [bucketKey, ...(hasOwner ? [ownerId] : []), ...(byDatabase ? [databaseId] : [])], + values, }); if (result.rows.length === 0) {