Skip to content

Write per-record rules for end users

Rules decide which rows each person signed in to your app may see and change. For example: "a to-do is visible only to its owner".

6 min readUpdated December 2026Markdown
On this page

At a glance

  • Rules apply to your app's end usersA person who signs in to an app built on Kovlet, through the app, without a Kovlet account. and to servers acting as one. Members, Machine Auth Keys and the console see every row.
  • Anything without a rule is denied. Each end user sees only their own _users row by default.
  • The server checks the rules on every read and write. Rows a user may not see never reach their device.
  • Rules are one JSON document per Data Space. They take effect only when an owner saves or approves them.
  • You can test them as any end user in the console before you save.

Prerequisites

  • End-user sign-in turned on for your app. See Let strangers sign in to your app (end users).
  • A table that points at its owner, such as todos with owner: t.ref("_users").
  • The owner role on the Data Space, to save rules. An editor (an app, the CLI or a coding agent) can propose rules for an owner to approve.

Step 1: Write the rules document

Rules are one JSON document per Data Space. This one makes each to-do private to the end user who owns it:

kovlet.perms.json
{
  "todos": {
    "bind": ["isOwner", "auth.id == data.owner"],
    "allow": {
      "view": "isOwner",
      "create": "auth.id == newData.owner",
      "update": "isOwner && newData.owner == data.owner",
      "delete": "isOwner"
    }
  }
}
  • Line 3bind names a condition once, so the rules below can reuse it. isOwner is true when the signed-in user is the row's owner.

The table points at its owner with owner: t.ref("_users"). With these rules:

  • useQuery(space, { todos: {} }) returns only the signed-in user's to-dos. Other users' changes never reach their socket.
  • A user can't create a to-do in someone else's name, or change or delete someone else's. The server rejects it with forbidden and details.rule ("todos.create", "todos.view").
  • update pins the owner. newData.owner == data.owner stops a user from giving a to-do away.

The rules reference lists everything a rule can read. That includes links to other tables with data.ref.

Step 2: Test the rules as an end user

Under the rules editor in the console, open *Test as an end user*. Pick one end user by email or ID. It shows:

  • what they see of a table
  • whether they may create, update or delete a row
  • the compiled view filter

You can test the saved rules or your unsaved changes. Nothing is written.

Over REST, owners call POST /v1/spaces/{space}/rules/test with {email | end_user_id, table, action?, record_id?, fields?, document?}.

Step 3: Save or propose the rules

Only an owner can change rules. So an app can never widen its own access. There are three ways to get rules in place:

  • Console: go to Access → End users → Rules. Tick whole tables in *Table*, or edit the document in *Code*. Saving counts as the owner's approval.
  • From code: an app, the CLI or a script with editor access proposes a document with POST /v1/spaces/{space}/rules/proposals {"document": …}. The proposal shows up with the schema proposals. An owner approves it there (POST …/schema/proposals/{id}/approve).
  • From a coding agent: the MCP tool propose_rules ({space, rules}) makes the same proposal. A proposal replaces the whole document. So start from the current rules, which inspect_space_schema returns.

Note A destructive schema change that drops or renames a column a rule reads is refused with rules_invalid. Change the rules first.

Check the result

Sign in to the app as an end user.

You should see

  • Your own to-dos only. Another end user's to-dos never reach your device.
  • Creating a to-do with another user's ID as owner rolls back. onError receives a MutationRejectedError with code: "forbidden" and details.rule: "todos.create".
  • After an owner changes the rules, open live queries refresh on their own. Rows that left your view disappear.

How rules work

Rules work row by row. Each rule is a condition, such as "a post is visible when it's published or the user is in its team".

Rules have the same shape as InstantDB's instant.perms.ts. They are written in the same language, CEL.

Rules bind end users and servers acting as one. Members, Machine Auth Keys and the console keep their space roles and see every row.

Rules are closed by default. An action with no rule is denied. The _users table defaults to "each end user sees their own row".

The server enforces rules everywhere an end user reaches data:

  • sync subscriptions
  • REST reads and queries
  • atomic batches
  • files
  • rooms

Rows a user may not view never reach their device.

Rules are one JSON document per Data Space. Keep it in your repo, as kovlet.perms.json for example. The server applies it only when an owner saves it in the console or approves a proposal from an app.

Next

Ready to try it? Start free. No credit card, no API keys.

For coding agents: this page is also Markdown. Add .md to the URL or send Accept: text/markdown. The full reference is at /llms-full.txt.