@ultimat3/policy 31.49.0 → 31.51.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/policy",
3
- "version": "31.49.0",
3
+ "version": "31.51.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.49.0"
45
+ "@ultimat3/core": "31.51.0"
46
46
  }
47
47
  }
@@ -42,6 +42,12 @@ const sites = new Map<string, Set<string>>();
42
42
 
43
43
  export const knownPermissions = (): readonly string[] => [...declared].sort();
44
44
 
45
+ /**
46
+ * Whether the declared set is nonempty — `can()`'s deferred check asks it per decision. A
47
+ * `definePermissions([])` leaves it empty, so it is not the same as "a declaration ran".
48
+ */
49
+ export const permissionSetDeclared = (): boolean => declared.size > 0;
50
+
45
51
  /**
46
52
  * Where each permission was declared: one stack frame per distinct declaring call, as
47
53
  * `roleDeclarationSites()` answers for roles. ALL of them rather than the first, because the
package/src/policy.ts CHANGED
@@ -4,7 +4,12 @@
4
4
  import type { Ctx } from '@ultimat3/core';
5
5
  import { emptyClauseList } from './errors';
6
6
  import { actorHas } from './grant-index';
7
- import { assertPermission, type KnownPermission, type Permission } from './permissions';
7
+ import {
8
+ assertPermission,
9
+ type KnownPermission,
10
+ type Permission,
11
+ permissionSetDeclared,
12
+ } from './permissions';
8
13
  import { markPreInput, type PreInputRun, preInputRun } from './pre-input-brand';
9
14
  import type { Actor } from './roles';
10
15
  import type { PolicyScope } from './scope';
@@ -132,10 +137,22 @@ export const can = <I = unknown, R = unknown>(
132
137
  ): Policy<I, R> => {
133
138
  assertPermission(permission);
134
139
  const label = permission;
140
+ // Built while no permission was declared — a rule at module scope imported ahead of the app's
141
+ // permission module — the check above had nothing to check against and let the name through.
142
+ // So it is made again at the first decision after the declared set becomes nonempty (an empty
143
+ // `definePermissions([])` does not count): a name that set lacks is `X_PERMISSION_UNKNOWN` there,
144
+ // never a deny for every actor with no reason given. Decisions before that stay unchecked.
145
+ let vetted = permissionSetDeclared();
146
+ const vet = (): void => {
147
+ if (vetted || !permissionSetDeclared()) return;
148
+ assertPermission(permission);
149
+ vetted = true;
150
+ };
135
151
  // The actor half of the rule, on its own: it reads nothing the caller sent, which is what lets
136
152
  // the before-input gate (`pre-input.ts`) refuse a caller who may never call this before their
137
153
  // payload is parsed — and a 403 then says nothing about the input schema.
138
154
  const gate = (actor: Actor | null): PolicyDecision | undefined => {
155
+ vet();
139
156
  if (actor === null) return denied(`no actor for ${label}`, 'X_UNAUTHENTICATED');
140
157
  if (!actorHas(actor, permission as Permission)) return denied(`actor lacks ${label}`);
141
158
  return undefined;