Who can change what

Hiding a button in the page is not security: anyone in the workspace can call the routes the SDK uses. openbelt.json rules are: the platform checks every page's read and write against them (this app's pages, other apps', and anything else that calls the app's /_db). A poll only its author changes; one vote per person on a poll that exists, changed or taken back only while the poll is open:

{
  "collections": {
    "polls": "one doc per poll: { title, options, closed }",
    "votes": "one doc per person per poll, id <poll>:<email>: { poll, choice }"
  },
  "rules": {
    "polls": {
      "read": "anyone",
      "create": "signedIn",
      "update": "doc.author == me",
      "delete": "doc.author == me"
    },
    "votes": {
      "read": "anyone",
      "create": "signedIn && id == newDoc.data.poll + ':' + me && get('polls', newDoc.data.poll) != null && get('polls', newDoc.data.poll).data.closed != true",
      "update": "doc.author == me && newDoc.data.poll == doc.data.poll && get('polls', doc.data.poll) != null && get('polls', doc.data.poll).data.closed != true",
      "delete": "doc.author == me && get('polls', doc.data.poll) != null && get('polls', doc.data.poll).data.closed != true"
    }
  }
}
await db.collection("votes").set(poll.id + ":" + me.email, { poll: poll.id, choice }) // set: the rule checks the id
await db.collection("polls").update(poll.id, { closed: true }) // closing is an update: its author only

The vocabulary

More on the vocabulary

Denials

Testing a denial

One test person creates a poll and another, who isn't its author, tries to close it:

{
  "app": "polls",
  "preview": "rules",
  "people": ["alice", "carol"],
  "steps": [
    {
      "as": "alice",
      "action": "eval",
      "save": "pollId",
      "script": "const { db } = await import('/sdk.js'); return (await db.collection('polls').add({ title: 'Lunch?', closed: false })).id"
    },
    {
      "as": "carol",
      "action": "eval",
      "script": "const { db } = await import('/sdk.js'); await db.collection('polls').update('{{pollId}}', { closed: true }).catch(() => {})"
    },
    { "as": "carol", "action": "expect", "status": 403 }
  ]
}

The expect passes when the write was refused (answered 403 (rules.polls.update)) and fails with expected 403 from PATCH /_db/polls/<id>, got 200 when it went through. Write to a poll that exists: an update or delete of one that doesn't is a 404, before any rule.

Deploy also refuses doc in a create rule (use newDoc) and a third distinct get(). Rules take effect with their version: a preview environment uses the rules deployed there, so test a rule change in a preview before promoting it. A read rule can leave a page of a list with fewer than limit documents while cursor isn't null; when a change hides a document from a viewer, their page gets a delete for it, with data null.

MCP writes

An agent gets what its person's page gets, unless that person owns the app or is a workspace admin:

For moving data (merging duplicates, re-keying documents), the owner's or an admin's writes without as may pass author and updatedBy per document: it keeps who wrote it, so bob can still delete the vote you moved for him. They're refused together with as. A batch (docs: [...], up to 500) is one transaction: if any document is refused, none is written and the refusal lists each one.

// bob's vote moves from duplicate poll p1 to p2, still his
collection_update({
  app: "polls",
  collection: "votes",
  docs: [
    {
      id: "p2:bob@acme.com",
      data: { poll: "p2", choice: "Tacos" },
      replace: true,
      ifSeq: 0,
      author: "bob@acme.com",
      updatedBy: "bob@acme.com",
    },
  ],
})

Source: docs/rules.md on GitHub