RolebookDocs

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.

RootReadsReserved
subjectThe bound subject’s attributes, plus type and idsubject.type, subject.id
resourceThe trusted resource’s attributes, plus type and idresource.type, resource.id
contextThe binding’s contextnone

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 }, where value is 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 a fact key, 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? }, where value is an optional boolean, true by 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

OperatorMatches whenOperand rules
equalsThe fact deeply equals the value. Arrays compare in order, objects by keys.Any JSON value.
notEqualsThe fact does not deeply equal the value.Any JSON value.
oneOfThe fact equals one of the listed values.A non-empty array.
noneOfThe fact equals none of the listed values.A non-empty array.
containsAn array fact has an element equal to the value, or a string fact contains the string value.For strings, a non-empty needle.
containsAllAn array fact contains every listed value.A non-empty array.
sizeEqualsAn array or string fact has exactly this length.A non-negative integer.
startsWith, endsWithA string fact starts or ends with the value.A non-empty string.
lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqualBoth sides are finite numbers, or both are strings, and the comparison holds.A number or a string.
existsThe 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.suspended blocks a suspended-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.* grants metadata.visibility, but not metadata and not metadata.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.secret matches metadata, metadata.secret, and metadata.secret.value, but not title or metadata.public. A deny on *.secret matches every single-segment field, since each may hold a secret child, and a deny pattern that starts with ** matches every field.
RuleRequestResult
allow title, metadata.*no fieldnot granted
allow title, metadata.*titleallowed
allow title, metadata.*metadatanot granted
allow title, metadata.*metadata.visibilityallowed
allow project:read, deny metadata.secretno fielddenied
allow project:read, deny metadata.secretmetadatadenied
allow project:read, deny metadata.secretmetadata.secret.valuedenied
allow project:read, deny metadata.secretmetadata.publicallowed

A rule without fields applies to every request for its permission, with or without a field.