Trusted resources and ancestry
Resource snapshots with ancestry, and what they do and do not prove.
Every check targets a TrustedResource: an immutable snapshot of one resource, its ordered ancestry,
and the attributes conditions may read.
import { trustedResource } from "rolebook";
const project = trustedResource(
{ type: "project", id: "project_1" },
[
{ type: "workspace", id: "ws_456" }, // immediate parent first
{ type: "organization", id: "org_1" }, // root last
],
{ ownerId: "user_123", status: "draft" },
); trustedResource(ref, ancestors = [], attributes = {}) deep-copies its input into a frozen,
null-prototype snapshot. It throws an Error when the reference, the ancestry, or the attributes are
malformed: a missing or empty ID, an extra key on a reference, more than 16 ancestors, or attribute
values that are not JSON-like (an undefined, a Date, a class instance, a function, a cycle).
Ancestry
ancestors is the complete chain from the immediate parent to the root, in the order the policy
declares parents. At check time Rolebook verifies it against the policy:
ref.typemust be the resource type of the requested permission. Checkingworkspace:readagainst a project isRESOURCE_MISMATCH.- Each ancestor’s type must be the declared parent of the type before it, and the chain must end at a
type with no parent. A project snapshot that stops at its workspace, when workspaces have an
organization parent, is
RESOURCE_MISMATCH. - A snapshot carries exactly
ref,ancestors, andattributes. An extra key isRESOURCE_MISMATCH.
Scoped assignments are matched against this chain. A workspace_editor assignment on ws_456 with
appliesTo: "descendants" applies to project_1 only because ws_456 is in the project’s ancestry.
What a snapshot proves, and what it does not
trustedResource() validates structure, declared ancestry types, bounds, and immutability. It records
no database provenance and no tenant ownership. An ancestor ID of the right type is caller-supplied
data, not evidence that the resource really lives under that ancestor.
Rolebook also accepts any value with the trusted-resource shape at check time: it snapshots and validates the value the same way. The name is a reminder of your responsibility, not a seal. Build every snapshot from authoritative data:
- Load the resource by
(tenantId, resourceId)through a query scoped to the authenticated tenant. - Build the ancestry from the stored parent links, not from IDs in the URL or the request body.
- Copy only the attributes your policy reads, from the row you just loaded.
A structurally valid resource from another tenant is still the wrong input, and Rolebook cannot tell. See multi-tenant setup.
Attributes
Attributes are the facts conditions read under the resource root: resource.ownerId,
resource.status. resource.type and resource.id are reserved and always come from ref; an
attribute cannot replace them.
Keep attributes to what the policy needs. Snapshots are bounded (at most 1,024 values, 128 entries per
array, 64 keys per object, 16 levels deep, 512 characters per string; see limits), and
oversized input throws in trustedResource() or denies at check time. Every attribute key follows the
policy-name grammar: a key such as _id or created at throws here, and a
Date value must be passed as a number or an ISO string (see
keys and values inside snapshots).
Do not spread database rows
Map rows to exactly the attributes you need. A spread can carry undefined for a nullable column,
which is not JSON-like and fails the snapshot, or a value the policy should never see. Omit an
optional column rather than setting it to undefined.