Przejdź do treści
Biblioteka skilli
Bezpieczeństwo · Budowanie i kod

secure-supabase-rls

/secure-supabase-rls

Włącza RLS w tabelach i widokach Supabase, pisze polityki dla ról, indeksuje je i testuje dostęp w pgTAP.

Licencja
MIT
Ostatnia zmiana repozytorium
23.10.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
3
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 23.10.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Włącza RLS w tabelach i widokach Supabase, pisze polityki dla ról, indeksuje je i testuje dostęp w pgTAP.

Oryginalny opis (EN)

Use when adding or changing a Supabase table, view, function or storage bucket exposed through the Data API, or auditing one — enables RLS, writes per-operation role-scoped policies, indexes them, keeps definer functions private, and proves allow and deny with pgTAP.

Jak działa

  1. 1. Audit current state
  2. 2. Decide what to do
  3. 3. Detect what the table is
  4. 4. Install only what's missing
  5. 5. Write the migration — grants, RLS, policies, indexes in one file
  6. 6. Test allow and deny with pgTAP
  7. 7. Wire into CI
  8. 8. Verify
  9. References
Sekcje z nagłówków SKILL.md, w oryginale · 116 linii, 945 słów
Podgląd SKILL.md
---
name: secure-supabase-rls
description: Use when adding or changing a Supabase table, view, function or storage bucket exposed through the Data API, or auditing one — enables RLS, writes per-operation role-scoped policies, indexes them, keeps definer functions private, and proves allow and deny with pgTAP.
---

# Secure Supabase RLS

RLS is the authorization boundary: the publishable key is public, so every row is reachable by anyone who holds a grant, and only a policy narrows it. The app's route guards are UX.

## 1. Audit current state

Read `.claude/stack-profile.md` (`database.host: supabase`, `backend.track`). Exposed schemas are `[api] schemas` in `supabase/config.toml` (default `public`, `graphql_public`). Run against the local stack (`--linked` for a remote):

```bash
pnpm supabase db advisors --local --type security            # the Security Advisor, same checks as the Dashboard
pnpm supabase db query --local "select n.nspname, c.relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where c.relkind in ('r','p') and n.nspname='public' and not c.relrowsecurity"
pnpm supabase db query --local "select schemaname, tablename, policyname, roles from pg_policies where roles = '{public}'"      # policies without a TO clause
pnpm supabase db query --local "select tablename, policyname from pg_policies where qual ~* 'user_metadata|raw_user_meta' or with_check ~* 'user_metadata|raw_user_meta'"
ls supabase/tests/*rls* 2>/dev/null
```

More queries (views, definer functions, grants, storage, unwrapped `auth.uid()`): [rls-patterns.md](./rls-patterns.md), "Audit queries". Each hit is a finding; report them before changing anything.

## 2. Decide what to do

- Table without RLS, or with RLS and no policy for an operation the app uses → steps 4–6.
- Policy without `to`, with `user_metadata`, or with a bare `auth.uid()` → fix in place (step 5), new migration.
- View without `security_invoker`, definer function in an exposed schema → step 5.
- No `supabase/tests/<table>_rls.test.sql` → step 6 even if the policies look right.
- All present and `supabase test db` green → "already in place".

## 3. Detect what the table is

Ask of each table, not of the codebase: who may read it, who may write it, and which column ties a row to its owner or tenant.

| Shape | Policy basis |
|---|---|
| Owner rows (`user_id`) | `(select auth.uid()) = user_id` |
| Tenant rows (`team_id`) | membership through a private definer function ([rls-patterns.md](./rls-patterns.md)) |
| Public read, server write | `select` grant and policy for `anon, authenticated`; no write grant |
| Server-only (jobs, audit log) | no grant to `anon`/`authenticated` at all; RLS on as a backstop |

## 4. Install only what's missing

Nothing to install: pgTAP ships with the local stack (`supabase test new`, `supabase test db`). For tenant tables, `supabase-test-helpers` is optional; plain `set local role` works without it ([rls-test-patterns.md](./rls-test-patterns.md)). Make sure the baseline privileges migration from [set-up-supabase](../set-up-supabase/SKILL.md) exists, so a new table does not start with every grant.

## 5. Write the migration — grants, RLS, policies, indexes in one file

```bash
pnpm supabase migration new secure_notes
```

