Conditions, facts, and fields
Constrain a rule by facts about the subject, resource, and request, and by field paths.
A rule may carry a where condition and a fields list. Every constraint a rule carries must match
for the rule to match.
Facts
Conditions read facts: dot-separated paths under one of three roots.
| Root | Reads | Reserved |
|---|---|---|
subject | The bound subject’s attributes, plus type and id | subject.type, subject.id |
resource | The trusted resource’s attributes, plus type and id | resource.type, resource.id |
context | The binding’s context | none |
A path has 1 to 16 segments after the root, each a policy name:
subject.department, resource.metadata.visibility, context.ipCountry. Lookups read own properties
of plain objects only. A path that does not resolve is a missing fact.
definePolicy() accepts any well-formed path, so a mistyped one such as resource.stauts is not an
error: the fact is simply always missing. To catch that before deployment, declare your facts in a
fact schema and check the policy with the optional rolebook/authoring entry
point. It also reports operators applied to facts of the wrong type. It never changes a decision.
Conditions
import type { PolicyCondition } from "rolebook";
const ownerOfDraft: PolicyCondition = {
all: [
{ fact: "resource.ownerId", operator: "equals", value: { fact: "subject.id" } },
{ fact: "resource.status", operator: "oneOf", value: ["draft", "review"] },
{ not: { fact: "subject.suspended", operator: "equals", value: true } },
],
}; A condition is one of:
- a comparison
{ fact, operator, value }, wherevalueis a JSON literal or a fact reference. A fact reference is exactly{ fact: "subject.<path>" },{ fact: "resource.<path>" }, or{ fact: "context.<path>" }. Any other value that contains an object with afactkey, at any depth, fails compilation, so a mistyped reference such as{ fact: "subjct.id" }is a policy error and is never compared as a literal; { fact, operator: "exists", value? }, wherevalueis an optional boolean,trueby default;{ all: [...] },{ any: [...] }, or{ none: [...] }with a non-empty list of conditions;{ not: condition }.
A whole condition has at most 256 nodes and is at most 16 levels deep. Malformed conditions fail
definePolicy().
Operators
| Operator | Matches when | Operand rules |
|---|---|---|
equals | The fact deeply equals the value. Arrays compare in order, objects by keys. | Any JSON value. |
notEquals | The fact does not deeply equal the value. | Any JSON value. |
oneOf | The fact equals one of the listed values. | A non-empty array. |
noneOf | The fact equals none of the listed values. | A non-empty array. |
contains | An array fact has an element equal to the value, or a string fact contains the string value. | For strings, a non-empty needle. |
containsAll | An array fact contains every listed value. | A non-empty array. |
sizeEquals | An array or string fact has exactly this length. | A non-negative integer. |
startsWith, endsWith | A string fact starts or ends with the value. | A non-empty string. |
lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual | Both sides are finite numbers, or both are strings, and the comparison holds. | A number or a string. |
exists | The fact is present (value: true, the default) or absent (value: false). | An optional boolean. |
Literal operands are checked when the policy is defined. A fact-reference operand is checked when it
resolves: a dynamic oneOf, noneOf, or containsAll operand that is not a non-empty array, or a
comparison between a number and a string, makes the comparison unknown rather than false. Strings
compare by UTF-16 code units, as JavaScript’s < does.
Missing facts and unknown results
Conditions are three-valued: true, false, or unknown. A comparison is unknown when its fact or its
fact-reference operand is missing, or when the values have the wrong shape for the operator. all,
any, none, and not combine results the way SQL’s three-valued logic does: all is false if any
child is false, otherwise unknown if any child is unknown; not of unknown is unknown.
The rule’s effect decides what unknown means:
- An allow matches only when its condition is true. A missing fact never grants access.
- A deny matches when its condition is true or unknown. A conditional deny fires when Rolebook
cannot tell, so a missing
subject.suspendedblocks asuspended-conditioned deny.
exists is the one operator that tests presence itself, so it is never unknown: exists: false is a
known match when the fact is absent.
import { createAuthorizer, definePolicy } from "rolebook";
const guarded = definePolicy({
resources: policy.resources,
roles: {
editor: {
scope: "workspace",
rules: [
{ effect: "allow", permission: "project:update" },
{
effect: "deny",
permission: "project:update",
where: { fact: "subject.suspended", operator: "equals", value: true },
},
],
},
},
});
const check = (attributes: Record<string, unknown>) =>
createAuthorizer(guarded)
.bind({
subject: { type: "user", id: "user_123", attributes },
assignments: [
{
subject: { type: "user", id: "user_123" },
role: "editor",
scope: { resource: { type: "workspace", id: "ws_456" }, appliesTo: "descendants" },
},
],
})
.check({ permission: "project:update", resource: project }).code;
check({ suspended: false }); // "ALLOWED"
check({ suspended: true }); // "EXPLICIT_DENY"
check({}); // "EXPLICIT_DENY": the fact is missing, so the deny's condition is unknown explain(request, { detail: "conditions" }) records the same result as a tree of operators and
fact paths. It does not include the values that were compared. See Decisions.
Fields
A rule may be limited to field paths of the resource with fields, a list of 1 to 64 patterns:
import type { PolicyRule } from "rolebook";
const titleAndMetadata: PolicyRule = {
effect: "allow",
permission: "project:update",
fields: ["title", "metadata.*"],
}; A request names the field it touches with field:
authorization.check({ permission: "project:update", resource: project, field: "metadata.visibility" }); A requested field is a dot-separated path of 1 to 16 policy-name segments. A pattern segment is a
policy name, * (exactly one segment), or ** (zero or more segments). A request without field, or
with field: undefined, is a resource-wide request.
Allows and denies apply field patterns differently, so that neither can widen access:
- A field-restricted allow grants only a requested field that one of its patterns matches exactly.
It never grants a resource-wide request.
metadata.*grantsmetadata.visibility, but notmetadataand notmetadata.visibility.level. - A field-restricted deny matches every request that could expose a path one of its patterns
matches: a resource-wide request, and a requested field that equals, is an ancestor of, or is a
descendant of such a path. A deny on
metadata.secretmatchesmetadata,metadata.secret, andmetadata.secret.value, but nottitleormetadata.public. A deny on*.secretmatches every single-segment field, since each may hold asecretchild, and a deny pattern that starts with**matches every field.
| Rule | Request | Result |
|---|---|---|
allow title, metadata.* | no field | not granted |
allow title, metadata.* | title | allowed |
allow title, metadata.* | metadata | not granted |
allow title, metadata.* | metadata.visibility | allowed |
allow project:read, deny metadata.secret | no field | denied |
allow project:read, deny metadata.secret | metadata | denied |
allow project:read, deny metadata.secret | metadata.secret.value | denied |
allow project:read, deny metadata.secret | metadata.public | allowed |
A rule without fields applies to every request for its permission, with or without a field.