@zudojs/permissions 1.1.0 → 1.3.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.
Files changed (45) hide show
  1. package/README.md +120 -31
  2. package/dist/ability/ability.core.js +4 -24
  3. package/dist/cache/cache.actorDigest.d.ts +26 -0
  4. package/dist/cache/cache.actorDigest.js +90 -0
  5. package/dist/cache/cache.core.d.ts +7 -1
  6. package/dist/cache/cache.core.js +9 -2
  7. package/dist/cache/index.d.ts +1 -0
  8. package/dist/cache/index.js +1 -0
  9. package/dist/evaluator/authorizationEngine.d.ts +58 -5
  10. package/dist/evaluator/authorizationEngine.js +76 -68
  11. package/dist/evaluator/engineSupport/authorizationEngine.validation.d.ts +37 -0
  12. package/dist/evaluator/engineSupport/authorizationEngine.validation.js +90 -0
  13. package/dist/evaluator/engineSupport/evaluator.cacheKey.d.ts +28 -0
  14. package/dist/evaluator/engineSupport/evaluator.cacheKey.js +70 -0
  15. package/dist/evaluator/engineSupport/evaluator.observed.d.ts +18 -0
  16. package/dist/evaluator/engineSupport/evaluator.observed.js +49 -0
  17. package/dist/evaluator/engineSupport/index.d.ts +10 -0
  18. package/dist/evaluator/engineSupport/index.js +10 -0
  19. package/dist/evaluator/evaluator.core.js +19 -37
  20. package/dist/evaluator/evaluator.pipeline.d.ts +11 -0
  21. package/dist/evaluator/evaluator.pipeline.js +25 -3
  22. package/dist/http/httpMiddleware.core.d.ts +15 -5
  23. package/dist/http/httpMiddleware.core.js +18 -4
  24. package/dist/http/httpResource.helper.d.ts +30 -0
  25. package/dist/http/httpResource.helper.js +32 -0
  26. package/dist/http/httpTypes.d.ts +17 -4
  27. package/dist/http/index.d.ts +7 -5
  28. package/dist/http/index.js +6 -4
  29. package/dist/permission/index.d.ts +1 -1
  30. package/dist/permission/index.js +1 -1
  31. package/dist/permission/permission.core.d.ts +12 -0
  32. package/dist/permission/permission.core.js +20 -1
  33. package/dist/permission/permissionRegistry.d.ts +14 -1
  34. package/dist/permission/permissionRegistry.js +15 -2
  35. package/dist/policy/policyRegistry.d.ts +12 -0
  36. package/dist/policy/policyRegistry.js +17 -1
  37. package/dist/role/roleRegistry.d.ts +9 -0
  38. package/dist/role/roleRegistry.js +19 -1
  39. package/dist/rule/rule.core.d.ts +14 -22
  40. package/dist/rule/rule.core.js +49 -86
  41. package/dist/rule/rule.pattern.d.ts +44 -0
  42. package/dist/rule/rule.pattern.js +72 -0
  43. package/dist/utils/utils.notifier.d.ts +19 -0
  44. package/dist/utils/utils.notifier.js +31 -0
  45. package/package.json +2 -10
@@ -4,7 +4,8 @@
4
4
  * @module evaluator/evaluator.pipeline
5
5
  */
6
6
  import { resolveRolePermissions } from "../role/roleHierarchy.js";
7
- import { matches } from "../permission/permission.core.js";
7
+ import { matches, permissionsOverlap, parsePermissionSafe, } from "../permission/permission.core.js";
8
+ import { isWildcardTarget } from "../rule/rule.pattern.js";
8
9
  import { AuthorizationAbortedError, PermissionResolverError, PolicyError, PolicyTimeoutError, } from "../permissionErrors/index.js";
