@astram/core 0.1.0 → 1.0.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/dist/admin/admin-service.d.ts +1 -1
- package/dist/admin/command/grants.js +3 -3
- package/dist/admin/command/grants.js.map +1 -1
- package/dist/admin/command/invitations.d.ts +2 -2
- package/dist/admin/command/invitations.js +8 -8
- package/dist/admin/command/invitations.js.map +1 -1
- package/dist/admin/command/service-accounts.d.ts +1 -1
- package/dist/admin/command/service-accounts.js +1 -1
- package/dist/admin/query/simulate.d.ts.map +1 -1
- package/dist/admin/query/simulate.js +4 -0
- package/dist/admin/query/simulate.js.map +1 -1
- package/dist/admin/safeguards-impl.d.ts +2 -2
- package/dist/admin/safeguards-impl.js +9 -9
- package/dist/admin/safeguards-impl.js.map +1 -1
- package/dist/admin/safeguards.d.ts +4 -4
- package/dist/admin/safeguards.js +4 -4
- package/dist/admin/types.d.ts +1 -1
- package/dist/admin/types.d.ts.map +1 -1
- package/dist/authentication/authenticator.d.ts +2 -2
- package/dist/authentication/authenticator.js +2 -2
- package/dist/authentication/authenticator.js.map +1 -1
- package/dist/authentication/credential-resolver.d.ts +1 -1
- package/dist/authorization/cross-tenant/grant-evaluator.d.ts +2 -2
- package/dist/authorization/cross-tenant/grant-evaluator.js +2 -2
- package/dist/authorization/decision-authority.d.ts +2 -2
- package/dist/authorization/decision-authority.d.ts.map +1 -1
- package/dist/authorization/decision-authority.js +11 -3
- package/dist/authorization/decision-authority.js.map +1 -1
- package/dist/authorization/errors.d.ts +37 -0
- package/dist/authorization/errors.d.ts.map +1 -1
- package/dist/authorization/errors.js +43 -0
- package/dist/authorization/errors.js.map +1 -1
- package/dist/authorization/gates/object-type.d.ts +2 -2
- package/dist/authorization/gates/object-type.d.ts.map +1 -1
- package/dist/authorization/gates/object-type.js +20 -7
- package/dist/authorization/gates/object-type.js.map +1 -1
- package/dist/authorization/gates/tenant.d.ts +3 -3
- package/dist/authorization/gates/tenant.js +7 -7
- package/dist/authorization/gates/tenant.js.map +1 -1
- package/dist/authorization/index.d.ts +23 -4
- package/dist/authorization/index.d.ts.map +1 -1
- package/dist/authorization/index.js +29 -4
- package/dist/authorization/index.js.map +1 -1
- package/dist/authorization/ports.d.ts +3 -3
- package/dist/authorization/reason-codes.d.ts +7 -1
- package/dist/authorization/reason-codes.d.ts.map +1 -1
- package/dist/authorization/reason-codes.js +9 -1
- package/dist/authorization/reason-codes.js.map +1 -1
- package/dist/authorization/record/owner-baseline.d.ts +43 -0
- package/dist/authorization/record/owner-baseline.d.ts.map +1 -0
- package/dist/authorization/record/owner-baseline.js +72 -0
- package/dist/authorization/record/owner-baseline.js.map +1 -0
- package/dist/authorization/record/permit-tier.d.ts.map +1 -1
- package/dist/authorization/record/permit-tier.js +16 -5
- package/dist/authorization/record/permit-tier.js.map +1 -1
- package/dist/identity/broker.d.ts +8 -8
- package/dist/identity/broker.d.ts.map +1 -1
- package/dist/identity/broker.js +8 -8
- package/dist/identity/broker.js.map +1 -1
- package/dist/identity/claim-mapper.d.ts +8 -8
- package/dist/identity/claim-mapper.d.ts.map +1 -1
- package/dist/identity/claim-mapper.js +4 -4
- package/dist/identity/index.d.ts +2 -2
- package/dist/identity/index.d.ts.map +1 -1
- package/dist/identity/internal-adapter.d.ts +12 -12
- package/dist/identity/internal-adapter.d.ts.map +1 -1
- package/dist/identity/internal-adapter.js +9 -9
- package/dist/identity/internal-adapter.js.map +1 -1
- package/dist/metadata/index.d.ts +43 -0
- package/dist/metadata/index.d.ts.map +1 -1
- package/dist/metadata/index.js +6 -0
- package/dist/metadata/index.js.map +1 -1
- package/dist/metadata/registry.d.ts.map +1 -1
- package/dist/metadata/registry.js +2 -1
- package/dist/metadata/registry.js.map +1 -1
- package/dist-cjs/admin/admin-service.js +159 -0
- package/dist-cjs/admin/command/grants.js +200 -0
- package/dist-cjs/admin/command/index.js +38 -0
- package/dist-cjs/admin/command/invitations.js +246 -0
- package/dist-cjs/admin/command/permissions.js +69 -0
- package/dist-cjs/admin/command/pipeline.js +110 -0
- package/dist-cjs/admin/command/policies.js +223 -0
- package/dist-cjs/admin/command/service-accounts.js +278 -0
- package/dist-cjs/admin/command/service.js +130 -0
- package/dist-cjs/admin/command/tiers.js +81 -0
- package/dist-cjs/admin/index.js +78 -0
- package/dist-cjs/admin/ports.js +41 -0
- package/dist-cjs/admin/query/audit-view.js +88 -0
- package/dist-cjs/admin/query/catalog.js +174 -0
- package/dist-cjs/admin/query/explain.js +149 -0
- package/dist-cjs/admin/query/index.js +74 -0
- package/dist-cjs/admin/query/simulate.js +98 -0
- package/dist-cjs/admin/safeguards-impl.js +322 -0
- package/dist-cjs/admin/safeguards.js +95 -0
- package/dist-cjs/admin/types.js +40 -0
- package/dist-cjs/audit/audit-recorder.js +269 -0
- package/dist-cjs/audit/canonicalize.js +176 -0
- package/dist-cjs/audit/errors.js +44 -0
- package/dist-cjs/audit/index.js +39 -0
- package/dist-cjs/audit/ports.js +27 -0
- package/dist-cjs/audit/sensitive.js +162 -0
- package/dist-cjs/audit/walk.js +123 -0
- package/dist-cjs/authentication/authenticator.js +210 -0
- package/dist-cjs/authentication/credential-resolver.js +21 -0
- package/dist-cjs/authentication/index.js +42 -0
- package/dist-cjs/authentication/password-hasher.js +21 -0
- package/dist-cjs/authentication/totp.js +155 -0
- package/dist-cjs/authorization/cross-tenant/grant-evaluator.js +167 -0
- package/dist-cjs/authorization/decision-authority.js +213 -0
- package/dist-cjs/authorization/default-gates.js +53 -0
- package/dist-cjs/authorization/delegation/operation-identity.js +226 -0
- package/dist-cjs/authorization/errors.js +110 -0
- package/dist-cjs/authorization/gates/object-type.js +192 -0
- package/dist-cjs/authorization/gates/tenant.js +89 -0
- package/dist-cjs/authorization/gates/wiring.js +59 -0
- package/dist-cjs/authorization/index.js +124 -0
- package/dist-cjs/authorization/ports.js +36 -0
- package/dist-cjs/authorization/reason-codes.js +117 -0
- package/dist-cjs/authorization/record/owner-baseline.js +74 -0
- package/dist-cjs/authorization/record/permit-tier.js +92 -0
- package/dist-cjs/authorization/record/rule-evaluator.js +128 -0
- package/dist-cjs/authorization/record/shares.js +110 -0
- package/dist-cjs/identity/broker.js +139 -0
- package/dist-cjs/identity/claim-mapper.js +51 -0
- package/dist-cjs/identity/index.js +39 -0
- package/dist-cjs/identity/internal-adapter.js +91 -0
- package/dist-cjs/index.js +63 -0
- package/dist-cjs/metadata/completeness.js +108 -0
- package/dist-cjs/metadata/dependencies.js +110 -0
- package/dist-cjs/metadata/index.js +76 -0
- package/dist-cjs/metadata/registry.js +207 -0
- package/dist-cjs/package.json +3 -0
- package/dist-cjs/risk/evaluator.js +63 -0
- package/dist-cjs/risk/index.js +35 -0
- package/dist-cjs/risk/risk-engine.js +290 -0
- package/dist-cjs/risk/signals.js +106 -0
- package/dist-cjs/risk/thresholds.js +63 -0
- package/dist-cjs/session/index.js +35 -0
- package/dist-cjs/session/lifetimes.js +70 -0
- package/dist-cjs/session/ports.js +23 -0
- package/dist-cjs/session/session-authority.js +398 -0
- package/package.json +13 -10
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* INTEGRATION WIRING — assembles the four REAL team-supplied gate functions into the
|
|
4
|
+
* `GateFunctions` set the frozen pipeline (`decision-authority.ts`) calls at gates
|
|
5
|
+
* 3/5/6/7, and offers a convenience factory that returns a fully-wired
|
|
6
|
+
* {@link DecisionAuthority} from just the ports + registry + per-request session facts.
|
|
7
|
+
*
|
|
8
|
+
* This is the ONE place the disjoint team slices come together into the single
|
|
9
|
+
* decision path (the "one decision path" invariant): every gate is the production
|
|
10
|
+
* evaluator, no deny-by-default placeholder survives.
|
|
11
|
+
*
|
|
12
|
+
* gate 3 (tenant) → TEAM A `createTenantGate()`
|
|
13
|
+
* gate 5 (object-type) → TEAM A `createObjectTypeGate()`
|
|
14
|
+
* gate 6 (record permit) → the COMPOSED `createRecordPermitGate()` (TEAM B policy
|
|
15
|
+
* ∪ TEAM C share/grant, deny-wins) — the integration
|
|
16
|
+
* owner's composer, NOT either team's slice-only view.
|
|
17
|
+
* gate 7 (delegation) → TEAM D `createDelegationOverlay()`
|
|
18
|
+
*
|
|
19
|
+
* ⛔ The prelude's `DEFAULT_DENY_BY_DEFAULT_GATES` (`default-gates.ts`) STAYS as-is —
|
|
20
|
+
* it is the fail-closed fallback a not-yet-wired authority uses. This file is the
|
|
21
|
+
* PRODUCTION gate set that replaces it; `default-gates.ts` is untouched.
|
|
22
|
+
*
|
|
23
|
+
* ── ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports; zero-dep.
|
|
24
|
+
*/
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.createProductionGates = createProductionGates;
|
|
27
|
+
exports.createWiredDecisionAuthority = createWiredDecisionAuthority;
|
|
28
|
+
const decision_authority_js_1 = require("../decision-authority.js");
|
|
29
|
+
const tenant_js_1 = require("./tenant.js");
|
|
30
|
+
const object_type_js_1 = require("./object-type.js");
|
|
31
|
+
const operation_identity_js_1 = require("../delegation/operation-identity.js");
|
|
32
|
+
const permit_tier_js_1 = require("../record/permit-tier.js");
|
|
33
|
+
/**
|
|
34
|
+
* Build the PRODUCTION {@link GateFunctions} — the four real evaluators wired into
|
|
35
|
+
* the pipeline's gate seam. Inject this as `DecisionAuthorityDeps.gates` to run the
|
|
36
|
+
* real decision path (in place of the prelude's `DEFAULT_DENY_BY_DEFAULT_GATES`).
|
|
37
|
+
*/
|
|
38
|
+
function createProductionGates(options = {}) {
|
|
39
|
+
const recordPermitOptions = options.now === undefined ? {} : { now: options.now };
|
|
40
|
+
return {
|
|
41
|
+
tenantGate: (0, tenant_js_1.createTenantGate)(),
|
|
42
|
+
objectTypeGate: (0, object_type_js_1.createObjectTypeGate)(),
|
|
43
|
+
recordPermit: (0, permit_tier_js_1.createRecordPermitGate)(recordPermitOptions),
|
|
44
|
+
delegationOverlay: (0, operation_identity_js_1.createDelegationOverlay)(),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Convenience: build a fully-wired {@link DecisionAuthority} from just the ports +
|
|
49
|
+
* registry + per-request session facts. The four production gates are assembled
|
|
50
|
+
* internally via {@link createProductionGates}. This is the entry point a consumer
|
|
51
|
+
* (the enforcement adapter) uses to get the ONE, real decision authority — no
|
|
52
|
+
* deny-by-default placeholder anywhere in the path.
|
|
53
|
+
*/
|
|
54
|
+
function createWiredDecisionAuthority(deps, options = {}) {
|
|
55
|
+
return (0, decision_authority_js_1.createDecisionAuthority)({
|
|
56
|
+
...deps,
|
|
57
|
+
gates: createProductionGates(options),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Engine seam: **Authorization** — THE single decision authority.
|
|
4
|
+
*
|
|
5
|
+
* `decide(principal, action, resource, context) → Allow | Deny (+ reason)`
|
|
6
|
+
* (050 AUTHORIZATION_REQUIREMENTS §7.1). Every enforcement point calls this same
|
|
7
|
+
* authority — no divergent logic per feature/resource-level/principal-type.
|
|
8
|
+
*
|
|
9
|
+
* This module defines the decision **seam** (types + the `DecisionAuthority`
|
|
10
|
+
* interface) ONLY. The evaluation logic — hard gates → permit tier, conditions,
|
|
11
|
+
* tenancy, shares, delegation, cross-tenant grants — is built in a MUCH later
|
|
12
|
+
* layer (050). Nothing here evaluates anything.
|
|
13
|
+
*
|
|
14
|
+
* ADR-0003: framework-agnostic.
|
|
15
|
+
*/
|
|
16
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
19
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
20
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
21
|
+
}
|
|
22
|
+
Object.defineProperty(o, k2, desc);
|
|
23
|
+
}) : (function(o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
o[k2] = m[k];
|
|
26
|
+
}));
|
|
27
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
28
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.SEGMENT_FORMAT = exports.PERMISSION_KEY_SEPARATOR = exports.DEFAULT_ACCESS_TIER = void 0;
|
|
32
|
+
exports.derivePermissionKey = derivePermissionKey;
|
|
33
|
+
/**
|
|
34
|
+
* The default access tier applied when an action carries no explicit tier —
|
|
35
|
+
* `gated` (ADR-0004: unclassified ⇒ Gated, and absent a grant, denied).
|
|
36
|
+
*/
|
|
37
|
+
exports.DEFAULT_ACCESS_TIER = 'gated';
|
|
38
|
+
/**
|
|
39
|
+
* The separator between the three key segments of a {@link PermissionKey}.
|
|
40
|
+
* A single-source constant so producers/consumers can't drift.
|
|
41
|
+
*/
|
|
42
|
+
exports.PERMISSION_KEY_SEPARATOR = ':';
|
|
43
|
+
/**
|
|
44
|
+
* The required format for each permission-key segment (moduleKey / typeKey /
|
|
45
|
+
* actionKey): `snake_case` — a lowercase letter, then lowercase letters, digits,
|
|
46
|
+
* and underscores. Single-source so the rule can't drift between producers.
|
|
47
|
+
* Enforced in {@link derivePermissionKey} as a fail-loud reject (never normalized),
|
|
48
|
+
* so every consumer app names its modules/types/actions uniformly and a casing
|
|
49
|
+
* typo fails at boot instead of silently denying at request time.
|
|
50
|
+
*/
|
|
51
|
+
exports.SEGMENT_FORMAT = /^[a-z][a-z0-9_]*$/;
|
|
52
|
+
/**
|
|
53
|
+
* Derive the stable permission identity `feature:object:action` from a module's
|
|
54
|
+
* `moduleKey`, an object type's `typeKey`, and an action's `actionKey`
|
|
55
|
+
* (ADR-0010, confirmed decision #1). PURE — no I/O, no state, deterministic.
|
|
56
|
+
*
|
|
57
|
+
* This is the ONLY sanctioned way to construct a {@link PermissionKey}; grants
|
|
58
|
+
* bind to its result. Segments must be non-empty, free of the separator, and in
|
|
59
|
+
* `snake_case` so the key round-trips unambiguously AND every consumer app names
|
|
60
|
+
* its modules/types/actions uniformly.
|
|
61
|
+
*
|
|
62
|
+
* `snake_case` = {@link SEGMENT_FORMAT} = lowercase start, then lowercase letters,
|
|
63
|
+
* digits, and underscores (`^[a-z][a-z0-9_]*$`). This is enforced here — the single
|
|
64
|
+
* sanctioned key constructor — so a stray `CompanyBranch`/`company-branch` typeKey
|
|
65
|
+
* is a HARD, loud failure at boot (when the registry builds keys), never a silent
|
|
66
|
+
* casing mismatch that denies at request time. It is a format REJECT, never a
|
|
67
|
+
* normalization: the key is used exactly as written (the gate compares with `===`),
|
|
68
|
+
* so we refuse a bad name rather than silently rewrite an identity string.
|
|
69
|
+
*
|
|
70
|
+
* @throws if any segment is empty, contains the `:` separator, or is not snake_case.
|
|
71
|
+
*/
|
|
72
|
+
function derivePermissionKey(moduleKey, typeKey, actionKey) {
|
|
73
|
+
for (const [name, seg] of [
|
|
74
|
+
['moduleKey', moduleKey],
|
|
75
|
+
['typeKey', typeKey],
|
|
76
|
+
['actionKey', actionKey],
|
|
77
|
+
]) {
|
|
78
|
+
// Order matters: empty and separator checks first so their existing error
|
|
79
|
+
// messages/wording are preserved, then the generic snake_case format check.
|
|
80
|
+
if (seg.length === 0) {
|
|
81
|
+
throw new Error(`derivePermissionKey: ${name} must be non-empty`);
|
|
82
|
+
}
|
|
83
|
+
if (seg.includes(exports.PERMISSION_KEY_SEPARATOR)) {
|
|
84
|
+
throw new Error(`derivePermissionKey: ${name} must not contain "${exports.PERMISSION_KEY_SEPARATOR}" (got "${seg}")`);
|
|
85
|
+
}
|
|
86
|
+
if (!exports.SEGMENT_FORMAT.test(seg)) {
|
|
87
|
+
throw new Error(`derivePermissionKey: ${name} must be snake_case (${exports.SEGMENT_FORMAT.source}) — got "${seg}"`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return `${moduleKey}${exports.PERMISSION_KEY_SEPARATOR}${typeKey}${exports.PERMISSION_KEY_SEPARATOR}${actionKey}`;
|
|
91
|
+
}
|
|
92
|
+
// --- Engine implementation behind the FROZEN seam above (Layer 8) -------------
|
|
93
|
+
// APPEND-ONLY re-exports (mirrors identity/session/metadata barrels). Nothing
|
|
94
|
+
// above this line changes; the engine is added as SIBLING files.
|
|
95
|
+
// - the typed transient signal (ADR-0016):
|
|
96
|
+
__exportStar(require("./errors.js"), exports);
|
|
97
|
+
// - the six evaluation PORTs (adapters ship in @astram/guard — Core stays zero-dep):
|
|
98
|
+
__exportStar(require("./ports.js"), exports);
|
|
99
|
+
// - the Decision reason-code constructors + the strict-`===true` fail-open guard:
|
|
100
|
+
__exportStar(require("./reason-codes.js"), exports);
|
|
101
|
+
// - the decision pipeline engine (gate order + fail-closed terminal) + the
|
|
102
|
+
// `createDecisionAuthority` factory + the gate-fn seam Teams A–D implement:
|
|
103
|
+
__exportStar(require("./decision-authority.js"), exports);
|
|
104
|
+
// - the prelude's deny-by-default gate defaults (teams replace at integration):
|
|
105
|
+
__exportStar(require("./default-gates.js"), exports);
|
|
106
|
+
// - INTEGRATION: the real team gate evaluators + composer + wiring, surfaced on the
|
|
107
|
+
// package barrel (append-only; nothing above this line changed).
|
|
108
|
+
// - TEAM A hard gates (tenant gate 3 + feature/object-type gate 5):
|
|
109
|
+
__exportStar(require("./gates/tenant.js"), exports);
|
|
110
|
+
__exportStar(require("./gates/object-type.js"), exports);
|
|
111
|
+
// - TEAM B: record_permission_rule POLICY evaluator (gate 6 policy half):
|
|
112
|
+
__exportStar(require("./record/rule-evaluator.js"), exports);
|
|
113
|
+
// - TEAM C: per-record shares + cross-tenant grant (share half of gate 6):
|
|
114
|
+
__exportStar(require("./record/shares.js"), exports);
|
|
115
|
+
// - TEAM C: shared cross-tenant grant scope/action-ceiling evaluator (gates 3/5/6):
|
|
116
|
+
__exportStar(require("./cross-tenant/grant-evaluator.js"), exports);
|
|
117
|
+
// - TEAM D: delegation overlay / trusted operation identity (gate 7):
|
|
118
|
+
__exportStar(require("./delegation/operation-identity.js"), exports);
|
|
119
|
+
// - FRAMEWORK BASELINE: the declaration-driven owner/system/tenantScoped permit (gate 6):
|
|
120
|
+
__exportStar(require("./record/owner-baseline.js"), exports);
|
|
121
|
+
// - INTEGRATION: the composed gate-6 record permit tier (baseline ∪ policy ∪ share, deny-wins):
|
|
122
|
+
__exportStar(require("./record/permit-tier.js"), exports);
|
|
123
|
+
// - INTEGRATION: the production gate-set wiring + fully-wired DecisionAuthority factory:
|
|
124
|
+
__exportStar(require("./gates/wiring.js"), exports);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The Authorization engine **PORTS** — the framework-agnostic contracts the
|
|
4
|
+
* `DecisionAuthority` engine (`./decision-authority.ts`) uses to reach the facts,
|
|
5
|
+
* rules, shares, cross-tenant grants, and delegated authority it needs to render a
|
|
6
|
+
* decision, WITHOUT the engine ever knowing which DB / cache backs them.
|
|
7
|
+
*
|
|
8
|
+
* ADR-0003 (zero-dep Core): every concrete adapter (MariaDB, Redis, …) is a real
|
|
9
|
+
* runtime dependency and CANNOT live here. The Core defines only these interfaces;
|
|
10
|
+
* `@astram/guard` ships the concretes and INJECTS them into
|
|
11
|
+
* `createDecisionAuthority(...)`. This keeps `@astram/core` at ZERO runtime
|
|
12
|
+
* dependencies while the substrate stays swappable.
|
|
13
|
+
*
|
|
14
|
+
* ⛔ TRANSIENT-vs-DENY (ADR-0016 / KICKOFF §"BINDING ARCHITECT DECISION") — every
|
|
15
|
+
* port method below distinguishes TWO fundamentally different "no" answers,
|
|
16
|
+
* mirroring Session's `found|absent|unavailable` trichotomy:
|
|
17
|
+
*
|
|
18
|
+
* 1. **Normal data-absence** — the store is UP and simply has no matching row
|
|
19
|
+
* (no rule / no share / no grant / facts genuinely empty). This is a legitimate
|
|
20
|
+
* **Deny INPUT**: it contributes to a returned `Decision` (`no_record_permit` /
|
|
21
|
+
* `missing_object_type_grant` / `deny_by_default`). The port returns the empty
|
|
22
|
+
* value (`false` / `null` / empty grants). It MUST NOT throw.
|
|
23
|
+
* 2. **Transient unavailable** — the store genuinely CANNOT answer (down, timeout,
|
|
24
|
+
* outage). The adapter MUST throw {@link AuthorizationUnavailableError} so the
|
|
25
|
+
* engine RE-THROWS it and the request fails closed as 503-retryable. It MUST
|
|
26
|
+
* NOT return an empty value (that would silently downgrade an outage to a Deny
|
|
27
|
+
* and erase the ADR-0016 retryable signal), and it MUST NOT fabricate a permit.
|
|
28
|
+
*
|
|
29
|
+
* ⛔ FAIL-OPEN DISCIPLINE (the Layer-5/6 bug class): a port returning a boolean
|
|
30
|
+
* verdict is gated by the engine on STRICT `=== true` (see `strictTrue` in
|
|
31
|
+
* `./reason-codes.ts`). A truthy non-boolean is NOT a pass. Adapters SHOULD return
|
|
32
|
+
* a genuine `boolean`; the engine defends regardless.
|
|
33
|
+
*
|
|
34
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
35
|
+
*/
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reason-code MAPPING — the single-source constructors that turn a gate outcome
|
|
4
|
+
* into a frozen {@link Decision} (§7.5). Every gate + the pipeline build their
|
|
5
|
+
* `Decision` HERE, never by hand-rolling a `{ effect, reason }` literal, so the
|
|
6
|
+
* frozen {@link DecisionReasonCode} taxonomy has exactly one producer and cannot
|
|
7
|
+
* drift.
|
|
8
|
+
*
|
|
9
|
+
* ⛔ §7.5 NO-LEAK RULE: a Deny's `reason` names the failing gate for machine use,
|
|
10
|
+
* but must NEVER reveal the existence of a resource the principal may not know
|
|
11
|
+
* about. The optional `detail` is ADMIN-TROUBLESHOOTING ONLY — it is never
|
|
12
|
+
* returned to end users and must never encode resource existence, a UUID, or a
|
|
13
|
+
* grant's contents. Callers that surface a Decision to a user render the reason
|
|
14
|
+
* code (mapped to a generic message), not the detail.
|
|
15
|
+
*
|
|
16
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.strictTrue = strictTrue;
|
|
20
|
+
exports.allow = allow;
|
|
21
|
+
exports.denyNotAuthenticated = denyNotAuthenticated;
|
|
22
|
+
exports.denyTenantMismatch = denyTenantMismatch;
|
|
23
|
+
exports.denyMissingObjectTypeGrant = denyMissingObjectTypeGrant;
|
|
24
|
+
exports.denyNoRecordPermit = denyNoRecordPermit;
|
|
25
|
+
exports.denyExplicitPolicy = denyExplicitPolicy;
|
|
26
|
+
exports.denyRootDataPlane = denyRootDataPlane;
|
|
27
|
+
exports.denyResourceNotRegistered = denyResourceNotRegistered;
|
|
28
|
+
exports.denyByDefault = denyByDefault;
|
|
29
|
+
/**
|
|
30
|
+
* ⛔ THE LOAD-BEARING FAIL-OPEN GUARD (the Layer-5/6 bug class). Returns `true`
|
|
31
|
+
* ONLY for the boolean `true`; a truthy NON-boolean (`"true"`, `1`, `{}`, a
|
|
32
|
+
* hostile port's junk) is NOT a pass. EVERY port-boolean gate (share verdict,
|
|
33
|
+
* rule allow/deny, CAS-style wins, grant-action membership resolved to a boolean)
|
|
34
|
+
* MUST route its verdict through this before treating it as an Allow. This is what
|
|
35
|
+
* stops a misbehaving/hostile adapter from failing the decision OPEN.
|
|
36
|
+
*/
|
|
37
|
+
function strictTrue(x) {
|
|
38
|
+
return x === true;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* ALLOW (gate 1 public short-circuit, gate 4 authenticated-global short-circuit,
|
|
42
|
+
* gate 5 object-type grant held, gate 6 record permit held). The single Allow
|
|
43
|
+
* constructor — reason is always `allowed`.
|
|
44
|
+
*/
|
|
45
|
+
function allow() {
|
|
46
|
+
return Object.freeze({ effect: 'allow', reason: 'allowed' });
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* DENY — gate 2 (authentication gate): a non-public action reached by an
|
|
50
|
+
* unauthenticated request. Reason `not_authenticated`.
|
|
51
|
+
*/
|
|
52
|
+
function denyNotAuthenticated(detail) {
|
|
53
|
+
return freezeDeny('not_authenticated', detail);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* DENY — gate 3 (tenant gate): the principal's tenant differs from the resource's,
|
|
57
|
+
* with no active cross-tenant grant and not a root. Reason
|
|
58
|
+
* `tenant_mismatch`. FINAL — the tenant boundary is never relaxed downstream.
|
|
59
|
+
*/
|
|
60
|
+
function denyTenantMismatch(detail) {
|
|
61
|
+
return freezeDeny('tenant_mismatch', detail);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* DENY — gate 5 (feature/object-type gate): a gated action with no applicable
|
|
65
|
+
* coarse grant (and no covering cross-tenant grant scope). Reason
|
|
66
|
+
* `missing_object_type_grant`.
|
|
67
|
+
*/
|
|
68
|
+
function denyMissingObjectTypeGrant(detail) {
|
|
69
|
+
return freezeDeny('missing_object_type_grant', detail);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* DENY — gate 6 (record permit tier): no permit from any source (rule ∪ share ∪
|
|
73
|
+
* cross-tenant grant) affirmatively allowed this record+action. Reason
|
|
74
|
+
* `no_record_permit`.
|
|
75
|
+
*/
|
|
76
|
+
function denyNoRecordPermit(detail) {
|
|
77
|
+
return freezeDeny('no_record_permit', detail);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* DENY — gate 6 (record permit tier), DENY-WINS: an explicit record_permission_rule
|
|
81
|
+
* deny matched. Overrides any share/grant widening. Reason `explicit_deny_policy`.
|
|
82
|
+
*/
|
|
83
|
+
function denyExplicitPolicy(detail) {
|
|
84
|
+
return freezeDeny('explicit_deny_policy', detail);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* DENY — the ROOT CAP: a root attempted a DATA-PLANE
|
|
88
|
+
* capability. Root is control-plane only (§0/§2); data-plane is denied by
|
|
89
|
+
* construction, grant-independent. Reason `root_data_plane_denied`.
|
|
90
|
+
*/
|
|
91
|
+
function denyRootDataPlane(detail) {
|
|
92
|
+
return freezeDeny('root_data_plane_denied', detail);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* DENY — gate 0 (resource registration): the resource's object type is not
|
|
96
|
+
* registered with the {@link ModuleRegistry} (unregistered ⇒ Deny, §5.4/5.5).
|
|
97
|
+
* Reason `resource_not_registered`.
|
|
98
|
+
*/
|
|
99
|
+
function denyResourceNotRegistered(detail) {
|
|
100
|
+
return freezeDeny('resource_not_registered', detail);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* DENY — gate 8 (fail-closed terminal): any unresolved path, an unclassified
|
|
104
|
+
* operation, or an UNEXPECTED exception caught inside `decide()` (never a typed
|
|
105
|
+
* transient — that RE-THROWS as {@link AuthorizationUnavailableError}). Reason
|
|
106
|
+
* `deny_by_default` (ADR-0002 invariant #1). This is the terminal every Allow must
|
|
107
|
+
* have escaped via an earlier short-circuit.
|
|
108
|
+
*/
|
|
109
|
+
function denyByDefault(detail) {
|
|
110
|
+
return freezeDeny('deny_by_default', detail);
|
|
111
|
+
}
|
|
112
|
+
/** Internal: build + freeze a deny Decision, omitting `detail` when absent. */
|
|
113
|
+
function freezeDeny(reason, detail) {
|
|
114
|
+
return detail === undefined
|
|
115
|
+
? Object.freeze({ effect: 'deny', reason })
|
|
116
|
+
: Object.freeze({ effect: 'deny', reason, detail });
|
|
117
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The FRAMEWORK-OWNED record-permit BASELINE (gate 6, design §5.2) — a PURE,
|
|
4
|
+
* declaration-driven permit source computed IN CORE (no DB, no rows, no adapter).
|
|
5
|
+
*
|
|
6
|
+
* This replaces the app-supplied "no rule = allow" fail-open smell with a baseline
|
|
7
|
+
* the framework OWNS and the app cannot weaken. It is derived entirely from the
|
|
8
|
+
* object type's ACCESS POSTURE (design §3, threaded onto the {@link ObjectTypeDescriptor}
|
|
9
|
+
* by the registry bridge) and, for owned types, ONE decision attribute the ENGINE
|
|
10
|
+
* supplies (`ownerId` — the existing record's owner column, fetched tenant-scoped by
|
|
11
|
+
* the engine BEFORE `decide()`; core never touches the DB — ADR-0003).
|
|
12
|
+
*
|
|
13
|
+
* The three postures (design §5.2):
|
|
14
|
+
* - `owned` → allow IFF `attributes.ownerId === principal.id` (STRICT ===).
|
|
15
|
+
* The DAC owner baseline: a principal passes for their OWN records.
|
|
16
|
+
* A missing/mismatched/undefined `ownerId` does NOT allow.
|
|
17
|
+
* - `system` → allow. Identity/infra (company/user/role): the object-type grant
|
|
18
|
+
* (gate 5) already governs; the record tier adds no narrowing.
|
|
19
|
+
* - `tenantScoped` → allow. The tenant gate (gate 3) already isolated the record to the
|
|
20
|
+
* actor's tenant; the record baseline adds no further narrowing.
|
|
21
|
+
* - (no access declared / unknown type) → does NOT allow (fail-CLOSED). A rule/share
|
|
22
|
+
* must permit it; the baseline never manufactures an allow for an
|
|
23
|
+
* un-declared type.
|
|
24
|
+
*
|
|
25
|
+
* The baseline is a WIDENING source: it is UNIONed with the admin policy + shares in the
|
|
26
|
+
* permit tier (deny-wins is unchanged — an explicit admin deny still overrides it).
|
|
27
|
+
*
|
|
28
|
+
* PURE + zero-dep (ADR-0003): reads only the resolved descriptor from the registry, the
|
|
29
|
+
* principal, and `ctx.context.attributes`. No I/O.
|
|
30
|
+
*/
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.evaluateOwnerBaseline = evaluateOwnerBaseline;
|
|
33
|
+
/**
|
|
34
|
+
* Compute the framework-owned record-permit baseline for this record+principal.
|
|
35
|
+
*
|
|
36
|
+
* Returns `true` when the baseline affirmatively permits (owner match / system /
|
|
37
|
+
* tenantScoped), `false` otherwise (no framework baseline — the policy/share must
|
|
38
|
+
* permit). NEVER throws — it is a pure comparison over already-resolved inputs.
|
|
39
|
+
*
|
|
40
|
+
* @param ctx the shared gate context (record-level resource; `ctx.deps.registry`
|
|
41
|
+
* resolves the descriptor's access posture; `ctx.context.attributes.ownerId` is the
|
|
42
|
+
* engine-supplied owner of the existing row for `owned` types).
|
|
43
|
+
*/
|
|
44
|
+
function evaluateOwnerBaseline(ctx) {
|
|
45
|
+
const descriptor = ctx.deps.registry.resolveObjectType(ctx.resource.typeKey);
|
|
46
|
+
const access = descriptor?.access;
|
|
47
|
+
// No access posture on the type ⇒ NO framework baseline. Fail-closed: the baseline
|
|
48
|
+
// does not allow; a rule/share must permit (never invent an allow for an undeclared
|
|
49
|
+
// type — that would be the old fail-open hole).
|
|
50
|
+
if (!access)
|
|
51
|
+
return false;
|
|
52
|
+
// system / tenantScoped ⇒ grant-governed at the record tier (gate 5 / gate 3 already
|
|
53
|
+
// did the governing). The baseline allows; nothing further to narrow here.
|
|
54
|
+
if (access.system === true)
|
|
55
|
+
return true;
|
|
56
|
+
if (access.tenantScoped === true && access.owned !== true)
|
|
57
|
+
return true;
|
|
58
|
+
// owned ⇒ DAC owner baseline: allow ONLY the principal that owns the record. STRICT
|
|
59
|
+
// ===: a missing or non-matching ownerId does NOT allow, and a non-string attribute
|
|
60
|
+
// cannot masquerade as a match. The ownerId (and the principal id) MUST be a NON-EMPTY
|
|
61
|
+
// string — an empty-string identity ('') must never self-authorize: `'' === ''` would
|
|
62
|
+
// otherwise fail OPEN (a principal.id='' reading a record whose owner column is ''/coerced
|
|
63
|
+
// to '' would be permitted). Defense-in-depth: the HTTP layer already rejects empty ids,
|
|
64
|
+
// but this security primitive must not depend on that — it fails closed on empty identity.
|
|
65
|
+
if (access.owned === true) {
|
|
66
|
+
const ownerId = ctx.context.attributes?.ownerId;
|
|
67
|
+
return (typeof ownerId === 'string' &&
|
|
68
|
+
ownerId.length > 0 &&
|
|
69
|
+
ownerId === ctx.principal.id);
|
|
70
|
+
}
|
|
71
|
+
// owned:false, tenantScoped:false, system:false — an explicitly ungoverned posture.
|
|
72
|
+
// No baseline permit (fail-closed).
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Gate 6 — the FINAL **record permit tier** composer (INTEGRATION owner) — §7.3.4 /
|
|
4
|
+
* §9, ADR-0005 (policy) + ADR-0006 (share) + ADR-0009 (cross-tenant grant).
|
|
5
|
+
*
|
|
6
|
+
* Gate 6 is the ONE record-level permit gate the pipeline calls. It is a UNION of two
|
|
7
|
+
* WIDENING permit sources under a DENY-WINS policy — assembled here from TEAM B's
|
|
8
|
+
* policy half and TEAM C's share/cross-tenant-grant half (neither team's file ships
|
|
9
|
+
* the final gate; each ships only its own slice + a slice-only view for testing):
|
|
10
|
+
*
|
|
11
|
+
* gate6 = (policy allows) OR (share / cross-tenant grant permits) [union widens]
|
|
12
|
+
* — but an EXPLICIT policy deny OVERRIDES everything [deny-wins]
|
|
13
|
+
*
|
|
14
|
+
* ── THE ONE COMPOSER (B's & C's DONE.md snippets were proven equivalent by a
|
|
15
|
+
* reviewer; this is that single agreed composer):
|
|
16
|
+
*
|
|
17
|
+
* 1. Evaluate the POLICY (TEAM B `evaluateRecordPolicy`).
|
|
18
|
+
* 2. `policy.explicitDeny` ⇒ `deny(explicit_deny_policy)` — DENY-WINS, and it
|
|
19
|
+
* SHORT-CIRCUITS *before* the share is ever consulted. A share can NEVER beat
|
|
20
|
+
* an explicit policy deny (that is the load-bearing invariant of this gate).
|
|
21
|
+
* 3. else evaluate the SHARE (TEAM C `evaluateRecordShare` — same-tenant share ∪
|
|
22
|
+
* applicable cross-tenant grant) and union it with the policy allow:
|
|
23
|
+
* `policy.allow || share` ⇒ `allow`.
|
|
24
|
+
* 4. else ⇒ `deny(no_record_permit)` (no permit from ANY source).
|
|
25
|
+
*
|
|
26
|
+
* ── DENY-WINS IS LOAD-BEARING. The explicit-deny check happens BEFORE `evaluateRecordShare`
|
|
27
|
+
* is called, so a share is not merely ignored — it is never even consulted when the
|
|
28
|
+
* policy explicitly denies. (Consulting it would be harmless too, since the deny
|
|
29
|
+
* already short-circuited, but not calling it is the clearest expression of
|
|
30
|
+
* "deny beats every widening" and avoids a needless port round-trip.)
|
|
31
|
+
*
|
|
32
|
+
* ── TRANSIENT-vs-DENY (ADR-0016). This composer NEVER catches. A genuine transient
|
|
33
|
+
* ({@link AuthorizationUnavailableError}) thrown by EITHER evaluator PROPAGATES so
|
|
34
|
+
* the pipeline's top-level catch re-throws it (503-retryable); any OTHER throw also
|
|
35
|
+
* propagates (the pipeline fail-closes it to `deny_by_default`). A transient is
|
|
36
|
+
* NEVER swallowed into a deny/allow. (Because the policy is evaluated first and an
|
|
37
|
+
* explicit deny short-circuits, a transient in the SHARE evaluator is only surfaced
|
|
38
|
+
* on a policy-silent, non-explicit-deny record — exactly the path where the share
|
|
39
|
+
* is legitimately consulted.)
|
|
40
|
+
*
|
|
41
|
+
* ── PURE + zero-dep (ADR-0003). No framework / HTTP / DB / node imports; the only I/O
|
|
42
|
+
* is through the two team evaluators (which reach their ports via `ctx.deps`).
|
|
43
|
+
*/
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.createRecordPermitGate = createRecordPermitGate;
|
|
46
|
+
const decision_authority_js_1 = require("../decision-authority.js");
|
|
47
|
+
const reason_codes_js_1 = require("../reason-codes.js");
|
|
48
|
+
const rule_evaluator_js_1 = require("./rule-evaluator.js");
|
|
49
|
+
const owner_baseline_js_1 = require("./owner-baseline.js");
|
|
50
|
+
const shares_js_1 = require("./shares.js");
|
|
51
|
+
/**
|
|
52
|
+
* Build the FINAL gate-6 {@link RecordPermitFn} — the composed policy ∪ share
|
|
53
|
+
* permit tier with deny-wins. This is the ONLY `recordPermit` gate the integration
|
|
54
|
+
* owner injects as `DecisionAuthorityDeps.gates.recordPermit`; do NOT inject either
|
|
55
|
+
* team's slice-only view (`createRecordPolicyGate` ignores shares;
|
|
56
|
+
* `createRecordShareGate` ignores policy).
|
|
57
|
+
*
|
|
58
|
+
* @param options optional injectables threaded into TEAM C's share evaluator (the
|
|
59
|
+
* deterministic `now` clock for cross-tenant-grant expiry); defaults to the
|
|
60
|
+
* helper's own `Date.now`.
|
|
61
|
+
*/
|
|
62
|
+
function createRecordPermitGate(options = {}) {
|
|
63
|
+
return async function recordPermit(ctx) {
|
|
64
|
+
// 1. POLICY (TEAM B). Deny-wins is resolved inside `evaluateRecordPolicy` under
|
|
65
|
+
// strict `=== true`; a transient throw PROPAGATES (not caught).
|
|
66
|
+
const policy = await (0, rule_evaluator_js_1.evaluateRecordPolicy)(ctx);
|
|
67
|
+
// 2. DENY-WINS — an explicit policy deny SHORT-CIRCUITS *before* the share is
|
|
68
|
+
// consulted. A share can never beat an explicit deny (load-bearing).
|
|
69
|
+
if (policy.explicitDeny) {
|
|
70
|
+
return (0, decision_authority_js_1.gateDeny)((0, reason_codes_js_1.denyExplicitPolicy)());
|
|
71
|
+
}
|
|
72
|
+
// 3. FRAMEWORK-OWNED BASELINE (design §5.2) — the declaration-driven owner/system/
|
|
73
|
+
// tenantScoped permit computed IN CORE (pure, no DB, cannot be swapped/weakened).
|
|
74
|
+
// A widening source unioned with policy + share. Evaluated AFTER the explicit-deny
|
|
75
|
+
// short-circuit so an admin deny still wins over the owner's own record. Pure —
|
|
76
|
+
// never throws.
|
|
77
|
+
const baseline = (0, owner_baseline_js_1.evaluateOwnerBaseline)(ctx);
|
|
78
|
+
if (policy.allow || baseline) {
|
|
79
|
+
return (0, decision_authority_js_1.gateAllow)();
|
|
80
|
+
}
|
|
81
|
+
// 4. UNION — policy allow ∪ baseline ∪ share/cross-tenant-grant permit. The share is
|
|
82
|
+
// only consulted now that neither the policy nor the baseline permitted (and the
|
|
83
|
+
// policy did not explicitly deny). A transient throw from the share evaluator
|
|
84
|
+
// PROPAGATES (not caught).
|
|
85
|
+
const share = await (0, shares_js_1.evaluateRecordShare)(ctx, options);
|
|
86
|
+
if (share) {
|
|
87
|
+
return (0, decision_authority_js_1.gateAllow)();
|
|
88
|
+
}
|
|
89
|
+
// 5. No permit from any source ⇒ a normal `no_record_permit` deny input.
|
|
90
|
+
return (0, decision_authority_js_1.gateDeny)((0, reason_codes_js_1.denyNoRecordPermit)());
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* TEAM B — the **record_permission_rule POLICY evaluator** (gate 6, POLICY half).
|
|
4
|
+
*
|
|
5
|
+
* Gate 6 (§7.3.4, ADR-0005) permits a record-level resource when EITHER the
|
|
6
|
+
* administrator POLICY allows OR a share / cross-tenant grant grants — a UNION —
|
|
7
|
+
* with an EXPLICIT policy DENY overriding everything (deny-wins). This file owns
|
|
8
|
+
* ONLY the POLICY half:
|
|
9
|
+
*
|
|
10
|
+
* - {@link evaluateRecordPolicy} — a PURE policy evaluator. It calls
|
|
11
|
+
* {@link RecordPermissionRulePort.evaluate} and applies the deny-wins
|
|
12
|
+
* resolution AT THE SEAM, returning a {@link PolicyVerdict} (`allow` +
|
|
13
|
+
* `explicitDeny`). It does NOT compose with shares and does NOT return a final
|
|
14
|
+
* {@link GateOutcome}. Composing policy ∪ share (deny-wins over both) is the
|
|
15
|
+
* INTEGRATION owner's job (or a thin composer they write) — see the module
|
|
16
|
+
* contract note below.
|
|
17
|
+
*
|
|
18
|
+
* - {@link createRecordPolicyGate} — a POLICY-ONLY {@link RecordPermitFn} that
|
|
19
|
+
* turns the {@link PolicyVerdict} into a {@link GateOutcome}, so the policy
|
|
20
|
+
* evaluator is independently testable AND usable if the integration owner wants
|
|
21
|
+
* policy-only wiring. ⚠️ THIS IS NOT THE FINAL GATE 6 — see below.
|
|
22
|
+
*
|
|
23
|
+
* ── ⛔ GATE-6 COMPOSITION CONTRACT (read before wiring) ─────────────────────────
|
|
24
|
+
* FINAL gate 6 = (policyAllow OR shareGrant) with policyExplicitDeny WINS.
|
|
25
|
+
*
|
|
26
|
+
* `createRecordPolicyGate` implements the POLICY-ONLY view: a policy-silent
|
|
27
|
+
* record (no allow, no deny) maps to `deny(no_record_permit)` — but in the REAL
|
|
28
|
+
* integrated gate a Team-C share (or cross-tenant grant) could STILL permit that
|
|
29
|
+
* same silent record. So the integration owner MUST compose this evaluator's
|
|
30
|
+
* {@link PolicyVerdict} with Team C's share evaluator:
|
|
31
|
+
*
|
|
32
|
+
* 1. `explicitDeny` (policy) ⇒ `deny(explicit_deny_policy)` — deny-wins, final.
|
|
33
|
+
* 2. else if `policyAllow OR shareGrant` ⇒ `allow`.
|
|
34
|
+
* 3. else ⇒ `deny(no_record_permit)`.
|
|
35
|
+
*
|
|
36
|
+
* Do NOT ship `createRecordPolicyGate` as the final gate 6 — it ignores shares.
|
|
37
|
+
*
|
|
38
|
+
* ── ADR-0016 (transient-vs-deny) ───────────────────────────────────────────────
|
|
39
|
+
* The port distinguishes data-absence (`{allow:false, explicitDeny:false}` — a
|
|
40
|
+
* normal Deny input) from a transient outage (throws
|
|
41
|
+
* {@link AuthorizationUnavailableError}). This evaluator NEVER catches: an
|
|
42
|
+
* `AuthorizationUnavailableError` propagates (the pipeline re-throws it → the
|
|
43
|
+
* request fails closed as 503-retryable), and any OTHER throw also propagates (the
|
|
44
|
+
* pipeline's top-level catch fail-closes it to `deny_by_default`). A throw is NEVER
|
|
45
|
+
* swallowed into a permit.
|
|
46
|
+
*
|
|
47
|
+
* ── STRICT `=== true` (the Layer-5/6 fail-open bug class) ──────────────────────
|
|
48
|
+
* BOTH port booleans route through {@link strictTrue}. A truthy-non-boolean
|
|
49
|
+
* `allow` (`"true"`, `1`) is NOT a permit. A truthy-non-boolean `explicitDeny` is
|
|
50
|
+
* NOT a deny — only a real boolean `true` denies. This is fail-CLOSED overall: a
|
|
51
|
+
* non-boolean `allow` cannot open the gate, and a non-boolean `explicitDeny` that
|
|
52
|
+
* doesn't count as a deny still leaves the policy SILENT (no allow either) ⇒ the
|
|
53
|
+
* policy-only gate denies `no_record_permit`. There is no input that fails open.
|
|
54
|
+
*
|
|
55
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports; zero-dep.
|
|
56
|
+
*/
|
|
57
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
58
|
+
exports.evaluateRecordPolicy = evaluateRecordPolicy;
|
|
59
|
+
exports.createRecordPolicyGate = createRecordPolicyGate;
|
|
60
|
+
const decision_authority_js_1 = require("../decision-authority.js");
|
|
61
|
+
const reason_codes_js_1 = require("../reason-codes.js");
|
|
62
|
+
/**
|
|
63
|
+
* PURE record-policy evaluator (gate 6 policy half, ADR-0005, deny-wins).
|
|
64
|
+
*
|
|
65
|
+
* Calls {@link RecordPermissionRulePort.evaluate} with the record-level question and
|
|
66
|
+
* resolves its {@link RulePermit} into a {@link PolicyVerdict} under STRICT
|
|
67
|
+
* `=== true` + deny-wins:
|
|
68
|
+
*
|
|
69
|
+
* - `explicitDeny = strictTrue(result.explicitDeny)` — only a real `true` denies.
|
|
70
|
+
* - `allow = strictTrue(result.allow) && !explicitDeny` — a real allow that an
|
|
71
|
+
* explicit deny has NOT overridden.
|
|
72
|
+
*
|
|
73
|
+
* Does NOT compose with shares and does NOT produce a {@link GateOutcome} — the
|
|
74
|
+
* integration owner unions this with Team C's share evaluator (see the module
|
|
75
|
+
* contract note). Propagates every throw (ADR-0016): an
|
|
76
|
+
* {@link AuthorizationUnavailableError} from the port bubbles up so the pipeline
|
|
77
|
+
* re-throws it; any other throw bubbles up so the pipeline fail-closes it. NEVER
|
|
78
|
+
* swallows a throw into a permit.
|
|
79
|
+
*
|
|
80
|
+
* @param ctx the shared gate context; the port + record resource + attributes are
|
|
81
|
+
* read from it (`ctx.deps.recordRule`, `ctx.resource`, `ctx.context.attributes`).
|
|
82
|
+
*/
|
|
83
|
+
async function evaluateRecordPolicy(ctx) {
|
|
84
|
+
// Build the input, OMITTING `attributes` when absent (exactOptionalPropertyTypes:
|
|
85
|
+
// an explicit `attributes: undefined` is not assignable to the optional prop).
|
|
86
|
+
const input = {
|
|
87
|
+
principal: ctx.principal,
|
|
88
|
+
action: ctx.action,
|
|
89
|
+
resource: ctx.resource,
|
|
90
|
+
...(ctx.context.attributes === undefined ? {} : { attributes: ctx.context.attributes }),
|
|
91
|
+
};
|
|
92
|
+
const result = await ctx.deps.recordRule.evaluate(input);
|
|
93
|
+
// Deny-wins, resolved at the seam under STRICT === true.
|
|
94
|
+
const explicitDeny = (0, reason_codes_js_1.strictTrue)(result.explicitDeny);
|
|
95
|
+
const allow = (0, reason_codes_js_1.strictTrue)(result.allow) && !explicitDeny;
|
|
96
|
+
return { allow, explicitDeny };
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Build a POLICY-ONLY {@link RecordPermitFn} (gate 6) from {@link evaluateRecordPolicy}.
|
|
100
|
+
*
|
|
101
|
+
* ⚠️ NOT THE FINAL GATE 6 — this maps the policy verdict alone to a
|
|
102
|
+
* {@link GateOutcome}; it does NOT consider shares / cross-tenant grants. Use it for
|
|
103
|
+
* policy-only wiring / independent testing; the integration owner composes
|
|
104
|
+
* {@link evaluateRecordPolicy} with Team C's share evaluator for the real gate 6
|
|
105
|
+
* (see the module contract note).
|
|
106
|
+
*
|
|
107
|
+
* Mapping (policy-only):
|
|
108
|
+
* - `explicitDeny` ⇒ `deny(explicit_deny_policy)` (deny-wins).
|
|
109
|
+
* - `allow` (and not `explicitDeny`) ⇒ `allow`.
|
|
110
|
+
* - policy silent (neither) ⇒ `deny(no_record_permit)` (a share COULD still permit
|
|
111
|
+
* in the integrated gate — this policy-only view does not see it).
|
|
112
|
+
*
|
|
113
|
+
* Every throw propagates (ADR-0016) — see {@link evaluateRecordPolicy}.
|
|
114
|
+
*/
|
|
115
|
+
function createRecordPolicyGate() {
|
|
116
|
+
return async (ctx) => {
|
|
117
|
+
const verdict = await evaluateRecordPolicy(ctx);
|
|
118
|
+
if (verdict.explicitDeny) {
|
|
119
|
+
return (0, decision_authority_js_1.gateDeny)((0, reason_codes_js_1.denyExplicitPolicy)());
|
|
120
|
+
}
|
|
121
|
+
if (verdict.allow) {
|
|
122
|
+
return (0, decision_authority_js_1.gateAllow)();
|
|
123
|
+
}
|
|
124
|
+
// Policy silent — no permit from policy. In the integrated gate a share may
|
|
125
|
+
// still permit; the policy-only gate denies no_record_permit.
|
|
126
|
+
return (0, decision_authority_js_1.gateDeny)((0, reason_codes_js_1.denyNoRecordPermit)());
|
|
127
|
+
};
|
|
128
|
+
}
|