# Security recipes
Source: https://sovaria.in/docs/security-recipes

Tested patterns for common access needs: public catalogues, users owning their rows, teams, admins, public forms, read-only keys, protected owner columns, tenant isolation, column limits, private files and realtime channels.

Each recipe names the threat it stops, the rules (access policy and/or SQL) and how to test it from both sides. Replace the table and column names with yours. Test every recipe with [Simulate access before you ship](https://sovaria.in/docs/simulation) before relying on it.

> **Tip:** Where a recipe uses row-level security, enable **and force** it on the table (`alter table … force row level security`) — see [Row-level security and access](https://sovaria.in/docs/row-level-security).

## Public read-only catalogue

**Threat:** visitors or users changing prices. **Allows:** anyone reads `products`; nobody but your team writes.

Access policy:
```bash
ALLOW IF ctx.resource.name == "products" AND ctx.action == "SELECT"
DENY  IF ctx.resource.name == "products" AND ctx.action IN ["INSERT", "UPDATE", "DELETE"] AND ctx.actor.lane == "product"
```

**Test:** with the anon key, `select` returns rows and `update products set price = 0` is refused with `403`.

## Users read and change only their own rows

**Threat:** one user reading or editing another's data by guessing ids.

SQL:
```sql
alter table notes enable row level security;
alter table notes force row level security;
create policy "own notes" on notes
  for all using (owner_id = auth.uid()) with check (owner_id = auth.uid());
```

Access policy:
```bash
ALLOW IF ctx.resource.name == "notes" AND ctx.actor.is_anonymous == False
```

**Test:** as user A, `select * from notes` returns only A's notes; `update notes set body = 'x' where id = <B's note>` changes `0` rows. Signed out, the request is refused.

## Users create rows they own — and can't give them away

**Threat:** a user inserting a row with someone else's `owner_id`, or moving their row to another user.

SQL:
```sql
alter table notes alter column owner_id set default auth.uid();
create policy "insert as yourself" on notes
  for insert with check (owner_id = auth.uid());
create policy "keep the owner" on notes
  for update using (owner_id = auth.uid()) with check (owner_id = auth.uid());
```

`with check` runs on the new row, so an update that sets `owner_id` to another user fails. **Test:** insert with `owner_id` set to another user's id — refused.

## Team-level data

**Threat:** members of one team reading another team's projects.

SQL:
```sql
create table team_members (team_id uuid, user_id uuid, primary key (team_id, user_id));
create table team_docs (id bigint generated always as identity primary key, team_id uuid not null, body text);

create or replace function is_team_member(t uuid) returns boolean
language sql stable security definer set search_path = public as $$
  select exists (select 1 from team_members where team_id = t and user_id = auth.uid())
$$;

alter table team_docs enable row level security;
alter table team_docs force row level security;
create policy "team members" on team_docs
  for all using (is_team_member(team_id)) with check (is_team_member(team_id));
```

The `security definer` helper reads `team_members` without its own row security getting in the way, and still sees the caller through `auth.uid()`. **Test:** a user in team A gets `0` rows for team B's documents.

## Admin-only operations

**Threat:** ordinary users deleting or refunding. Give admins the app role `ADMIN` on the Users page.

Access policy:
```bash
DENY IF ctx.resource.name == "refunds" AND ctx.action IN ["INSERT", "UPDATE", "DELETE"] AND NOT ("ADMIN" IN ctx.actor.roles) AND ctx.actor.lane == "product"
```

SQL:
```sql
create policy "admins write refunds" on refunds
  for all using (auth.has_role('ADMIN')) with check (auth.has_role('ADMIN'));
```

## Public form submissions

**Threat:** visitors reading other submissions, or flooding the table. **Allows:** anyone inserts; nobody but your team reads.

Access policy:
```bash
ALLOW IF ctx.resource.name == "contact_requests" AND ctx.action == "INSERT"
```

SQL:
```sql
alter table contact_requests enable row level security;
alter table contact_requests force row level security;
create policy "anyone submits" on contact_requests for insert with check (true);
-- no select policy: app users and visitors can't read any row
```

Rate limits apply per IP and per project ([API keys, origins and security](https://sovaria.in/docs/settings-api-and-security)); add CAPTCHA on sign-up if you take sign-ups too. **Test:** insert with the anon key works; `select` returns nothing.

## Read-only access for a server or partner

Create a **custom API key** with the **viewer** role and an expiry under Project Settings › API keys ([API keys](https://sovaria.in/docs/api-keys)). Unlike the service role key it does not bypass row-level security: in SQL it acts as `anon`. Sovaria's default rules let a viewer key read but never write; only an ALLOW rule of yours that matches it can let it write. **Test:** run an `insert` with the key in [Simulate access before you ship](https://sovaria.in/docs/simulation) or against staging — it must be refused.

## No cross-tenant access (SaaS apps)

**Threat:** a customer of your app seeing another customer's data. Put the tenant in the token with a JWT hook ([Authentication settings reference](https://sovaria.in/docs/auth-settings)) and compare every row against it.

SQL:
```sql
create policy "own tenant" on invoices
  for all using (tenant_id = (auth.jwt() ->> 'tenant_id')::uuid)
  with check (tenant_id = (auth.jwt() ->> 'tenant_id')::uuid);
```

Never take the tenant id from the request body. **Test:** run the same query as users of two tenants and compare row counts.

## Only some columns

Row-level security works on rows, not columns. Expose the safe columns through a view and allow the view instead of the table:

SQL:
```sql
create view public_profiles with (security_invoker = true) as
  select id, display_name, avatar_url from profiles;   -- no email, no phone
```

Access policy:
```bash
ALLOW IF ctx.resource.name == "public_profiles" AND ctx.action == "SELECT"
```

`security_invoker` makes the view run with the caller's row security.

## Private files per user

**Threat:** users reading or overwriting each other's uploads. Store files as `<user id>/<file>` in a private bucket.

Access policy:
```bash
ALLOW IF ctx.resource.name == "documents" AND ctx.storage.in_own_folder == True AND ctx.action IN ["READ", "LIST", "WRITE", "DELETE"]
```

The bucket's **Policies** tab has this as a preset (rules there are named `[bucket:documents] …`). Share a file with someone else through a signed link that expires ([Storage](https://sovaria.in/docs/storage-overview)). **Test:** user A downloading `B/file.pdf` gets `403`.

## Private realtime channels

**Threat:** anyone joining a channel and reading its messages. Allow subscribing and publishing per channel:

Access policy:
```bash
# Everyone signed in hears announcements; only staff send them
ALLOW IF ctx.resource.name == "announcements" AND ctx.action == "SUBSCRIBE" AND ctx.actor.is_anonymous == False
ALLOW IF ctx.resource.name == "announcements" AND ctx.action == "PUBLISH" AND "STAFF" IN ctx.actor.roles

# Each user's private channel: user:<their id> (and user:<their id>:anything)
ALLOW IF ctx.realtime.in_own_channel == True
```

For data even Sovaria can't read, encrypt it in the client with your own key before it is sent: the SDK does this for documents with `putDocumentE2ee` ([JavaScript: Database and documents](https://sovaria.in/docs/javascript-database)). Encrypted realtime channels use keys Sovaria issues and holds, so they protect messages in transit and at rest, not from Sovaria itself ([Realtime](https://sovaria.in/docs/realtime-overview)).

> **Note:** Database change events for a table go to the channel `db.table.<table>`. They say that rows were inserted, updated or deleted and how many — not their contents — so anyone allowed to subscribe learns **that** the table changed, and then reads the rows through your rules like any other request. Allow `db.table.<table>` only to callers who may know that ([Database change events](https://sovaria.in/docs/realtime-database-changes)). A rule that explicitly allows reading the table also allows listening to it, unless a rule on the channel denies it.