9
10
  /**
10
11
  * Normalize a thrown value into an `Error` before it reaches `onError`.
@@ -126,6 +127,23 @@ export function selectPolicies(policies, permissionStr) {
126
127
  policy.permissions.some((pattern) => matches(pattern, permissionStr)))
127
128
  .sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
128
129
  }
130
+ /**
131
+ * Policies narrower than a wildcard target, which may deny it but not grant it.
132
+ *
133
+ * `can(actor, "post:*")` asks about every action under `post`. A policy
134
+ * registered for `post:delete` does not cover that question, so its allow
135
+ * must not grant it — but its denial is a denial of part of it, and a
136
+ * wildcard check must not side-step it.
137
+ */
138
+ function narrowerPolicies(policies, applicable, permissionStr) {
139
+ const target = parsePermissionSafe(permissionStr);
140
+ if (!target || !isWildcardTarget(target))
141
+ return [];
142
+ return policies
143
+ .filter((policy) => !applicable.includes(policy) &&
144
+ policy.permissions.some((pattern) => permissionsOverlap(pattern, permissionStr)))
145
+ .sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
146
+ }
129
147
  /**
130
148
  * Evaluate policies for a context.
131
149
  *
@@ -139,7 +157,8 @@ export async function evaluatePolicies(context, policies, options, authOptions)
139
157
  }
140
158
  const permissionStr = `${context.permission.resource}:${context.permission.action}`;
141
159
  const applicable = selectPolicies(policies, permissionStr);
142
- if (applicable.length === 0) {
160
+ const denyOnly = narrowerPolicies(policies, applicable, permissionStr);
161
+ if (applicable.length === 0 && denyOnly.length === 0) {
143
162
  return { decision: null, cacheable: true, evaluated: [] };
144
163
  }
145
164
  // The per-call timeout wins, but the engine-level default is what makes a
@@ -147,7 +166,7 @@ export async function evaluatePolicies(context, policies, options, authOptions)
147
166
  const timeoutMs = authOptions?.policyTimeout ?? options.policyTimeout;
148
167
  const evaluated = [];
149
168
  let cacheable = true;
150
- for (const policy of applicable) {
169
+ for (const policy of [...applicable, ...denyOnly]) {
151
170
  assertNotAborted(context.signal ?? authOptions?.signal);
152
171
  evaluated.push(policy.name);
153
172
  if (policy.cacheable === false)
@@ -184,6 +203,9 @@ export async function evaluatePolicies(context, policies, options, authOptions)
184
203
  };
185
204
  }
186
205
  }
206
+ if (applicable.length === 0) {
207
+ return { decision: null, cacheable, evaluated };
208
+ }
187
209
  return {
188
210
  decision: Object.freeze({
189
211
  allowed: true,
@@ -10,6 +10,7 @@ import type { PermissionActor, AuthorizationOptions } from "../permissionTypes/i
10
10
  import type { PermissionEngine } from "../evaluator/authorizationEngine.js";
11
11
  import type { HttpMiddleware, HttpMiddlewareContext } from "./httpTypes.js";
12
12
  import { type DeniedResponseOptions } from "./httpHelpers.js";
13
+ import { type ResourceExtractor } from "./httpResource.helper.js";
13
14
  /** Options shared by the permission middleware. */
14
15
  export interface AuthorizeMiddlewareOptions extends DeniedResponseOptions {
15
16
  /**
@@ -30,13 +31,18 @@ export interface AuthorizeMiddlewareOptions extends DeniedResponseOptions {
30
31
  readonly forwardSignal?: boolean;
31
32
  /** Builds per-request metadata for conditions and policies. */
32
33
  readonly extractMetadata?: (context: HttpMiddlewareContext) => Record<string, unknown> | undefined;
34
+ /** Reports a failure the guard turned into a denial (a failed resource load). */
35
+ readonly onError?: (error: unknown, source: string) => void;
33
36
  }
34
37
  /** Options for the requirePermission middleware. */
35
38
  export interface RequirePermissionMiddlewareOptions extends AuthorizeMiddlewareOptions {
36
39
  /** The permission to check (e.g. "post:update"). */
37
40
  readonly permission: string;
38
- /** Extracts the resource from the request (optional). */
39
- readonly extractResource?: (context: HttpMiddlewareContext) => unknown;
41
+ /**
42
+ * Loads the resource the permission is checked against (optional). May be
43
+ * async; it is awaited, and a loader that throws or rejects denies (403).
44
+ */
45
+ readonly extractResource?: ResourceExtractor;
40
46
  }
41
47
  /** Options for {@link createActorMiddleware}. */
