Guides
Security checklist
What clientdb protects on its own, and the settings and habits a production app needs on top: sessions and tokens, cookies and other sites, rules, limits and devices.
clientdb decides what each viewer receives and may write from your rules, and refuses everything else. A few things are yours to get right. The examples use Framecast, a video-hosting app with teams, subscriptions and license keys.
Who the viewer is
The session callback is the only place the server learns who is signed in. Everything else trusts what it returns.
- Verify tokens with an audience.
verifyJwt({ jwks, audience })only accepts tokens issued for your app. Withoutaudience, a token your auth provider issued for another of your apps signs that user in here too. Setissueras well when your provider documents one. - Use long, random secrets.
tokens.secretandverifyJwt({ secret })need at least 32 characters (openssl rand -base64 32). Anyone who learns the secret can mint a session for any user, so keep it in an environment variable, never in client code. - Keep tokens short-lived. Sync tokens last
ttlSeconds(300 by default). Their claims and read-only flag hold until they expire, and a live socket checks its session again at that interval. When you revoke access, clients notice within one interval. - Signed-out visitors get a session only with
anonymous: true, and then see only rows whose rule includes$.public, and can't write.
Cookies and other sites
A browser attaches your app's cookies to requests that other sites make. clientdb guards against the two ways that could matter:
POSTbodies must beContent-Type: application/json, and other bodies get 415. A form on another site can't send JSON without a CORS preflight, so it can't push writes as the viewer.- A WebSocket opened by a page on another origin is refused (403) when it was authenticated by cookies, unless
corsallows that origin. With a sync token in?token=it is accepted, because another site can't get the viewer's token.
On your side:
- Set session cookies
SameSite=Lax(orStrict),SecureandHttpOnly. - List only your own origins in
cors.credentials: trueneeds explicit origins; a function origin that returns true for anything has the same effect as"*".
Tokens in URLs
Browsers can't set headers on WebSockets, so the browser client sends its token as ?token= on the /live socket. The server reads ?token= on WebSocket upgrades only, and the Swift client sends a header there too. URLs end up in access logs:
- give the socket short-lived sync tokens rather than your auth provider's long-lived ones, so a logged token expires within minutes;
- keep query strings of the
/livepath out of your proxy's logs.
Rules and fields
- A Framecast license's
keybelongs to its customer and to billing staff. Mark it.restricted()with a field rule: other team members still get the license, without the key, and they can't change the key or even name it in an update. With Prisma,omitkeeps a column on the server altogether. - Models that clients never need, such as an audit log, are
serverOnly: rules can read them, clients never receive them. - The server checks every written value against its field: type, enum values and nullability. A
validatecallback is for business rules, like "seats must be positive". - Archives are stamped with the server's time and the signed-in user, so archiving a row can't make retention delete it early.
- The compiler refuses rules it can't route exactly; read the message rather than reshaping the rule until it compiles.
The database
- Run the engine as a role that owns clientdb's tables and the tables it syncs, not as a superuser. Superusers bypass row-level security.
- When a database constraint refuses a write, clients get
ConflictorValidationwith a plain message; the details are logged on the server.
Limits
clientdb doesn't rate-limit. Put limits in your proxy or platform, per IP and per signed-in user, on:
POST …/pushand the WebSocket's transactions (writes);POST …/query(server queries;maxLimitin the query rules caps the page size);GET …/tokenand new WebSocket connections.
Lower bodyLimit (16 MiB by default) to what your largest transaction needs.
Devices
- Local data isn't encrypted: IndexedDB in browsers, SQLite in the Swift client, protected by the operating system. Call
await db.$clear()on sign-out so the next person on a shared computer doesn't inherit the data. - Any script running on your pages can read what the viewer reads, local data included. A Content Security Policy that blocks inline and third-party scripts limits what an injected script can do.