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:
- Envelope. The binding must be a plain object with only
subject,principals,assignments, andcontext. Otherwise every check isINVALID_REQUEST. - Subject. A malformed subject is
INVALID_SUBJECT. - Assignments. The collection must be a dense array of at most 256 well-formed assignments within
the snapshot budget. Otherwise
INVALID_ASSIGNMENT. - Principals. At most 64 distinct references, none equal to the subject. Otherwise
INVALID_SUBJECT. - 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
- Request. The request must be a plain object with only
permission,resource, andfield; the permission must be a concreteresource:actionstring; afield, when present, must be a valid path. OtherwiseINVALID_REQUEST. - Declared permission. The policy must declare the action on the resource type. Otherwise
INVALID_REQUEST. - 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. - 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. - 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. - Denies. Every applicable deny rule for the permission is evaluated. The first that matches
decides
EXPLICIT_DENY. - Allows. Then every applicable allow rule. The first that matches decides
ALLOWED. - 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.