# Security, policies and audit
Source: https://sovaria.in/docs/security-and-policies

How access is decided, how to test it before you ship, and where every change is recorded.

Every request to your project is decided on the server, in three layers. Nothing in a browser or an app can skip them.

| Layer | Answers | Where |
|---|---|---|
| Identity | Who is calling: a signed-in user, an anonymous visitor, a server key or a team member | Sessions and keys ([Keys, tokens and credentials](https://sovaria.in/docs/credentials)) |
| Access policies | May this caller use this service and action: read a table, write a document, upload to a bucket, join a channel, run a function | **Policies**, in Sovaria's policy language ([Policy language (SPL)](https://sovaria.in/docs/policy-language)) |
| Row-level security | Which rows of a table | Postgres policies on each table ([Row-level security and access](https://sovaria.in/docs/row-level-security)) |

- [How access is decided](https://sovaria.in/docs/access-model): The full order of checks, from the request to the row.
- [Policy language](https://sovaria.in/docs/policy-language): Write rules with the caller, the action and the resource.
- [Row-level security](https://sovaria.in/docs/row-level-security): Owner-only tables, teams and shared rows in Postgres.
- [Security recipes](https://sovaria.in/docs/security-recipes): Copy-ready rules for the common cases.
- [Keys and credentials](https://sovaria.in/docs/credentials): Which key goes where, and how to rotate it.
- [Audit log](https://sovaria.in/docs/audit-logs): Every sign-in, key, policy and settings change.

## Project access policies

Policies are short rules managed under **Policies**. They cover tables, documents, storage, realtime channels and functions. Templates cover the common cases: owners only, signed-in read, staff write, own folder, own channel.

Policy:
```bash
# Signed-in users may read the catalogue; only staff may change it
ALLOW IF ctx.actor.is_anonymous == False AND ctx.action == "SELECT"
ALLOW IF "STAFF" IN ctx.actor.roles
```

## Test before you ship

- **Simulation** runs requests as different people against your policies without touching data, and shows which rule decided each one ([Simulate access before you ship](https://sovaria.in/docs/simulation)).
- **Rules tests** keep those expectations in your repository and run them in CI ([Access rules tests](https://sovaria.in/docs/rules-tests)).
- **Security advisor** flags tables without row-level security, public buckets and risky keys ([Security advisor](https://sovaria.in/docs/security-advisor)).
- **Test session** on a user runs real queries as that user.

## Before you go to production

- Row-level security is on for every table your app reads, and the Security advisor shows no warnings.
- Each collection, bucket and channel has a rule, and Simulation shows strangers are refused.
- Only the publishable key is in your app. Service keys live in function secrets or your server's secret store ([Keys, tokens and credentials](https://sovaria.in/docs/credentials)).
- Allowed origins list only your real sites; remove `localhost` when you no longer need it ([Allowed origins (CORS) for browser apps](https://sovaria.in/docs/allowed-origins)).
- Mobile apps use app attestation, so only your signed app can use the publishable key ([App attestation](https://sovaria.in/docs/mobile-attestation)).
- Every owner and admin uses two-factor authentication, and there are at least two owners ([Working as a team](https://sovaria.in/docs/team-access)).
- Rate limits and attack protection suit your sign-up and sign-in pages ([Authentication settings reference](https://sovaria.in/docs/auth-settings)).
- Alerts for security events go to a channel someone reads ([Alerts: Slack, Discord, Teams, webhooks](https://sovaria.in/docs/integration-alerts)).
- Backups match how much data you could afford to lose ([Backups and recovery](https://sovaria.in/docs/backups-and-recovery)).

## What Sovaria does and what you do

| Sovaria | You |
|---|---|
| Checks every request against your policies and row-level security on the server | Write the policies and rules that fit your app, and test them |
| Encrypts secrets at rest and never returns them after you save them | Keep service keys out of apps and repositories; rotate them when people leave |
| Isolates projects: they never share data, users or keys | Give each teammate the smallest role that works |
| Binds query values in the database, never splicing them into SQL | Avoid building SQL strings from user input in your own functions |
| Records security events in the audit log | Read alerts and review the log |

## Audit log

Sign-ins, key and policy changes, deploys, migrations and admin actions are recorded. See them in **Logs & Audit** and export them; request and function logs can also stream to your own tools ([Datadog, Loki and log drains](https://sovaria.in/docs/integration-observability)).

CLI:
```bash
sov logs api
```

Found a vulnerability? See [Reporting a vulnerability](https://sovaria.in/docs/vulnerability-disclosure).
