@ultimat3/policy 31.55.0 → 31.57.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +8 -1
- package/README.md +34 -1
- package/package.json +2 -2
- package/src/index.ts +3 -0
- package/src/policy.ts +87 -17
- package/src/type-pins.ts +26 -0
package/CLAUDE.md
CHANGED
|
@@ -68,6 +68,13 @@ two differ, and it is why a surface that decides on input alone needs no edit.
|
|
|
68
68
|
say it in a type (a config-driven route table, a policy resolved by name). Never default
|
|
69
69
|
to allow.
|
|
70
70
|
- `can()` validates its permission at declaration time, not at request time.
|
|
71
|
+
- **`canActor()` is `can()`'s clause with a narrower predicate, never a second clause kind**
|
|
72
|
+
(`As of 2026-11`). Both build through `permissionClause` in `policy.ts`: one kind
|
|
73
|
+
(`'permission'`), label, permission check and recording, so introspection and `admitsAnonymous`
|
|
74
|
+
cannot tell them apart. The predicate gets `ActorPolicyArgs` — a fresh `{ actor, ctx }`, on BOTH
|
|
75
|
+
paths, so a cast cannot reach the input after the parse and make the before-input answer wrong.
|
|
76
|
+
A throw before input is undecided (the evaluation re-runs it and throws there), never a throw
|
|
77
|
+
the evaluation would not reach.
|
|
71
78
|
- **Only `true` or a real decision allows** (`isDecision`, `policy.ts`). A predicate or a
|
|
72
79
|
`definePolicy` `check` returning anything else — `undefined`, `1`, `{ allowed: 'yes' }` — DENIES;
|
|
73
80
|
it used to reach `decision.allowed` as a bare `TypeError`, and a truthy `allowed` read as ALLOWED.
|
|
@@ -179,7 +186,7 @@ reappearing there is a failing test.
|
|
|
179
186
|
|
|
180
187
|
| File | Job |
|
|
181
188
|
|---|---|
|
|
182
|
-
| `policy.ts` | `can`/`allow`/`deny`/`and`/`or`/`not` + decision recording, and `policyPermissions` |
|
|
189
|
+
| `policy.ts` | `can`/`canActor`/`allow`/`deny`/`and`/`or`/`not` + decision recording, and `policyPermissions` |
|
|
183
190
|
| `scope.ts` | `scoped(policy, scope)` / `policyScope` — the rule's SQL projection: a `ScopeNode` tree (condition, `or`, `and`, `via`) in entity's `PredicateNode` shape restated structurally (same tier), frozen at every depth. Not tenancy; combinators carry none |
|
|
184
191
|
| `policy-anonymous.ts` | `admitsAnonymous` — the one question a SURFACE asks of a built tree, apart from the file that builds them |
|
|
185
192
|
| `evaluate.ts` | the single entry point; builds the trace, emits the one decision event. `evaluateEach` is it once per row — no batch shortcut |
|
package/README.md
CHANGED
|
@@ -87,6 +87,7 @@ missing row to `null`. A surface that has no row passes `{ input, actor, ctx }`
|
|
|
87
87
|
| Builder | Behaviour |
|
|
88
88
|
|---|---|
|
|
89
89
|
| `can(p, predicate?)` | permission first, then the row-level predicate |
|
|
90
|
+
| `canActor(p, predicate)` | `can(p, pred)` whose predicate reads only `{ actor, ctx }`, so it is decided **before the input parse** |
|
|
90
91
|
| `allow()` / `deny(reason)` | terminal; `allow()` is how "public" is said out loud |
|
|
91
92
|
| `and(...)` | first denial wins, its reason is the reason; **no clauses is `X_POLICY_CLAUSE_EMPTY`** |
|
|
92
93
|
| `or(...)` | first allowance wins; otherwise the last denial — except one carrying `X_UNAUTHENTICATED`, which outranks a later one. **No clauses is `X_POLICY_CLAUSE_EMPTY`** |
|
|
@@ -132,6 +133,38 @@ arguments, so no predicate is ever consulted and the tree alone decides. `true`
|
|
|
132
133
|
"unguarded" — it says only that a 401 before the handler is wrong; the surface still calls
|
|
133
134
|
`enforce()`. `@ultimat3/action` and `@ultimat3/query` derive `RouteMeta.auth` from it.
|
|
134
135
|
|
|
136
|
+
## `canActor` — a predicate over the actor alone
|
|
137
|
+
|
|
138
|
+
`canActor(permission, ({ actor, ctx }) => …)` (`As of 2026-11`) is `can(p, pred)` whose predicate
|
|
139
|
+
is TYPED to read only who is asking. Its args are `ActorPolicyArgs`: no `input`, no `row` — reading
|
|
140
|
+
either is a type error, and at run time the object it gets has neither key. `actor` is `Actor`,
|
|
141
|
+
never `null`: the permission check refused a null actor first.
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
import { type Actor } from '@ultimat3/core';
|
|
145
|
+
import { canActor, denied } from '@ultimat3/policy';
|
|
146
|
+
|
|
147
|
+
declare function isSession(actor: Actor): boolean;
|
|
148
|
+
|
|
149
|
+
export const sessionOnly = canActor('org:read', ({ actor }) =>
|
|
150
|
+
isSession(actor) ? true : denied('needs a signed-in session', 'X_APP_SESSION_ONLY'),
|
|
151
|
+
);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
What it buys: `decideBeforeInput` runs the predicate, so its exact `denied(…)` — reason and code —
|
|
155
|
+
is the before-input decision (the 403 before the 400), and a guard can prove "refused whatever the
|
|
156
|
+
input is" by asking it, not by probing inputs. A `can(p, pred)` clause stays undecided before the
|
|
157
|
+
input for an actor holding `p`: its predicate may read the input.
|
|
158
|
+
|
|
159
|
+
| Question | Answer |
|
|
160
|
+
|---|---|
|
|
161
|
+
| Semantics once it passes | identical to a passing `can(p, pred)`: not an unconditional allow, every other clause still applies. `and`/`or`/`not` combine it as they combine `can()`; only the time of decision moves |
|
|
162
|
+
| Null actor | `X_UNAUTHENTICATED`, before the predicate — `admitsAnonymous` answers `false`, as for `can()` |
|
|
163
|
+
| Introspection | `kind: 'permission'`, `label` and `permissions` the permission's, exactly `can()`'s: manifests, `x policy list`, the `policies_list` tool and grant reports read it as the permission clause it is. No new `PolicyKind`, so no exhaustive switch over kinds breaks |
|
|
164
|
+
| Async | not allowed — a predicate is synchronous; a returned Promise is neither `true` nor a decision and denies |
|
|
165
|
+
| A throwing predicate | before input: undecided, so the full evaluation runs it again and its throw propagates there, only if that evaluation reaches the clause. Never a throw before the parse that the evaluation after it would not raise |
|
|
166
|
+
| Rows, scopes, `evaluateEach` | ignores the row: the same answer for every row of a list. `scoped(canActor(…), scope)` projects only the scope |
|
|
167
|
+
|
|
135
168
|
## Scope — the rule's SQL projection
|
|
136
169
|
|
|
137
170
|
`scoped(policy, scope)` (`As of 2026-11`) attaches `scope(actor) → where` to a policy: the
|
|
@@ -254,7 +287,7 @@ if (refused !== undefined) console.log(refused.code, refused.reason);
|
|
|
254
287
|
| Field | Meaning |
|
|
255
288
|
|---|---|
|
|
256
289
|
| `kind` | `'action'` or `'query'` — picks the surface table |
|
|
257
|
-
| `subject.stage` | `'actor'`: before the input parse, only `can()`/`allow()`/`deny()` decide, undecided passes · `'input'`: the full evaluation, with `input` and the loaded `row` |
|
|
290
|
+
| `subject.stage` | `'actor'`: before the input parse, only `can()`/`canActor()`/`allow()`/`deny()` decide, undecided passes · `'input'`: the full evaluation, with `input` and the loaded `row` |
|
|
258
291
|
| `surface` | where the primitive ran: action `server · http · mcp · job`, query `server · http · mcp · live` |
|
|
259
292
|
| returns | `undefined` to proceed, or `{ kind, name, surface, code, reason, denial }` |
|
|
260
293
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/policy",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.57.0",
|
|
4
4
|
"description": "The one authz rule, evaluated identically in every surface",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -42,6 +42,6 @@
|
|
|
42
42
|
"test": "bun test"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@ultimat3/core": "31.
|
|
45
|
+
"@ultimat3/core": "31.57.0"
|
|
46
46
|
}
|
|
47
47
|
}
|
package/src/index.ts
CHANGED
|
@@ -44,6 +44,8 @@ export {
|
|
|
44
44
|
restorePermissions,
|
|
45
45
|
} from './permissions';
|
|
46
46
|
export type {
|
|
47
|
+
ActorPolicyArgs,
|
|
48
|
+
ActorPredicate,
|
|
47
49
|
Policy,
|
|
48
50
|
PolicyArgs,
|
|
49
51
|
PolicyDecision,
|
|
@@ -57,6 +59,7 @@ export {
|
|
|
57
59
|
allow,
|
|
58
60
|
and,
|
|
59
61
|
can,
|
|
62
|
+
canActor,
|
|
60
63
|
denied,
|
|
61
64
|
deny,
|
|
62
65
|
not,
|
package/src/policy.ts
CHANGED
|
@@ -50,6 +50,18 @@ export interface PolicyArgs<I = unknown, R = unknown> {
|
|
|
50
50
|
|
|
51
51
|
export type PolicyPredicate<I, R = unknown> = (args: PolicyArgs<I, R>) => boolean | PolicyDecision;
|
|
52
52
|
|
|
53
|
+
/**
|
|
54
|
+
* What a `canActor()` predicate sees: `PolicyArgs` narrowed to who is asking. No `input`, no
|
|
55
|
+
* `row` — reading either is a type error, and at run time the object handed over has neither key.
|
|
56
|
+
* `actor` is never `null`: the permission check refused a null actor before the predicate ran.
|
|
57
|
+
*/
|
|
58
|
+
export interface ActorPolicyArgs {
|
|
59
|
+
readonly actor: Actor;
|
|
60
|
+
readonly ctx?: Ctx;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type ActorPredicate = (args: ActorPolicyArgs) => boolean | PolicyDecision;
|
|
64
|
+
|
|
53
65
|
export type PolicyKind = 'permission' | 'allow' | 'deny' | 'and' | 'or' | 'not';
|
|
54
66
|
|
|
55
67
|
export interface TraceEntry {
|
|
@@ -126,14 +138,28 @@ const asDecision = (result: unknown, label: string): PolicyDecision => {
|
|
|
126
138
|
);
|
|
127
139
|
};
|
|
128
140
|
|
|
141
|
+
/** Which predicate a permission clause carries — and so what it may read. */
|
|
142
|
+
type ClausePredicate<I, R> =
|
|
143
|
+
| { readonly reads: 'input'; readonly predicate: PolicyPredicate<I, R> }
|
|
144
|
+
| { readonly reads: 'actor'; readonly predicate: ActorPredicate };
|
|
145
|
+
|
|
146
|
+
/** The permission check's answer: a refusal, or the actor it let through — never null. */
|
|
147
|
+
type Gated =
|
|
148
|
+
| { readonly refused: PolicyDecision; readonly actor?: never }
|
|
149
|
+
| { readonly refused?: never; readonly actor: Actor };
|
|
150
|
+
|
|
151
|
+
/** Built field by field: the object an actor-only predicate gets has no `input` or `row` key. */
|
|
152
|
+
const actorArgs = (actor: Actor, ctx: Ctx | undefined): ActorPolicyArgs =>
|
|
153
|
+
ctx === undefined ? { actor } : { actor, ctx };
|
|
154
|
+
|
|
129
155
|
/**
|
|
130
|
-
* The
|
|
131
|
-
*
|
|
132
|
-
*
|
|
156
|
+
* The one permission clause `can()` and `canActor()` both build: the same kind, label, permission
|
|
157
|
+
* check and recording, so a reader of the tree cannot tell them apart and the combinators treat
|
|
158
|
+
* them identically. Only the predicate's reach differs, and with it when the clause is decided.
|
|
133
159
|
*/
|
|
134
|
-
|
|
160
|
+
const permissionClause = <I, R>(
|
|
135
161
|
permission: KnownPermission,
|
|
136
|
-
|
|
162
|
+
clause: ClausePredicate<I, R> | undefined,
|
|
137
163
|
): Policy<I, R> => {
|
|
138
164
|
assertPermission(permission);
|
|
139
165
|
const label = permission;
|
|
@@ -151,11 +177,12 @@ export const can = <I = unknown, R = unknown>(
|
|
|
151
177
|
// The actor half of the rule, on its own: it reads nothing the caller sent, which is what lets
|
|
152
178
|
// the before-input gate (`pre-input.ts`) refuse a caller who may never call this before their
|
|
153
179
|
// payload is parsed — and a 403 then says nothing about the input schema.
|
|
154
|
-
const gate = (actor: Actor | null):
|
|
180
|
+
const gate = (actor: Actor | null): Gated => {
|
|
155
181
|
vet();
|
|
156
|
-
if (actor === null) return denied(`no actor for ${label}`, 'X_UNAUTHENTICATED');
|
|
157
|
-
if (!actorHas(actor, permission as Permission))
|
|
158
|
-
|
|
182
|
+
if (actor === null) return { refused: denied(`no actor for ${label}`, 'X_UNAUTHENTICATED') };
|
|
183
|
+
if (!actorHas(actor, permission as Permission))
|
|
184
|
+
return { refused: denied(`actor lacks ${label}`) };
|
|
185
|
+
return { actor };
|
|
159
186
|
};
|
|
160
187
|
const policy: Policy<I, R> = {
|
|
161
188
|
kind: 'permission',
|
|
@@ -163,20 +190,63 @@ export const can = <I = unknown, R = unknown>(
|
|
|
163
190
|
permissions: [permission as Permission],
|
|
164
191
|
children: [],
|
|
165
192
|
run(args, recorder, depth = 0) {
|
|
166
|
-
const
|
|
167
|
-
if (refused !== undefined) return record(recorder, this, depth, refused);
|
|
168
|
-
if (
|
|
169
|
-
|
|
193
|
+
const gated = gate(args.actor);
|
|
194
|
+
if (gated.refused !== undefined) return record(recorder, this, depth, gated.refused);
|
|
195
|
+
if (clause === undefined) return record(recorder, this, depth, ALLOWED);
|
|
196
|
+
const result =
|
|
197
|
+
clause.reads === 'input'
|
|
198
|
+
? clause.predicate(args)
|
|
199
|
+
: clause.predicate(actorArgs(gated.actor, args.ctx));
|
|
200
|
+
return record(recorder, this, depth, asDecision(result, label));
|
|
170
201
|
},
|
|
171
202
|
};
|
|
172
203
|
return markPreInput(policy, (args, recorder, depth) => {
|
|
173
|
-
const
|
|
174
|
-
if (refused !== undefined) return record(recorder, policy, depth, refused);
|
|
175
|
-
|
|
176
|
-
|
|
204
|
+
const gated = gate(args.actor);
|
|
205
|
+
if (gated.refused !== undefined) return record(recorder, policy, depth, gated.refused);
|
|
206
|
+
if (clause === undefined) return record(recorder, policy, depth, ALLOWED);
|
|
207
|
+
// A `can()` predicate reads the input (or the row), so the clause is undecided until then.
|
|
208
|
+
if (clause.reads === 'input') return undefined;
|
|
209
|
+
let decision: PolicyDecision;
|
|
210
|
+
try {
|
|
211
|
+
// Read into a PLAIN decision here: a returned object's `allowed`, `reason` or `code` may be a
|
|
212
|
+
// getter, and one that throws must land in this catch, not in `record()` below it.
|
|
213
|
+
const read = asDecision(clause.predicate(actorArgs(gated.actor, args.ctx)), label);
|
|
214
|
+
decision = read.allowed ? ALLOWED : denied(read.reason, read.code);
|
|
215
|
+
} catch {
|
|
216
|
+
// Not swallowed: undecided, so the full evaluation runs the predicate again and its throw
|
|
217
|
+
// propagates THERE — only if that evaluation reaches this clause. Throwing here would turn
|
|
218
|
+
// `and(denies, canActor(throws))`, a 403 after the parse, into a 500 before it.
|
|
219
|
+
return undefined;
|
|
220
|
+
}
|
|
221
|
+
return record(recorder, policy, depth, decision);
|
|
177
222
|
});
|
|
178
223
|
};
|
|
179
224
|
|
|
225
|
+
/**
|
|
226
|
+
* The blessed constructor. The permission is checked first and the optional
|
|
227
|
+
* predicate second, so a denial reason distinguishes "you may never do this" from
|
|
228
|
+
* "you may, but not to this row" — an agent can act on the difference.
|
|
229
|
+
*/
|
|
230
|
+
export const can = <I = unknown, R = unknown>(
|
|
231
|
+
permission: KnownPermission,
|
|
232
|
+
predicate?: PolicyPredicate<I, R>,
|
|
233
|
+
): Policy<I, R> =>
|
|
234
|
+
permissionClause<I, R>(
|
|
235
|
+
permission,
|
|
236
|
+
predicate === undefined ? undefined : { reads: 'input', predicate },
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* `can()` whose predicate reads only the actor (and ctx) — declared in its type, so the
|
|
241
|
+
* before-input gate (`decideBeforeInput`) runs it and its exact `denied(…)`, code and reason, is
|
|
242
|
+
* the answer before the payload is parsed. Same kind, label, permission check and combinator
|
|
243
|
+
* semantics as `can(p, pred)`; only the time of decision moves. Synchronous, like every predicate.
|
|
244
|
+
*/
|
|
245
|
+
export const canActor = <I = unknown, R = unknown>(
|
|
246
|
+
permission: KnownPermission,
|
|
247
|
+
predicate: ActorPredicate,
|
|
248
|
+
): Policy<I, R> => permissionClause<I, R>(permission, { reads: 'actor', predicate });
|
|
249
|
+
|
|
180
250
|
/** Explicitly public. Saying so is required; forgetting a policy is a build error. */
|
|
181
251
|
export const allow = <I = unknown, R = unknown>(label = 'allow'): Policy<I, R> => {
|
|
182
252
|
const policy: Policy<I, R> = {
|
package/src/type-pins.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Compile-time pins for what `canActor()`'s predicate may read. Source, not a `.test.ts`, on
|
|
2
|
+
// purpose: `tsconfig.json` excludes `src/**/*.test.ts`, so `tsc -b` never reads a test file and a
|
|
3
|
+
// claim written there can never fail. Nothing here emits or is imported — a regression is a build
|
|
4
|
+
// error in the `typecheck` step.
|
|
5
|
+
|
|
6
|
+
import type { ActorPolicyArgs, ActorPredicate } from './policy';
|
|
7
|
+
import type { Actor } from './roles';
|
|
8
|
+
|
|
9
|
+
/** Fails to compile when `T` is anything but `true`. The whole mechanism. */
|
|
10
|
+
type Assert<T extends true> = T;
|
|
11
|
+
|
|
12
|
+
type Equals<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* An actor-only predicate reads `actor` and `ctx`, and nothing else: no `input`, no `row`. That is
|
|
16
|
+
* what lets `decideBeforeInput` run it before the parse and answer for every input.
|
|
17
|
+
*/
|
|
18
|
+
export type _ActorArgsReadOnlyActorAndCtx = Assert<Equals<keyof ActorPolicyArgs, 'actor' | 'ctx'>>;
|
|
19
|
+
|
|
20
|
+
/** Never `null`: the permission check refused a null actor before the predicate ran. */
|
|
21
|
+
export type _ActorArgsActorIsNeverNull = Assert<Equals<ActorPolicyArgs['actor'], Actor>>;
|
|
22
|
+
|
|
23
|
+
/** The predicate's one parameter is that shape, so destructuring `input` is a type error too. */
|
|
24
|
+
export type _ActorPredicateTakesActorArgs = Assert<
|
|
25
|
+
Equals<Parameters<ActorPredicate>, [args: ActorPolicyArgs]>
|
|
26
|
+
>;
|