RolebookDocs

Evaluation order and deny precedence

Exactly how Rolebook turns a binding and a request into a decision.

Every check runs the same steps, and stops at the first one that decides. The first failing validation step determines the denial code.

Binding validation

When you call bind() (or a one-shot function), the binding is snapshotted once:

  1. Envelope. The binding must be a plain object with only subject, principals, assignments, and context. Otherwise every check is INVALID_REQUEST.
  2. Subject. A malformed subject is INVALID_SUBJECT.
  3. Assignments. The collection must be a dense array of at most 256 well-formed assignments within the snapshot budget. Otherwise INVALID_ASSIGNMENT.
  4. Principals. At most 64 distinct references, none equal to the subject. Otherwise INVALID_SUBJECT.
  5. Context. A plain object of JSON-like values, or omitted. Otherwise INVALID_REQUEST.

A binding that fails here returns that same denial for every check made on it.

Per-check steps

  1. Request. The request must be a plain object with only permission, resource, and field; the permission must be a concrete resource:action string; a field, when present, must be a valid path. Otherwise INVALID_REQUEST.
  2. Declared permission. The policy must declare the action on the resource type. Otherwise INVALID_REQUEST.
  3. Resource. The resource must be a well-formed snapshot of the permission’s resource type, with an ancestry that follows the declared parents to the root. Otherwise RESOURCE_MISMATCH.
  4. Roles. Every assignment must name a known role (UNKNOWN_ROLE) whose scope matches the assignment’s scope resource type (INVALID_ASSIGNMENT). This is checked for the whole collection, not only for applicable assignments.
  5. Applicable rules. Rolebook collects the effective rules (own and inherited) of every assignment whose subject is the bound subject or one of its principals and whose scope covers the resource: the scope resource is the checked resource itself, or, for descendants, appears in its ancestry. Assignments to the subject are collected before group assignments, and a rule held both ways is counted once, attributed to the subject.
  6. Denies. Every applicable deny rule for the permission is evaluated. The first that matches decides EXPLICIT_DENY.
  7. Allows. Then every applicable allow rule. The first that matches decides ALLOWED.
  8. Otherwise NOT_GRANTED.

An unexpected exception anywhere in these steps becomes INVALID_REQUEST and is passed to the authorizer’s onInternalError reporter.

When a rule matches

A rule matches a request when all of these hold:

  • Its resource type is the requested type, and its action is the requested action or *.
  • Its field constraint matches (see fields): an allow needs a requested field that a pattern matches exactly; a deny matches any request that could expose a denied path, including a resource-wide request.
  • Its condition is true, or, for a deny only, unknown.

Deny precedence

Denies are evaluated before allows across every applicable assignment, whichever role, scope, or principal contributed them. Consequences:

  • A deny in one role cannot be cancelled by an allow in another, even a more specific one. There is no rule ordering, priority, or specificity.
  • A deny reached through a group principal blocks the subject too.
  • A deny inherited from a base role stays a deny in every role that inherits it.
  • A conditional deny whose fact is missing still denies.

Use deny rules for invariants, and keep allows additive. When you need “allow everything except”, write the allow and a deny for the exception:

import { definePolicy } from "rolebook";

const policy = definePolicy({
  resources: {
    workspace: { actions: ["read"] },
    project: { parent: "workspace", actions: ["read", "update", "delete"] },
  },
  roles: {
    contributor: {
      scope: "workspace",
      rules: [
        { effect: "allow", permission: "project:*" },
        { effect: "deny", permission: "project:delete" },
      ],
    },
  },
});

The sharing package relies on this property when it decides who may hand out a role: a manager can only grant what it holds, and its denies travel with the grant.