# The threat model

> The attacks Sypnia Data is designed against, and what stops each one.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/security/threat-model/

For agents: Threats and controls: credential stuffing/brute force (rate limits, exponential lockout, CAPTCHA, breached passwords, no enumeration); stolen refresh token (rotation + reuse detection revokes session, HttpOnly cookie, short access tokens); stolen access token (1 h TTL, DPoP for machines); token forgery (Ed25519 per project, kid, JWKS); phishing links/open redirect (redirect allow list, single-use hashed tokens, 1 h expiry); CSRF on cookie mode (SameSite=Lax + double-submit header + CORS allow list); secret key leak (refused from browsers, hashed, revocable, secret scanning); SQL injection via the Data API (parameterised single statements, identifiers checked against the schema cache); privilege escalation in DB (no superuser, NOINHERIT authenticator, SET LOCAL ROLE per transaction); cross-project access (host→registry, one DB and keys per project); malicious agents (scopes, consent, audit, revocation); DoS (rate limits, statement timeouts, row caps, concurrency caps). Privileged scopes need an entitlement (app_metadata.scopes set with the secret key), checked at issuance (403 scope_not_allowed) and at use; fixed finding 2026-11-17 (self-granted sql:write via PAT/device flow).

| Threat                                           | What stops it                                                                                                                                                                                                                                         |
| ------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Guessing passwords, credential stuffing**      | rate limits per IP and per e-mail, waits that double after 10 failures, CAPTCHA, the breached-password check, and the same answer for unknown e-mails                                                                                                 |
| **A stolen refresh token**                       | rotation: the next use by either party reveals the theft and ends the session; in cookie mode it is `HttpOnly`, so page scripts cannot read it                                                                                                        |
| **A stolen access token**                        | it expires in an hour; machine tokens can be DPoP-bound and are useless without the private key                                                                                                                                                       |
| **Forged tokens**                                | each project signs with its own Ed25519 key; verification uses the project's JWKS and the `kid`                                                                                                                                                       |
| **Phishing with auth links**                     | redirects only to allowed addresses; links are single-use, stored as hashes, expire in an hour                                                                                                                                                        |
| **CSRF against cookie sessions**                 | `SameSite=Lax`, a double-submit CSRF header, and CORS limited to allowed origins                                                                                                                                                                      |
| **A leaked secret key**                          | refused from browsers, stored as a hash, revocable in a click, registered with GitHub secret scanning                                                                                                                                                 |
| **SQL injection through the Data API**           | every value is a parameter; tables and columns are checked against the schema before any SQL is built; one statement per request                                                                                                                      |
| **Escalating inside the database**               | no superuser; the engine's login has no rights until it takes a role for one transaction; `service_role` only with a secret key or `project:admin`                                                                                                    |
| **Taking a privileged scope you were not given** | `sql:*`, `auth:admin` and `project:admin` count on a user's token only when an admin listed them in `app_metadata.scopes`; PATs, device approvals and consents asking for more fail with `403 scope_not_allowed`; open registration never grants them |
| **Reading another project**                      | the host decides the project through the registry, never input; each project has its own database, roles and keys                                                                                                                                     |
| **An agent doing too much**                      | scopes, a consent screen, an audit log of every call, and revocation that takes effect at the next refresh                                                                                                                                            |
| **Exhausting the service**                       | rate limits, statement timeouts, row caps and a cap on concurrent instances                                                                                                                                                                           |

## Fixed findings

**2026-11-17: privileged scopes could be self-granted (critical, fixed before launch).**

- _What happened._ A signed-up user of a project could create a PAT for themselves with `sql:write`, or approve the device flow of the built-in CLI client with it. Either way they could run SQL as the project's owner role.
- _Where it reached._ That held for the `sypnia` project too, whose database holds the Sypnia Cloud control plane.
- _Exposure._ The engine had been live for hours, with no outside users. The only such tokens came from two QA accounts, and none of them ran SQL.
- _The fix._
  - The entitlement rule above, enforced at issuance and again at every use.
  - Account changes need the person's own session.
  - The CLI asks only for non-privileged scopes by default.
  - Regression tests cover PATs, the device flow, dynamic registration and authorization code with PKCE.

## Out of scope

Your app's own code, your database server's security (patches, network, backups when it is yours), and devices of your users that are already compromised.
