# Policies

> Rules written as Postgres row-level security on the files table, with auth.uid() and your own tables.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://files.docs.sypnia.com/access/policies/

For agents: Files policies: Postgres RLS on sypnia_files.objects; select (download, list), insert (upload), update (+insert new path) for upsert/move/metadata, delete; default deny. Examples: public read for a bucket, own folder via sypnia_files.foldername(name)[1] = auth.uid()::text, join with public.orders via metadata orderId. Helpers foldername, filename, extension; auth.uid/role/jwt from Sypnia Data or engine with other issuer (JWKS). Cloud console policy templates and SQL preview with lint.

Every request is checked against **policies** on `sypnia_files.objects`, the same row-level security Postgres uses for your tables. A request is allowed only if a policy for that command and role allows it; with no policy, nothing is allowed.

| Request                                   | Command checked                          |
| ----------------------------------------- | ---------------------------------------- |
| download, list                            | `select`                                 |
| upload                                    | `insert`                                 |
| replace (`upsert`), move, change metadata | `update` (and `insert` for the new path) |
| delete                                    | `delete`                                 |

## Examples

```sql
-- anyone can read the public menu photos
create policy "menu is public" on sypnia_files.objects for select to anon, authenticated
  using (bucket_id = 'menu');

-- a user reads and writes only their own folder
create policy "own folder" on sypnia_files.objects for all to authenticated
  using (bucket_id = 'avatars' and (sypnia_files.foldername(name))[1] = auth.uid()::text)
  with check (bucket_id = 'avatars' and (sypnia_files.foldername(name))[1] = auth.uid()::text);

-- a customer reads the receipt of their own order (joins your table)
create policy "own receipts" on sypnia_files.objects for select to authenticated
  using (bucket_id = 'receipts' and exists (
    select 1 from public.orders o
    where o.id = (sypnia_files.objects.metadata->>'orderId')::bigint and o.customer_id = auth.uid()));
```

Helpers: `sypnia_files.foldername(name)` (the folders as an array), `sypnia_files.filename(name)`, `sypnia_files.extension(name)`. `auth.uid()`, `auth.role()` and `auth.jwt()` come from [Sypnia Data](https://data.docs.sypnia.com/data/row-level-security), or from the engine when you use another issuer.

## Without Sypnia Data

With another issuer, the engine verifies the JWT against its JWKS and exposes the same `auth.*` functions in its own metadata database, so policies are written the same way.

## In the console **\[Cloud]**

The policy editor offers templates («public read», «own folder», «owner only», «role from app_metadata») and shows the SQL it will run. The lint that warns about unqualified columns in sub-selects runs here too.