42
48
  export interface ActorMiddlewareOptions extends AuthorizeMiddlewareOptions {
@@ -72,8 +78,11 @@ export declare function createRequirePermissionMiddleware(engine: PermissionEngi
72
78
  export declare function authorize(engine: PermissionEngine, permission: string, options?: Omit<RequirePermissionMiddlewareOptions, "permission">): HttpMiddleware;
73
79
  /** Options for {@link createRequirePermissionsMiddleware}. */
74
80
  export interface RequirePermissionsMiddlewareOptions extends AuthorizeMiddlewareOptions {
75
- /** Extracts the resource checked for every permission (optional). */
76
- readonly extractResource?: (context: HttpMiddlewareContext) => unknown;
81
+ /**
82
+ * Loads the resource checked for every permission (optional). May be
83
+ * async; it is awaited, and a loader that throws or rejects denies (403).
84
+ */
85
+ readonly extractResource?: ResourceExtractor;
77
86
  /**
78
87
  * `"all"` (default) requires every permission; `"any"` requires one.
79
88
  */
@@ -83,7 +92,8 @@ export interface RequirePermissionsMiddlewareOptions extends AuthorizeMiddleware
83
92
  * Create middleware that checks multiple permissions.
84
93
  *
85
94
  * Under `"all"` the first denial short-circuits: evaluating the rest costs
86
- * policy calls and timeouts for an answer that is already decided.
95
+ * policy calls and timeouts for an answer that is already decided. An empty
96
+ * permission list denies in either mode.
87
97
  */
88
98
  export declare function createRequirePermissionsMiddleware(engine: PermissionEngine, permissions: readonly string[], options?: RequirePermissionsMiddlewareOptions): HttpMiddleware;
89
99
  //# sourceMappingURL=httpMiddleware.core.d.ts.map
@@ -7,6 +7,7 @@
7
7
  * @module http/httpMiddleware
8
8
  */
9
9
  import { createForbiddenResponse, createUnauthorizedResponse, } from "./httpHelpers.js";
10
+ import { loadResource } from "./httpResource.helper.js";
10
11
  // ─── State Keys ───────────────────────────────────────────────────────────
11
12
  /** State key for the current actor. */
12
13
  export const ACTOR_STATE_KEY = "permissions:actor";
@@ -64,8 +65,12 @@ export function createRequirePermissionMiddleware(engine, options) {
64
65
  const actor = await resolveActor(context, options);
65
66
  if (!actor)
66
67
  return createUnauthorizedResponse(options);
67
- const resource = options.extractResource?.(context);
68
- const decision = await engine.check(actor, options.permission, resource, buildAuthorization(context, options));
68
+ const loaded = await loadResource(context, options.extractResource, options.onError);
69
+ if (!loaded.ok) {
70
+ context.state.set(DECISION_STATE_KEY, loaded.decision);
71
+ return createForbiddenResponse(loaded.decision, options);
72
+ }
73
+ const decision = await engine.check(actor, options.permission, loaded.resource, buildAuthorization(context, options));
69
74
  context.state.set(DECISION_STATE_KEY, decision);
70
75
  if (!decision.allowed)
71
76
  return createForbiddenResponse(decision, options);
@@ -85,7 +90,8 @@ export function authorize(engine, permission, options = {}) {
85
90
  * Create middleware that checks multiple permissions.
86
91
  *
87
92
  * Under `"all"` the first denial short-circuits: evaluating the rest costs
88
- * policy calls and timeouts for an answer that is already decided.
93
+ * policy calls and timeouts for an answer that is already decided. An empty
94
+ * permission list denies in either mode.
89
95
  */
90
96
  export function createRequirePermissionsMiddleware(engine, permissions, options = {}) {
91
97
  const mode = options.mode ?? "all";
@@ -93,7 +99,15 @@ export function createRequirePermissionsMiddleware(engine, permissions, options
93
99
  const actor = await resolveActor(context, options);
94
100
  if (!actor)
95
101
  return createUnauthorizedResponse(options);
96
- const resource = options.extractResource?.(context);
102
+ // An empty list is a configuration error, not a grant: under "all" it
103
+ // used to fall through to next() for any authenticated actor.
104
+ if (permissions.length === 0) {
105
+ return createForbiddenResponse({ allowed: false, reason: "no_permissions", publicReason: "Access denied" }, options);
106
+ }
107
+ const loaded = await loadResource(context, options.extractResource, options.onError);
108
+ if (!loaded.ok)
109
+ return createForbiddenResponse(loaded.decision, options);
110
+ const resource = loaded.resource;
97
111
  const authorization = buildAuthorization(context, options);
98
112
  const results = new Map();
99
113
  let lastDenial;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Resource loading for the permission guards.
3
+ *
4
+ * @module http/httpResource.helper
5
+ */
6
+ import type { PermissionDecision } from "../permissionTypes/index.js";
7
+ import type { HttpMiddlewareContext } from "./httpTypes.js";
8
+ /** Loads the resource a permission is checked against. May be async. */
9
+ export type ResourceExtractor = (context: HttpMiddlewareContext) => unknown | Promise<unknown>;
10
+ /** The resource, or the denial to answer with when it could not be loaded. */
11
+ export type ResourceOutcome = {
12
+ readonly ok: true;
13
+ readonly resource: unknown;
14
+ } | {
15
+ readonly ok: false;
16
+ readonly decision: PermissionDecision;
17
+ };
18
+ /** The decision recorded when the resource loader fails. */
19
+ export declare const RESOURCE_ERROR_DECISION: PermissionDecision;
20
+ /**
21
+ * Run the extractor and await it.
22
+ *
23
+ * The guard used to hand the engine whatever the extractor returned — for an
24
+ * async loader, a Promise — so every resource condition saw `undefined`:
25
+ * deny rules on a locked or foreign resource never fired, and a loader that
26
+ * rejected let the request through while its rejection went unhandled. A
27
+ * loader that throws or rejects now denies; the error goes to `onError`.
28
+ */
29
+ export declare function loadResource(context: HttpMiddlewareContext, extract: ResourceExtractor | undefined, onError?: (error: unknown, source: string) => void): Promise<ResourceOutcome>;
30
+ //# sourceMappingURL=httpResource.helper.d.ts.map
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Resource loading for the permission guards.
3
+ *
4
+ * @module http/httpResource.helper
5
+ */
6
+ /** The decision recorded when the resource loader fails. */
7
+ export const RESOURCE_ERROR_DECISION = Object.freeze({
8
+ allowed: false,
9
+ reason: "resource_error",
10
+ publicReason: "Access denied",
11
+ });
12
+ /**
13
+ * Run the extractor and await it.
14
+ *
15
+ * The guard used to hand the engine whatever the extractor returned — for an
16
+ * async loader, a Promise — so every resource condition saw `undefined`:
17
+ * deny rules on a locked or foreign resource never fired, and a loader that
18
+ * rejected let the request through while its rejection went unhandled. A
19
+ * loader that throws or rejects now denies; the error goes to `onError`.
20
+ */
21
+ export async function loadResource(context, extract, onError) {
22
+ if (!extract)
23
+ return { ok: true, resource: undefined };
24
+ try {
25
+ return { ok: true, resource: await extract(context) };
26
+ }
27
+ catch (error) {
28
+ onError?.(error, "extractResource");
29
+ return { ok: false, decision: RESOURCE_ERROR_DECISION };
30
+ }
31
+ }
32
+ //# sourceMappingURL=httpResource.helper.js.map
@@ -16,20 +16,33 @@ export interface HttpMiddlewareContext {
16
16
  readonly signal: AbortSignal;
17
17
  readonly metadata: Readonly<Record<string, unknown>>;
18
18
  }
19
+ /**
20
+ * A request's headers, params or query as either shape a caller may hold.
21
+ *
22
+ * The real `@zudojs/http` request exposes plain frozen objects
23
+ * (`Readonly<Record<…>>`); this mirror used to say `ReadonlyMap`, so code
24
+ * written against it called `.get()` on an object that has none. Both shapes
25
+ * are accepted, and the accessor methods below are the portable way to read.
26
+ */
27
+ export type HttpRequestBag<V> = ReadonlyMap<string, V> | Readonly<Record<string, V | undefined>>;
19
28
  /** HTTP request context from @zudojs/http. */
20
29
  export interface HttpRequestContext {
21
30
  readonly id: string;
22
31
  readonly method: string;
23
32
  readonly url: string;
24
33
  readonly path: string;
25
- readonly headers: ReadonlyMap<string, string>;
26
- readonly params: ReadonlyMap<string, string>;
27
- readonly query: ReadonlyMap<string, string | readonly string[] | undefined>;
34
+ readonly headers: HttpRequestBag<string>;
35
+ readonly params: HttpRequestBag<string>;
36
+ readonly query: HttpRequestBag<string | readonly string[]>;
37
+ /** Case-insensitive header lookup, as `@zudojs/http` provides it. */
38
+ getHeader?(name: string): string | undefined;
39
+ /** Route parameter lookup, as `@zudojs/http` provides it. */
40
+ getParam?(name: string): string | undefined;
28
41
  }
29
42
  /** HTTP response context from @zudojs/http. */
30
43
  export interface HttpResponseContext {
31
44
  readonly status: number;
32
- readonly headers: Headers | Record<string, string>;
45
+ readonly headers: Headers | Readonly<Record<string, string | readonly string[] | undefined>>;
33
46
  readonly body?: unknown;
34
47
  }
35
48
  /** HTTP middleware state from @zudojs/http. */
@@ -2,14 +2,16 @@
2
2
  * HTTP middleware adapter for @zudojs/permissions.
3
3
  *
4
4
  * The HTTP types are mirrored locally in `httpTypes.ts` so this package has no
5
- * hard dependency on @zudojs/http, which is an optional peer. The mirror is
6
- * structural: anything satisfying the real `HttpMiddlewareContext` satisfies
7
- * the local one, so the middleware composes with the real pipeline. Keep the
8
- * two in step when @zudojs/http changes — nothing here can check it for you.
5
+ * dependency on @zudojs/http at all — http sits in a higher architecture tier,
6
+ * so it cannot be a dependency or a peer. The mirror is structural: anything
7
+ * satisfying the real `HttpMiddlewareContext` satisfies the local one, so the
8
+ * middleware composes with the real pipeline. A test runs the guard inside
9
+ * the real `HttpMiddlewarePipeline` to keep the two in step.
9
10
  *
10
11
  * @module http
11
12
  */
12
13
  export { createActorMiddleware, createRequirePermissionMiddleware, authorize, createRequirePermissionsMiddleware, ACTOR_STATE_KEY, DECISION_STATE_KEY, DECISIONS_STATE_KEY, type AuthorizeMiddlewareOptions, type ActorMiddlewareOptions, type RequirePermissionMiddlewareOptions, type RequirePermissionsMiddlewareOptions, } from "./httpMiddleware.core.js";
13
14
  export { createForbiddenResponse, createUnauthorizedResponse, createJsonResponse, type DeniedResponseOptions, type PermissionHttpResponse, } from "./httpHelpers.js";
14
- export type { HttpMiddleware, HttpMiddlewareContext, HttpRequestContext, HttpResponseContext, HttpMiddlewareState, } from "./httpTypes.js";
15
+ export { loadResource, RESOURCE_ERROR_DECISION, type ResourceExtractor, type ResourceOutcome, } from "./httpResource.helper.js";
16
+ export type { HttpRequestBag, HttpMiddleware, HttpMiddlewareContext, HttpRequestContext, HttpResponseContext, HttpMiddlewareState, } from "./httpTypes.js";
15
17
  //# sourceMappingURL=index.d.ts.map
@@ -2,13 +2,15 @@
2
2
  * HTTP middleware adapter for @zudojs/permissions.
3
3
  *
4
4
  * The HTTP types are mirrored locally in `httpTypes.ts` so this package has no
5
- * hard dependency on @zudojs/http, which is an optional peer. The mirror is
6
- * structural: anything satisfying the real `HttpMiddlewareContext` satisfies
7
- * the local one, so the middleware composes with the real pipeline. Keep the
8
- * two in step when @zudojs/http changes — nothing here can check it for you.
5
+ * dependency on @zudojs/http at all — http sits in a higher architecture tier,
6
+ * so it cannot be a dependency or a peer. The mirror is structural: anything
7
+ * satisfying the real `HttpMiddlewareContext` satisfies the local one, so the
8
+ * middleware composes with the real pipeline. A test runs the guard inside
9
+ * the real `HttpMiddlewarePipeline` to keep the two in step.
9
10
  *
10
11
  * @module http
11
12
  */
12
13
  export { createActorMiddleware, createRequirePermissionMiddleware, authorize, createRequirePermissionsMiddleware, ACTOR_STATE_KEY, DECISION_STATE_KEY, DECISIONS_STATE_KEY, } from "./httpMiddleware.core.js";
13
14
  export { createForbiddenResponse, createUnauthorizedResponse, createJsonResponse, } from "./httpHelpers.js";
15
+ export { loadResource, RESOURCE_ERROR_DECISION, } from "./httpResource.helper.js";
14
16
  //# sourceMappingURL=index.js.map
@@ -3,6 +3,6 @@
3
3
  *
4
4
  * @module permission
5
5
  */
6
- export { parsePermission, parsePermissionSafe, isValidPermission, matches, matchesPermission, formatPermission, } from "./permission.core.js";
6
+ export { parsePermission, parsePermissionSafe, isValidPermission, matches, matchesPermission, permissionsOverlap, formatPermission, } from "./permission.core.js";
7
7
  export { createPermissionRegistry, type PermissionRegistry, type PermissionRegistryOptions, type RegisteredPermission, } from "./permissionRegistry.js";
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -3,6 +3,6 @@
3
3
  *
4
4
  * @module permission
5
5
  */
6
- export { parsePermission, parsePermissionSafe, isValidPermission, matches, matchesPermission, formatPermission, } from "./permission.core.js";
6
+ export { parsePermission, parsePermissionSafe, isValidPermission, matches, matchesPermission, permissionsOverlap, formatPermission, } from "./permission.core.js";
7
7
  export { createPermissionRegistry, } from "./permissionRegistry.js";
8
8
  //# sourceMappingURL=index.js.map
@@ -32,6 +32,18 @@ export declare function matches(pattern: string, target: string): boolean;
32
32
  * Check if a permission pattern matches a structured Permission object.
33
33
  */
34
34
  export declare function matchesPermission(pattern: string, permission: Permission): boolean;
35
+ /**
36
+ * Check if two permission patterns can name a common permission.
37
+ *
38
+ * This is the test a *deny* needs. For a concrete target it is exactly
39
+ * {@link matches}; for a wildcard target such as `post:*` it also holds when
40
+ * the deny is narrower (`post:delete`), which `matches` does not see.
41
+ *
42
+ * @example permissionsOverlap("post:delete", "post:*") → true
43
+ * @example permissionsOverlap("*:delete", "post:*") → true
44
+ * @example permissionsOverlap("post:delete", "post:read") → false
45
+ */
46
+ export declare function permissionsOverlap(a: string, b: string): boolean;
35
47
  /**
36
48
  * Parse a permission string, returning null on invalid format instead of throwing.
37
49
  */
@@ -4,7 +4,7 @@
4
4
  * @module permission/permission
5
5
  */
6
6
  import { InvalidPermissionError } from "../permissionErrors/index.js";
7
- import { patternStrMatches } from "../rule/rule.core.js";
7
+ import { patternStrMatches, patternsOverlap } from "../rule/rule.pattern.js";
8
8
  /**
9
9
  * Valid permission format: `resource:action`.
10
10
  *
@@ -62,6 +62,25 @@ export function matchesPermission(pattern, permission) {
62
62
  return (patternStrMatches(parsed.resource, permission.resource) &&
63
63
  patternStrMatches(parsed.action, permission.action));
64
64
  }
65
+ /**
66
+ * Check if two permission patterns can name a common permission.
67
+ *
68
+ * This is the test a *deny* needs. For a concrete target it is exactly
69
+ * {@link matches}; for a wildcard target such as `post:*` it also holds when
70
+ * the deny is narrower (`post:delete`), which `matches` does not see.
71
+ *
72
+ * @example permissionsOverlap("post:delete", "post:*") → true
73
+ * @example permissionsOverlap("*:delete", "post:*") → true
74
+ * @example permissionsOverlap("post:delete", "post:read") → false
75
+ */
76
+ export function permissionsOverlap(a, b) {
77
+ const left = parsePermissionSafe(a);
78
+ const right = parsePermissionSafe(b);
79
+ if (!left || !right)
80
+ return false;
81
+ return (patternsOverlap(left.resource, right.resource) &&
82
+ patternsOverlap(left.action, right.action));
83
+ }
65
84
  /**
66
85
  * Parse a permission string, returning null on invalid format instead of throwing.
67
86
  */
@@ -39,6 +39,16 @@ export interface PermissionRegistry {
39
39
  expandImplied(permission: string): readonly string[];
40
40
  remove(permission: string): boolean;
41
41
  clear(): void;
42
+ /**
43
+ * Be told whenever the permission set changes (`define`, a `remove` that
44
+ * removed something, `clear`). Returns an unsubscribe function.
45
+ *
46
+ * An engine given this registry as its `expandImplied` source subscribes
47
+ * itself, which is what makes revoking an implication take effect
48
+ * immediately: the engine's cached decisions were written under the old
49
+ * implication and would otherwise keep granting for the whole TTL.
50
+ */
51
+ subscribe(listener: () => void): () => void;
42
52
  }
43
53
  /**
44
54
  * Create a permission registry.
@@ -49,7 +59,10 @@ export interface PermissionRegistry {
49
59
  *
50
60
  * const engine = createPermissionEngine({
51
61
  * roles,
52
- * expandImplied: (permission) => permissions.expandImplied(permission),
62
+ * // Pass the registry itself, not a closure over it: the engine
63
+ * // subscribes, so revoking an implication drops the cached decisions
64
+ * // that were made under it.
65
+ * expandImplied: permissions,
53
66
  * });
54
67
  * ```
55
68
  */
@@ -4,6 +4,7 @@
4
4
  * @module permission/permissionRegistry
5
5
  */
6
6
  import { DuplicatePermissionError, InvalidPermissionError, PermissionNotFoundError, } from "../permissionErrors/index.js";
7
+ import { createChangeNotifier } from "../utils/utils.notifier.js";
7
8
  import { formatPermission, isValidPermission, matchesPermission, parsePermission, } from "./permission.core.js";
8
9
  /**
9
10
  * Create a permission registry.
@@ -14,13 +15,17 @@ import { formatPermission, isValidPermission, matchesPermission, parsePermission
14
15
  *
15
16
  * const engine = createPermissionEngine({
16
17
  * roles,
17
- * expandImplied: (permission) => permissions.expandImplied(permission),
18
+ * // Pass the registry itself, not a closure over it: the engine
19
+ * // subscribes, so revoking an implication drops the cached decisions
20
+ * // that were made under it.
21
+ * expandImplied: permissions,
18
22
  * });
19
23
  * ```
20
24
  */
21
25
  export function createPermissionRegistry(options) {
22
26
  const permissions = new Map();
23
27
  const allowOverride = options?.allowOverride ?? false;
28
+ const changes = createChangeNotifier();
24
29
  function keyOf(permission) {
25
30
  if (typeof permission === "string") {
26
31
  // Validate strings and structured values alike; a structured value
@@ -53,6 +58,7 @@ export function createPermissionRegistry(options) {
53
58
  ? Object.freeze([...defineOptions.implies])
54
59
  : undefined,
55
60
  });
61
+ changes.notify();
56
62
  },
57
63
  get(permission) {
58
64
  return permissions.get(permission)?.parsed;
@@ -96,10 +102,17 @@ export function createPermissionRegistry(options) {
96
102
  return [...expanded];
97
103
  },
98
104
  remove(permission) {
99
- return permissions.delete(permission);
105
+ const removed = permissions.delete(permission);
106
+ if (removed)
107
+ changes.notify();
108
+ return removed;
100
109
  },
101
110
  clear() {
102
111
  permissions.clear();
112
+ changes.notify();
113
+ },
114
+ subscribe(listener) {
115
+ return changes.subscribe(listener);
103
116
  },
104
117
  };
105
118
  }
@@ -8,6 +8,12 @@ import type { PermissionPolicyDefinition } from "../permissionTypes/index.js";
8
8
  export interface PolicyRegistryOptions {
9
9
  /** Allow overwriting an existing policy. Defaults to false. */
10
10
  readonly allowOverride?: boolean;
11
+ /**
12
+ * Reject permission patterns that are not `resource:action`. Defaults to
13
+ * true — a policy scoped to a pattern that never matches never runs, so a
14
+ * mistyped lockdown would deny nothing.
15
+ */
16
+ readonly validatePermissions?: boolean;
11
17
  }
12
18
  /** A policy registry, usable directly as the engine's `policies` source. */
13
19
  export interface PolicyRegistry {
@@ -19,6 +25,12 @@ export interface PolicyRegistry {
19
25
  all(): readonly PermissionPolicyDefinition[];
20
26
  remove(name: string): boolean;
21
27
  clear(): void;
28
+ /**
29
+ * Be told whenever the policy set changes. Returns an unsubscribe function.
30
+ * An engine built on this registry subscribes itself, so a policy change
31
+ * is not bypassed by a cached decision.
32
+ */
33
+ subscribe(listener: () => void): () => void;
22
34
  }
23
35
  /**
24
36
  * Create a policy registry.
@@ -5,6 +5,8 @@
5
5
  */
6
6
  import { DuplicatePolicyError } from "../permissionErrors/index.js";
7
7
  import { selectPolicies } from "../evaluator/evaluator.pipeline.js";
8
+ import { validatePolicy } from "../evaluator/engineSupport/index.js";
9
+ import { createChangeNotifier } from "../utils/utils.notifier.js";
8
10
  /**
9
11
  * Create a policy registry.
10
12
  *
@@ -13,6 +15,8 @@ import { selectPolicies } from "../evaluator/evaluator.pipeline.js";
13
15
  export function createPolicyRegistry(options) {
14
16
  const policies = new Map();
15
17
  const allowOverride = options?.allowOverride ?? false;
18
+ const validatePermissions = options?.validatePermissions ?? true;
19
+ const changes = createChangeNotifier();
16
20
  return {
17
21
  /**
18
22
  * Register a policy.
@@ -20,8 +24,12 @@ export function createPolicyRegistry(options) {
20
24
  * Re-registering a name is rejected unless `allowOverride` was set: a
21
25
  * second `define("owner-only", …)` used to replace the first in silence,
22
26
  * which is an authorization rule vanishing without a trace.
27
+ *
28
+ * @throws {InvalidPermissionError} when a permission pattern is malformed.
23
29
  */
24
30
  define(definition) {
31
+ if (validatePermissions)
32
+ validatePolicy(definition);
25
33
  if (policies.has(definition.name) && !allowOverride) {
26
34
  throw new DuplicatePolicyError(definition.name);
27
35
  }
@@ -31,6 +39,7 @@ export function createPolicyRegistry(options) {
31
39
  ...definition,
32
40
  permissions: Object.freeze([...definition.permissions]),
33
41
  }));
42
+ changes.notify();
34
43
  },
35
44
  get(name) {
36
45
  return policies.get(name);
@@ -55,10 +64,17 @@ export function createPolicyRegistry(options) {
55
64
  return [...policies.values()];
56
65
  },
57
66
  remove(name) {
58
- return policies.delete(name);
67
+ const removed = policies.delete(name);
68
+ if (removed)
69
+ changes.notify();
70
+ return removed;
59
71
  },
60
72
  clear() {
61
73
  policies.clear();
74
+ changes.notify();
75
+ },
76
+ subscribe(listener) {
77
+ return changes.subscribe(listener);
62
78
  },
63
79
  };
64
80
  }
@@ -26,6 +26,15 @@ export interface RoleRegistry {
26
26
  all(): readonly RoleDefinition[];
27
27
  remove(name: string): boolean;
28
28
  clear(): void;
29
+ /**
30
+ * Be told whenever the role set changes (`define`, a `remove` that removed
31
+ * something, `clear`). Returns an unsubscribe function.
32
+ *
33
+ * An engine built on this registry subscribes itself, which is what makes
34
+ * revoking a role take effect on the next check instead of whenever
35
+ * somebody remembers to call `engine.invalidateRoles()`.
36
+ */
37
+ subscribe(listener: () => void): () => void;
29
38
  }
30
39
  /**
31
40
  * Copy a role definition so it no longer shares arrays with the caller.
@@ -5,6 +5,8 @@
5
5
  */
6
6
  import { DuplicateRoleError, InvalidRoleError, RoleNotFoundError, } from "../permissionErrors/index.js";
7
7
  import { isValidPermission } from "../permission/permission.core.js";
8
+ import { invalidRulePattern } from "../evaluator/engineSupport/index.js";
9
+ import { createChangeNotifier } from "../utils/utils.notifier.js";
8
10
  /**
9
11
  * Copy a role definition so it no longer shares arrays with the caller.
10
12
  *
@@ -32,6 +34,7 @@ export function createRoleRegistry(options) {
32
34
  const roles = new Map();
33
35
  const allowOverride = options?.allowOverride ?? false;
34
36
  const validatePermissions = options?.validatePermissions ?? true;
37
+ const changes = createChangeNotifier();
35
38
  return {
36
39
  /**
37
40
  * Register a role definition.
@@ -54,11 +57,19 @@ export function createRoleRegistry(options) {
54
57
  `valid "resource:action" permission`);
55
58
  }
56
59
  }
60
+ for (const rule of definition.rules ?? []) {
61
+ const invalid = invalidRulePattern(rule);
62
+ if (invalid !== undefined) {
63
+ throw new InvalidRoleError(`Role "${definition.name}" has a rule on "${invalid}", which is ` +
64
+ `not a valid "resource:action" pattern`);
65
+ }
66
+ }
57
67
  }
58
68
  if (roles.has(definition.name) && !allowOverride) {
59
69
  throw new DuplicateRoleError(definition.name);
60
70
  }
61
71
  roles.set(definition.name, freezeRoleDefinition(definition));
72
+ changes.notify();
62
73
  },
63
74
  get(name) {
64
75
  return roles.get(name);
@@ -79,10 +90,17 @@ export function createRoleRegistry(options) {
79
90
  return [...roles.values()];
80
91
  },
81
92
  remove(name) {
82
- return roles.delete(name);
93
+ const removed = roles.delete(name);
94
+ if (removed)
95
+ changes.notify();
96
+ return removed;
83
97
  },
84
98
  clear() {
85
99
  roles.clear();
100
+ changes.notify();
101
+ },
102
+ subscribe(listener) {
103
+ return changes.subscribe(listener);
86
104
  },
87
105
  };
88
106
  }