```sql
create table public.notes (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
  body text not null,
  created_at timestamptz not null default now()
);

alter table public.notes enable row level security;
revoke all on table public.notes from anon, authenticated;
grant select, insert, update, delete on table public.notes to authenticated;

create policy "notes: owner reads" on public.notes for select
  to authenticated using ((select auth.uid()) = user_id);

create policy "notes: owner inserts" on public.notes for insert
  to authenticated with check ((select auth.uid()) = user_id);

create policy "notes: owner updates" on public.notes for update
  to authenticated
  using ((select auth.uid()) = user_id)
  with check ((select auth.uid()) = user_id);

create policy "notes: owner deletes" on public.notes for delete
  to authenticated using ((select auth.uid()) = user_id);

create index notes_user_id_idx on public.notes (user_id);
```

Rules in force (reasons in [rls-patterns.md](./rls-patterns.md)):

- One policy per operation, each `to authenticated` (or `anon, authenticated` for deliberate public reads).
- `(select auth.uid())`, never bare `auth.uid()`: the `select` makes Postgres evaluate it once per statement.
- An index on every column a policy filters, with that column first in the index.
- Authorization data comes from `app_metadata` or a table, never `user_metadata` (users can edit it).
- Views: `create view … with (security_invoker = true) as …`. A definer view hands out the rows the table policies withhold.
- Definer functions live in a schema that is not exposed (`private`), with `set search_path = ''`, schema-qualified names, and `revoke execute … from public` plus an explicit grant.
- Storage: policies on `storage.objects`, scoped by `bucket_id` and the owner folder; `anon` listing needs `storage.allow_any_operation`. See [rls-patterns.md](./rls-patterns.md), "Storage".
- The admin client (`SUPABASE_SECRET_KEY`) bypasses all of this: it must filter by the caller's id itself.

## 6. Test allow and deny with pgTAP

```bash
pnpm supabase test new notes_rls.test        # creates supabase/tests/notes_rls.test.sql
```

Cover `select`, `insert`, `update`, `delete` for `anon`, the owner and a stranger; pair every denied write with a check that the row is intact. A denied `using` filters silently (zero rows, no error), so assert emptiness, not an error; a missing grant or a failed `with check` raises `42501`. Template and the assertion table: [rls-test-patterns.md](./rls-test-patterns.md).

## 7. Wire into CI

`supabase test db` and `supabase db advisors --local --fail-on error` run in the PR job from [set-up-supabase](../set-up-supabase/SKILL.md). A new table without a test file fails review, not CI: add the audit query for tables lacking RLS to the PR checklist.

## 8. Verify

```bash
pnpm supabase db reset && pnpm supabase test db     # Result: PASS
pnpm supabase db advisors --local --type security --level warn   # no ERROR; warnings explained or fixed
curl -s "$SUPABASE_URL/rest/v1/notes?select=*" -H "apikey: $SUPABASE_PUBLISHABLE_KEY"   # anon: 401/permission denied or [], never rows
```

Run the curl against the local stack. Expected: no rows for `anon`, and the test run lists a denial for every operation.

## References
- [rls-patterns.md](./rls-patterns.md) — rules, anti-examples of common leaks, audit queries, storage.
- [rls-test-patterns.md](./rls-test-patterns.md) — pgTAP structure for owner and tenant tables.
- [../set-up-supabase/SKILL.md](../set-up-supabase/SKILL.md) — baseline privileges, CI.
- [../../core/_shared/security-baseline.md](../../core/_shared/security-baseline.md) — rule 3, authorization at the data boundary.
Pliki w paczce (3)
  • SKILL.md 7 KB
  • rls-patterns.md 14 KB
  • rls-test-patterns.md 5 KB

Wersje

W bazie nie ma innych skilli o nazwie secure-supabase-rls.

Migawka bazy z 13.11.2026 · wersja paczki 6a29119008a5 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 2 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100supabase-rls-policies
    Pisze, przegląda i testuje polityki Row Level Security w Supabase, żeby jeden użytkownik nie czytał ani nie zmieniał cudzych wierszy.
  • 100pgtap-rls
    Pisze, przegląda i uruchamia testy pgTAP dla Row Level Security, uprawnień i funkcji SQL w projektach Supabase.
  • 100pgtap-rls
    Pisze, przegląda i uruchamia testy pgTAP dla Row Level Security, uprawnień i funkcji SQL w projektach Supabase.
  • 100supabase-rls-audit
    Sprawdza zabezpieczenia Row Level Security i ekspozycję bazy Supabase w aplikacji: polityki, widoki, funkcje, klucze service_role i zasobniki.
  • 100supabase-rls-audit
    Audytuje Supabase RLS na podstawie migracji w repozytorium: tabele bez RLS, zbyt szerokie polityki i funkcje bez stałej ścieżki wyszukiwania.
  • 100data
    Projektuje lub przegląda schemat Supabase i Postgres, migracje, polityki RLS, funkcje SECURITY DEFINER, indeksy i wydajność zapytań.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.