# Policy language (SPL)
Source: https://sovaria.in/docs/policy-language

The syntax of access policies: ALLOW and DENY rules, the ctx fields you can test, operators, precedence, and how to write, check, version and roll back rules.

An access policy is one rule: `ALLOW IF <condition>` or `DENY IF <condition>`. The condition reads facts about the request — who is calling, what they want to do and to which resource — under `ctx`. Rules are compiled and checked on the server for every request.

SPL:
```bash
ALLOW IF ctx.resource.name == "products" AND ctx.action == "SELECT"
ALLOW IF ctx.resource.name == "orders" AND ctx.actor.is_anonymous == False
DENY  IF ctx.resource.name == "audit_log" AND ctx.action IN ["UPDATE", "DELETE"]
ALLOW IF "STAFF" IN ctx.actor.roles
```

## Syntax

| Part | Written as | Notes |
|---|---|---|
| Rule | `ALLOW IF …` or `DENY IF …` | One rule per policy. |
| Comparisons | `==` `!=` `>` `<` `>=` `<=` | Strings, whole numbers and `True` / `False`. |
| Membership | `value IN list` · `list CONTAINS value` | `"STAFF" IN ctx.actor.roles`; `ctx.action IN ["INSERT", "UPDATE"]`. |
| Range | `x BETWEEN low AND high` | Inclusive. |
| Logic | `AND`, `OR`, `NOT`, parentheses | `AND` binds tighter than `OR`; use parentheses when mixing. |
| Values | `"text"`, `42`, `True`, `False`, `["a", "b"]` | Keywords are case-insensitive; values are not. |
| Fields | `ctx.…` dot paths | See below. |

There are no loops, functions or backward jumps: every rule runs in a fixed number of steps, and a rule that can't be evaluated never allows anything.

## Fields you can test

| Field | What it holds |
|---|---|
| `ctx.action` | What the caller wants to do. Tables: `SELECT`, `INSERT`, `UPDATE`, `DELETE`. Documents: `doc:read`, `doc:write`, `doc:delete`. Storage: `READ`, `LIST`, `WRITE`, `DELETE`. Realtime: `SUBSCRIBE`, `PUBLISH`. |
| `ctx.action_name` | The service's own name for the action (`sql:select`, `doc:read`, …), whichever spelling your rule uses. |
| `ctx.resource.name` | The table, collection, bucket or channel. |
| `ctx.resource.tables` | Every table a SQL statement reads or writes (a join names several). |
| `ctx.actor.user_id` | The signed-in app user's id; empty for visitors and server keys. |
| `ctx.actor.is_anonymous` | `True` for the anon key, requests without a user, and anonymous app users (anonymous sign-in). So `ctx.actor.is_anonymous == False` means a user who signed up. |
| `ctx.actor.signed_in` | `True` for any user session, anonymous app users included; `False` for visitors and every API key. Use `ctx.actor.signed_in == True` to let anonymous app users in as well. |
| `ctx.actor.roles` | The caller's roles: an app user's app roles (`STAFF`, `ADMIN`…), a team member's member role. |
| `ctx.actor.role` | The main role (`anonymous` when there is none). |
| `ctx.actor.lane` | `product` for app users and the anon key; `console` for team members. |
| `ctx.storage.path`, `ctx.storage.folder`, `ctx.storage.in_own_folder` | Storage only: the object path, its first folder, and whether that folder is the caller's user id (`<user id>/avatar.png`). |
| `ctx.realtime.in_own_channel` | Realtime only: the channel is the caller's private channel, `user:<user id>` or anything under it (`user:<user id>:notifications`). |
| `ctx.org_id`, `ctx.project_id` | Where the request is going. |

> **Tip:** Write `ctx.action == "SELECT"` or `ctx.action == "sql:select"` — both work. A rule that mentions `"sql:` anywhere sees the service's spelling; any other rule sees the SQL verb.

## How rules combine

- A matching **DENY** always wins, whichever order rules were created in.
- Otherwise any matching **ALLOW** lets the request through.
- If nothing matches, the request is refused (tables with forced row-level security are the exception — see [How access is decided](https://sovaria.in/docs/access-model)).
- A **DENY** that tests a field the request doesn't carry refuses; an **ALLOW** in the same situation doesn't match.

## Write, check and ship a rule

1. **Start from a template** — **Policies › New policy** lists templates (signed-in read, owner only, staff write, own folder, own channel…). Pick one and change the names.
2. **Name it after what it covers** — Prefix the name with `[table:orders]`, `[collection:carts]` or `[bucket:avatars]` and the console shows the rule with that table, collection or bucket.
3. **Check it** — The editor validates the syntax as you type. **Check** evaluates it for a sample request; `sov policy check` does the same from a terminal.
4. **Simulate before shipping** — [Simulate access before you ship](https://sovaria.in/docs/simulation) replays real requests as every user type against your changed rules and shows what would change.
5. **Save** — Saving applies the rule to the next requests. Every save is a new version.

CLI:
```bash
sov policy templates
sov policy upsert pol_orders_owner --org <project-id> --name "[table:orders] Signed-in read" \
  --spl 'ALLOW IF ctx.resource.name == "orders" AND ctx.action == "SELECT" AND ctx.actor.is_anonymous == False'
sov policy check --action sql:select --resource orders
sov policy history pol_orders_owner
sov policy rollback pol_orders_owner --version 2
```

JavaScript:
```js
await sovaria.policy.create({
  policy_id: 'pol_orders_owner',
  name: '[table:orders] Signed-in read',
  spl_source: 'ALLOW IF ctx.resource.name == "orders" AND ctx.action == "SELECT" AND ctx.actor.is_anonymous == False',
  org_id: '<project-id>',   // project rules are stored under the project
})
const history = await sovaria.policy.history('pol_orders_owner')
await sovaria.policy.rollback('pol_orders_owner', 2)
```

Exact flags and methods: [CLI: Policies, rules tests, simulation, audit and compliance](https://sovaria.in/docs/cli-security), [JavaScript: Policies and audit](https://sovaria.in/docs/javascript-policies), [Python: Policies and audit](https://sovaria.in/docs/python-policies), [HTTP API: policies, simulation, audit and encryption keys](https://sovaria.in/docs/rest-security).

## Versions, environments and rollback

- Every change to a rule is a numbered version; **History** shows who changed what, and **Roll back** restores an earlier version as a new one.
- Deleting a policy deactivates it; its history stays.
- An environment (staging, a version) can have its own rules that replace production's while you test; promote them when ready — [Environments, staging and deploying to production](https://sovaria.in/docs/environments-and-staging).
- Policy changes are recorded in the audit log ([Audit log](https://sovaria.in/docs/audit-logs)).

## Common mistakes

| Mistake | What happens | Fix |
|---|---|---|
| `ALLOW IF ctx.actor.is_anonymous == False` with no resource | Every signed-in user may use every table, bucket and channel. | Always name the resource: `ctx.resource.name == "orders" AND …`. |
| `"STAFF" in ctx.actor.roles` vs a role named `Staff` | Role values are compared exactly. | Use the role names exactly as on the Users page (they are stored in capitals). |
| Relying on a DENY to hide some rows | A DENY refuses the whole request. | Filter rows with row-level security; use DENY for whole actions. |
| `AND` and `OR` mixed without parentheses | `a OR b AND c` means `a OR (b AND c)`. | Add parentheses. |
