Datasets:
File size: 145,788 Bytes
037dbef | 1 2 3 4 5 6 7 8 9 10 11 12 | {"id": "T028", "title": "Kanban boards that become read-only when archived", "tier": 2, "split": "dev", "tags": ["membership", "role-column", "negative-condition", "client-returning"], "traps": ["negative-condition", "recursive-policy", "weak-with-check"], "extra_tables": [], "fixture_notes": ["archived board 2 where cy (…03) is editor and ana (…01) viewer", "viewer on active board: ben (…02)", "user on no board: dee (…04)"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A kanban app built with supabase-js. Board members are viewers or editors. Cards are created with `supabase.from('cards').insert({...}).select()` and moved or edited with `.update({...}).eq('id', id).select()`.\n\nR1. Members can see the boards they belong to and every member of those boards.\nR2. Members can read all cards on their boards.\nR3. Editors can create, edit, move between columns and delete cards on their boards. A card can only end up on a board where the user is an editor.\nR4. Viewers cannot create, change or delete cards.\nR5. Archived boards are read-only: nobody can create, change or delete cards on an archived board, not even its editors.\nR6. Boards and memberships cannot be changed through the app.\nR7. Anonymous visitors cannot see or change anything.\nR8. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.boards (\n id bigint primary key,\n name text not null,\n is_archived boolean not null default false\n);\n\ncreate table public.board_members (\n board_id bigint not null references public.boards(id),\n user_id uuid not null references auth.users(id),\n role text not null check (role in ('viewer', 'editor')),\n primary key (board_id, user_id)\n);\n\ncreate table public.cards (\n id bigint generated by default as identity primary key,\n board_id bigint not null references public.boards(id),\n title text not null,\n column_name text not null default 'todo' check (column_name in ('todo', 'doing', 'done')),\n assignee_id uuid references auth.users(id)\n);\n\ncreate index board_members_user_id_idx on public.board_members (user_id);\ncreate index cards_board_id_idx on public.cards (board_id);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com'),\n ('00000000-0000-0000-0000-000000000003', 'cy@example.com'),\n ('00000000-0000-0000-0000-000000000004', 'dee@example.com');\n\ninsert into public.boards (id, name, is_archived) values\n (1, 'Launch', false),\n (2, 'Q1 2020', true);\n\ninsert into public.board_members (board_id, user_id, role) values\n (1, '00000000-0000-0000-0000-000000000001', 'editor'),\n (1, '00000000-0000-0000-0000-000000000002', 'viewer'),\n (2, '00000000-0000-0000-0000-000000000001', 'viewer'),\n (2, '00000000-0000-0000-0000-000000000003', 'editor');\n\ninsert into public.cards (id, board_id, title, column_name, assignee_id) values\n (1, 1, 'Write copy', 'todo', '00000000-0000-0000-0000-000000000002'),\n (2, 1, 'Design hero', 'doing', null),\n (3, 2, 'Old task', 'done', null);\n\nselect setval(pg_get_serial_sequence('public.cards', 'id'), 3);\n", "tests_yaml": "- name: member_sees_own_boards\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from boards order by id\n expect:\n rows:\n - [1]\n - [2]\n\n- name: viewer_sees_only_own_board\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n sql: select id from boards order by id\n expect:\n rows:\n - [1]\n\n- name: outsider_sees_no_boards\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n sql: select id from boards\n expect: {count: 0}\n\n- name: member_sees_board_members\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n sql: select user_id from board_members order by user_id\n expect:\n rows:\n - [00000000-0000-0000-0000-000000000001]\n - [00000000-0000-0000-0000-000000000002]\n\n- name: member_reads_board_cards\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n sql: select id from cards order by id\n expect:\n rows:\n - [1]\n - [2]\n\n- name: member_of_archived_board_reads_its_cards\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select id from cards order by id\n expect:\n rows:\n - [3]\n\n- name: editor_creates_card\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: cards\n values: {board_id: 1, title: QA pass, column_name: todo}\n returning: true\n expect: {affected: 1}\n\n- name: editor_moves_card\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: cards\n values: {column_name: done}\n match: {id: 2}\n returning: true\n expect: {affected: 1}\n\n- name: editor_deletes_card\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: cards\n match: {id: 2}\n expect: {affected: 1}\n\n- name: editor_cannot_move_card_to_board_where_viewer\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: cards\n values: {board_id: 2}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: viewer_cannot_move_card\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: update\n table: cards\n values: {column_name: doing}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: viewer_cannot_create_card\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: insert\n table: cards\n values: {board_id: 1, title: x, column_name: todo}\n returning: false\n expect: {denied: true}\n\n- name: editor_cannot_add_card_to_archived_board\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: insert\n table: cards\n values: {board_id: 2, title: Late item, column_name: todo}\n returning: false\n expect: {denied: true}\n\n- name: editor_cannot_edit_card_on_archived_board\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: cards\n values: {title: Reopened}\n match: {id: 3}\n returning: false\n expect: {denied: true}\n\n- name: editor_cannot_delete_card_on_archived_board\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: delete\n table: cards\n match: {id: 3}\n expect: {denied: true}\n\n- name: editor_cannot_unarchive_board\n covers: [R6]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: boards\n values: {is_archived: false}\n match: {id: 2}\n returning: false\n expect: {denied: true}\n\n- name: viewer_cannot_promote_self\n covers: [R6]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: update\n table: board_members\n values: {role: editor}\n match: {board_id: 1, user_id: 00000000-0000-0000-0000-000000000002}\n returning: false\n expect: {denied: true}\n\n- name: anon_sees_no_cards\n covers: [R7]\n as: {role: anon}\n sql: select * from cards\n expect: {denied: true}\n\n- name: service_role_sees_all_cards\n covers: [R8]\n as: {role: service_role}\n sql: select count(*) from cards\n expect:\n rows:\n - [3]\n", "n_tests": 19, "solution_sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.board_role(p_board_id bigint) returns text\nlanguage sql stable security definer set search_path = ''\nas $$\n select m.role from public.board_members m where m.board_id = p_board_id and m.user_id = auth.uid()\n$$;\n\ncreate function private.board_archived(p_board_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select coalesce((select b.is_archived from public.boards b where b.id = p_board_id), true)\n$$;\n\nalter table public.boards enable row level security;\nalter table public.board_members enable row level security;\nalter table public.cards enable row level security;\n\ncreate policy boards_select on public.boards\n for select to authenticated\n using (private.board_role(id) is not null);\n\ncreate policy board_members_select on public.board_members\n for select to authenticated\n using (private.board_role(board_id) is not null);\n\ncreate policy cards_select on public.cards\n for select to authenticated\n using (private.board_role(board_id) is not null);\n\ncreate policy cards_insert on public.cards\n for insert to authenticated\n with check (private.board_role(board_id) = 'editor' and not private.board_archived(board_id));\n\ncreate policy cards_update on public.cards\n for update to authenticated\n using (private.board_role(board_id) = 'editor' and not private.board_archived(board_id))\n with check (private.board_role(board_id) = 'editor' and not private.board_archived(board_id));\n\ncreate policy cards_delete on public.cards\n for delete to authenticated\n using (private.board_role(board_id) = 'editor' and not private.board_archived(board_id));\n", "trap_files": [{"name": "negative-condition", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.board_role(p_board_id bigint) returns text\nlanguage sql stable security definer set search_path = ''\nas $$\n select m.role from public.board_members m where m.board_id = p_board_id and m.user_id = auth.uid()\n$$;\n\ncreate function private.board_archived(p_board_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select coalesce((select b.is_archived from public.boards b where b.id = p_board_id), true)\n$$;\n\nalter table public.boards enable row level security;\nalter table public.board_members enable row level security;\nalter table public.cards enable row level security;\n\ncreate policy boards_select on public.boards\n for select to authenticated\n using (private.board_role(id) is not null);\n\ncreate policy board_members_select on public.board_members\n for select to authenticated\n using (private.board_role(board_id) is not null);\n\ncreate policy cards_select on public.cards\n for select to authenticated\n using (private.board_role(board_id) is not null);\n\ncreate policy cards_insert on public.cards\n for insert to authenticated\n with check (private.board_role(board_id) = 'editor');\n\ncreate policy cards_update on public.cards\n for update to authenticated\n using (private.board_role(board_id) = 'editor')\n with check (private.board_role(board_id) = 'editor');\n\ncreate policy cards_delete on public.cards\n for delete to authenticated\n using (private.board_role(board_id) = 'editor');\n"}, {"name": "recursive-policy", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.board_role(p_board_id bigint) returns text\nlanguage sql stable security definer set search_path = ''\nas $$\n select m.role from public.board_members m where m.board_id = p_board_id and m.user_id = auth.uid()\n$$;\n\ncreate function private.board_archived(p_board_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select coalesce((select b.is_archived from public.boards b where b.id = p_board_id), true)\n$$;\n\nalter table public.boards enable row level security;\nalter table public.board_members enable row level security;\nalter table public.cards enable row level security;\n\ncreate policy boards_select on public.boards\n for select to authenticated\n using (private.board_role(id) is not null);\n\ncreate policy board_members_select on public.board_members\n for select to authenticated\n using (exists (\n select 1 from public.board_members mine\n where mine.board_id = board_members.board_id and mine.user_id = auth.uid()\n ));\n\ncreate policy cards_select on public.cards\n for select to authenticated\n using (private.board_role(board_id) is not null);\n\ncreate policy cards_insert on public.cards\n for insert to authenticated\n with check (private.board_role(board_id) = 'editor' and not private.board_archived(board_id));\n\ncreate policy cards_update on public.cards\n for update to authenticated\n using (private.board_role(board_id) = 'editor' and not private.board_archived(board_id))\n with check (private.board_role(board_id) = 'editor' and not private.board_archived(board_id));\n\ncreate policy cards_delete on public.cards\n for delete to authenticated\n using (private.board_role(board_id) = 'editor' and not private.board_archived(board_id));\n"}, {"name": "weak-with-check", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.board_role(p_board_id bigint) returns text\nlanguage sql stable security definer set search_path = ''\nas $$\n select m.role from public.board_members m where m.board_id = p_board_id and m.user_id = auth.uid()\n$$;\n\ncreate function private.board_archived(p_board_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select coalesce((select b.is_archived from public.boards b where b.id = p_board_id), true)\n$$;\n\nalter table public.boards enable row level security;\nalter table public.board_members enable row level security;\nalter table public.cards enable row level security;\n\ncreate policy boards_select on public.boards\n for select to authenticated\n using (private.board_role(id) is not null);\n\ncreate policy board_members_select on public.board_members\n for select to authenticated\n using (private.board_role(board_id) is not null);\n\ncreate policy cards_select on public.cards\n for select to authenticated\n using (private.board_role(board_id) is not null);\n\ncreate policy cards_insert on public.cards\n for insert to authenticated\n with check (private.board_role(board_id) = 'editor' and not private.board_archived(board_id));\n\ncreate policy cards_update on public.cards\n for update to authenticated\n using (private.board_role(board_id) = 'editor' and not private.board_archived(board_id))\n with check (private.board_role(board_id) is not null);\n\ncreate policy cards_delete on public.cards\n for delete to authenticated\n using (private.board_role(board_id) = 'editor' and not private.board_archived(board_id));\n"}]}
{"id": "T033", "title": "Friend requests and friends-only posts", "tier": 2, "split": "dev", "tags": ["social-graph", "audience", "state-transition", "column-restriction", "client-returning"], "traps": ["state-transition", "private-row-leak", "old-vs-new"], "extra_tables": [], "fixture_notes": ["accepted: ana–ben, dee–cy; pending: cy → ana", "posts with each audience: public 1 and 6, friends 2/4/5, only_me 3"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A social app built with supabase-js. Friend requests are sent with `supabase.from('friendships').insert({ requester_id, addressee_id }).select()` and accepted with `.update({ status: 'accepted' }).match({ requester_id, addressee_id })`. Two users are friends when a friendship between them, in either direction, is `accepted`.\n\nR1. Signed-in users can read public posts, posts with audience `friends` by people they are friends with, and all of their own posts. Anonymous visitors can read public posts only.\nR2. Users can write, edit and delete only their own posts, and a post can never be given to another author.\nR3. Users can send friend requests as the requester, and new requests are always pending.\nR4. Only the addressee of a pending request can accept it. Requesters cannot accept their own requests, and nothing else about a friendship can be changed.\nR5. Either person can delete a friendship or request (unfriend, cancel or decline). Users can see the friendships and requests they are part of.\nR6. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.friendships (\n requester_id uuid not null references auth.users(id),\n addressee_id uuid not null references auth.users(id),\n status text not null default 'pending' check (status in ('pending', 'accepted')),\n primary key (requester_id, addressee_id),\n check (requester_id <> addressee_id)\n);\n\ncreate table public.posts (\n id bigint generated by default as identity primary key,\n author_id uuid not null default auth.uid() references auth.users(id),\n body text not null,\n audience text not null default 'public' check (audience in ('public', 'friends', 'only_me'))\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com'),\n ('00000000-0000-0000-0000-000000000003', 'cy@example.com'),\n ('00000000-0000-0000-0000-000000000004', 'dee@example.com');\n\ninsert into public.friendships (requester_id, addressee_id, status) values\n ('00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000002', 'accepted'),\n ('00000000-0000-0000-0000-000000000003', '00000000-0000-0000-0000-000000000001', 'pending'),\n ('00000000-0000-0000-0000-000000000004', '00000000-0000-0000-0000-000000000003', 'accepted');\n\ninsert into public.posts (id, author_id, body, audience) values\n (1, '00000000-0000-0000-0000-000000000001', 'Hello world', 'public'),\n (2, '00000000-0000-0000-0000-000000000001', 'Moving next month', 'friends'),\n (3, '00000000-0000-0000-0000-000000000001', 'Note to self', 'only_me'),\n (4, '00000000-0000-0000-0000-000000000002', 'Party on Friday', 'friends'),\n (5, '00000000-0000-0000-0000-000000000003', 'New job!', 'friends'),\n (6, '00000000-0000-0000-0000-000000000004', 'Sunset photo', 'public');\n\nselect setval(pg_get_serial_sequence('public.posts', 'id'), 6);\n", "tests_yaml": "- name: friend_reads_friends_posts\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n sql: select id from posts order by id\n expect:\n rows:\n - [1]\n - [2]\n - [4]\n - [6]\n\n- name: author_reads_all_own_posts\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from posts order by id\n expect:\n rows:\n - [1]\n - [2]\n - [3]\n - [4]\n - [6]\n\n- name: pending_request_grants_no_friends_posts\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select id from posts order by id\n expect:\n rows:\n - [1]\n - [5]\n - [6]\n\n- name: anon_reads_public_posts\n covers: [R1]\n as: {role: anon}\n sql: select id from posts order by id\n expect:\n rows:\n - [1]\n - [6]\n\n- name: friend_cannot_read_only_me_post\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n sql: select id from posts where id = 3\n expect: {count: 0}\n\n- name: user_writes_post\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: posts\n values: {author_id: 00000000-0000-0000-0000-000000000001, body: Hi friends, audience: friends}\n returning: true\n expect: {affected: 1}\n\n- name: user_cannot_post_as_someone_else\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: posts\n values: {author_id: 00000000-0000-0000-0000-000000000002, body: spoof, audience: public}\n returning: false\n expect: {denied: true}\n\n- name: user_edits_own_post\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: posts\n values: {body: Hello again}\n match: {id: 1}\n returning: true\n expect: {affected: 1}\n\n- name: user_cannot_edit_friends_post\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: posts\n values: {body: x}\n match: {id: 4}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_give_post_to_someone_else\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: posts\n values: {author_id: 00000000-0000-0000-0000-000000000002}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: user_deletes_own_post\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: posts\n match: {id: 3}\n expect: {affected: 1}\n\n- name: user_sends_friend_request\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: insert\n table: friendships\n values: {requester_id: 00000000-0000-0000-0000-000000000002, addressee_id: 00000000-0000-0000-0000-000000000003}\n returning: true\n expect: {affected: 1}\n\n- name: cannot_send_request_as_someone_else\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: insert\n table: friendships\n values: {requester_id: 00000000-0000-0000-0000-000000000003, addressee_id: 00000000-0000-0000-0000-000000000004}\n returning: false\n expect: {denied: true}\n\n- name: cannot_create_accepted_friendship\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: insert\n table: friendships\n values: {requester_id: 00000000-0000-0000-0000-000000000002, addressee_id: 00000000-0000-0000-0000-000000000003, status: accepted}\n returning: false\n expect: {denied: true}\n\n- name: addressee_accepts_request\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: friendships\n values: {status: accepted}\n match: {requester_id: 00000000-0000-0000-0000-000000000003, addressee_id: 00000000-0000-0000-0000-000000000001}\n returning: true\n expect: {affected: 1}\n\n- name: requester_cannot_accept_own_request\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: friendships\n values: {status: accepted}\n match: {requester_id: 00000000-0000-0000-0000-000000000003, addressee_id: 00000000-0000-0000-0000-000000000001}\n returning: false\n expect: {denied: true}\n\n- name: addressee_cannot_rewire_request\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: friendships\n values: {requester_id: 00000000-0000-0000-0000-000000000004, status: accepted}\n match: {requester_id: 00000000-0000-0000-0000-000000000003, addressee_id: 00000000-0000-0000-0000-000000000001}\n returning: false\n expect: {denied: true}\n\n- name: outsider_cannot_accept_request\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: update\n table: friendships\n values: {status: accepted}\n match: {requester_id: 00000000-0000-0000-0000-000000000003, addressee_id: 00000000-0000-0000-0000-000000000001}\n returning: false\n expect: {denied: true}\n\n- name: user_unfriends\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: friendships\n match: {requester_id: 00000000-0000-0000-0000-000000000001, addressee_id: 00000000-0000-0000-0000-000000000002}\n expect: {affected: 1}\n\n- name: requester_cancels_request\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: delete\n table: friendships\n match: {requester_id: 00000000-0000-0000-0000-000000000003, addressee_id: 00000000-0000-0000-0000-000000000001}\n expect: {affected: 1}\n\n- name: outsider_cannot_delete_friendship\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: delete\n table: friendships\n match: {requester_id: 00000000-0000-0000-0000-000000000004, addressee_id: 00000000-0000-0000-0000-000000000003}\n expect: {denied: true}\n\n- name: user_sees_own_friendships\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select requester_id, addressee_id from friendships order by requester_id\n expect:\n rows:\n - [00000000-0000-0000-0000-000000000001, 00000000-0000-0000-0000-000000000002]\n - [00000000-0000-0000-0000-000000000003, 00000000-0000-0000-0000-000000000001]\n\n- name: anon_sees_no_friendships\n covers: [R1, R5]\n as: {role: anon}\n sql: select * from friendships\n expect: {denied: true}\n\n- name: service_role_sees_all_posts\n covers: [R6]\n as: {role: service_role}\n sql: select count(*) from posts\n expect:\n rows:\n - [6]\n", "n_tests": 24, "solution_sql": "create schema private;\n\ngrant usage on schema private to anon, authenticated;\n\ncreate function private.are_friends(p_other uuid) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (\n select 1 from public.friendships f\n where f.status = 'accepted'\n and ((f.requester_id = auth.uid() and f.addressee_id = p_other)\n or (f.addressee_id = auth.uid() and f.requester_id = p_other))\n )\n$$;\n\nalter table public.friendships enable row level security;\nalter table public.posts enable row level security;\n\nrevoke update on public.friendships from anon, authenticated;\ngrant update (status) on public.friendships to authenticated;\n\ncreate policy posts_select on public.posts\n for select to anon, authenticated\n using (\n audience = 'public'\n or author_id = auth.uid()\n or (audience = 'friends' and private.are_friends(author_id))\n );\n\ncreate policy posts_insert on public.posts\n for insert to authenticated\n with check (author_id = auth.uid());\n\ncreate policy posts_update on public.posts\n for update to authenticated\n using (author_id = auth.uid())\n with check (author_id = auth.uid());\n\ncreate policy posts_delete on public.posts\n for delete to authenticated\n using (author_id = auth.uid());\n\ncreate policy friendships_select on public.friendships\n for select to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid());\n\ncreate policy friendships_insert on public.friendships\n for insert to authenticated\n with check (requester_id = auth.uid() and status = 'pending');\n\ncreate policy friendships_update on public.friendships\n for update to authenticated\n using (addressee_id = auth.uid() and status = 'pending')\n with check (addressee_id = auth.uid() and status = 'accepted');\n\ncreate policy friendships_delete on public.friendships\n for delete to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid());\n", "trap_files": [{"name": "old-vs-new", "sql": "create schema private;\n\ngrant usage on schema private to anon, authenticated;\n\ncreate function private.are_friends(p_other uuid) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (\n select 1 from public.friendships f\n where f.status = 'accepted'\n and ((f.requester_id = auth.uid() and f.addressee_id = p_other)\n or (f.addressee_id = auth.uid() and f.requester_id = p_other))\n )\n$$;\n\nalter table public.friendships enable row level security;\nalter table public.posts enable row level security;\n\ncreate policy posts_select on public.posts\n for select to anon, authenticated\n using (\n audience = 'public'\n or author_id = auth.uid()\n or (audience = 'friends' and private.are_friends(author_id))\n );\n\ncreate policy posts_insert on public.posts\n for insert to authenticated\n with check (author_id = auth.uid());\n\ncreate policy posts_update on public.posts\n for update to authenticated\n using (author_id = auth.uid())\n with check (author_id = auth.uid());\n\ncreate policy posts_delete on public.posts\n for delete to authenticated\n using (author_id = auth.uid());\n\ncreate policy friendships_select on public.friendships\n for select to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid());\n\ncreate policy friendships_insert on public.friendships\n for insert to authenticated\n with check (requester_id = auth.uid() and status = 'pending');\n\ncreate policy friendships_update on public.friendships\n for update to authenticated\n using (addressee_id = auth.uid() and status = 'pending')\n with check (addressee_id = auth.uid() and status = 'accepted');\n\ncreate policy friendships_delete on public.friendships\n for delete to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid());\n"}, {"name": "private-row-leak", "sql": "create schema private;\n\ngrant usage on schema private to anon, authenticated;\n\ncreate function private.are_friends(p_other uuid) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (\n select 1 from public.friendships f\n where ((f.requester_id = auth.uid() and f.addressee_id = p_other)\n or (f.addressee_id = auth.uid() and f.requester_id = p_other))\n )\n$$;\n\nalter table public.friendships enable row level security;\nalter table public.posts enable row level security;\n\nrevoke update on public.friendships from anon, authenticated;\ngrant update (status) on public.friendships to authenticated;\n\ncreate policy posts_select on public.posts\n for select to anon, authenticated\n using (\n audience = 'public'\n or author_id = auth.uid()\n or (audience = 'friends' and private.are_friends(author_id))\n );\n\ncreate policy posts_insert on public.posts\n for insert to authenticated\n with check (author_id = auth.uid());\n\ncreate policy posts_update on public.posts\n for update to authenticated\n using (author_id = auth.uid())\n with check (author_id = auth.uid());\n\ncreate policy posts_delete on public.posts\n for delete to authenticated\n using (author_id = auth.uid());\n\ncreate policy friendships_select on public.friendships\n for select to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid());\n\ncreate policy friendships_insert on public.friendships\n for insert to authenticated\n with check (requester_id = auth.uid() and status = 'pending');\n\ncreate policy friendships_update on public.friendships\n for update to authenticated\n using (addressee_id = auth.uid() and status = 'pending')\n with check (addressee_id = auth.uid() and status = 'accepted');\n\ncreate policy friendships_delete on public.friendships\n for delete to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid());\n"}, {"name": "state-transition", "sql": "create schema private;\n\ngrant usage on schema private to anon, authenticated;\n\ncreate function private.are_friends(p_other uuid) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (\n select 1 from public.friendships f\n where f.status = 'accepted'\n and ((f.requester_id = auth.uid() and f.addressee_id = p_other)\n or (f.addressee_id = auth.uid() and f.requester_id = p_other))\n )\n$$;\n\nalter table public.friendships enable row level security;\nalter table public.posts enable row level security;\n\nrevoke update on public.friendships from anon, authenticated;\ngrant update (status) on public.friendships to authenticated;\n\ncreate policy posts_select on public.posts\n for select to anon, authenticated\n using (\n audience = 'public'\n or author_id = auth.uid()\n or (audience = 'friends' and private.are_friends(author_id))\n );\n\ncreate policy posts_insert on public.posts\n for insert to authenticated\n with check (author_id = auth.uid());\n\ncreate policy posts_update on public.posts\n for update to authenticated\n using (author_id = auth.uid())\n with check (author_id = auth.uid());\n\ncreate policy posts_delete on public.posts\n for delete to authenticated\n using (author_id = auth.uid());\n\ncreate policy friendships_select on public.friendships\n for select to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid());\n\ncreate policy friendships_insert on public.friendships\n for insert to authenticated\n with check (requester_id = auth.uid() and status = 'pending');\n\ncreate policy friendships_update on public.friendships\n for update to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid())\n with check (status = 'accepted');\n\ncreate policy friendships_delete on public.friendships\n for delete to authenticated\n using (requester_id = auth.uid() or addressee_id = auth.uid());\n"}]}
{"id": "T042", "title": "Document archive where legal holds override owners and admins", "tier": 2, "split": "dev", "tags": ["restrictive-policy", "admin-role", "negative-condition", "client-returning"], "traps": ["restrictive-vs-permissive", "negative-condition"], "extra_tables": [], "fixture_notes": ["ana's (…01) file 2 is under legal hold; file 1 is not", "ben's (…02) file 3", "org admin: adm (…03)"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A company document archive built with supabase-js. Uploads use `.insert({...}).select()`, renames `.update({ name }).eq('id', id).select()`, and deletes `.delete().eq('id', id)`. The legal team places and lifts legal holds from a server tool that uses the service role.\n\nR1. Users can see their own files. Org admins (users in `org_admins`) can see every file.\nR2. Users can upload files as themselves, and rename and delete their own files. Org admins can rename and delete any file.\nR3. A file under legal hold cannot be renamed or deleted by anyone through the app, including its owner and org admins.\nR4. Nobody can place or lift a legal hold through the app.\nR5. Nobody can change `org_admins` through the app.\nR6. Anonymous visitors cannot see or change anything.\nR7. Server-side code using the service role keeps full access, including legal holds.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.org_admins (\n user_id uuid primary key references auth.users(id)\n);\n\ncreate table public.files (\n id bigint generated by default as identity primary key,\n owner_id uuid not null default auth.uid() references auth.users(id),\n name text not null,\n legal_hold boolean not null default false\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com'),\n ('00000000-0000-0000-0000-000000000003', 'adm@example.com');\n\ninsert into public.org_admins (user_id) values ('00000000-0000-0000-0000-000000000003');\n\ninsert into public.files (id, owner_id, name, legal_hold) values\n (1, '00000000-0000-0000-0000-000000000001', 'q3-forecast.xlsx', false),\n (2, '00000000-0000-0000-0000-000000000001', 'vendor-dispute-emails.pdf', true),\n (3, '00000000-0000-0000-0000-000000000002', 'offsite-photos.zip', false);\n\nselect setval(pg_get_serial_sequence('public.files', 'id'), 3);\n", "tests_yaml": "- name: owner_sees_own_files\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from files order by id\n expect:\n rows:\n - [1]\n - [2]\n\n- name: admin_sees_every_file\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select id from files order by id\n expect:\n rows:\n - [1]\n - [2]\n - [3]\n\n- name: user_sees_only_own_files\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n sql: select id from files order by id\n expect:\n rows:\n - [3]\n\n- name: user_uploads_file\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: files\n values: {owner_id: 00000000-0000-0000-0000-000000000001, name: notes.md}\n returning: true\n expect: {affected: 1}\n\n- name: user_cannot_upload_for_someone_else\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: files\n values: {owner_id: 00000000-0000-0000-0000-000000000002, name: x}\n returning: false\n expect: {denied: true}\n\n- name: owner_renames_file\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: files\n values: {name: q3-forecast-final.xlsx}\n match: {id: 1}\n returning: true\n expect: {affected: 1}\n\n- name: owner_deletes_file\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: files\n match: {id: 1}\n expect: {affected: 1}\n\n- name: admin_renames_any_file\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: files\n values: {name: offsite-2020.zip}\n match: {id: 3}\n returning: true\n expect: {affected: 1}\n\n- name: admin_deletes_any_file\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: delete\n table: files\n match: {id: 3}\n expect: {affected: 1}\n\n- name: user_cannot_rename_someone_elses_file\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: update\n table: files\n values: {name: x}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: owner_cannot_rename_held_file\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: files\n values: {name: x.pdf}\n match: {id: 2}\n returning: false\n expect: {denied: true}\n\n- name: owner_cannot_delete_held_file\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: files\n match: {id: 2}\n expect: {denied: true}\n\n- name: admin_cannot_rename_held_file\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: files\n values: {name: x.pdf}\n match: {id: 2}\n returning: false\n expect: {denied: true}\n\n- name: admin_cannot_delete_held_file\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: delete\n table: files\n match: {id: 2}\n expect: {denied: true}\n\n- name: user_cannot_upload_held_file\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: files\n values: {owner_id: 00000000-0000-0000-0000-000000000001, name: x, legal_hold: true}\n returning: false\n expect: {denied: true}\n\n- name: owner_cannot_lift_hold\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: files\n values: {legal_hold: false}\n match: {id: 2}\n returning: false\n expect: {denied: true}\n\n- name: admin_cannot_lift_hold\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: files\n values: {legal_hold: false}\n match: {id: 2}\n returning: false\n expect: {denied: true}\n\n- name: admin_cannot_place_hold\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: files\n values: {legal_hold: true}\n match: {id: 3}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_become_admin\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: insert\n table: org_admins\n values: {user_id: 00000000-0000-0000-0000-000000000002}\n returning: false\n expect: {denied: true}\n\n- name: anon_sees_no_files\n covers: [R6]\n as: {role: anon}\n sql: select * from files\n expect: {denied: true}\n\n- name: service_role_lifts_hold\n covers: [R7]\n as: {role: service_role}\n sql: update files set legal_hold = false where id = 2\n expect: {affected: 1}\n", "n_tests": 21, "solution_sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_admin() returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_admins a where a.user_id = auth.uid())\n$$;\n\nalter table public.org_admins enable row level security;\nalter table public.files enable row level security;\n\ncreate policy files_select on public.files\n for select to authenticated\n using (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_insert on public.files\n for insert to authenticated\n with check (owner_id = auth.uid() and not legal_hold);\n\ncreate policy files_update on public.files\n for update to authenticated\n using (owner_id = auth.uid() or private.is_admin())\n with check (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_delete on public.files\n for delete to authenticated\n using (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_hold_blocks_update on public.files\n as restrictive\n for update to authenticated\n using (not legal_hold)\n with check (not legal_hold);\n\ncreate policy files_hold_blocks_delete on public.files\n as restrictive\n for delete to authenticated\n using (not legal_hold);\n", "trap_files": [{"name": "negative-condition", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_admin() returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_admins a where a.user_id = auth.uid())\n$$;\n\nalter table public.org_admins enable row level security;\nalter table public.files enable row level security;\n\ncreate policy files_select on public.files\n for select to authenticated\n using (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_insert on public.files\n for insert to authenticated\n with check (owner_id = auth.uid() and not legal_hold);\n\ncreate policy files_update on public.files\n for update to authenticated\n using (owner_id = auth.uid() or private.is_admin())\n with check (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_delete on public.files\n for delete to authenticated\n using (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_hold_blocks_update on public.files\n as restrictive\n for update to authenticated\n using (not legal_hold)\n with check (not legal_hold);\n"}, {"name": "restrictive-vs-permissive", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_admin() returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_admins a where a.user_id = auth.uid())\n$$;\n\nalter table public.org_admins enable row level security;\nalter table public.files enable row level security;\n\ncreate policy files_select on public.files\n for select to authenticated\n using (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_insert on public.files\n for insert to authenticated\n with check (owner_id = auth.uid() and not legal_hold);\n\ncreate policy files_update on public.files\n for update to authenticated\n using (owner_id = auth.uid() or private.is_admin())\n with check (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_delete on public.files\n for delete to authenticated\n using (owner_id = auth.uid() or private.is_admin());\n\ncreate policy files_hold_blocks_update on public.files\n for update to authenticated\n using (not legal_hold)\n with check (not legal_hold);\n\ncreate policy files_hold_blocks_delete on public.files\n for delete to authenticated\n using (not legal_hold);\n"}]}
{"id": "T055", "title": "Notification inbox where users can only mark and dismiss", "tier": 1, "split": "dev", "tags": ["single-owner", "column-restriction", "server-created-rows"], "traps": ["over-broad-for-all", "old-vs-new"], "extra_tables": [], "fixture_notes": ["ana (…01): notifications 1 (unread) and 2 (read); ben (…02): notification 3"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "An app's notification inbox built with supabase-js. Notifications are created by server code using the service role. The inbox marks a notification read or unread with `supabase.from('notifications').update({ read_at }).eq('id', id)` and dismisses it with `.delete().eq('id', id)`.\n\nR1. Users can read only their own notifications.\nR2. Users can mark their own notifications read or unread (set or clear `read_at`) and delete them. They cannot change anything else about a notification.\nR3. Nobody can create notifications through the app, not even for themselves.\nR4. Anonymous visitors cannot see or change anything.\nR5. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.notifications (\n id bigint generated by default as identity primary key,\n user_id uuid not null references auth.users(id),\n title text not null,\n body text not null default '',\n read_at timestamptz,\n created_at timestamptz not null default '2020-01-01'\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com');\n\ninsert into public.notifications (id, user_id, title, body, read_at) values\n (1, '00000000-0000-0000-0000-000000000001', 'Your invoice is ready', 'Invoice #1042 is available.', null),\n (2, '00000000-0000-0000-0000-000000000001', 'Password changed', 'If this was not you, contact support.', '2020-01-05'),\n (3, '00000000-0000-0000-0000-000000000002', 'New login from Pune', 'A new device signed in.', null);\n\nselect setval(pg_get_serial_sequence('public.notifications', 'id'), 3);\n", "tests_yaml": "- name: user_reads_own_notifications\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from notifications order by id\n expect:\n rows:\n - [1]\n - [2]\n\n- name: user_cannot_read_someone_elses_notification\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from notifications where id = 3\n expect: {count: 0}\n\n- name: user_marks_notification_read\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: notifications\n values: {read_at: '2020-02-01'}\n match: {id: 1}\n returning: false\n expect: {affected: 1}\n\n- name: user_marks_notification_unread\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: notifications\n values: {read_at: null}\n match: {id: 2}\n returning: false\n expect: {affected: 1}\n\n- name: user_cannot_edit_notification_text\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: notifications\n values: {title: You won a prize}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_mark_someone_elses_notification\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: notifications\n values: {read_at: '2020-02-01'}\n match: {id: 3}\n returning: false\n expect: {denied: true}\n\n- name: user_dismisses_own_notification\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: notifications\n match: {id: 2}\n expect: {affected: 1}\n\n- name: user_cannot_dismiss_someone_elses_notification\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: notifications\n match: {id: 3}\n expect: {denied: true}\n\n- name: user_cannot_create_own_notification\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: notifications\n values: {user_id: 00000000-0000-0000-0000-000000000001, title: 'Admin: you are verified'}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_create_notification_for_someone_else\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: notifications\n values: {user_id: 00000000-0000-0000-0000-000000000002, title: Reset your password here}\n returning: false\n expect: {denied: true}\n\n- name: anon_sees_no_notifications\n covers: [R4]\n as: {role: anon}\n sql: select * from notifications\n expect: {denied: true}\n\n- name: service_role_sees_all_notifications\n covers: [R5]\n as: {role: service_role}\n sql: select count(*) from notifications\n expect:\n rows:\n - [3]\n", "n_tests": 12, "solution_sql": "alter table public.notifications enable row level security;\n\nrevoke update on public.notifications from anon, authenticated;\ngrant update (read_at) on public.notifications to authenticated;\n\ncreate policy notifications_select on public.notifications\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy notifications_update on public.notifications\n for update to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid());\n\ncreate policy notifications_delete on public.notifications\n for delete to authenticated\n using (user_id = auth.uid());\n", "trap_files": [{"name": "old-vs-new", "sql": "alter table public.notifications enable row level security;\n\ncreate policy notifications_select on public.notifications\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy notifications_update on public.notifications\n for update to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid());\n\ncreate policy notifications_delete on public.notifications\n for delete to authenticated\n using (user_id = auth.uid());\n"}, {"name": "over-broad-for-all", "sql": "alter table public.notifications enable row level security;\n\nrevoke update on public.notifications from anon, authenticated;\ngrant update (read_at) on public.notifications to authenticated;\n\ncreate policy notifications_own on public.notifications\n for all to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid());\n"}]}
{"id": "T062", "title": "Organizations whose data is only reachable in SSO sessions", "tier": 3, "split": "dev", "tags": ["jwt-claims", "amr", "restrictive-policy", "session-assurance", "client-returning"], "traps": ["restrictive-vs-permissive", "claim-trust", "sso-not-enforced"], "extra_tables": [], "fixture_notes": ["org 1 'Bank' requires SSO; org 2 'Startup' does not", "ana (…01) admin of org 1 and member of org 2; ben (…02) member of org 1; cy (…03) admin of org 2", "each user is tested with an `amr` of sso/saml or password; ben once with user_metadata sso=true"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A B2B workspace built with supabase-js. Supabase puts the session's sign-in methods in the JWT claim `amr`, an array of objects like `{ \"method\": \"password\", \"timestamp\": 1700000000 }`; a single sign-on session contains an entry with method `sso/saml`. Users can edit their `user_metadata`. Writes read the row back with `.select()`.\n\nR1. Members can see their organizations, the members of those organizations and their documents. For an organization with `require_sso`, all of this is visible only in sessions whose `amr` contains the method `sso/saml`.\nR2. Organization admins can create, edit and delete their organization's documents, under the same session requirement.\nR3. Organization admins can change their organization's settings, under the same session requirement.\nR4. Only the `amr` claim counts as proof of an SSO session; nothing in `user_metadata` does. Nobody can change memberships through the app.\nR5. Anonymous visitors cannot see or change anything.\nR6. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.orgs (\n id bigint primary key,\n name text not null,\n require_sso boolean not null default false\n);\n\ncreate table public.org_members (\n org_id bigint not null references public.orgs(id),\n user_id uuid not null references auth.users(id),\n role text not null check (role in ('admin', 'member')),\n primary key (org_id, user_id)\n);\n\ncreate table public.org_documents (\n id bigint generated by default as identity primary key,\n org_id bigint not null references public.orgs(id),\n title text not null\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com'),\n ('00000000-0000-0000-0000-000000000003', 'cy@example.com');\n\ninsert into public.orgs (id, name, require_sso) values (1, 'Bank', true), (2, 'Startup', false);\n\ninsert into public.org_members (org_id, user_id, role) values\n (1, '00000000-0000-0000-0000-000000000001', 'admin'), (1, '00000000-0000-0000-0000-000000000002', 'member'), (2, '00000000-0000-0000-0000-000000000001', 'member'), (2, '00000000-0000-0000-0000-000000000003', 'admin');\n\ninsert into public.org_documents (id, org_id, title) values\n (1, 1, 'Risk report'), (2, 1, 'Audit findings'), (3, 2, 'Pitch deck');\n\nselect setval(pg_get_serial_sequence('public.org_documents', 'id'), 3);\n", "tests_yaml": "- name: admin_in_sso_session_reads_all_docs\n covers: [R1]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n sql: select id from org_documents order by id\n expect:\n rows:\n - [1]\n - [2]\n - [3]\n\n- name: password_session_hides_sso_org_docs\n covers: [R1]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n sql: select id from org_documents order by id\n expect:\n rows:\n - [3]\n\n- name: member_in_sso_session_reads_org_docs\n covers: [R1]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000002\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n sql: select id from org_documents order by id\n expect:\n rows:\n - [1]\n - [2]\n\n- name: member_in_password_session_reads_nothing\n covers: [R1]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000002\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n sql: select id from org_documents\n expect: {count: 0}\n\n- name: non_sso_org_readable_in_password_session\n covers: [R1]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000003\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n sql: select id from org_documents order by id\n expect:\n rows:\n - [3]\n\n- name: password_session_hides_sso_org\n covers: [R1]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n sql: select id from orgs order by id\n expect:\n rows:\n - [2]\n\n- name: password_session_hides_sso_org_members\n covers: [R1]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000002\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n sql: select user_id from org_members\n expect: {count: 0}\n\n- name: user_metadata_sso_flag_does_not_count\n covers: [R4]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000002\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n user_metadata: {sso: true}\n sql: select id from org_documents\n expect: {count: 0}\n\n- name: admin_creates_doc_in_sso_session\n covers: [R2]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n client:\n op: insert\n table: org_documents\n values: {org_id: 1, title: Q3 board pack}\n returning: true\n expect: {affected: 1}\n\n- name: admin_cannot_create_doc_in_password_session\n covers: [R2]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n client:\n op: insert\n table: org_documents\n values: {org_id: 1, title: x}\n returning: false\n expect: {denied: true}\n\n- name: member_cannot_create_doc\n covers: [R2]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000002\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n client:\n op: insert\n table: org_documents\n values: {org_id: 1, title: x}\n returning: false\n expect: {denied: true}\n\n- name: admin_of_non_sso_org_creates_doc\n covers: [R2]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000003\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n client:\n op: insert\n table: org_documents\n values: {org_id: 2, title: Hiring plan}\n returning: true\n expect: {affected: 1}\n\n- name: admin_edits_doc_in_sso_session\n covers: [R2]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n client:\n op: update\n table: org_documents\n values: {title: Risk report v2}\n match: {id: 1}\n returning: true\n expect: {affected: 1}\n\n- name: admin_cannot_edit_doc_in_password_session\n covers: [R2]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n client:\n op: update\n table: org_documents\n values: {title: x}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: admin_deletes_doc_in_sso_session\n covers: [R2]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n client:\n op: delete\n table: org_documents\n match: {id: 2}\n expect: {affected: 1}\n\n- name: admin_cannot_disable_sso_from_password_session\n covers: [R3]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: password, timestamp: 1893456000}\n client:\n op: update\n table: orgs\n values: {require_sso: false}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: admin_renames_org_in_sso_session\n covers: [R3]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n client:\n op: update\n table: orgs\n values: {name: Bank Ltd}\n match: {id: 1}\n returning: true\n expect: {affected: 1}\n\n- name: non_admin_cannot_change_org\n covers: [R3]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000002\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n client:\n op: update\n table: orgs\n values: {name: x}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: member_cannot_promote_self\n covers: [R4]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000002\n claims:\n amr:\n - {method: sso/saml, timestamp: 1893456000}\n client:\n op: update\n table: org_members\n values: {role: admin}\n match: {org_id: 1, user_id: 00000000-0000-0000-0000-000000000002}\n returning: false\n expect: {denied: true}\n\n- name: anon_sees_no_documents\n covers: [R5]\n as: {role: anon}\n sql: select * from org_documents\n expect: {denied: true}\n\n- name: service_role_sees_all_documents\n covers: [R6]\n as: {role: service_role}\n sql: select count(*) from org_documents\n expect:\n rows:\n - [3]\n", "n_tests": 21, "solution_sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_member(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_members m where m.org_id = p_org_id and m.user_id = auth.uid())\n$$;\n\ncreate function private.is_admin(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_members m where m.org_id = p_org_id and m.user_id = auth.uid() and m.role = 'admin')\n$$;\n\ncreate function private.sso_session() returns boolean\nlanguage sql stable\nas $$\n select exists (\n select 1 from jsonb_array_elements(coalesce(auth.jwt() -> 'amr', '[]'::jsonb)) as e\n where e ->> 'method' = 'sso/saml'\n )\n$$;\n\ncreate function private.org_session_ok(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select coalesce((select not o.require_sso or private.sso_session() from public.orgs o where o.id = p_org_id), false)\n$$;\n\nalter table public.orgs enable row level security;\nalter table public.org_members enable row level security;\nalter table public.org_documents enable row level security;\n\ncreate policy orgs_select on public.orgs\n for select to authenticated\n using (private.is_member(id) and (not require_sso or private.sso_session()));\n\ncreate policy orgs_update on public.orgs\n for update to authenticated\n using (private.is_admin(id) and (not require_sso or private.sso_session()))\n with check (private.is_admin(id));\n\ncreate policy org_members_select on public.org_members\n for select to authenticated\n using (private.is_member(org_id) and private.org_session_ok(org_id));\n\ncreate policy documents_session on public.org_documents\n as restrictive\n for all to authenticated\n using (private.org_session_ok(org_id))\n with check (private.org_session_ok(org_id));\n\ncreate policy documents_select on public.org_documents\n for select to authenticated\n using (private.is_member(org_id));\n\ncreate policy documents_insert on public.org_documents\n for insert to authenticated\n with check (private.is_admin(org_id));\n\ncreate policy documents_update on public.org_documents\n for update to authenticated\n using (private.is_admin(org_id))\n with check (private.is_admin(org_id));\n\ncreate policy documents_delete on public.org_documents\n for delete to authenticated\n using (private.is_admin(org_id));\n", "trap_files": [{"name": "claim-trust", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_member(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_members m where m.org_id = p_org_id and m.user_id = auth.uid())\n$$;\n\ncreate function private.is_admin(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_members m where m.org_id = p_org_id and m.user_id = auth.uid() and m.role = 'admin')\n$$;\n\ncreate function private.sso_session() returns boolean\nlanguage sql stable\nas $$\n select exists (\n select 1 from jsonb_array_elements(coalesce(auth.jwt() -> 'amr', '[]'::jsonb)) as e\n where e ->> 'method' = 'sso/saml'\n ) or coalesce((auth.jwt() -> 'user_metadata' ->> 'sso')::boolean, false)\n$$;\n\ncreate function private.org_session_ok(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select coalesce((select not o.require_sso or private.sso_session() from public.orgs o where o.id = p_org_id), false)\n$$;\n\nalter table public.orgs enable row level security;\nalter table public.org_members enable row level security;\nalter table public.org_documents enable row level security;\n\ncreate policy orgs_select on public.orgs\n for select to authenticated\n using (private.is_member(id) and (not require_sso or private.sso_session()));\n\ncreate policy orgs_update on public.orgs\n for update to authenticated\n using (private.is_admin(id) and (not require_sso or private.sso_session()))\n with check (private.is_admin(id));\n\ncreate policy org_members_select on public.org_members\n for select to authenticated\n using (private.is_member(org_id) and private.org_session_ok(org_id));\n\ncreate policy documents_session on public.org_documents\n as restrictive\n for all to authenticated\n using (private.org_session_ok(org_id))\n with check (private.org_session_ok(org_id));\n\ncreate policy documents_select on public.org_documents\n for select to authenticated\n using (private.is_member(org_id));\n\ncreate policy documents_insert on public.org_documents\n for insert to authenticated\n with check (private.is_admin(org_id));\n\ncreate policy documents_update on public.org_documents\n for update to authenticated\n using (private.is_admin(org_id))\n with check (private.is_admin(org_id));\n\ncreate policy documents_delete on public.org_documents\n for delete to authenticated\n using (private.is_admin(org_id));\n"}, {"name": "restrictive-vs-permissive", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_member(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_members m where m.org_id = p_org_id and m.user_id = auth.uid())\n$$;\n\ncreate function private.is_admin(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_members m where m.org_id = p_org_id and m.user_id = auth.uid() and m.role = 'admin')\n$$;\n\ncreate function private.sso_session() returns boolean\nlanguage sql stable\nas $$\n select exists (\n select 1 from jsonb_array_elements(coalesce(auth.jwt() -> 'amr', '[]'::jsonb)) as e\n where e ->> 'method' = 'sso/saml'\n )\n$$;\n\ncreate function private.org_session_ok(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select coalesce((select not o.require_sso or private.sso_session() from public.orgs o where o.id = p_org_id), false)\n$$;\n\nalter table public.orgs enable row level security;\nalter table public.org_members enable row level security;\nalter table public.org_documents enable row level security;\n\ncreate policy orgs_select on public.orgs\n for select to authenticated\n using (private.is_member(id) and (not require_sso or private.sso_session()));\n\ncreate policy orgs_update on public.orgs\n for update to authenticated\n using (private.is_admin(id) and (not require_sso or private.sso_session()))\n with check (private.is_admin(id));\n\ncreate policy org_members_select on public.org_members\n for select to authenticated\n using (private.is_member(org_id) and private.org_session_ok(org_id));\n\ncreate policy documents_session on public.org_documents\n for all to authenticated\n using (private.org_session_ok(org_id))\n with check (private.org_session_ok(org_id));\n\ncreate policy documents_select on public.org_documents\n for select to authenticated\n using (private.is_member(org_id));\n\ncreate policy documents_insert on public.org_documents\n for insert to authenticated\n with check (private.is_admin(org_id));\n\ncreate policy documents_update on public.org_documents\n for update to authenticated\n using (private.is_admin(org_id))\n with check (private.is_admin(org_id));\n\ncreate policy documents_delete on public.org_documents\n for delete to authenticated\n using (private.is_admin(org_id));\n"}, {"name": "sso-not-enforced", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_member(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_members m where m.org_id = p_org_id and m.user_id = auth.uid())\n$$;\n\ncreate function private.is_admin(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.org_members m where m.org_id = p_org_id and m.user_id = auth.uid() and m.role = 'admin')\n$$;\n\ncreate function private.sso_session() returns boolean\nlanguage sql stable\nas $$\n select exists (\n select 1 from jsonb_array_elements(coalesce(auth.jwt() -> 'amr', '[]'::jsonb)) as e\n where e ->> 'method' = 'sso/saml'\n )\n$$;\n\ncreate function private.org_session_ok(p_org_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select coalesce((select not o.require_sso or private.sso_session() from public.orgs o where o.id = p_org_id), false)\n$$;\n\nalter table public.orgs enable row level security;\nalter table public.org_members enable row level security;\nalter table public.org_documents enable row level security;\n\ncreate policy orgs_select on public.orgs\n for select to authenticated\n using (private.is_member(id) and (not require_sso or private.sso_session()));\n\ncreate policy orgs_update on public.orgs\n for update to authenticated\n using (private.is_admin(id) and (not require_sso or private.sso_session()))\n with check (private.is_admin(id));\n\ncreate policy org_members_select on public.org_members\n for select to authenticated\n using (private.is_member(org_id) and private.org_session_ok(org_id));\n\ncreate policy documents_select on public.org_documents\n for select to authenticated\n using (private.is_member(org_id));\n\ncreate policy documents_insert on public.org_documents\n for insert to authenticated\n with check (private.is_admin(org_id));\n\ncreate policy documents_update on public.org_documents\n for update to authenticated\n using (private.is_admin(org_id))\n with check (private.is_admin(org_id));\n\ncreate policy documents_delete on public.org_documents\n for delete to authenticated\n using (private.is_admin(org_id));\n"}]}
{"id": "T064", "title": "Workout log whose sets belong to the workout's owner", "tier": 1, "split": "dev", "tags": ["single-owner", "parent-child", "client-returning"], "traps": ["forgot-enable", "child-leak"], "extra_tables": [], "fixture_notes": ["ana's (…01) workout 1 with sets 1 and 2; ben's (…02) workout 2 with set 3"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A fitness app built with supabase-js. Users log a workout with `supabase.from('workouts').insert({...}).select()` and its sets with `supabase.from('workout_sets').insert({...}).select()`; edits use `.update({...}).eq('id', id).select()`.\n\nR1. Users can read, create, edit and delete only their own workouts.\nR2. A workout's sets can be read, added, edited and deleted only by the workout's owner, and a set can never be moved to someone else's workout.\nR3. Anonymous visitors cannot see or change anything.\nR4. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.workouts (\n id bigint generated by default as identity primary key,\n user_id uuid not null default auth.uid() references auth.users(id),\n performed_on date not null,\n kind text not null\n);\n\ncreate table public.workout_sets (\n id bigint generated by default as identity primary key,\n workout_id bigint not null references public.workouts(id) on delete cascade,\n exercise text not null,\n reps integer not null check (reps > 0),\n weight_kg numeric(6, 2)\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com');\n\ninsert into public.workouts (id, user_id, performed_on, kind) values\n (1, '00000000-0000-0000-0000-000000000001', '2020-01-06', 'strength'),\n (2, '00000000-0000-0000-0000-000000000002', '2020-01-07', 'strength');\n\ninsert into public.workout_sets (id, workout_id, exercise, reps, weight_kg) values\n (1, 1, 'Squat', 5, 80),\n (2, 1, 'Bench press', 5, 55),\n (3, 2, 'Deadlift', 3, 120);\n\nselect setval(pg_get_serial_sequence('public.workouts', 'id'), 2);\nselect setval(pg_get_serial_sequence('public.workout_sets', 'id'), 3);\n", "tests_yaml": "- name: user_reads_own_workouts\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from workouts order by id\n expect:\n rows:\n - [1]\n\n- name: user_logs_workout\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: workouts\n values: {user_id: 00000000-0000-0000-0000-000000000001, performed_on: '2020-01-08', kind: cardio}\n returning: true\n expect: {affected: 1}\n\n- name: user_cannot_log_workout_for_someone_else\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: workouts\n values: {user_id: 00000000-0000-0000-0000-000000000002, performed_on: '2020-01-08', kind: x}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_edit_someone_elses_workout\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: workouts\n values: {kind: x}\n match: {id: 2}\n returning: false\n expect: {denied: true}\n\n- name: user_reads_own_sets\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from workout_sets order by id\n expect:\n rows:\n - [1]\n - [2]\n\n- name: user_cannot_read_someone_elses_sets\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from workout_sets where id = 3\n expect: {count: 0}\n\n- name: user_adds_set_to_own_workout\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: workout_sets\n values: {workout_id: 1, exercise: Row, reps: 8, weight_kg: 40}\n returning: true\n expect: {affected: 1}\n\n- name: user_cannot_add_set_to_someone_elses_workout\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: workout_sets\n values: {workout_id: 2, exercise: x, reps: 1, weight_kg: 1}\n returning: false\n expect: {denied: true}\n\n- name: user_edits_own_set\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: workout_sets\n values: {reps: 6}\n match: {id: 1}\n returning: true\n expect: {affected: 1}\n\n- name: user_cannot_edit_someone_elses_set\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n client:\n op: update\n table: workout_sets\n values: {reps: 99}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_move_set_to_someone_elses_workout\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: workout_sets\n values: {workout_id: 2}\n match: {id: 2}\n returning: false\n expect: {denied: true}\n\n- name: user_deletes_own_workout\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: workouts\n match: {id: 1}\n expect: {affected: 1}\n\n- name: anon_sees_no_sets\n covers: [R3]\n as: {role: anon}\n sql: select * from workout_sets\n expect: {denied: true}\n\n- name: service_role_sees_all_sets\n covers: [R4]\n as: {role: service_role}\n sql: select count(*) from workout_sets\n expect:\n rows:\n - [3]\n", "n_tests": 14, "solution_sql": "alter table public.workouts enable row level security;\nalter table public.workout_sets enable row level security;\n\ncreate policy workouts_select on public.workouts\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy workouts_insert on public.workouts\n for insert to authenticated\n with check (user_id = auth.uid());\n\ncreate policy workouts_update on public.workouts\n for update to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid());\n\ncreate policy workouts_delete on public.workouts\n for delete to authenticated\n using (user_id = auth.uid());\n\ncreate policy workout_sets_select on public.workout_sets\n for select to authenticated\n using (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n\ncreate policy workout_sets_insert on public.workout_sets\n for insert to authenticated\n with check (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n\ncreate policy workout_sets_update on public.workout_sets\n for update to authenticated\n using (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()))\n with check (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n\ncreate policy workout_sets_delete on public.workout_sets\n for delete to authenticated\n using (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n", "trap_files": [{"name": "child-leak", "sql": "alter table public.workouts enable row level security;\nalter table public.workout_sets enable row level security;\n\ncreate policy workouts_select on public.workouts\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy workouts_insert on public.workouts\n for insert to authenticated\n with check (user_id = auth.uid());\n\ncreate policy workouts_update on public.workouts\n for update to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid());\n\ncreate policy workouts_delete on public.workouts\n for delete to authenticated\n using (user_id = auth.uid());\n\ncreate policy workout_sets_select on public.workout_sets\n for select to authenticated\n using (true);\n\ncreate policy workout_sets_insert on public.workout_sets\n for insert to authenticated\n with check (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n\ncreate policy workout_sets_update on public.workout_sets\n for update to authenticated\n using (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()))\n with check (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n\ncreate policy workout_sets_delete on public.workout_sets\n for delete to authenticated\n using (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n"}, {"name": "forgot-enable", "sql": "alter table public.workouts enable row level security;\n\ncreate policy workouts_select on public.workouts\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy workouts_insert on public.workouts\n for insert to authenticated\n with check (user_id = auth.uid());\n\ncreate policy workouts_update on public.workouts\n for update to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid());\n\ncreate policy workouts_delete on public.workouts\n for delete to authenticated\n using (user_id = auth.uid());\n\ncreate policy workout_sets_select on public.workout_sets\n for select to authenticated\n using (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n\ncreate policy workout_sets_insert on public.workout_sets\n for insert to authenticated\n with check (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n\ncreate policy workout_sets_update on public.workout_sets\n for update to authenticated\n using (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()))\n with check (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n\ncreate policy workout_sets_delete on public.workout_sets\n for delete to authenticated\n using (exists (select 1 from public.workouts w where w.id = workout_id and w.user_id = auth.uid()));\n"}]}
{"id": "T067", "title": "Apartment maintenance requests handled by building staff", "tier": 2, "split": "dev", "tags": ["membership", "parent-child", "state-transition", "column-restriction", "client-returning"], "traps": ["reference-check", "child-leak", "state-transition"], "extra_tables": [], "fixture_notes": ["building 1: units 101 (ana …01) and 102 (ben …02), staff sam (…04); building 2: unit 201 (cy …03), staff tia (…05)", "photos on requests 1 and 2"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A building-management app built with supabase-js. Residents report problems with `supabase.from('requests').insert({ unit_id, created_by, description }).select()` and attach photos with `.from('request_photos').insert({ request_id, path }).select()`. Building staff update progress with `.update({ status }).eq('id', id).select()`.\n\nR1. Residents can see their own units and the requests and photos of their own units. Building staff can see the units, requests and photos of every unit in their buildings.\nR2. Residents can create requests for their own units, as themselves; new requests are always `open`.\nR3. Residents and staff can add photos to the requests they can see.\nR4. Building staff can change the status of requests in their buildings. Nobody can change anything else about a request, residents cannot change status, and requests are never deleted.\nR5. Users can see their own residence and staff rows. Nobody can change units, residents or staff through the app.\nR6. Anonymous visitors cannot see or change anything.\nR7. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.units (\n id bigint primary key,\n building_id bigint not null,\n label text not null\n);\n\ncreate table public.residents (\n unit_id bigint not null references public.units(id),\n user_id uuid not null references auth.users(id),\n primary key (unit_id, user_id)\n);\n\ncreate table public.building_staff (\n building_id bigint not null,\n user_id uuid not null references auth.users(id),\n primary key (building_id, user_id)\n);\n\ncreate table public.requests (\n id bigint generated by default as identity primary key,\n unit_id bigint not null references public.units(id),\n created_by uuid not null default auth.uid() references auth.users(id),\n description text not null,\n status text not null default 'open' check (status in ('open', 'in_progress', 'done'))\n);\n\ncreate table public.request_photos (\n id bigint generated by default as identity primary key,\n request_id bigint not null references public.requests(id) on delete cascade,\n path text not null\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com'),\n ('00000000-0000-0000-0000-000000000003', 'cy@example.com'),\n ('00000000-0000-0000-0000-000000000004', 'sam@example.com'),\n ('00000000-0000-0000-0000-000000000005', 'tia@example.com');\n\ninsert into public.units (id, building_id, label) values (101, 1, 'A-101'), (102, 1, 'A-102'), (201, 2, 'B-201');\n\ninsert into public.residents (unit_id, user_id) values (101, '00000000-0000-0000-0000-000000000001'), (102, '00000000-0000-0000-0000-000000000002'), (201, '00000000-0000-0000-0000-000000000003');\n\ninsert into public.building_staff (building_id, user_id) values (1, '00000000-0000-0000-0000-000000000004'), (2, '00000000-0000-0000-0000-000000000005');\n\ninsert into public.requests (id, unit_id, created_by, description, status) values\n (1, 101, '00000000-0000-0000-0000-000000000001', 'Leaking tap', 'open'),\n (2, 102, '00000000-0000-0000-0000-000000000002', 'Broken window', 'in_progress'),\n (3, 201, '00000000-0000-0000-0000-000000000003', 'No hot water', 'open');\n\ninsert into public.request_photos (id, request_id, path) values (1, 1, 'req1/tap.jpg'), (2, 2, 'req2/window.jpg');\n\nselect setval(pg_get_serial_sequence('public.requests', 'id'), 3);\nselect setval(pg_get_serial_sequence('public.request_photos', 'id'), 2);\n", "tests_yaml": "- name: resident_sees_own_unit_requests\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from requests order by id\n expect:\n rows:\n - [1]\n\n- name: staff_sees_building_requests\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n sql: select id from requests order by id\n expect:\n rows:\n - [1]\n - [2]\n\n- name: other_building_staff_sees_only_own_building\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000005}\n sql: select id from requests order by id\n expect:\n rows:\n - [3]\n\n- name: resident_sees_own_unit_photos\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from request_photos order by id\n expect:\n rows:\n - [1]\n\n- name: other_building_staff_sees_no_photos\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000005}\n sql: select id from request_photos\n expect: {count: 0}\n\n- name: resident_files_request\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: requests\n values: {unit_id: 101, created_by: 00000000-0000-0000-0000-000000000001, description: Fan not working}\n returning: true\n expect: {affected: 1}\n\n- name: resident_cannot_file_for_other_unit\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: requests\n values: {unit_id: 102, created_by: 00000000-0000-0000-0000-000000000001, description: Fan not working}\n returning: false\n expect: {denied: true}\n\n- name: resident_cannot_file_as_someone_else\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: requests\n values: {unit_id: 101, created_by: 00000000-0000-0000-0000-000000000002, description: Fan not working}\n returning: false\n expect: {denied: true}\n\n- name: resident_cannot_file_done_request\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: requests\n values: {unit_id: 101, created_by: 00000000-0000-0000-0000-000000000001, description: Fan not working, status: done}\n returning: false\n expect: {denied: true}\n\n- name: resident_adds_photo\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: request_photos\n values: {request_id: 1, path: req1/close-up.jpg}\n returning: true\n expect: {affected: 1}\n\n- name: resident_cannot_add_photo_to_neighbours_request\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: request_photos\n values: {request_id: 2, path: x.jpg}\n returning: false\n expect: {denied: true}\n\n- name: staff_marks_request_done\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n client:\n op: update\n table: requests\n values: {status: done}\n match: {id: 1}\n returning: true\n expect: {affected: 1}\n\n- name: other_building_staff_cannot_update\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000005}\n client:\n op: update\n table: requests\n values: {status: done}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: resident_cannot_close_own_request\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: requests\n values: {status: done}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: staff_cannot_rewrite_description\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n client:\n op: update\n table: requests\n values: {description: x}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: resident_cannot_delete_request\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: requests\n match: {id: 1}\n expect: {denied: true}\n\n- name: resident_sees_own_residence\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select unit_id from residents order by unit_id\n expect:\n rows:\n - [101]\n\n- name: resident_cannot_move_in_elsewhere\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: residents\n values: {unit_id: 102, user_id: 00000000-0000-0000-0000-000000000001}\n returning: false\n expect: {denied: true}\n\n- name: anon_sees_no_requests\n covers: [R6]\n as: {role: anon}\n sql: select * from requests\n expect: {denied: true}\n\n- name: service_role_sees_all_photos\n covers: [R7]\n as: {role: service_role}\n sql: select count(*) from request_photos\n expect:\n rows:\n - [2]\n", "n_tests": 20, "solution_sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.lives_in(p_unit_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.residents r where r.unit_id = p_unit_id and r.user_id = auth.uid())\n$$;\n\ncreate function private.staffs_unit(p_unit_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.units u join public.building_staff s on s.building_id = u.building_id where u.id = p_unit_id and s.user_id = auth.uid())\n$$;\n\ncreate function private.can_see_request(p_request_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.requests q where q.id = p_request_id and (private.lives_in(q.unit_id) or private.staffs_unit(q.unit_id)))\n$$;\n\nalter table public.units enable row level security;\nalter table public.residents enable row level security;\nalter table public.building_staff enable row level security;\nalter table public.requests enable row level security;\nalter table public.request_photos enable row level security;\n\nrevoke update on public.requests from anon, authenticated;\ngrant update (status) on public.requests to authenticated;\n\ncreate policy units_select on public.units\n for select to authenticated\n using (private.lives_in(id) or private.staffs_unit(id));\n\ncreate policy residents_select on public.residents\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy building_staff_select on public.building_staff\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy requests_select on public.requests\n for select to authenticated\n using (private.lives_in(unit_id) or private.staffs_unit(unit_id));\n\ncreate policy requests_insert on public.requests\n for insert to authenticated\n with check (created_by = auth.uid() and status = 'open' and private.lives_in(unit_id));\n\ncreate policy requests_update on public.requests\n for update to authenticated\n using (private.staffs_unit(unit_id))\n with check (private.staffs_unit(unit_id));\n\ncreate policy request_photos_select on public.request_photos\n for select to authenticated\n using (private.can_see_request(request_id));\n\ncreate policy request_photos_insert on public.request_photos\n for insert to authenticated\n with check (private.can_see_request(request_id));\n", "trap_files": [{"name": "child-leak", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.lives_in(p_unit_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.residents r where r.unit_id = p_unit_id and r.user_id = auth.uid())\n$$;\n\ncreate function private.staffs_unit(p_unit_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.units u join public.building_staff s on s.building_id = u.building_id where u.id = p_unit_id and s.user_id = auth.uid())\n$$;\n\ncreate function private.can_see_request(p_request_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.requests q where q.id = p_request_id and (private.lives_in(q.unit_id) or private.staffs_unit(q.unit_id)))\n$$;\n\nalter table public.units enable row level security;\nalter table public.residents enable row level security;\nalter table public.building_staff enable row level security;\nalter table public.requests enable row level security;\nalter table public.request_photos enable row level security;\n\nrevoke update on public.requests from anon, authenticated;\ngrant update (status) on public.requests to authenticated;\n\ncreate policy units_select on public.units\n for select to authenticated\n using (private.lives_in(id) or private.staffs_unit(id));\n\ncreate policy residents_select on public.residents\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy building_staff_select on public.building_staff\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy requests_select on public.requests\n for select to authenticated\n using (private.lives_in(unit_id) or private.staffs_unit(unit_id));\n\ncreate policy requests_insert on public.requests\n for insert to authenticated\n with check (created_by = auth.uid() and status = 'open' and private.lives_in(unit_id));\n\ncreate policy requests_update on public.requests\n for update to authenticated\n using (private.staffs_unit(unit_id))\n with check (private.staffs_unit(unit_id));\n\ncreate policy request_photos_select on public.request_photos\n for select to authenticated\n using (true);\n\ncreate policy request_photos_insert on public.request_photos\n for insert to authenticated\n with check (private.can_see_request(request_id));\n"}, {"name": "reference-check", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.lives_in(p_unit_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.residents r where r.unit_id = p_unit_id and r.user_id = auth.uid())\n$$;\n\ncreate function private.staffs_unit(p_unit_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.units u join public.building_staff s on s.building_id = u.building_id where u.id = p_unit_id and s.user_id = auth.uid())\n$$;\n\ncreate function private.can_see_request(p_request_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.requests q where q.id = p_request_id and (private.lives_in(q.unit_id) or private.staffs_unit(q.unit_id)))\n$$;\n\nalter table public.units enable row level security;\nalter table public.residents enable row level security;\nalter table public.building_staff enable row level security;\nalter table public.requests enable row level security;\nalter table public.request_photos enable row level security;\n\nrevoke update on public.requests from anon, authenticated;\ngrant update (status) on public.requests to authenticated;\n\ncreate policy units_select on public.units\n for select to authenticated\n using (private.lives_in(id) or private.staffs_unit(id));\n\ncreate policy residents_select on public.residents\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy building_staff_select on public.building_staff\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy requests_select on public.requests\n for select to authenticated\n using (private.lives_in(unit_id) or private.staffs_unit(unit_id));\n\ncreate policy requests_insert on public.requests\n for insert to authenticated\n with check (created_by = auth.uid() and status = 'open');\n\ncreate policy requests_update on public.requests\n for update to authenticated\n using (private.staffs_unit(unit_id))\n with check (private.staffs_unit(unit_id));\n\ncreate policy request_photos_select on public.request_photos\n for select to authenticated\n using (private.can_see_request(request_id));\n\ncreate policy request_photos_insert on public.request_photos\n for insert to authenticated\n with check (private.can_see_request(request_id));\n"}, {"name": "state-transition", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.lives_in(p_unit_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.residents r where r.unit_id = p_unit_id and r.user_id = auth.uid())\n$$;\n\ncreate function private.staffs_unit(p_unit_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.units u join public.building_staff s on s.building_id = u.building_id where u.id = p_unit_id and s.user_id = auth.uid())\n$$;\n\ncreate function private.can_see_request(p_request_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.requests q where q.id = p_request_id and (private.lives_in(q.unit_id) or private.staffs_unit(q.unit_id)))\n$$;\n\nalter table public.units enable row level security;\nalter table public.residents enable row level security;\nalter table public.building_staff enable row level security;\nalter table public.requests enable row level security;\nalter table public.request_photos enable row level security;\n\nrevoke update on public.requests from anon, authenticated;\ngrant update (status) on public.requests to authenticated;\n\ncreate policy units_select on public.units\n for select to authenticated\n using (private.lives_in(id) or private.staffs_unit(id));\n\ncreate policy residents_select on public.residents\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy building_staff_select on public.building_staff\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy requests_select on public.requests\n for select to authenticated\n using (private.lives_in(unit_id) or private.staffs_unit(unit_id));\n\ncreate policy requests_insert on public.requests\n for insert to authenticated\n with check (created_by = auth.uid() and status = 'open' and private.lives_in(unit_id));\n\ncreate policy requests_update on public.requests\n for update to authenticated\n using (private.lives_in(unit_id) or private.staffs_unit(unit_id))\n with check (private.lives_in(unit_id) or private.staffs_unit(unit_id));\n\ncreate policy request_photos_select on public.request_photos\n for select to authenticated\n using (private.can_see_request(request_id));\n\ncreate policy request_photos_insert on public.request_photos\n for insert to authenticated\n with check (private.can_see_request(request_id));\n"}]}
{"id": "T073", "title": "Support agents see masked customers of their own region only", "tier": 3, "split": "dev", "tags": ["view", "column-masking", "region-scoping", "security-definer-view", "claim-trust"], "traps": ["view-bypass", "column-leak", "claim-trust"], "extra_tables": [], "fixture_notes": ["agents: ag_n (…01) region north, ag_s (…02) region south; supervisor sup (…03); plain user usr (…04)", "customers 1 and 2 in north, 3 in south", "one test gives ag_n a user_metadata region of south"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A support console built with supabase-js. Support agents (listed in `support_agents` with their region) look customers up with `supabase.from('support_customers').select()`; supervisors (listed in `supervisors`) use the full `customers` table. Users can edit their own `user_metadata`.\n\nR1. Supervisors can read the full `customers` table. Nobody else can read it, including support agents.\nR2. Provide a view `public.support_customers` with exactly the columns `id`, `region`, `full_name`, `email_masked` and `phone_masked`. `email_masked` is the email's first character, then `***`, then everything from the `@` on (asha@example.com becomes a***@example.com). `phone_masked` is `******` followed by the phone's last four digits.\nR3. A support agent sees in `support_customers` only the customers of their own region, as recorded in `support_agents`; nothing in `user_metadata` changes their region. Other signed-in users see no rows in it.\nR4. Users can see their own `support_agents` and `supervisors` rows. Nobody can change customers, agents or supervisors through the app.\nR5. Anonymous visitors cannot see anything.\nR6. Server-side code using the service role keeps full access and sees every customer in `support_customers`.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.support_agents (\n user_id uuid primary key references auth.users(id),\n region text not null\n);\n\ncreate table public.supervisors (\n user_id uuid primary key references auth.users(id)\n);\n\ncreate table public.customers (\n id bigint primary key,\n region text not null,\n full_name text not null,\n email text not null,\n phone text not null\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ag_n@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ag_s@example.com'),\n ('00000000-0000-0000-0000-000000000003', 'sup@example.com'),\n ('00000000-0000-0000-0000-000000000004', 'usr@example.com');\n\ninsert into public.support_agents (user_id, region) values ('00000000-0000-0000-0000-000000000001', 'north'), ('00000000-0000-0000-0000-000000000002', 'south');\n\ninsert into public.supervisors (user_id) values ('00000000-0000-0000-0000-000000000003');\n\ninsert into public.customers (id, region, full_name, email, phone) values\n (1, 'north', 'Asha Rao', 'asha@example.com', '9876543210'),\n (2, 'north', 'Bilal Khan', 'bilal@mail.in', '9123456780'),\n (3, 'south', 'Chitra Nair', 'chitra@example.org', '9988776655');\n", "tests_yaml": "- name: agent_sees_masked_customers_of_own_region\n covers: [R2, R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id, full_name, email_masked, phone_masked from support_customers order by id\n expect:\n rows:\n - [1, Asha Rao, a***@example.com, '******3210']\n - [2, Bilal Khan, b***@mail.in, '******6780']\n\n- name: other_region_agent_sees_only_own_region\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n sql: select id, region from support_customers order by id\n expect:\n rows:\n - [3, south]\n\n- name: user_metadata_region_is_ignored\n covers: [R3]\n as:\n role: authenticated\n sub: 00000000-0000-0000-0000-000000000001\n claims:\n user_metadata: {region: south}\n sql: select id from support_customers order by id\n expect:\n rows:\n - [1]\n - [2]\n\n- name: non_agent_sees_no_view_rows\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n sql: select * from support_customers\n expect: {denied: true}\n\n- name: support_customers_has_exactly_five_columns\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select column_name from information_schema.columns where table_schema = 'public' and table_name = 'support_customers' order by ordinal_position\n expect:\n rows:\n - [id]\n - [region]\n - [full_name]\n - [email_masked]\n - [phone_masked]\n\n- name: agent_cannot_read_raw_customers\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select email, phone from customers\n expect: {denied: true}\n\n- name: supervisor_reads_raw_customers\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select count(*) from customers\n expect:\n rows:\n - [3]\n\n- name: plain_user_cannot_read_raw_customers\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n sql: select * from customers\n expect: {denied: true}\n\n- name: agent_sees_own_agent_row\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select region from support_agents\n expect:\n rows:\n - [north]\n\n- name: agent_cannot_change_own_region\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: support_agents\n values: {region: south}\n match: {user_id: 00000000-0000-0000-0000-000000000001}\n returning: false\n expect: {denied: true}\n\n- name: supervisor_cannot_change_customer\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: customers\n values: {phone: '0000000000'}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_become_supervisor\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n client:\n op: insert\n table: supervisors\n values: {user_id: 00000000-0000-0000-0000-000000000004}\n returning: false\n expect: {denied: true}\n\n- name: anon_sees_no_view_rows\n covers: [R5]\n as: {role: anon}\n sql: select * from support_customers\n expect: {denied: true}\n\n- name: service_role_sees_every_customer_in_view\n covers: [R6]\n as: {role: service_role}\n sql: select count(*) from support_customers\n expect:\n rows:\n - [3]\n", "n_tests": 14, "solution_sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_supervisor() returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.supervisors s where s.user_id = auth.uid())\n$$;\n\nalter table public.support_agents enable row level security;\nalter table public.supervisors enable row level security;\nalter table public.customers enable row level security;\n\ncreate policy support_agents_select on public.support_agents\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy supervisors_select on public.supervisors\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy customers_select on public.customers\n for select to authenticated\n using (private.is_supervisor());\n\ncreate view public.support_customers as\n select\n c.id,\n c.region,\n c.full_name,\n left(c.email, 1) || '***' || substr(c.email, strpos(c.email, '@')) as email_masked,\n '******' || right(c.phone, 4) as phone_masked\n from public.customers c\n where c.region = (select a.region from public.support_agents a where a.user_id = auth.uid())\n or auth.role() = 'service_role';\n", "trap_files": [{"name": "claim-trust", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_supervisor() returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.supervisors s where s.user_id = auth.uid())\n$$;\n\nalter table public.support_agents enable row level security;\nalter table public.supervisors enable row level security;\nalter table public.customers enable row level security;\n\ncreate policy support_agents_select on public.support_agents\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy supervisors_select on public.supervisors\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy customers_select on public.customers\n for select to authenticated\n using (private.is_supervisor());\n\ncreate view public.support_customers as\n select\n c.id,\n c.region,\n c.full_name,\n left(c.email, 1) || '***' || substr(c.email, strpos(c.email, '@')) as email_masked,\n '******' || right(c.phone, 4) as phone_masked\n from public.customers c\n where c.region = coalesce(auth.jwt() -> 'user_metadata' ->> 'region', (select a.region from public.support_agents a where a.user_id = auth.uid()))\n or auth.role() = 'service_role';\n"}, {"name": "column-leak", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_supervisor() returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.supervisors s where s.user_id = auth.uid())\n$$;\n\nalter table public.support_agents enable row level security;\nalter table public.supervisors enable row level security;\nalter table public.customers enable row level security;\n\ncreate policy support_agents_select on public.support_agents\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy supervisors_select on public.supervisors\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy customers_select on public.customers\n for select to authenticated\n using (private.is_supervisor() or exists (select 1 from public.support_agents a where a.user_id = auth.uid() and a.region = customers.region));\n\ncreate view public.support_customers as\n select\n c.id,\n c.region,\n c.full_name,\n left(c.email, 1) || '***' || substr(c.email, strpos(c.email, '@')) as email_masked,\n '******' || right(c.phone, 4) as phone_masked\n from public.customers c\n where c.region = (select a.region from public.support_agents a where a.user_id = auth.uid())\n or auth.role() = 'service_role';\n"}, {"name": "view-bypass", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_supervisor() returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.supervisors s where s.user_id = auth.uid())\n$$;\n\nalter table public.support_agents enable row level security;\nalter table public.supervisors enable row level security;\nalter table public.customers enable row level security;\n\ncreate policy support_agents_select on public.support_agents\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy supervisors_select on public.supervisors\n for select to authenticated\n using (user_id = auth.uid());\n\ncreate policy customers_select on public.customers\n for select to authenticated\n using (private.is_supervisor());\n\ncreate view public.support_customers as\n select\n c.id,\n c.region,\n c.full_name,\n left(c.email, 1) || '***' || substr(c.email, strpos(c.email, '@')) as email_masked,\n '******' || right(c.phone, 4) as phone_masked\n from public.customers c;\n"}]}
{"id": "T074", "title": "Public roadmap with one vote per user on unshipped items", "tier": 1, "split": "dev", "tags": ["public-read", "reference-check", "single-owner"], "traps": ["reference-check", "over-broad-for-all"], "extra_tables": [], "fixture_notes": ["item 3 is shipped", "votes: ana (…01) on item 1; ben (…02) on items 1 and 3"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A product's public roadmap built with supabase-js. Users vote with `supabase.from('item_votes').insert({ user_id, item_id }).select()` and remove a vote with `.delete()`; the primary key allows one vote per user per item. The product team manages items from a server tool using the service role.\n\nR1. Anyone, including anonymous visitors, can read the roadmap items.\nR2. Signed-in users can see all votes. Anonymous visitors cannot.\nR3. Signed-in users can vote, as themselves, on items that are not shipped, and can remove their own votes.\nR4. Nobody can change roadmap items, or anyone else's votes, through the app.\nR5. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.roadmap_items (\n id bigint primary key,\n title text not null,\n status text not null default 'planned' check (status in ('planned', 'in_progress', 'shipped'))\n);\n\ncreate table public.item_votes (\n user_id uuid not null default auth.uid() references auth.users(id),\n item_id bigint not null references public.roadmap_items(id),\n primary key (user_id, item_id)\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com');\n\ninsert into public.roadmap_items (id, title, status) values\n (1, 'Dark mode', 'planned'),\n (2, 'Offline sync', 'in_progress'),\n (3, 'CSV export', 'shipped');\n\ninsert into public.item_votes (user_id, item_id) values ('00000000-0000-0000-0000-000000000001', 1), ('00000000-0000-0000-0000-000000000002', 1), ('00000000-0000-0000-0000-000000000002', 3);\n", "tests_yaml": "- name: anon_reads_roadmap\n covers: [R1]\n as: {role: anon}\n sql: select count(*) from roadmap_items\n expect:\n rows:\n - [3]\n\n- name: user_sees_all_votes\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select count(*) from item_votes\n expect:\n rows:\n - [3]\n\n- name: anon_cannot_see_votes\n covers: [R2]\n as: {role: anon}\n sql: select * from item_votes\n expect: {denied: true}\n\n- name: user_votes_for_item\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: item_votes\n values: {user_id: 00000000-0000-0000-0000-000000000001, item_id: 2}\n returning: true\n expect: {affected: 1}\n\n- name: user_cannot_vote_for_shipped_item\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: item_votes\n values: {user_id: 00000000-0000-0000-0000-000000000001, item_id: 3}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_vote_as_someone_else\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: insert\n table: item_votes\n values: {user_id: 00000000-0000-0000-0000-000000000002, item_id: 2}\n returning: false\n expect: {denied: true}\n\n- name: user_removes_own_vote\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: item_votes\n match: {user_id: 00000000-0000-0000-0000-000000000001, item_id: 1}\n expect: {affected: 1}\n\n- name: user_cannot_remove_someone_elses_vote\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: item_votes\n match: {user_id: 00000000-0000-0000-0000-000000000002, item_id: 1}\n expect: {denied: true}\n\n- name: user_cannot_change_item_status\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: update\n table: roadmap_items\n values: {status: shipped}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: anon_cannot_vote\n covers: [R3]\n as: {role: anon}\n client:\n op: insert\n table: item_votes\n values: {user_id: 00000000-0000-0000-0000-000000000001, item_id: 2}\n returning: false\n expect: {denied: true}\n\n- name: service_role_sees_all_votes\n covers: [R5]\n as: {role: service_role}\n sql: select count(*) from item_votes\n expect:\n rows:\n - [3]\n", "n_tests": 11, "solution_sql": "alter table public.roadmap_items enable row level security;\nalter table public.item_votes enable row level security;\n\ncreate policy roadmap_items_select on public.roadmap_items\n for select to anon, authenticated\n using (true);\n\ncreate policy item_votes_select on public.item_votes\n for select to authenticated\n using (true);\n\ncreate policy item_votes_insert on public.item_votes\n for insert to authenticated\n with check (\n user_id = auth.uid()\n and exists (select 1 from public.roadmap_items i where i.id = item_id and i.status <> 'shipped')\n );\n\ncreate policy item_votes_delete on public.item_votes\n for delete to authenticated\n using (user_id = auth.uid());\n", "trap_files": [{"name": "over-broad-for-all", "sql": "alter table public.roadmap_items enable row level security;\nalter table public.item_votes enable row level security;\n\ncreate policy roadmap_items_select on public.roadmap_items\n for select to anon, authenticated\n using (true);\n\ncreate policy item_votes_all on public.item_votes\n for all to authenticated\n using (true)\n with check (user_id = auth.uid());\n\ncreate policy item_votes_insert on public.item_votes\n for insert to authenticated\n with check (\n user_id = auth.uid()\n and exists (select 1 from public.roadmap_items i where i.id = item_id and i.status <> 'shipped')\n );\n"}, {"name": "reference-check", "sql": "alter table public.roadmap_items enable row level security;\nalter table public.item_votes enable row level security;\n\ncreate policy roadmap_items_select on public.roadmap_items\n for select to anon, authenticated\n using (true);\n\ncreate policy item_votes_select on public.item_votes\n for select to authenticated\n using (true);\n\ncreate policy item_votes_insert on public.item_votes\n for insert to authenticated\n with check (user_id = auth.uid());\n\ncreate policy item_votes_delete on public.item_votes\n for delete to authenticated\n using (user_id = auth.uid());\n"}]}
{"id": "T081", "title": "Auction bidding RPC with increments, end times and no self-bidding", "tier": 3, "split": "dev", "tags": ["rpc", "security-definer", "time-window", "input-validation", "read-only-tables"], "traps": ["time-window", "self-approval", "rpc-input-validation"], "extra_tables": [], "fixture_notes": ["seller sel (…01); bidders b1 (…02), b2 (…03)", "auction 1: ends 2099, highest 150 by b1, increment 10; auction 2 ended in 2020; auction 3: ends 2099, no bids, starting price 200"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "An auction site built with supabase-js. Bidders call `supabase.rpc('place_bid', { p_auction_id, p_amount })`. Implement it as `public.place_bid(p_auction_id bigint, p_amount numeric) returns numeric`, returning the new highest bid. Auction end times are compared with `now()`.\n\nR1. Signed-in users can see every auction, including its current highest bid. Bidders can see their own bids; sellers can see every bid on their auctions.\nR2. Nobody can create, change or delete auctions or bids directly through the app.\nR3. `place_bid` records a bid by the caller and makes it the auction's highest bid.\nR4. `place_bid` must raise an error and change nothing when the caller is not signed in, the auction does not exist or has ended, the caller is the seller, or the amount is too low: below the starting price when there are no bids yet, otherwise below the highest bid plus the minimum increment.\nR5. Anonymous visitors cannot see or change anything.\nR6. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.auctions (\n id bigint primary key,\n seller_id uuid not null references auth.users(id),\n title text not null,\n starting_price numeric(10, 2) not null,\n min_increment numeric(10, 2) not null default 10,\n ends_at timestamptz not null,\n highest_bid numeric(10, 2),\n highest_bidder uuid references auth.users(id)\n);\n\ncreate table public.bids (\n id bigint generated by default as identity primary key,\n auction_id bigint not null references public.auctions(id),\n bidder_id uuid not null references auth.users(id),\n amount numeric(10, 2) not null,\n placed_at timestamptz not null default '2020-01-01'\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'sel@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'b1@example.com'),\n ('00000000-0000-0000-0000-000000000003', 'b2@example.com');\n\ninsert into public.auctions (id, seller_id, title, starting_price, min_increment, ends_at, highest_bid, highest_bidder) values\n (1, '00000000-0000-0000-0000-000000000001', 'Vintage watch', 100, 10, '2099-01-01', 150, '00000000-0000-0000-0000-000000000002'),\n (2, '00000000-0000-0000-0000-000000000001', 'Old radio', 50, 5, '2020-01-01', null, null),\n (3, '00000000-0000-0000-0000-000000000001', 'Signed cricket bat', 200, 25, '2099-01-01', null, null);\n\ninsert into public.bids (id, auction_id, bidder_id, amount) values (1, 1, '00000000-0000-0000-0000-000000000002', 150);\n\nselect setval(pg_get_serial_sequence('public.bids', 'id'), 1);\n", "tests_yaml": "- name: user_sees_all_auctions\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select count(*) from auctions\n expect:\n rows:\n - [3]\n\n- name: bidder_sees_own_bids\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000002}\n sql: select id from bids order by id\n expect:\n rows:\n - [1]\n\n- name: other_bidder_sees_no_bids\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select id from bids\n expect: {count: 0}\n\n- name: seller_sees_bids_on_own_auctions\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select id from bids order by id\n expect:\n rows:\n - [1]\n\n- name: user_cannot_set_highest_bid_directly\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: auctions\n values: {highest_bid: 1, highest_bidder: 00000000-0000-0000-0000-000000000003}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: user_cannot_insert_bid_directly\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: insert\n table: bids\n values: {auction_id: 1, bidder_id: 00000000-0000-0000-0000-000000000003, amount: 999}\n returning: false\n expect: {denied: true}\n\n- name: bidder_outbids_with_increment\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select public.place_bid(1, 160)\n expect:\n rows:\n - [160]\n\n- name: winning_bid_is_stored\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select public.place_bid(1, 160); select highest_bid, highest_bidder from auctions where id = 1\n expect:\n rows:\n - [160, 00000000-0000-0000-0000-000000000003]\n\n- name: first_bid_at_starting_price\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select public.place_bid(3, 200)\n expect:\n rows:\n - [200]\n\n- name: bid_below_increment_is_refused\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select public.place_bid(1, 155)\n expect: {denied: true}\n\n- name: first_bid_below_starting_price_is_refused\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select public.place_bid(3, 199)\n expect: {denied: true}\n\n- name: bid_on_ended_auction_is_refused\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select public.place_bid(2, 500)\n expect: {denied: true}\n\n- name: seller_cannot_bid_on_own_auction\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n sql: select public.place_bid(1, 500)\n expect: {denied: true}\n\n- name: bid_on_unknown_auction_is_refused\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select public.place_bid(99, 500)\n expect: {denied: true}\n\n- name: anon_cannot_bid\n covers: [R4, R5]\n as: {role: anon}\n sql: select public.place_bid(1, 500)\n expect: {denied: true}\n\n- name: anon_sees_no_auctions\n covers: [R5]\n as: {role: anon}\n sql: select * from auctions\n expect: {denied: true}\n\n- name: service_role_sees_all_bids\n covers: [R6]\n as: {role: service_role}\n sql: select count(*) from bids\n expect:\n rows:\n - [1]\n", "n_tests": 17, "solution_sql": "alter table public.auctions enable row level security;\nalter table public.bids enable row level security;\n\ncreate policy auctions_select on public.auctions\n for select to authenticated\n using (true);\n\ncreate policy bids_select on public.bids\n for select to authenticated\n using (\n bidder_id = auth.uid()\n or exists (select 1 from public.auctions a where a.id = auction_id and a.seller_id = auth.uid())\n );\n\ncreate function public.place_bid(p_auction_id bigint, p_amount numeric) returns numeric\nlanguage plpgsql security definer set search_path = ''\nas $$\ndeclare\n v_uid uuid := auth.uid();\n v_auction public.auctions%rowtype;\nbegin\n if v_uid is null then\n raise exception 'not signed in';\n end if;\n select * into v_auction from public.auctions where id = p_auction_id for update;\n if not found then\n raise exception 'unknown auction';\n end if;\n if v_auction.ends_at <= now() then\n raise exception 'auction has ended';\n end if;\n if v_auction.seller_id = v_uid then\n raise exception 'sellers cannot bid on their own auctions';\n end if;\n if p_amount is null\n or (v_auction.highest_bid is null and p_amount < v_auction.starting_price)\n or (v_auction.highest_bid is not null and p_amount < v_auction.highest_bid + v_auction.min_increment) then\n raise exception 'bid too low';\n end if;\n insert into public.bids (auction_id, bidder_id, amount) values (p_auction_id, v_uid, p_amount);\n update public.auctions set highest_bid = p_amount, highest_bidder = v_uid where id = p_auction_id;\n return p_amount;\nend\n$$;\n", "trap_files": [{"name": "rpc-input-validation", "sql": "alter table public.auctions enable row level security;\nalter table public.bids enable row level security;\n\ncreate policy auctions_select on public.auctions\n for select to authenticated\n using (true);\n\ncreate policy bids_select on public.bids\n for select to authenticated\n using (\n bidder_id = auth.uid()\n or exists (select 1 from public.auctions a where a.id = auction_id and a.seller_id = auth.uid())\n );\n\ncreate function public.place_bid(p_auction_id bigint, p_amount numeric) returns numeric\nlanguage plpgsql security definer set search_path = ''\nas $$\ndeclare\n v_uid uuid := auth.uid();\n v_auction public.auctions%rowtype;\nbegin\n if v_uid is null then\n raise exception 'not signed in';\n end if;\n select * into v_auction from public.auctions where id = p_auction_id for update;\n if not found then\n raise exception 'unknown auction';\n end if;\n if v_auction.ends_at <= now() then\n raise exception 'auction has ended';\n end if;\n if v_auction.seller_id = v_uid then\n raise exception 'sellers cannot bid on their own auctions';\n end if;\n if p_amount is null or p_amount <= coalesce(v_auction.highest_bid, 0) then\n raise exception 'bid too low';\n end if;\n insert into public.bids (auction_id, bidder_id, amount) values (p_auction_id, v_uid, p_amount);\n update public.auctions set highest_bid = p_amount, highest_bidder = v_uid where id = p_auction_id;\n return p_amount;\nend\n$$;\n"}, {"name": "self-approval", "sql": "alter table public.auctions enable row level security;\nalter table public.bids enable row level security;\n\ncreate policy auctions_select on public.auctions\n for select to authenticated\n using (true);\n\ncreate policy bids_select on public.bids\n for select to authenticated\n using (\n bidder_id = auth.uid()\n or exists (select 1 from public.auctions a where a.id = auction_id and a.seller_id = auth.uid())\n );\n\ncreate function public.place_bid(p_auction_id bigint, p_amount numeric) returns numeric\nlanguage plpgsql security definer set search_path = ''\nas $$\ndeclare\n v_uid uuid := auth.uid();\n v_auction public.auctions%rowtype;\nbegin\n if v_uid is null then\n raise exception 'not signed in';\n end if;\n select * into v_auction from public.auctions where id = p_auction_id for update;\n if not found then\n raise exception 'unknown auction';\n end if;\n if v_auction.ends_at <= now() then\n raise exception 'auction has ended';\n end if;\n if p_amount is null\n or (v_auction.highest_bid is null and p_amount < v_auction.starting_price)\n or (v_auction.highest_bid is not null and p_amount < v_auction.highest_bid + v_auction.min_increment) then\n raise exception 'bid too low';\n end if;\n insert into public.bids (auction_id, bidder_id, amount) values (p_auction_id, v_uid, p_amount);\n update public.auctions set highest_bid = p_amount, highest_bidder = v_uid where id = p_auction_id;\n return p_amount;\nend\n$$;\n"}, {"name": "time-window", "sql": "alter table public.auctions enable row level security;\nalter table public.bids enable row level security;\n\ncreate policy auctions_select on public.auctions\n for select to authenticated\n using (true);\n\ncreate policy bids_select on public.bids\n for select to authenticated\n using (\n bidder_id = auth.uid()\n or exists (select 1 from public.auctions a where a.id = auction_id and a.seller_id = auth.uid())\n );\n\ncreate function public.place_bid(p_auction_id bigint, p_amount numeric) returns numeric\nlanguage plpgsql security definer set search_path = ''\nas $$\ndeclare\n v_uid uuid := auth.uid();\n v_auction public.auctions%rowtype;\nbegin\n if v_uid is null then\n raise exception 'not signed in';\n end if;\n select * into v_auction from public.auctions where id = p_auction_id for update;\n if not found then\n raise exception 'unknown auction';\n end if;\n if v_auction.seller_id = v_uid then\n raise exception 'sellers cannot bid on their own auctions';\n end if;\n if p_amount is null\n or (v_auction.highest_bid is null and p_amount < v_auction.starting_price)\n or (v_auction.highest_bid is not null and p_amount < v_auction.highest_bid + v_auction.min_increment) then\n raise exception 'bid too low';\n end if;\n insert into public.bids (auction_id, bidder_id, amount) values (p_auction_id, v_uid, p_amount);\n update public.auctions set highest_bid = p_amount, highest_bidder = v_uid where id = p_auction_id;\n return p_amount;\nend\n$$;\n"}]}
{"id": "T098", "title": "Gym classes with capacity limits and a waitlist", "tier": 2, "split": "dev", "tags": ["aggregate-limit", "waitlist", "instructor-role", "client-returning"], "traps": ["aggregate-limit", "state-transition"], "extra_tables": [], "fixture_notes": ["class 1 Yoga (capacity 2) is full: ana (…01) and ben (…02) enrolled, cy (…03) waitlisted", "class 2 HIIT (capacity 3): ana enrolled, eli (…05) waitlisted — room left", "instructor ins (…06) teaches classes 1 and 2; ins2 (…07) teaches class 3"], "created": "2026-10-25", "canary": "RLSBENCH-CANARY-f9002c750838f00b", "requirement": "A gym's class-booking app built with supabase-js. Members join with `supabase.from('class_spots').insert({ class_id, user_id, status }).select()`, choosing `enrolled` or `waitlisted`; a waitlisted member moves up with `.update({ status: 'enrolled' }).match({ class_id, user_id }).select()` and leaves with `.delete()`.\n\nR1. Signed-in users can see every class. Members can see their own spots; instructors can see every spot in the classes they teach.\nR2. Members can join a class as themselves: as `enrolled` only while the class has fewer enrolled members than its capacity, otherwise only as `waitlisted`.\nR3. Members can move their own waitlisted spot to `enrolled` only when there is room, and can leave a class by deleting their own spot. Nothing else about a spot can change.\nR4. Instructors can remove any spot from the classes they teach.\nR5. Nobody can change classes through the app. Anonymous visitors cannot see or change anything.\nR6. Server-side code using the service role keeps full access.\n\nRLSBENCH-CANARY-f9002c750838f00b\n", "schema_sql": "create table public.classes (\n id bigint primary key,\n title text not null,\n capacity integer not null check (capacity > 0),\n instructor_id uuid not null references auth.users(id)\n);\n\ncreate table public.class_spots (\n class_id bigint not null references public.classes(id),\n user_id uuid not null default auth.uid() references auth.users(id),\n status text not null check (status in ('enrolled', 'waitlisted')),\n primary key (class_id, user_id)\n);\n", "seed_sql": "insert into auth.users (id, email) values\n ('00000000-0000-0000-0000-000000000001', 'ana@example.com'),\n ('00000000-0000-0000-0000-000000000002', 'ben@example.com'),\n ('00000000-0000-0000-0000-000000000003', 'cy@example.com'),\n ('00000000-0000-0000-0000-000000000004', 'dee@example.com'),\n ('00000000-0000-0000-0000-000000000005', 'eli@example.com'),\n ('00000000-0000-0000-0000-000000000006', 'ins@example.com'),\n ('00000000-0000-0000-0000-000000000007', 'ins2@example.com');\n\ninsert into public.classes (id, title, capacity, instructor_id) values\n (1, 'Yoga', 2, '00000000-0000-0000-0000-000000000006'), (2, 'HIIT', 3, '00000000-0000-0000-0000-000000000006'), (3, 'Pilates', 5, '00000000-0000-0000-0000-000000000007');\n\ninsert into public.class_spots (class_id, user_id, status) values\n (1, '00000000-0000-0000-0000-000000000001', 'enrolled'), (1, '00000000-0000-0000-0000-000000000002', 'enrolled'), (1, '00000000-0000-0000-0000-000000000003', 'waitlisted'),\n (2, '00000000-0000-0000-0000-000000000001', 'enrolled'), (2, '00000000-0000-0000-0000-000000000005', 'waitlisted');\n", "tests_yaml": "- name: member_sees_classes\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n sql: select count(*) from classes\n expect:\n rows:\n - [3]\n\n- name: member_sees_own_spots\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n sql: select class_id, status from class_spots order by class_id\n expect:\n rows:\n - [1, waitlisted]\n\n- name: instructor_sees_class_spots\n covers: [R1]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000006}\n sql: select count(*) from class_spots\n expect:\n rows:\n - [5]\n\n- name: member_enrolls_with_room\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n client:\n op: insert\n table: class_spots\n values: {class_id: 2, user_id: 00000000-0000-0000-0000-000000000004, status: enrolled}\n returning: true\n expect: {affected: 1}\n\n- name: member_cannot_enroll_in_full_class\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n client:\n op: insert\n table: class_spots\n values: {class_id: 1, user_id: 00000000-0000-0000-0000-000000000004, status: enrolled}\n returning: false\n expect: {denied: true}\n\n- name: member_joins_waitlist_of_full_class\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n client:\n op: insert\n table: class_spots\n values: {class_id: 1, user_id: 00000000-0000-0000-0000-000000000004, status: waitlisted}\n returning: true\n expect: {affected: 1}\n\n- name: member_cannot_join_for_someone_else\n covers: [R2]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n client:\n op: insert\n table: class_spots\n values: {class_id: 2, user_id: 00000000-0000-0000-0000-000000000003, status: waitlisted}\n returning: false\n expect: {denied: true}\n\n- name: waitlisted_member_moves_up_when_room\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000005}\n client:\n op: update\n table: class_spots\n values: {status: enrolled}\n match: {class_id: 2, user_id: 00000000-0000-0000-0000-000000000005}\n returning: true\n expect: {affected: 1}\n\n- name: waitlisted_member_cannot_jump_full_class\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: class_spots\n values: {status: enrolled}\n match: {class_id: 1, user_id: 00000000-0000-0000-0000-000000000003}\n returning: false\n expect: {denied: true}\n\n- name: member_leaves_class\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: class_spots\n match: {class_id: 1, user_id: 00000000-0000-0000-0000-000000000001}\n expect: {affected: 1}\n\n- name: member_cannot_remove_someone_else\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000001}\n client:\n op: delete\n table: class_spots\n match: {class_id: 1, user_id: 00000000-0000-0000-0000-000000000003}\n expect: {denied: true}\n\n- name: member_cannot_move_spot_to_other_class\n covers: [R3]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000003}\n client:\n op: update\n table: class_spots\n values: {class_id: 2}\n match: {class_id: 1, user_id: 00000000-0000-0000-0000-000000000003}\n returning: false\n expect: {denied: true}\n\n- name: instructor_removes_spot\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000006}\n client:\n op: delete\n table: class_spots\n match: {class_id: 1, user_id: 00000000-0000-0000-0000-000000000003}\n expect: {affected: 1}\n\n- name: other_instructor_cannot_remove_spot\n covers: [R4]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000007}\n client:\n op: delete\n table: class_spots\n match: {class_id: 1, user_id: 00000000-0000-0000-0000-000000000003}\n expect: {denied: true}\n\n- name: member_cannot_raise_capacity\n covers: [R5]\n as: {role: authenticated, sub: 00000000-0000-0000-0000-000000000004}\n client:\n op: update\n table: classes\n values: {capacity: 99}\n match: {id: 1}\n returning: false\n expect: {denied: true}\n\n- name: anon_sees_no_spots\n covers: [R5]\n as: {role: anon}\n sql: select * from class_spots\n expect: {denied: true}\n\n- name: service_role_sees_all_spots\n covers: [R6]\n as: {role: service_role}\n sql: select count(*) from class_spots\n expect:\n rows:\n - [5]\n", "n_tests": 17, "solution_sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_instructor(p_class_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.classes c where c.id = p_class_id and c.instructor_id = auth.uid())\n$$;\n\ncreate function private.has_room(p_class_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select (select count(*) from public.class_spots s where s.class_id = p_class_id and s.status = 'enrolled') < (select c.capacity from public.classes c where c.id = p_class_id)\n$$;\n\nalter table public.classes enable row level security;\nalter table public.class_spots enable row level security;\n\nrevoke update on public.class_spots from anon, authenticated;\ngrant update (status) on public.class_spots to authenticated;\n\ncreate policy classes_select on public.classes\n for select to authenticated\n using (true);\n\ncreate policy spots_select on public.class_spots\n for select to authenticated\n using (user_id = auth.uid() or private.is_instructor(class_id));\n\ncreate policy spots_insert on public.class_spots\n for insert to authenticated\n with check (user_id = auth.uid() and (status = 'waitlisted' or private.has_room(class_id)));\n\ncreate policy spots_update on public.class_spots\n for update to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid() and (status = 'waitlisted' or private.has_room(class_id)));\n\ncreate policy spots_delete on public.class_spots\n for delete to authenticated\n using (user_id = auth.uid() or private.is_instructor(class_id));\n", "trap_files": [{"name": "aggregate-limit", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_instructor(p_class_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.classes c where c.id = p_class_id and c.instructor_id = auth.uid())\n$$;\n\ncreate function private.has_room(p_class_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select (select count(*) from public.class_spots s where s.class_id = p_class_id and s.status = 'enrolled') < (select c.capacity from public.classes c where c.id = p_class_id)\n$$;\n\nalter table public.classes enable row level security;\nalter table public.class_spots enable row level security;\n\nrevoke update on public.class_spots from anon, authenticated;\ngrant update (status) on public.class_spots to authenticated;\n\ncreate policy classes_select on public.classes\n for select to authenticated\n using (true);\n\ncreate policy spots_select on public.class_spots\n for select to authenticated\n using (user_id = auth.uid() or private.is_instructor(class_id));\n\ncreate policy spots_insert on public.class_spots\n for insert to authenticated\n with check (user_id = auth.uid());\n\ncreate policy spots_update on public.class_spots\n for update to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid() and (status = 'waitlisted' or private.has_room(class_id)));\n\ncreate policy spots_delete on public.class_spots\n for delete to authenticated\n using (user_id = auth.uid() or private.is_instructor(class_id));\n"}, {"name": "state-transition", "sql": "create schema private;\n\ngrant usage on schema private to authenticated;\n\ncreate function private.is_instructor(p_class_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select exists (select 1 from public.classes c where c.id = p_class_id and c.instructor_id = auth.uid())\n$$;\n\ncreate function private.has_room(p_class_id bigint) returns boolean\nlanguage sql stable security definer set search_path = ''\nas $$\n select (select count(*) from public.class_spots s where s.class_id = p_class_id and s.status = 'enrolled') < (select c.capacity from public.classes c where c.id = p_class_id)\n$$;\n\nalter table public.classes enable row level security;\nalter table public.class_spots enable row level security;\n\nrevoke update on public.class_spots from anon, authenticated;\ngrant update (status) on public.class_spots to authenticated;\n\ncreate policy classes_select on public.classes\n for select to authenticated\n using (true);\n\ncreate policy spots_select on public.class_spots\n for select to authenticated\n using (user_id = auth.uid() or private.is_instructor(class_id));\n\ncreate policy spots_insert on public.class_spots\n for insert to authenticated\n with check (user_id = auth.uid() and (status = 'waitlisted' or private.has_room(class_id)));\n\ncreate policy spots_update on public.class_spots\n for update to authenticated\n using (user_id = auth.uid())\n with check (user_id = auth.uid());\n\ncreate policy spots_delete on public.class_spots\n for delete to authenticated\n using (user_id = auth.uid() or private.is_instructor(class_id));\n"}]}
|