Files
Drew McLellan 706dba62f9 fix(studio): attribute storage policies to every bucket they reference (#52692)
## Problem

Studio's storage policy parser (\`extractBucketNameFromDefinition\`)
returned only the first quoted string near \`bucket_id\` and ignored the
operator. As a result:

- Policies covering several buckets (\`bucket_id in ('a', 'b')\`) were
listed and counted under the first bucket only.
- Negated policies (\`bucket_id <> 'a'\`) were attributed to the
excluded bucket.
- \`DeleteBucketModal\` could delete a negated or shared policy that
still protects other buckets.

Fixes FE-3993, supabase/supabase#48164.

Before:

<img width="884" height="444" alt="Screenshot 2026-11-03 at 10 46 08"
src="https://github.com/user-attachments/assets/fc6e4842-b07f-40be-8057-b94f4034e4eb"
/>

After (same data):

<img width="891" height="515" alt="Screenshot 2026-11-03 at 10 46 21"
src="https://github.com/user-attachments/assets/47248b30-a309-4774-8349-05260ad02111"
/>


## Solution

- Replace the parser with \`extractBucketNamesFromDefinition\`, which
returns every bucket referenced via \`=\`, \`= ANY (ARRAY[...])\` and
\`OR\`, and returns none for negated conditions (those fall into
Ungrouped).
- Add \`getPolicyBucketNames\`, which combines \`definition\` and
\`check\`, and use it in the Policies page grouping,
\`useBucketPolicyCount\`, and \`DeleteBucketModal\` so they agree.
- A policy is now listed under each bucket it covers.
- Bucket deletion only removes policies whose sole target is the deleted
bucket. Shared and negated policies are left in place. This is the
conservative choice: an unparsed policy is left behind, never deleted.
- Added unit tests for the parser shapes, negation, \`check\`-only
policies, and grouping.
2026-11-04 11:36:11 +00:00

170 lines
6.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import type { Policy } from '@/components/interfaces/Database/Policies/PolicyTableRow/PolicyTableRow.utils'
import {
extractBucketNamesFromDefinition,
formatPoliciesForStorage,
getPolicyBucketNames,
UNGROUPED_POLICY_SYMBOL,
UNKNOWN_BUCKET_SYMBOL,
} from '@/components/interfaces/Storage/Storage.utils'
import type { Bucket } from '@/data/storage/buckets-query'
const makePolicy = (
name: string,
{
definition = null,
check = null,
command = 'ALL',
}: { definition?: string | null; check?: string | null; command?: Policy['command'] }
) => ({ name, definition, check, command }) as unknown as Policy
const makeBucket = (name: string) => ({ id: name, name }) as unknown as Bucket
describe('extractBucketNamesFromDefinition', () => {
it.each([
["(bucket_id = 'avatars'::text)", ['avatars']],
["((bucket_id = 'avatars'::text) AND (auth.role() = 'authenticated'::text))", ['avatars']],
["(bucket_id = ANY (ARRAY['avatars'::text, 'logos'::text]))", ['avatars', 'logos']],
["((bucket_id = 'a'::text) OR (bucket_id = 'b'::text))", ['a', 'b']],
["(bucket_id = 'bob''s files'::text)", ["bob's files"]],
["(auth.role() = 'authenticated'::text)", []],
[null, []],
])('extracts buckets from %s', (definition, expected) => {
expect(extractBucketNamesFromDefinition(definition)).toEqual(expected)
})
it.each([
"(bucket_id <> 'avatars'::text)",
"(bucket_id != 'avatars'::text)",
"(bucket_id <> ALL (ARRAY['avatars'::text, 'logos'::text]))",
"(NOT (bucket_id = 'avatars'::text))",
"(NOT (bucket_id = ANY (ARRAY['avatars'::text, 'logos'::text])))",
"((bucket_id = 'logos'::text) OR (bucket_id <> 'avatars'::text))",
])('returns no buckets for negated condition %s', (definition) => {
expect(extractBucketNamesFromDefinition(definition)).toEqual([])
})
})
describe('extractBucketNamesFromDefinition with unrestricted branches', () => {
it.each([
"((bucket_id = 'a'::text) OR (auth.uid() IS NOT NULL))",
"(bucket_id = 'a'::text OR auth.uid() IS NOT NULL)",
"((bucket_id = 'a'::text) OR true)",
])('returns no buckets when an OR branch is not bucket-limited: %s', (definition) => {
expect(extractBucketNamesFromDefinition(definition)).toEqual([])
})
it('still extracts buckets when an unrestricted condition is ANDed in', () => {
expect(
extractBucketNamesFromDefinition(
"((bucket_id = 'a'::text) AND ((auth.uid() IS NOT NULL) OR (owner = 'x'::text)))"
)
).toEqual(['a'])
})
it('does not split on OR/AND inside string literals', () => {
expect(extractBucketNamesFromDefinition("(bucket_id = 'this OR that'::text)")).toEqual([
'this OR that',
])
})
})
describe('getPolicyBucketNames', () => {
it('returns no buckets when definition has an unrestricted OR branch but check names a bucket', () => {
const policy = makePolicy('p', {
definition: "((bucket_id = 'a'::text) OR (auth.uid() IS NOT NULL))",
check: "(bucket_id = 'a'::text)",
})
expect(getPolicyBucketNames(policy)).toEqual([])
})
it('returns no buckets when only one of definition and check is negated', () => {
const policy = makePolicy('p', {
definition: "(bucket_id <> 'a'::text)",
check: "(bucket_id = 'a'::text)",
})
expect(getPolicyBucketNames(policy)).toEqual([])
})
it('reads buckets from check for INSERT policies, which have no using expression', () => {
const policy = makePolicy('p', { check: "(bucket_id = 'avatars'::text)", command: 'INSERT' })
expect(getPolicyBucketNames(policy)).toEqual(['avatars'])
})
it('reads buckets from using for SELECT policies', () => {
const policy = makePolicy('p', {
definition: "(bucket_id = 'avatars'::text)",
command: 'SELECT',
})
expect(getPolicyBucketNames(policy)).toEqual(['avatars'])
})
it('returns no buckets when using is not bucket-limited but check names a bucket', () => {
const policy = makePolicy('p', {
definition: "(auth.role() = 'authenticated'::text)",
check: "(bucket_id = 'avatars'::text)",
command: 'ALL',
})
expect(getPolicyBucketNames(policy)).toEqual([])
})
it('falls back to using for UPDATE policies without a check', () => {
const policy = makePolicy('p', {
definition: "(bucket_id = 'avatars'::text)",
command: 'UPDATE',
})
expect(getPolicyBucketNames(policy)).toEqual(['avatars'])
})
it('merges buckets from definition and check without duplicates', () => {
const policy = makePolicy('p', {
definition: "(bucket_id = 'a'::text)",
check: "(bucket_id = ANY (ARRAY['a'::text, 'b'::text]))",
})
expect(getPolicyBucketNames(policy)).toEqual(['a', 'b'])
})
})
describe('formatPoliciesForStorage', () => {
const buckets = [makeBucket('avatars'), makeBucket('logos')]
const groupNamesFor = (policies: Policy[]) =>
Object.fromEntries(
formatPoliciesForStorage(buckets, policies).map((group) => [
group.name,
group.policies.map((policy) => policy.name),
])
)
it('lists a multi-bucket policy under every bucket it covers', () => {
const multi = makePolicy('multi', {
definition: "(bucket_id = ANY (ARRAY['avatars'::text, 'logos'::text]))",
})
expect(groupNamesFor([multi])).toEqual({ avatars: ['multi'], logos: ['multi'] })
})
it('lists a negated policy as ungrouped rather than under the excluded bucket', () => {
const negated = makePolicy('negated', { definition: "(bucket_id <> 'avatars'::text)" })
const result = formatPoliciesForStorage(buckets, [negated])
expect(result).toHaveLength(1)
expect(result[0].name).toBe(UNGROUPED_POLICY_SYMBOL)
})
it('maps buckets that are not loaded to the unknown group, once', () => {
const policy = makePolicy('p', {
definition: "(bucket_id = ANY (ARRAY['x'::text, 'y'::text, 'logos'::text]))",
})
const result = formatPoliciesForStorage(buckets, [policy])
expect(result.map((group) => group.name)).toEqual([UNKNOWN_BUCKET_SYMBOL, 'logos'])
})
it('groups check-only policies under their bucket', () => {
const insert = makePolicy('insert', {
check: "(bucket_id = 'logos'::text)",
command: 'INSERT',
})
expect(groupNamesFor([insert])).toEqual({ logos: ['insert'] })
})
})