@ultimat3/policy 31.8.0 → 31.10.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 +1 -0
- package/README.md +36 -0
- package/package.json +2 -2
- package/src/index.ts +2 -0
- package/src/policy.ts +8 -0
- package/src/scope.ts +98 -0
package/CLAUDE.md
CHANGED
|
@@ -180,6 +180,7 @@ reappearing there is a failing test.
|
|
|
180
180
|
| File | Job |
|
|
181
181
|
|---|---|
|
|
182
182
|
| `policy.ts` | `can`/`allow`/`deny`/`and`/`or`/`not` + decision recording, and `policyPermissions` |
|
|
183
|
+
| `scope.ts` | `scoped(policy, scope)` / `policyScope` — the rule's SQL projection, a conjunction in entity's predicate shape restated structurally (same tier). Not tenancy; combinators carry none |
|
|
183
184
|
| `policy-anonymous.ts` | `admitsAnonymous` — the one question a SURFACE asks of a built tree, apart from the file that builds them |
|
|
184
185
|
| `evaluate.ts` | the single entry point; builds the trace, emits the one decision event |
|
|
185
186
|
| `decisions.ts` | the `policy.decision` bus event — built only while subscribed, one call site, never PII. No slot (`X_INSTRUMENT_SLOT_FORBIDDEN`) |
|
package/README.md
CHANGED
|
@@ -116,6 +116,42 @@ arguments, so no predicate is ever consulted and the tree alone decides. `true`
|
|
|
116
116
|
"unguarded" — it says only that a 401 before the handler is wrong; the surface still calls
|
|
117
117
|
`enforce()`. `@ultimat3/action` and `@ultimat3/query` derive `RouteMeta.auth` from it.
|
|
118
118
|
|
|
119
|
+
## Scope — the rule's SQL projection
|
|
120
|
+
|
|
121
|
+
`scoped(policy, scope)` (`As of 2026-11`) attaches `scope(actor) → where` to a policy: the
|
|
122
|
+
conjunction a read hands the database, in the entity chain's predicate shape (`{ column, op,
|
|
123
|
+
value }`), so the visible rows arrive as one page an index can serve — not a JS filter over an
|
|
124
|
+
over-fetch that leaves a viewer with many hidden rows a short page. One definition, two
|
|
125
|
+
projections. The policy decides exactly as before; the JS rule stays the authority.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import type { Actor } from '@ultimat3/core';
|
|
129
|
+
import { can, policyScope, scoped } from '@ultimat3/policy';
|
|
130
|
+
|
|
131
|
+
declare const actor: Actor | null;
|
|
132
|
+
const postRule = can('post:read');
|
|
133
|
+
|
|
134
|
+
export const seePost = scoped(postRule, (actor) =>
|
|
135
|
+
actor === null
|
|
136
|
+
? [{ column: 'audience', op: 'eq', value: 'public' }, { column: 'deletedAt', op: 'is-null' }]
|
|
137
|
+
: [{ column: 'orgId', op: 'eq', value: actor.orgId }],
|
|
138
|
+
);
|
|
139
|
+
policyScope(seePost, actor); // the frozen where, or `undefined` for a policy nobody scoped
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
| Answer | Means |
|
|
143
|
+
|---|---|
|
|
144
|
+
| `undefined` | no scope declared: nothing is handed down |
|
|
145
|
+
| `[]` | every row (an admin) — an empty conjunction |
|
|
146
|
+
| `[…]` | every condition must hold. `op` is one of `eq neq in gt gte lt lte is-null is-not-null` |
|
|
147
|
+
|
|
148
|
+
- **A scope belongs to the clause it is declared on.** `and`/`or`/`not` over a scoped policy carry
|
|
149
|
+
none: the conjunction of children's scopes is not the scope of `or`. Scope the outermost policy.
|
|
150
|
+
- **Not tenancy.** Tenant isolation is the entity layer's (`where({ orgId })`, the tenancy guard);
|
|
151
|
+
a scope narrows what one actor sees inside it and never replaces it.
|
|
152
|
+
- Composing it into a read is `@ultimat3/query`'s; a scope that disagrees with its rule on the
|
|
153
|
+
exercise seed is to be a finding (`X_POLICY_SCOPE_MISMATCH`, registered with its raiser).
|
|
154
|
+
|
|
119
155
|
## Four surfaces, four adapters, one rule
|
|
120
156
|
|
|
121
157
|
`surfaces.ts` is the proof. Each adapter evaluates and maps a denial to that surface's
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/policy",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.10.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.10.0"
|
|
46
46
|
}
|
|
47
47
|
}
|
package/src/index.ts
CHANGED
package/src/policy.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { actorHas } from './grant-index';
|
|
|
7
7
|
import { assertPermission, type KnownPermission, type Permission } from './permissions';
|
|
8
8
|
import { markPreInput, type PreInputRun, preInputRun } from './pre-input-brand';
|
|
9
9
|
import type { Actor } from './roles';
|
|
10
|
+
import type { PolicyScope } from './scope';
|
|
10
11
|
|
|
11
12
|
export type PolicyDecision =
|
|
12
13
|
| { readonly allowed: true }
|
|
@@ -65,6 +66,13 @@ export interface Policy<I = unknown, R = unknown> {
|
|
|
65
66
|
readonly permissions: readonly Permission[];
|
|
66
67
|
readonly children: readonly Policy<I, R>[];
|
|
67
68
|
run(args: PolicyArgs<I, R>, record?: Recorder, depth?: number): PolicyDecision;
|
|
69
|
+
/**
|
|
70
|
+
* The rule's SQL projection, attached by `scoped()` (`scope.ts`) and read through `policyScope`.
|
|
71
|
+
* Absent on everything else, combinators included: a read without one hands nothing down. Typed
|
|
72
|
+
* over the row where it is WRITTEN (`scoped`), stored untyped here: a field typed by `R` would
|
|
73
|
+
* make `Policy<I, Post>` unassignable to `Policy<I, Post | null>`, which every surface relies on.
|
|
74
|
+
*/
|
|
75
|
+
readonly scope?: PolicyScope | undefined;
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
const record = (
|
package/src/scope.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// A policy's second projection: `scope(actor) → where`, the predicate a read hands the database so
|
|
2
|
+
// the visible rows arrive as one page, index-served, instead of a JS filter over an over-fetch. The
|
|
3
|
+
// JS rule stays the authority and still runs on every row returned. This file declares the shape and
|
|
4
|
+
// attaches it; composing it into a read is `@ultimat3/query`'s, which may import both sides.
|
|
5
|
+
import type { Policy } from './policy';
|
|
6
|
+
import { markPreInput, preInputRun } from './pre-input-brand';
|
|
7
|
+
import type { Actor } from './roles';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The comparisons a scope may hand down — the entity chain's `andWhere` operators that compare a
|
|
11
|
+
* column to a value. Restated rather than imported: `@ultimat3/entity` is this package's tier, and a
|
|
12
|
+
* sideways import is a boundaries error. Every member is one of entity's `OPERATORS`; the composer
|
|
13
|
+
* in `@ultimat3/query` (tier 3, which sees both) is where that subset is pinned.
|
|
14
|
+
*/
|
|
15
|
+
export type ScopeOperator =
|
|
16
|
+
| 'eq'
|
|
17
|
+
| 'neq'
|
|
18
|
+
| 'in'
|
|
19
|
+
| 'gt'
|
|
20
|
+
| 'gte'
|
|
21
|
+
| 'lt'
|
|
22
|
+
| 'lte'
|
|
23
|
+
| 'is-null'
|
|
24
|
+
| 'is-not-null';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A column of the row the policy decides about; `string` for an untyped row (`keyof unknown` is
|
|
28
|
+
* `never`). `NonNullable`, because a policy's row is often `Post | null` and `keyof` of a union with
|
|
29
|
+
* `null` is `never` too.
|
|
30
|
+
*/
|
|
31
|
+
export type ScopeColumn<R> = unknown extends R ? string : Extract<keyof NonNullable<R>, string>;
|
|
32
|
+
|
|
33
|
+
/** One conjunct: entity's `Predicate` (`{ column, op, value }`), typed over the policy's row. */
|
|
34
|
+
export interface ScopeCondition<R = unknown> {
|
|
35
|
+
readonly column: ScopeColumn<R>;
|
|
36
|
+
readonly op: ScopeOperator;
|
|
37
|
+
/** Absent for `is-null` / `is-not-null`; an array for `in`. Always bound, never spliced. */
|
|
38
|
+
readonly value?: unknown;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Every condition must hold — a conjunction, because the chain's `where` is one. `[]` is "every
|
|
43
|
+
* row" (an admin), which is not the same as a policy with no scope: that one hands NOTHING down and
|
|
44
|
+
* its read stays the JS rule over whatever was read.
|
|
45
|
+
*
|
|
46
|
+
* Called only for an actor the policy's own gate admitted: a caller refused before the read never
|
|
47
|
+
* reaches it. Pure and synchronous, like a predicate — it runs once per read, not once per row.
|
|
48
|
+
*/
|
|
49
|
+
export type PolicyScope<R = unknown> = (actor: Actor | null) => readonly ScopeCondition<R>[];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* `policy`, deciding exactly as before, carrying `scope` beside it: one definition, two projections
|
|
53
|
+
* (axiom 2). The scope must admit what the rule admits; a scope that disagrees with its rule on the
|
|
54
|
+
* exercise seed is a finding, never a silent short page.
|
|
55
|
+
*
|
|
56
|
+
* The scope belongs to the clause it is declared on. `and`, `or` and `not` build a new policy and
|
|
57
|
+
* carry no scope of a child's: a conjunction of children's scopes is not the scope of `or`, and a
|
|
58
|
+
* guess there would hide rows the rule admits. Scope the outermost policy, over the whole rule.
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* export const seePost = scoped(postRule, (actor) =>
|
|
62
|
+
* actor === null
|
|
63
|
+
* ? [{ column: 'audience', op: 'eq', value: 'public' }]
|
|
64
|
+
* : [{ column: 'orgId', op: 'eq', value: actor.orgId }],
|
|
65
|
+
* );
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export const scoped = <I = unknown, R = unknown>(
|
|
69
|
+
policy: Policy<I, R>,
|
|
70
|
+
scope: PolicyScope<R>,
|
|
71
|
+
): Policy<I, R> => {
|
|
72
|
+
const carrying: Policy<I, R> = {
|
|
73
|
+
kind: policy.kind,
|
|
74
|
+
label: policy.label,
|
|
75
|
+
permissions: policy.permissions,
|
|
76
|
+
children: policy.children,
|
|
77
|
+
run: (args, recorder, depth) => policy.run(args, recorder, depth),
|
|
78
|
+
// Checked against `R` where it is written; stored as the untyped shape `Policy` holds, which
|
|
79
|
+
// a generic `R` cannot be proven to fit (`ScopeColumn<R>` is deferred until `R` is known).
|
|
80
|
+
scope: scope as PolicyScope,
|
|
81
|
+
};
|
|
82
|
+
// The before-input gate answers for the scoped policy exactly as for the one it wraps.
|
|
83
|
+
return markPreInput(carrying, (args, recorder, depth) =>
|
|
84
|
+
preInputRun(policy, args, recorder, depth),
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The where a read composes for `actor`, frozen, or `undefined` when the policy declares no scope.
|
|
90
|
+
* What `@ultimat3/query` calls — never `policy.scope` directly, so the freeze cannot be skipped.
|
|
91
|
+
*/
|
|
92
|
+
export const policyScope = <I = unknown, R = unknown>(
|
|
93
|
+
policy: Policy<I, R>,
|
|
94
|
+
actor: Actor | null,
|
|
95
|
+
): readonly ScopeCondition[] | undefined => {
|
|
96
|
+
if (policy.scope === undefined) return undefined;
|
|
97
|
+
return Object.freeze(policy.scope(actor).map((condition) => Object.freeze({ ...condition })));
|
|
98
|
+
};
|