Policies
Resources and actions, rules, wildcards, versions, and the policy lifecycle.
A policy is plain data: the resource types your product has, the actions each one supports, and the
roles that grant or deny those actions. definePolicy() validates it, canonicalizes it, compiles it,
and deeply freezes it.
import { definePolicy } from "rolebook";
export const policy = definePolicy({
version: "2026-11-01",
resources: {
organization: { actions: ["read", "manage_billing"] },
workspace: { parent: "organization", actions: ["read", "update"] },
project: { parent: "workspace", actions: ["read", "update", "delete", "share"] },
},
roles: {
workspace_viewer: {
scope: "workspace",
rules: [
{ effect: "allow", permission: "workspace:read" },
{ effect: "allow", permission: "project:read" },
],
},
workspace_editor: {
scope: "workspace",
inherits: ["workspace_viewer"],
rules: [{ effect: "allow", permission: "project:update" }],
},
project_admin: {
scope: "project",
rules: [{ effect: "allow", permission: "project:*" }],
},
},
}); A policy has exactly three top-level keys: an optional version, resources, and roles. Any other
key is an error.
Resources and actions
Each entry in resources is a resource type. It declares its concrete actions and at most one
parent type. Parents form the ancestry that scopes and
trusted resources are checked against.
actionsis a non-empty list of distinct action names, at most 128.parentmust name another declared resource type. Parent chains may not loop, and a chain may be at most 16 types above any resource.recursiveis optional and, when present, must be exactlytrue(see below).- A resource definition accepts only
actions,parent, andrecursive.
Recursive resource types
recursive: true lets resources of a type nest under resources of the same type, for example a
folder inside a folder. parent keeps its meaning and never names the type itself: with
folder: { parent: "workspace", actions: ["read"], recursive: true }, a document’s ancestry may be
[folder, folder, workspace, organization]. A run of folders must still continue to the root type;
when the recursive type declares no parent, the chain may end at any folder. Every ancestor of every
type counts against the 16-ancestor limit, and an ancestor that repeats the resource or an earlier
ancestor is refused (RESOURCE_MISMATCH, “ancestor i repeats a resource”). A policy without the
flag is unchanged: same accepted chains, same refusals, same canonical JSON and digest.
Inside a run of same-type ancestors, Rolebook cannot tell that a folder was left out of the chain,
and that folder may carry a deny. Supplying the complete chain, read to the root from authoritative
data, is the caller’s obligation. For that reason @rolebook/sharing refuses a Sharing resource type
that is recursive or sits below a recursive type until it can read chains from a managed hierarchy,
and managed move refuses recursive parent and child types. An engine older than this flag refuses a
policy that uses it, so upgrade every process that compiles the policy before any stored policy
carries the key.
A permission is resource:action, for example project:update. Requests always name one
concrete, declared permission.
Names
Resource types, actions, role names, condition fact segments, field segments, and every key inside
subject attributes, resource attributes, and context are policy names. A policy name starts with a
letter, continues with letters, digits, ., _, or -, and is at most 128 characters. __proto__, prototype, and constructor are refused. Subject and resource IDs are not
policy names: they are opaque, non-empty strings of up to 128 characters, so numeric and UUID-style IDs
work as they are.
Rules
A rule is an allow or a deny for one permission, with optional constraints:
import type { PolicyRule } from "rolebook";
const rules: PolicyRule[] = [
{ effect: "allow", permission: "project:read" },
{ effect: "deny", permission: "project:delete" },
{
effect: "allow",
permission: "project:update",
where: { fact: "resource.status", operator: "equals", value: "draft" },
fields: ["title", "metadata.*"],
},
]; | Key | Meaning |
|---|---|
effect | "allow" or "deny". |
permission | A declared resource:action, or resource:* for every action of that type. |
where | Optional condition over subject, resource, and context facts. |
fields | Optional list of 1 to 64 field patterns. |
Every supplied constraint must match for a rule to match. A rule accepts only these four keys.
Wildcards
A rule may use an action wildcard, project:*, which matches every action project declares. A
request never may: every check names one concrete action, and a wildcard in a request is denied with
INVALID_REQUEST. The typed authorizer rejects it at compile time too:
authorization.check({ permission: "project:update", resource: project });
// @ts-expect-error: requests name one concrete action
authorization.check({ permission: "project:*", resource: project });
// @ts-expect-error: project does not declare archive
authorization.check({ permission: "project:archive", resource: project }); There is no wildcard for resource types and no implication between actions: update does not imply
read. Grant each action explicitly.
Version labels
version is an optional, non-empty label of up to 512 characters that you control. Every decision
echoes it as policyVersion, so an audit record can say which policy produced a result. It does not
change evaluation, and it is excluded from canonicalPolicyJson(),
so two policies that differ only in their label have the same canonical form.
Policy lifecycle
- Define once. Call
definePolicy()at module level and reuse the returned object. It is frozen, and its compiled form and its shared authorizer are cached by object identity, socreateAuthorizer(policy)on the same object is cheap and returns the same authorizer. - Raw objects are revalidated. A plain policy object that did not come from
definePolicy()is snapshotted and validated again every time it is passed in, because it is mutable. - Failures are deployment failures.
definePolicy()andcreateAuthorizer()throw aPolicyErrorwhen the policy is malformed, inconsistent, or over a limit. Its message starts withInvalid Rolebook policy:and names where the problem is, for example(role "member", rule 1). Itscodeis a stable classification such asMALFORMED_RULEorTOO_MANY_RULES, and itslocationholds theresource,role,ruleindex, andconditionpointer that apply. Treat that as a failed configuration rollout, not a request error. - Typed facts are optional checks.
definePolicy()does not know which facts your application supplies, so a mistyped fact path deploys, and every comparison on it is unknown. To fail the rollout on it as well, check the policy against a fact schema withcompilePolicy()fromrolebook/authoring. Core still accepts a policy those checks reject.
Comparing policies
diffPolicies(from, to) describes what changed between two policies: added, removed, and changed
resource types and roles, including the rules a role gains or loses through inheritance.
canonicalPolicyJson(policy) is the deterministic serialization of a policy’s meaning, useful for
digests and change detection.
import { canonicalPolicyJson, definePolicy, diffPolicies } from "rolebook";
const next = definePolicy({
version: "4.1.0",
resources: {
organization: { actions: ["read"] },
workspace: { parent: "organization", actions: ["read", "update"] },
project: { parent: "workspace", actions: ["read", "update", "delete", "archive"] },
},
roles: {
workspace_editor: {
scope: "workspace",
rules: [
{ effect: "allow", permission: "project:read" },
{ effect: "allow", permission: "project:update" },
],
},
},
});
const diff = diffPolicies(policy, next);
// diff.identical === false
// diff.version -> { from: "4.0.0", to: "4.1.0" }
// diff.resources.changed -> [{ resourceType: "project", actionsAdded: ["archive"], actionsRemoved: [] }]
const same = canonicalPolicyJson(policy) === canonicalPolicyJson(next); // false The sharing package builds published policy versions on these two functions.
No bypass role
There is no built-in super administrator and no action-level implication. Elevated access is an ordinary scoped assignment of a role with explicit rules. Every grant stays visible in the policy, and every decision stays explainable.