@astram/core 0.1.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 (259) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +47 -0
  3. package/dist/admin/admin-service.d.ts +221 -0
  4. package/dist/admin/admin-service.d.ts.map +1 -0
  5. package/dist/admin/admin-service.js +155 -0
  6. package/dist/admin/admin-service.js.map +1 -0
  7. package/dist/admin/command/grants.d.ts +40 -0
  8. package/dist/admin/command/grants.d.ts.map +1 -0
  9. package/dist/admin/command/grants.js +196 -0
  10. package/dist/admin/command/grants.js.map +1 -0
  11. package/dist/admin/command/index.d.ts +23 -0
  12. package/dist/admin/command/index.d.ts.map +1 -0
  13. package/dist/admin/command/index.js +23 -0
  14. package/dist/admin/command/index.js.map +1 -0
  15. package/dist/admin/command/invitations.d.ts +72 -0
  16. package/dist/admin/command/invitations.d.ts.map +1 -0
  17. package/dist/admin/command/invitations.js +241 -0
  18. package/dist/admin/command/invitations.js.map +1 -0
  19. package/dist/admin/command/permissions.d.ts +33 -0
  20. package/dist/admin/command/permissions.d.ts.map +1 -0
  21. package/dist/admin/command/permissions.js +67 -0
  22. package/dist/admin/command/permissions.js.map +1 -0
  23. package/dist/admin/command/pipeline.d.ts +79 -0
  24. package/dist/admin/command/pipeline.d.ts.map +1 -0
  25. package/dist/admin/command/pipeline.js +106 -0
  26. package/dist/admin/command/pipeline.js.map +1 -0
  27. package/dist/admin/command/policies.d.ts +50 -0
  28. package/dist/admin/command/policies.d.ts.map +1 -0
  29. package/dist/admin/command/policies.js +219 -0
  30. package/dist/admin/command/policies.js.map +1 -0
  31. package/dist/admin/command/service-accounts.d.ts +58 -0
  32. package/dist/admin/command/service-accounts.d.ts.map +1 -0
  33. package/dist/admin/command/service-accounts.js +272 -0
  34. package/dist/admin/command/service-accounts.js.map +1 -0
  35. package/dist/admin/command/service.d.ts +65 -0
  36. package/dist/admin/command/service.d.ts.map +1 -0
  37. package/dist/admin/command/service.js +128 -0
  38. package/dist/admin/command/service.js.map +1 -0
  39. package/dist/admin/command/tiers.d.ts +40 -0
  40. package/dist/admin/command/tiers.d.ts.map +1 -0
  41. package/dist/admin/command/tiers.js +79 -0
  42. package/dist/admin/command/tiers.js.map +1 -0
  43. package/dist/admin/index.d.ts +36 -0
  44. package/dist/admin/index.d.ts.map +1 -0
  45. package/dist/admin/index.js +63 -0
  46. package/dist/admin/index.js.map +1 -0
  47. package/dist/admin/ports.d.ts +288 -0
  48. package/dist/admin/ports.d.ts.map +1 -0
  49. package/dist/admin/ports.js +39 -0
  50. package/dist/admin/ports.js.map +1 -0
  51. package/dist/admin/query/audit-view.d.ts +52 -0
  52. package/dist/admin/query/audit-view.d.ts.map +1 -0
  53. package/dist/admin/query/audit-view.js +84 -0
  54. package/dist/admin/query/audit-view.js.map +1 -0
  55. package/dist/admin/query/catalog.d.ts +70 -0
  56. package/dist/admin/query/catalog.d.ts.map +1 -0
  57. package/dist/admin/query/catalog.js +169 -0
  58. package/dist/admin/query/catalog.js.map +1 -0
  59. package/dist/admin/query/explain.d.ts +52 -0
  60. package/dist/admin/query/explain.d.ts.map +1 -0
  61. package/dist/admin/query/explain.js +145 -0
  62. package/dist/admin/query/explain.js.map +1 -0
  63. package/dist/admin/query/index.d.ts +45 -0
  64. package/dist/admin/query/index.d.ts.map +1 -0
  65. package/dist/admin/query/index.js +58 -0
  66. package/dist/admin/query/index.js.map +1 -0
  67. package/dist/admin/query/simulate.d.ts +42 -0
  68. package/dist/admin/query/simulate.d.ts.map +1 -0
  69. package/dist/admin/query/simulate.js +91 -0
  70. package/dist/admin/query/simulate.js.map +1 -0
  71. package/dist/admin/safeguards-impl.d.ts +67 -0
  72. package/dist/admin/safeguards-impl.d.ts.map +1 -0
  73. package/dist/admin/safeguards-impl.js +318 -0
  74. package/dist/admin/safeguards-impl.js.map +1 -0
  75. package/dist/admin/safeguards.d.ts +89 -0
  76. package/dist/admin/safeguards.d.ts.map +1 -0
  77. package/dist/admin/safeguards.js +91 -0
  78. package/dist/admin/safeguards.js.map +1 -0
  79. package/dist/admin/types.d.ts +485 -0
  80. package/dist/admin/types.d.ts.map +1 -0
  81. package/dist/admin/types.js +38 -0
  82. package/dist/admin/types.js.map +1 -0
  83. package/dist/audit/audit-recorder.d.ts +129 -0
  84. package/dist/audit/audit-recorder.d.ts.map +1 -0
  85. package/dist/audit/audit-recorder.js +265 -0
  86. package/dist/audit/audit-recorder.js.map +1 -0
  87. package/dist/audit/canonicalize.d.ts +38 -0
  88. package/dist/audit/canonicalize.d.ts.map +1 -0
  89. package/dist/audit/canonicalize.js +174 -0
  90. package/dist/audit/canonicalize.js.map +1 -0
  91. package/dist/audit/errors.d.ts +56 -0
  92. package/dist/audit/errors.d.ts.map +1 -0
  93. package/dist/audit/errors.js +41 -0
  94. package/dist/audit/errors.js.map +1 -0
  95. package/dist/audit/index.d.ts +45 -0
  96. package/dist/audit/index.d.ts.map +1 -0
  97. package/dist/audit/index.js +24 -0
  98. package/dist/audit/index.js.map +1 -0
  99. package/dist/audit/ports.d.ts +131 -0
  100. package/dist/audit/ports.d.ts.map +1 -0
  101. package/dist/audit/ports.js +27 -0
  102. package/dist/audit/ports.js.map +1 -0
  103. package/dist/audit/sensitive.d.ts +68 -0
  104. package/dist/audit/sensitive.d.ts.map +1 -0
  105. package/dist/audit/sensitive.js +159 -0
  106. package/dist/audit/sensitive.js.map +1 -0
  107. package/dist/audit/walk.d.ts +109 -0
  108. package/dist/audit/walk.d.ts.map +1 -0
  109. package/dist/audit/walk.js +117 -0
  110. package/dist/audit/walk.js.map +1 -0
  111. package/dist/authentication/authenticator.d.ts +74 -0
  112. package/dist/authentication/authenticator.d.ts.map +1 -0
  113. package/dist/authentication/authenticator.js +208 -0
  114. package/dist/authentication/authenticator.js.map +1 -0
  115. package/dist/authentication/credential-resolver.d.ts +76 -0
  116. package/dist/authentication/credential-resolver.d.ts.map +1 -0
  117. package/dist/authentication/credential-resolver.js +21 -0
  118. package/dist/authentication/credential-resolver.js.map +1 -0
  119. package/dist/authentication/index.d.ts +52 -0
  120. package/dist/authentication/index.d.ts.map +1 -0
  121. package/dist/authentication/index.js +27 -0
  122. package/dist/authentication/index.js.map +1 -0
  123. package/dist/authentication/password-hasher.d.ts +41 -0
  124. package/dist/authentication/password-hasher.d.ts.map +1 -0
  125. package/dist/authentication/password-hasher.js +21 -0
  126. package/dist/authentication/password-hasher.js.map +1 -0
  127. package/dist/authentication/totp.d.ts +101 -0
  128. package/dist/authentication/totp.d.ts.map +1 -0
  129. package/dist/authentication/totp.js +151 -0
  130. package/dist/authentication/totp.js.map +1 -0
  131. package/dist/authorization/cross-tenant/grant-evaluator.d.ts +90 -0
  132. package/dist/authorization/cross-tenant/grant-evaluator.d.ts.map +1 -0
  133. package/dist/authorization/cross-tenant/grant-evaluator.js +165 -0
  134. package/dist/authorization/cross-tenant/grant-evaluator.js.map +1 -0
  135. package/dist/authorization/decision-authority.d.ts +212 -0
  136. package/dist/authorization/decision-authority.d.ts.map +1 -0
  137. package/dist/authorization/decision-authority.js +200 -0
  138. package/dist/authorization/decision-authority.js.map +1 -0
  139. package/dist/authorization/default-gates.d.ts +41 -0
  140. package/dist/authorization/default-gates.d.ts.map +1 -0
  141. package/dist/authorization/default-gates.js +47 -0
  142. package/dist/authorization/default-gates.js.map +1 -0
  143. package/dist/authorization/delegation/operation-identity.d.ts +132 -0
  144. package/dist/authorization/delegation/operation-identity.d.ts.map +1 -0
  145. package/dist/authorization/delegation/operation-identity.js +220 -0
  146. package/dist/authorization/delegation/operation-identity.js.map +1 -0
  147. package/dist/authorization/errors.d.ts +54 -0
  148. package/dist/authorization/errors.d.ts.map +1 -0
  149. package/dist/authorization/errors.js +63 -0
  150. package/dist/authorization/errors.js.map +1 -0
  151. package/dist/authorization/gates/object-type.d.ts +63 -0
  152. package/dist/authorization/gates/object-type.d.ts.map +1 -0
  153. package/dist/authorization/gates/object-type.js +177 -0
  154. package/dist/authorization/gates/object-type.js.map +1 -0
  155. package/dist/authorization/gates/tenant.d.ts +50 -0
  156. package/dist/authorization/gates/tenant.d.ts.map +1 -0
  157. package/dist/authorization/gates/tenant.js +87 -0
  158. package/dist/authorization/gates/tenant.js.map +1 -0
  159. package/dist/authorization/gates/wiring.d.ts +57 -0
  160. package/dist/authorization/gates/wiring.d.ts.map +1 -0
  161. package/dist/authorization/gates/wiring.js +56 -0
  162. package/dist/authorization/gates/wiring.js.map +1 -0
  163. package/dist/authorization/index.d.ts +136 -0
  164. package/dist/authorization/index.d.ts.map +1 -0
  165. package/dist/authorization/index.js +82 -0
  166. package/dist/authorization/index.js.map +1 -0
  167. package/dist/authorization/ports.d.ts +271 -0
  168. package/dist/authorization/ports.d.ts.map +1 -0
  169. package/dist/authorization/ports.js +36 -0
  170. package/dist/authorization/ports.js.map +1 -0
  171. package/dist/authorization/reason-codes.d.ts +75 -0
  172. package/dist/authorization/reason-codes.d.ts.map +1 -0
  173. package/dist/authorization/reason-codes.js +98 -0
  174. package/dist/authorization/reason-codes.js.map +1 -0
  175. package/dist/authorization/record/permit-tier.d.ts +57 -0
  176. package/dist/authorization/record/permit-tier.d.ts.map +1 -0
  177. package/dist/authorization/record/permit-tier.js +79 -0
  178. package/dist/authorization/record/permit-tier.js.map +1 -0
  179. package/dist/authorization/record/rule-evaluator.d.ts +116 -0
  180. package/dist/authorization/record/rule-evaluator.d.ts.map +1 -0
  181. package/dist/authorization/record/rule-evaluator.js +125 -0
  182. package/dist/authorization/record/rule-evaluator.js.map +1 -0
  183. package/dist/authorization/record/shares.d.ts +70 -0
  184. package/dist/authorization/record/shares.d.ts.map +1 -0
  185. package/dist/authorization/record/shares.js +107 -0
  186. package/dist/authorization/record/shares.js.map +1 -0
  187. package/dist/identity/broker.d.ts +59 -0
  188. package/dist/identity/broker.d.ts.map +1 -0
  189. package/dist/identity/broker.js +135 -0
  190. package/dist/identity/broker.js.map +1 -0
  191. package/dist/identity/claim-mapper.d.ts +84 -0
  192. package/dist/identity/claim-mapper.d.ts.map +1 -0
  193. package/dist/identity/claim-mapper.js +48 -0
  194. package/dist/identity/claim-mapper.js.map +1 -0
  195. package/dist/identity/index.d.ts +60 -0
  196. package/dist/identity/index.d.ts.map +1 -0
  197. package/dist/identity/index.js +24 -0
  198. package/dist/identity/index.js.map +1 -0
  199. package/dist/identity/internal-adapter.d.ts +54 -0
  200. package/dist/identity/internal-adapter.d.ts.map +1 -0
  201. package/dist/identity/internal-adapter.js +88 -0
  202. package/dist/identity/internal-adapter.js.map +1 -0
  203. package/dist/index.d.ts +43 -0
  204. package/dist/index.d.ts.map +1 -0
  205. package/dist/index.js +48 -0
  206. package/dist/index.js.map +1 -0
  207. package/dist/metadata/completeness.d.ts +136 -0
  208. package/dist/metadata/completeness.d.ts.map +1 -0
  209. package/dist/metadata/completeness.js +106 -0
  210. package/dist/metadata/completeness.js.map +1 -0
  211. package/dist/metadata/dependencies.d.ts +114 -0
  212. package/dist/metadata/dependencies.d.ts.map +1 -0
  213. package/dist/metadata/dependencies.js +105 -0
  214. package/dist/metadata/dependencies.js.map +1 -0
  215. package/dist/metadata/index.d.ts +153 -0
  216. package/dist/metadata/index.d.ts.map +1 -0
  217. package/dist/metadata/index.js +51 -0
  218. package/dist/metadata/index.js.map +1 -0
  219. package/dist/metadata/registry.d.ts +23 -0
  220. package/dist/metadata/registry.d.ts.map +1 -0
  221. package/dist/metadata/registry.js +204 -0
  222. package/dist/metadata/registry.js.map +1 -0
  223. package/dist/risk/evaluator.d.ts +76 -0
  224. package/dist/risk/evaluator.d.ts.map +1 -0
  225. package/dist/risk/evaluator.js +59 -0
  226. package/dist/risk/evaluator.js.map +1 -0
  227. package/dist/risk/index.d.ts +40 -0
  228. package/dist/risk/index.d.ts.map +1 -0
  229. package/dist/risk/index.js +20 -0
  230. package/dist/risk/index.js.map +1 -0
  231. package/dist/risk/risk-engine.d.ts +76 -0
  232. package/dist/risk/risk-engine.d.ts.map +1 -0
  233. package/dist/risk/risk-engine.js +287 -0
  234. package/dist/risk/risk-engine.js.map +1 -0
  235. package/dist/risk/signals.d.ts +60 -0
  236. package/dist/risk/signals.d.ts.map +1 -0
  237. package/dist/risk/signals.js +104 -0
  238. package/dist/risk/signals.js.map +1 -0
  239. package/dist/risk/thresholds.d.ts +54 -0
  240. package/dist/risk/thresholds.d.ts.map +1 -0
  241. package/dist/risk/thresholds.js +60 -0
  242. package/dist/risk/thresholds.js.map +1 -0
  243. package/dist/session/index.d.ts +47 -0
  244. package/dist/session/index.d.ts.map +1 -0
  245. package/dist/session/index.js +20 -0
  246. package/dist/session/index.js.map +1 -0
  247. package/dist/session/lifetimes.d.ts +50 -0
  248. package/dist/session/lifetimes.d.ts.map +1 -0
  249. package/dist/session/lifetimes.js +67 -0
  250. package/dist/session/lifetimes.js.map +1 -0
  251. package/dist/session/ports.d.ts +266 -0
  252. package/dist/session/ports.d.ts.map +1 -0
  253. package/dist/session/ports.js +23 -0
  254. package/dist/session/ports.js.map +1 -0
  255. package/dist/session/session-authority.d.ts +97 -0
  256. package/dist/session/session-authority.d.ts.map +1 -0
  257. package/dist/session/session-authority.js +393 -0
  258. package/dist/session/session-authority.js.map +1 -0
  259. package/package.json +32 -0
@@ -0,0 +1,287 @@
1
+ /**
2
+ * The **Risk engine** — the body behind the FROZEN {@link RiskEngine} seam
3
+ * (`./index.ts`). SCORES a request from its signals into a `RiskScore` VALUE, and
4
+ * exposes a SEPARATE advisory `recommendStepUp` derivation (ADR-0017). Mirrors the
5
+ * Authentication / Session engines EXACTLY: a pure `createRiskEngine(...)` factory
6
+ * over injected capabilities (evaluators + thresholds), a stateless impl, STRICT
7
+ * gates, a top-level try/catch → a FAIL-SAFE result, no logging.
8
+ *
9
+ * ⛔ THE ASSESS-vs-ENFORCE LINE (this layer's whole point):
10
+ * Risk ASSESSES + RECOMMENDS; it NEVER ENFORCES. `assess()` returns a `RiskScore`
11
+ * VALUE — it NEVER blocks, mints, denies, or throws-in-a-way-that-acts-as-a-hard-
12
+ * deny. `recommendStepUp` is a PURE derivation OVER a `RiskScore`, NOT a field on
13
+ * the frozen type and NOT an enforcement action — advice a later layer reads.
14
+ *
15
+ * ⛔ THE FAIL-SAFE DIRECTION (uncertainty ⇒ HIGHER risk, NEVER "allow"):
16
+ * An unknown signal (no evaluator), a malformed value bag, or an evaluator that
17
+ * THROWS is treated as ELEVATING — it raises the score, never lowers or drops it.
18
+ * BUT fail-safe here = advise HARDER (higher value / recommend step-up), NOT
19
+ * throw-to-deny. `assess()` ALWAYS RESOLVES to a `RiskScore` — the outer catch
20
+ * yields a fail-safe HIGH-ish score, never an exception.
21
+ *
22
+ * ⛔ DETERMINISM: same `(principal, signals)` ⇒ same `RiskScore`, every call. NO
23
+ * wall-clock, NO RNG, NO order-dependence — aggregation is a commutative combine.
24
+ *
25
+ * ⛔ MONOTONICITY: adding a risky signal never DECREASES `value`; a superset of
26
+ * signals never scores lower than its subset. The combine is non-decreasing.
27
+ *
28
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
29
+ */
30
+ import { RiskSignalEvaluatorRegistry, clampContribution, } from './evaluator.js';
31
+ import { DEFAULT_RISK_EVALUATORS, FAIL_SAFE_CONTRIBUTION } from './signals.js';
32
+ import { DEFAULT_RISK_THRESHOLDS, resolveThresholds, } from './thresholds.js';
33
+ /**
34
+ * The contribution assigned when a signal CANNOT be evaluated (no registered
35
+ * evaluator, or an evaluator threw). Equal to the reference fail-safe floor — an
36
+ * un-evaluable signal ELEVATES, never lowers or drops. This is the fail-safe
37
+ * DIRECTION expressed at the engine level.
38
+ */
39
+ const UNEVALUABLE_CONTRIBUTION = FAIL_SAFE_CONTRIBUTION;
40
+ /**
41
+ * The score `assess()` resolves to when the WHOLE assessment throws unexpectedly
42
+ * (the top-level catch). A fail-safe HIGH score with no explainable kinds — Risk
43
+ * still RESOLVES (never throws-to-deny), but advises maximally hard.
44
+ */
45
+ const FAIL_SAFE_SCORE = Object.freeze({
46
+ level: 'high',
47
+ value: 1,
48
+ contributingSignals: Object.freeze([]),
49
+ });
50
+ /**
51
+ * Aggregate contributions into a single `0..1` value, DETERMINISTICALLY and
52
+ * ORDER-INDEPENDENTLY. Uses a BOUNDED probabilistic-OR combine:
53
+ *
54
+ * value = 1 − Π(1 − cᵢ)
55
+ *
56
+ * Properties this gives us for free (all required by the layer):
57
+ * - RANGE: always in `[0,1]` (each factor `(1−cᵢ) ∈ [0,1]`, product ∈ `[0,1]`).
58
+ * - COMMUTATIVE: multiplication is order-independent in exact arithmetic. ⚠️ IEEE-754
59
+ * multiplication is commutative but NOT associative, so a naive left-fold differs
60
+ * by ~1 ULP with input order — and `RiskScore.value` is audit-loggable, so it must
61
+ * be BYTE-identical for a given multiset. We therefore CANONICALIZE the fold:
62
+ * SORT the (clamped) contributions ascending so the multiplication order is fixed
63
+ * for a given multiset, THEN round the result to 1e-9 to kill any residual FP drift.
64
+ * Result: same multiset (any input order) ⇒ identical `value`, to the bit.
65
+ * - MONOTONIC: every factor `(1−cᵢ) ≤ 1`, so adding ANY signal multiplies the
66
+ * product by `≤ 1` ⇒ the product never grows ⇒ `value` never DECREASES. A
67
+ * zero-risk signal (`c=0`) multiplies by exactly 1 ⇒ no change (never lowers).
68
+ * (Sorting doesn't touch this — the multiset of factors is unchanged; the 1e-9
69
+ * round is coarse enough never to invert the order of two genuinely-distinct scores.)
70
+ * - No single signal can drop the score below the running floor.
71
+ */
72
+ function combine(contributions) {
73
+ // Clamp each contribution to [0,1] (defense-in-depth; the caller already clamps),
74
+ // then SORT ascending so the fold order is deterministic for a given multiset —
75
+ // making `value` order-independent to the BIT (IEEE-754 is commutative but not
76
+ // associative, so a fixed order is required, not just commutativity).
77
+ const sorted = contributions
78
+ .map((c) => (c < 0 ? 0 : c > 1 ? 1 : c))
79
+ .sort((a, b) => a - b);
80
+ let complement = 1;
81
+ for (const c of sorted) {
82
+ complement *= 1 - c;
83
+ }
84
+ let value = 1 - complement;
85
+ // Clamp against floating-point drift so the contract's [0,1] is exact.
86
+ value = value < 0 ? 0 : value > 1 ? 1 : value;
87
+ // Round to 1e-9 to eliminate residual FP drift ⇒ a byte-reproducible, audit-stable
88
+ // value for the same multiset. Coarse enough never to flip a band/threshold.
89
+ return Math.round(value * 1e9) / 1e9;
90
+ }
91
+ /**
92
+ * Map an aggregated `value` onto a coarse `RiskLevel` via the band cutoffs.
93
+ *
94
+ * ⛔ FAIL-SAFE BOUNDARY: a value exactly AT a cutoff maps to the HIGHER-risk band
95
+ * (`>=`), never rounding down into a calmer band. `high` at `>= highCutoff`;
96
+ * `medium` at `>= mediumCutoff`; else `low`.
97
+ */
98
+ function bandFor(value, thresholds) {
99
+ if (value >= thresholds.highCutoff)
100
+ return 'high';
101
+ if (value >= thresholds.mediumCutoff)
102
+ return 'medium';
103
+ return 'low';
104
+ }
105
+ /**
106
+ * Route one signal to its evaluator and score it. Selection is DETERMINISTIC:
107
+ * - an explicit `value.__evaluator` string names the evaluator to use;
108
+ * - otherwise the signal's `kind` is used as the default evaluator name lookup
109
+ * (the reference evaluators register under a name AND cover a kind — the engine
110
+ * falls back to a per-kind default registered by that convention).
111
+ *
112
+ * FAIL-SAFE: no evaluator found, a kind/evaluator mismatch, a non-number
113
+ * contribution, OR the evaluator THROWS (including a `.evaluate` that is not a
114
+ * function, or a throwing property getter on `signal.kind`/`.value`/`.__evaluator`)
115
+ * ⇒ the {@link UNEVALUABLE_CONTRIBUTION} elevation attributed to the signal's
116
+ * declared kind. Never throws, never drops.
117
+ *
118
+ * ⚠️ ATTRIBUTION OF UN-KINDED JUNK (by design): a signal whose `kind` is missing,
119
+ * non-string, or throws when read is attributed to `'context'` for the
120
+ * `contributingSignals` list. This is an ARBITRARY-but-stable bucket — the point is
121
+ * that the elevation is still explainable somewhere; there is no "unknown" kind in
122
+ * the frozen `RiskSignalKind` union to attribute it to.
123
+ */
124
+ function scoreSignal(signal, registry, defaultByKind) {
125
+ // Determine the signal's declared kind for attribution (fail-safe even if junk).
126
+ // Wrapped: reading `.kind` may itself THROW (a hostile getter) — that must NOT
127
+ // escape scoreSignal; it degrades to the 'context' bucket + a fail-safe elevate.
128
+ const declaredKind = safeDeclaredKind(signal);
129
+ try {
130
+ const bag = signal?.value;
131
+ const selector = typeof bag?.__evaluator === 'string' && bag.__evaluator.length > 0
132
+ ? bag.__evaluator
133
+ : undefined;
134
+ // Resolve: explicit selector wins; else the per-kind default evaluator.
135
+ const evaluator = (selector !== undefined ? registry.resolve(selector) : undefined) ??
136
+ defaultByKind.get(declaredKind);
137
+ // UNKNOWN signal — no evaluator for this kind/name ⇒ ELEVATE (never ignore).
138
+ // A resolved-but-malformed evaluator whose `.evaluate` is not callable is
139
+ // likewise treated as "cannot evaluate" ⇒ elevate (the call below would throw
140
+ // and be caught anyway; this makes the intent explicit and the path direct).
141
+ if (evaluator === undefined || typeof evaluator.evaluate !== 'function') {
142
+ return { contribution: UNEVALUABLE_CONTRIBUTION, kind: declaredKind };
143
+ }
144
+ const verdict = evaluator.evaluate(signal);
145
+ // STRICT: a well-formed verdict must carry a numeric contribution. A hostile /
146
+ // misbehaving evaluator returning a truthy non-number ⇒ fail-safe elevate
147
+ // (clampContribution maps a non-number to the fail-safe floor), NOT fail-open.
148
+ const contribution = clampContribution(verdict?.contribution, UNEVALUABLE_CONTRIBUTION);
149
+ const kind = typeof verdict?.kind === 'string'
150
+ ? verdict.kind
151
+ : declaredKind;
152
+ return { contribution, kind };
153
+ }
154
+ catch {
155
+ // An evaluator THREW ⇒ uncertainty ⇒ ELEVATE (never lower / drop / rethrow).
156
+ return { contribution: UNEVALUABLE_CONTRIBUTION, kind: declaredKind };
157
+ }
158
+ }
159
+ /**
160
+ * Read a signal's declared `kind` for attribution WITHOUT ever throwing. A hostile
161
+ * signal can expose `kind` as a getter that throws — reading it must not escape.
162
+ * A missing / non-string / throwing kind degrades to the `'context'` bucket (see
163
+ * {@link scoreSignal}'s attribution note).
164
+ */
165
+ function safeDeclaredKind(signal) {
166
+ try {
167
+ if (signal !== null &&
168
+ typeof signal === 'object' &&
169
+ typeof signal.kind === 'string') {
170
+ return signal.kind;
171
+ }
172
+ }
173
+ catch {
174
+ // A throwing `kind` getter → fall through to the stable bucket.
175
+ }
176
+ return 'context';
177
+ }
178
+ /**
179
+ * The concrete engine. Stateless per call; holds only its injected capabilities.
180
+ * Instances come from {@link createRiskEngine}.
181
+ */
182
+ class RiskEngineImpl {
183
+ registry;
184
+ defaultByKind;
185
+ thresholds;
186
+ constructor(registry, defaultByKind, thresholds) {
187
+ this.registry = registry;
188
+ this.defaultByKind = defaultByKind;
189
+ this.thresholds = thresholds;
190
+ }
191
+ assess(_principal, signals) {
192
+ // NOTE: `_principal` is part of the FROZEN seam signature. Step-7 signals are
193
+ // computable from the RiskSignal facts alone; the principal is reserved for
194
+ // future principal-aware signals (e.g. role-weighted risk) — unused today.
195
+ try {
196
+ return Promise.resolve(this.doAssess(signals));
197
+ }
198
+ catch {
199
+ // FAIL-SAFE (not fail-open, not throw-to-deny): ANY unexpected throw ⇒ RESOLVE
200
+ // to a fail-safe HIGH score. Risk never throws in a way that acts as a deny.
201
+ return Promise.resolve(FAIL_SAFE_SCORE);
202
+ }
203
+ }
204
+ doAssess(signals) {
205
+ // No signals (or a non-array) ⇒ a genuine LOW score (0). Absence of signal is
206
+ // not uncertainty — there is simply nothing elevating. (An UNKNOWN signal, by
207
+ // contrast, IS uncertainty and elevates — see scoreSignal.)
208
+ const list = Array.isArray(signals) ? signals : [];
209
+ const contributions = [];
210
+ // Collect the DISTINCT kinds that pushed the score up, for explainability. A
211
+ // Set keeps this order-independent; we materialize it deterministically below.
212
+ const contributingKinds = new Set();
213
+ for (const signal of list) {
214
+ const { contribution, kind } = scoreSignal(signal, this.registry, this.defaultByKind);
215
+ contributions.push(contribution);
216
+ if (contribution > 0) {
217
+ contributingKinds.add(kind);
218
+ }
219
+ }
220
+ const value = combine(contributions);
221
+ const level = bandFor(value, this.thresholds);
222
+ // Deterministic ordering of the explainability list (canonical kind order),
223
+ // so the same inputs always yield an identical RiskScore.
224
+ const contributingSignals = CANONICAL_KIND_ORDER.filter((k) => contributingKinds.has(k));
225
+ return Object.freeze({
226
+ level,
227
+ value,
228
+ contributingSignals: Object.freeze(contributingSignals),
229
+ });
230
+ }
231
+ }
232
+ /** Canonical kind order for the deterministic `contributingSignals` list. */
233
+ const CANONICAL_KIND_ORDER = Object.freeze([
234
+ 'device',
235
+ 'behaviour',
236
+ 'context',
237
+ 'network',
238
+ ]);
239
+ /**
240
+ * Wire the injected capabilities and return an object implementing the FROZEN
241
+ * {@link RiskEngine}. The engine's public entry point (mirrors `createAuthenticator`
242
+ * / `createSessionAuthority`). Validates the threshold config eagerly at
243
+ * construction (throws LOUD on a nonsensical config — construction-time).
244
+ */
245
+ export function createRiskEngine(options = {}) {
246
+ const thresholds = resolveThresholds(options.thresholds);
247
+ const registry = new RiskSignalEvaluatorRegistry();
248
+ const defaultByKind = new Map();
249
+ const evaluators = options.evaluators ?? DEFAULT_RISK_EVALUATORS;
250
+ for (const evaluator of evaluators) {
251
+ registry.register(evaluator); // duplicate NAME → throws LOUD (fail-closed)
252
+ // First evaluator registered for a kind becomes that kind's default router
253
+ // target (a later one keeps its NAME-based selectability but not the default).
254
+ if (!defaultByKind.has(evaluator.kind)) {
255
+ defaultByKind.set(evaluator.kind, evaluator);
256
+ }
257
+ }
258
+ return new RiskEngineImpl(registry, defaultByKind, thresholds);
259
+ }
260
+ /**
261
+ * The ADR-0017 **step-up recommendation** — a SEPARATE, PURE derivation OVER a
262
+ * {@link RiskScore}. Returns `true` when the score's `level === 'high'` OR its
263
+ * `value >= stepUpThreshold`.
264
+ *
265
+ * ⛔ THIS IS ADVISORY, NOT ENFORCEMENT. It is NOT a field on the frozen `RiskScore`
266
+ * (which has no recommendation/stepUp field — semver, ADR-0023) and it takes NO
267
+ * action. A later layer (Auth performs `mfa_required`; the SDK does UX) READS this
268
+ * advice and decides. Risk never blocks/mints/denies.
269
+ *
270
+ * FAIL-SAFE: a malformed / missing score (null, non-object, non-number value) ⇒
271
+ * recommend step-up (`true`) — uncertainty advises HARDER, never silently `false`.
272
+ */
273
+ export function recommendStepUp(score, config) {
274
+ const stepUpThreshold = typeof config?.stepUpThreshold === 'number' && Number.isFinite(config.stepUpThreshold)
275
+ ? config.stepUpThreshold
276
+ : DEFAULT_RISK_THRESHOLDS.stepUpThreshold;
277
+ // Malformed score ⇒ fail-safe: recommend step-up.
278
+ if (score === null || typeof score !== 'object')
279
+ return true;
280
+ if (score.level === 'high')
281
+ return true;
282
+ const value = score.value;
283
+ if (typeof value !== 'number' || !Number.isFinite(value))
284
+ return true;
285
+ return value >= stepUpThreshold;
286
+ }
287
+ //# sourceMappingURL=risk-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"risk-engine.js","sourceRoot":"","sources":["../../src/risk/risk-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAUH,OAAO,EACL,2BAA2B,EAC3B,iBAAiB,GAGlB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,uBAAuB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAC/E,OAAO,EACL,uBAAuB,EACvB,iBAAiB,GAElB,MAAM,iBAAiB,CAAC;AAEzB;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,sBAAsB,CAAC;AAExD;;;;GAIG;AACH,MAAM,eAAe,GAAc,MAAM,CAAC,MAAM,CAAC;IAC/C,KAAK,EAAE,MAAM;IACb,KAAK,EAAE,CAAC;IACR,mBAAmB,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;CACvC,CAAC,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAS,OAAO,CAAC,aAAgC;IAC/C,kFAAkF;IAClF,gFAAgF;IAChF,+EAA+E;IAC/E,sEAAsE;IACtE,MAAM,MAAM,GAAG,aAAa;SACzB,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,C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,CAAC;QAC/C,CAAC;IACH,CAAC;IAED,OAAO,IAAI,cAAc,CAAC,QAAQ,EAAE,aAAa,EAAE,UAAU,CAAC,CAAC;AACjE,CAAC;AAUD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,eAAe,CAAC,KAAgB,EAAE,MAA8B;IAC9E,MAAM,eAAe,GACnB,OAAO,MAAM,EAAE,eAAe,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,eAAe,CAAC;QACpF,CAAC,CAAC,MAAM,CAAC,eAAe;QACxB,CAAC,CAAC,uBAAuB,CAAC,eAAe,CAAC;IAE9C,kDAAkD;IAClD,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAE7D,IAAK,KAA6B,CAAC,KAAK,KAAK,MAAM;QAAE,OAAO,IAAI,CAAC;IAEjE,MAAM,KAAK,GAAI,KAA6B,CAAC,KAAK,CAAC;IACnD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAEtE,OAAO,KAAK,IAAI,eAAe,CAAC;AAClC,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * **Reference risk-signal evaluators** (Rule of Three — ADR-0023 §7). A minimal
3
+ * pair of PURE, ZERO-IO evaluators that exercise the {@link RiskSignalEvaluator}
4
+ * contract without standing up any speculative port:
5
+ *
6
+ * 1. `UNKNOWN_DEVICE_EVALUATOR` — a `'device'` signal whose value indicates the
7
+ * device is not in a KNOWN-LIST carried in the signal itself (no history / no
8
+ * IO). Unknown/new device ⇒ ELEVATES risk (ADR-0017 new/unknown-device trigger).
9
+ * 2. `SENSITIVE_ACTION_EVALUATOR` — a `'context'` signal the caller stamps
10
+ * (`value.sensitiveAction === true`) marking the request as a sensitive action
11
+ * ⇒ ELEVATES risk (ADR-0017 sensitive-action trigger).
12
+ *
13
+ * These two suffice to prove the contract + the aggregation + the fail-safe
14
+ * direction. The DEFERRED signals (impossible-travel / geo / behavioural / UEBA)
15
+ * need IO or history and are NOT built here — adding a speculative GeoLookup /
16
+ * threat-feed port for a signal that doesn't exist yet would violate Rule of Three
17
+ * AND the zero-dep invariant.
18
+ *
19
+ * ⛔ FAIL-SAFE: each evaluator ELEVATES on an ambiguous / malformed value bag
20
+ * (never returns a low score for "couldn't tell"), and NEVER throws. STRICT checks
21
+ * (`=== true`, explicit membership) — no truthiness fail-open.
22
+ *
23
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
24
+ */
25
+ import type { RiskSignalEvaluator } from './evaluator.js';
26
+ /**
27
+ * The contribution an ambiguous/malformed signal yields — a HIGH elevation, so
28
+ * "couldn't evaluate" always raises risk rather than lowering it. Chosen ≥ the
29
+ * default `highCutoff` (0.67) so an unreadable signal alone pushes toward the high
30
+ * band. NOT 1.0 — a genuinely-confirmed risk can still exceed it.
31
+ */
32
+ export declare const FAIL_SAFE_CONTRIBUTION = 0.8;
33
+ /** Names — the evaluator identities in the registry (also the per-kind default routing). */
34
+ export declare const UNKNOWN_DEVICE_EVALUATOR_NAME = "unknown_device";
35
+ export declare const SENSITIVE_ACTION_EVALUATOR_NAME = "sensitive_action";
36
+ /**
37
+ * `'device'`-kind evaluator. The signal carries `value.deviceId` (the current
38
+ * device's stable id) and `value.knownDeviceIds` (a list the CALLER supplies — no
39
+ * history lookup, no IO). If `deviceId` is present in `knownDeviceIds` ⇒ known ⇒ no
40
+ * risk. Otherwise ⇒ NEW/UNKNOWN device ⇒ elevate.
41
+ *
42
+ * FAIL-SAFE: a missing / non-string `deviceId`, or a missing / malformed
43
+ * `knownDeviceIds`, is treated as "cannot confirm the device is known" ⇒ ELEVATE
44
+ * (never assume known). STRICT membership check. Never throws.
45
+ */
46
+ export declare const UNKNOWN_DEVICE_EVALUATOR: RiskSignalEvaluator;
47
+ /**
48
+ * `'context'`-kind evaluator. The caller stamps `value.sensitiveAction === true`
49
+ * when the request targets a sensitive action (ADR-0017). STRICT `=== true` ⇒
50
+ * elevate. An explicit `=== false` ⇒ no risk.
51
+ *
52
+ * FAIL-SAFE: a missing / non-boolean / truthy-non-boolean `sensitiveAction` is
53
+ * AMBIGUOUS — we do NOT treat "truthy" as confirmed (no fail-open) NOR "absent" as
54
+ * benign silently. Absent/non-boolean ⇒ we cannot tell ⇒ ELEVATE. Only an explicit
55
+ * boolean `false` is benign. Never throws.
56
+ */
57
+ export declare const SENSITIVE_ACTION_EVALUATOR: RiskSignalEvaluator;
58
+ /** The default reference evaluators the engine registers when none are supplied. */
59
+ export declare const DEFAULT_RISK_EVALUATORS: readonly RiskSignalEvaluator[];
60
+ //# sourceMappingURL=signals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signals.d.ts","sourceRoot":"","sources":["../../src/risk/signals.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,KAAK,EAAoB,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAE5E;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAQ1C,4FAA4F;AAC5F,eAAO,MAAM,6BAA6B,mBAAmB,CAAC;AAC9D,eAAO,MAAM,+BAA+B,qBAAqB,CAAC;AAOlE;;;;;;;;;GASG;AACH,eAAO,MAAM,wBAAwB,EAAE,mBAmBrC,CAAC;AAEH;;;;;;;;;GASG;AACH,eAAO,MAAM,0BAA0B,EAAE,mBAiBvC,CAAC;AAEH,oFAAoF;AACpF,eAAO,MAAM,uBAAuB,EAAE,SAAS,mBAAmB,EAGhE,CAAC"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * **Reference risk-signal evaluators** (Rule of Three — ADR-0023 §7). A minimal
3
+ * pair of PURE, ZERO-IO evaluators that exercise the {@link RiskSignalEvaluator}
4
+ * contract without standing up any speculative port:
5
+ *
6
+ * 1. `UNKNOWN_DEVICE_EVALUATOR` — a `'device'` signal whose value indicates the
7
+ * device is not in a KNOWN-LIST carried in the signal itself (no history / no
8
+ * IO). Unknown/new device ⇒ ELEVATES risk (ADR-0017 new/unknown-device trigger).
9
+ * 2. `SENSITIVE_ACTION_EVALUATOR` — a `'context'` signal the caller stamps
10
+ * (`value.sensitiveAction === true`) marking the request as a sensitive action
11
+ * ⇒ ELEVATES risk (ADR-0017 sensitive-action trigger).
12
+ *
13
+ * These two suffice to prove the contract + the aggregation + the fail-safe
14
+ * direction. The DEFERRED signals (impossible-travel / geo / behavioural / UEBA)
15
+ * need IO or history and are NOT built here — adding a speculative GeoLookup /
16
+ * threat-feed port for a signal that doesn't exist yet would violate Rule of Three
17
+ * AND the zero-dep invariant.
18
+ *
19
+ * ⛔ FAIL-SAFE: each evaluator ELEVATES on an ambiguous / malformed value bag
20
+ * (never returns a low score for "couldn't tell"), and NEVER throws. STRICT checks
21
+ * (`=== true`, explicit membership) — no truthiness fail-open.
22
+ *
23
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
24
+ */
25
+ /**
26
+ * The contribution an ambiguous/malformed signal yields — a HIGH elevation, so
27
+ * "couldn't evaluate" always raises risk rather than lowering it. Chosen ≥ the
28
+ * default `highCutoff` (0.67) so an unreadable signal alone pushes toward the high
29
+ * band. NOT 1.0 — a genuinely-confirmed risk can still exceed it.
30
+ */
31
+ export const FAIL_SAFE_CONTRIBUTION = 0.8;
32
+ /** The clear-risk contribution a positively-confirmed reference signal emits. */
33
+ const CONFIRMED_RISK_CONTRIBUTION = 0.7;
34
+ /** No-risk contribution — the signal is present and clearly benign. */
35
+ const NO_RISK_CONTRIBUTION = 0;
36
+ /** Names — the evaluator identities in the registry (also the per-kind default routing). */
37
+ export const UNKNOWN_DEVICE_EVALUATOR_NAME = 'unknown_device';
38
+ export const SENSITIVE_ACTION_EVALUATOR_NAME = 'sensitive_action';
39
+ /** Is `v` a readonly array of strings (a known-device list)? */
40
+ function isStringArray(v) {
41
+ return Array.isArray(v) && v.every((x) => typeof x === 'string');
42
+ }
43
+ /**
44
+ * `'device'`-kind evaluator. The signal carries `value.deviceId` (the current
45
+ * device's stable id) and `value.knownDeviceIds` (a list the CALLER supplies — no
46
+ * history lookup, no IO). If `deviceId` is present in `knownDeviceIds` ⇒ known ⇒ no
47
+ * risk. Otherwise ⇒ NEW/UNKNOWN device ⇒ elevate.
48
+ *
49
+ * FAIL-SAFE: a missing / non-string `deviceId`, or a missing / malformed
50
+ * `knownDeviceIds`, is treated as "cannot confirm the device is known" ⇒ ELEVATE
51
+ * (never assume known). STRICT membership check. Never throws.
52
+ */
53
+ export const UNKNOWN_DEVICE_EVALUATOR = Object.freeze({
54
+ name: UNKNOWN_DEVICE_EVALUATOR_NAME,
55
+ kind: 'device',
56
+ evaluate(signal) {
57
+ const bag = signal?.value;
58
+ const deviceId = bag?.deviceId;
59
+ const known = bag?.knownDeviceIds;
60
+ // Malformed / ambiguous bag → cannot confirm known → elevate (fail-safe).
61
+ if (typeof deviceId !== 'string' || deviceId.length === 0 || !isStringArray(known)) {
62
+ return Object.freeze({ contribution: FAIL_SAFE_CONTRIBUTION, kind: 'device' });
63
+ }
64
+ // STRICT membership: only an exact match in the caller's known-list is "known".
65
+ const isKnown = known.includes(deviceId);
66
+ return Object.freeze({
67
+ contribution: isKnown ? NO_RISK_CONTRIBUTION : CONFIRMED_RISK_CONTRIBUTION,
68
+ kind: 'device',
69
+ });
70
+ },
71
+ });
72
+ /**
73
+ * `'context'`-kind evaluator. The caller stamps `value.sensitiveAction === true`
74
+ * when the request targets a sensitive action (ADR-0017). STRICT `=== true` ⇒
75
+ * elevate. An explicit `=== false` ⇒ no risk.
76
+ *
77
+ * FAIL-SAFE: a missing / non-boolean / truthy-non-boolean `sensitiveAction` is
78
+ * AMBIGUOUS — we do NOT treat "truthy" as confirmed (no fail-open) NOR "absent" as
79
+ * benign silently. Absent/non-boolean ⇒ we cannot tell ⇒ ELEVATE. Only an explicit
80
+ * boolean `false` is benign. Never throws.
81
+ */
82
+ export const SENSITIVE_ACTION_EVALUATOR = Object.freeze({
83
+ name: SENSITIVE_ACTION_EVALUATOR_NAME,
84
+ kind: 'context',
85
+ evaluate(signal) {
86
+ const flag = signal?.value
87
+ ?.sensitiveAction;
88
+ if (flag === true) {
89
+ return Object.freeze({ contribution: CONFIRMED_RISK_CONTRIBUTION, kind: 'context' });
90
+ }
91
+ if (flag === false) {
92
+ return Object.freeze({ contribution: NO_RISK_CONTRIBUTION, kind: 'context' });
93
+ }
94
+ // Absent / non-boolean / truthy-non-boolean → ambiguous → elevate (fail-safe,
95
+ // NOT fail-open: a truthy "yes-ish" is not accepted as a confirmed benign).
96
+ return Object.freeze({ contribution: FAIL_SAFE_CONTRIBUTION, kind: 'context' });
97
+ },
98
+ });
99
+ /** The default reference evaluators the engine registers when none are supplied. */
100
+ export const DEFAULT_RISK_EVALUATORS = Object.freeze([
101
+ UNKNOWN_DEVICE_EVALUATOR,
102
+ SENSITIVE_ACTION_EVALUATOR,
103
+ ]);
104
+ //# sourceMappingURL=signals.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signals.js","sourceRoot":"","sources":["../../src/risk/signals.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAKH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAE1C,iFAAiF;AACjF,MAAM,2BAA2B,GAAG,GAAG,CAAC;AAExC,uEAAuE;AACvE,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAE/B,4FAA4F;AAC5F,MAAM,CAAC,MAAM,6BAA6B,GAAG,gBAAgB,CAAC;AAC9D,MAAM,CAAC,MAAM,+BAA+B,GAAG,kBAAkB,CAAC;AAElE,gEAAgE;AAChE,SAAS,aAAa,CAAC,CAAU;IAC/B,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,Q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,CAAC;QACvF,CAAC;QACD,IAAI,IAAI,KAAK,KAAK,EAAE,CAAC;YACnB,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,oBAAoB,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;QAChF,CAAC;QACD,8EAA8E;QAC9E,4EAA4E;QAC5E,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,sBAAsB,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;IAClF,CAAC;CACF,CAAC,CAAC;AAEH,oFAAoF;AACpF,MAAM,CAAC,MAAM,uBAAuB,GAAmC,MAAM,CAAC,MAAM,CAAC;IACnF,wBAAwB;IACxB,0BAA0B;CAC3B,CAAC,CAAC"}
@@ -0,0 +1,54 @@
1
+ /**
2
+ * **Risk threshold configuration** (ADR-0017 step-up trigger). The band cutoffs
3
+ * that map a normalized `RiskScore.value ∈ [0,1]` onto a coarse `RiskLevel`, plus
4
+ * the separate `stepUpThreshold` the advisory `recommendStepUp` derivation reads.
5
+ *
6
+ * All three values are runtime-configurable per deployment; the DEFAULTS below are
7
+ * conservative middle-of-the-road cutoffs. They are validated + frozen at
8
+ * CONSTRUCTION (`resolveThresholds`) — a nonsensical config throws LOUD at boot,
9
+ * never silently at `assess()` time (mirrors `resolveLifetimes`).
10
+ *
11
+ * ⛔ FAIL-SAFE BAND MAPPING (this layer's #1 rule leaning into the config): a value
12
+ * exactly AT a cutoff maps to the HIGHER-risk (safer-for-security) band. `medium`
13
+ * starts at `>= mediumCutoff`; `high` starts at `>= highCutoff`. A boundary never
14
+ * rounds DOWN into a calmer band. The mapping itself lives in `risk-engine.ts`;
15
+ * this file owns only the numbers + their bounds validation.
16
+ *
17
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
18
+ */
19
+ /**
20
+ * The three cutoffs the Risk engine bands a score with, plus the step-up trigger.
21
+ * All are normalized fractions in `[0,1]`. Validated at construction by
22
+ * {@link resolveThresholds}:
23
+ *
24
+ * 0 ≤ mediumCutoff ≤ highCutoff ≤ 1 and 0 ≤ stepUpThreshold ≤ 1
25
+ */
26
+ export interface RiskThresholdConfig {
27
+ /** `value >= mediumCutoff` (and `< highCutoff`) ⇒ `RiskLevel` `'medium'`. */
28
+ readonly mediumCutoff: number;
29
+ /** `value >= highCutoff` ⇒ `RiskLevel` `'high'` (the safer band at the boundary). */
30
+ readonly highCutoff: number;
31
+ /**
32
+ * The ADR-0017 step-up trigger threshold: `recommendStepUp` returns `true` when
33
+ * `value >= stepUpThreshold` (OR the level is already `'high'`). ADVISORY only —
34
+ * a later layer reads it; the engine never enforces it.
35
+ */
36
+ readonly stepUpThreshold: number;
37
+ }
38
+ /**
39
+ * Conservative deployment defaults: medium at 0.34, high at 0.67, step-up
40
+ * recommended at 0.67 (i.e. aligned with the high band). A deployment tightens or
41
+ * loosens these per its risk appetite.
42
+ */
43
+ export declare const DEFAULT_RISK_THRESHOLDS: RiskThresholdConfig;
44
+ /**
45
+ * Validate + freeze a threshold config, applying {@link DEFAULT_RISK_THRESHOLDS}
46
+ * for any omitted field. Enforces:
47
+ *
48
+ * 0 ≤ mediumCutoff ≤ highCutoff ≤ 1 and 0 ≤ stepUpThreshold ≤ 1
49
+ *
50
+ * Throws LOUD on a nonsensical config — this is construction-time, not a hot path;
51
+ * a misconfiguration must fail the boot, never silently ship a broken banding.
52
+ */
53
+ export declare function resolveThresholds(partial?: Partial<RiskThresholdConfig>): RiskThresholdConfig;
54
+ //# sourceMappingURL=thresholds.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"thresholds.d.ts","sourceRoot":"","sources":["../../src/risk/thresholds.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH;;;;;;GAMG;AACH,MAAM,WAAW,mBAAmB;IAClC,6EAA6E;IAC7E,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,qFAAqF;IACrF,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B;;;;OAIG;IACH,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC;AAED;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,mBAIpC,CAAC;AAOH;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,GACrC,mBAAmB,CA0BrB"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * **Risk threshold configuration** (ADR-0017 step-up trigger). The band cutoffs
3
+ * that map a normalized `RiskScore.value ∈ [0,1]` onto a coarse `RiskLevel`, plus
4
+ * the separate `stepUpThreshold` the advisory `recommendStepUp` derivation reads.
5
+ *
6
+ * All three values are runtime-configurable per deployment; the DEFAULTS below are
7
+ * conservative middle-of-the-road cutoffs. They are validated + frozen at
8
+ * CONSTRUCTION (`resolveThresholds`) — a nonsensical config throws LOUD at boot,
9
+ * never silently at `assess()` time (mirrors `resolveLifetimes`).
10
+ *
11
+ * ⛔ FAIL-SAFE BAND MAPPING (this layer's #1 rule leaning into the config): a value
12
+ * exactly AT a cutoff maps to the HIGHER-risk (safer-for-security) band. `medium`
13
+ * starts at `>= mediumCutoff`; `high` starts at `>= highCutoff`. A boundary never
14
+ * rounds DOWN into a calmer band. The mapping itself lives in `risk-engine.ts`;
15
+ * this file owns only the numbers + their bounds validation.
16
+ *
17
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
18
+ */
19
+ /**
20
+ * Conservative deployment defaults: medium at 0.34, high at 0.67, step-up
21
+ * recommended at 0.67 (i.e. aligned with the high band). A deployment tightens or
22
+ * loosens these per its risk appetite.
23
+ */
24
+ export const DEFAULT_RISK_THRESHOLDS = Object.freeze({
25
+ mediumCutoff: 0.34,
26
+ highCutoff: 0.67,
27
+ stepUpThreshold: 0.67,
28
+ });
29
+ /** A finite number within the closed unit interval `[0,1]`. */
30
+ function isUnitFraction(v) {
31
+ return typeof v === 'number' && Number.isFinite(v) && v >= 0 && v <= 1;
32
+ }
33
+ /**
34
+ * Validate + freeze a threshold config, applying {@link DEFAULT_RISK_THRESHOLDS}
35
+ * for any omitted field. Enforces:
36
+ *
37
+ * 0 ≤ mediumCutoff ≤ highCutoff ≤ 1 and 0 ≤ stepUpThreshold ≤ 1
38
+ *
39
+ * Throws LOUD on a nonsensical config — this is construction-time, not a hot path;
40
+ * a misconfiguration must fail the boot, never silently ship a broken banding.
41
+ */
42
+ export function resolveThresholds(partial) {
43
+ const mediumCutoff = partial?.mediumCutoff ?? DEFAULT_RISK_THRESHOLDS.mediumCutoff;
44
+ const highCutoff = partial?.highCutoff ?? DEFAULT_RISK_THRESHOLDS.highCutoff;
45
+ const stepUpThreshold = partial?.stepUpThreshold ?? DEFAULT_RISK_THRESHOLDS.stepUpThreshold;
46
+ if (!isUnitFraction(mediumCutoff)) {
47
+ throw new Error('RiskThresholdConfig: mediumCutoff must be a finite number in [0,1]');
48
+ }
49
+ if (!isUnitFraction(highCutoff)) {
50
+ throw new Error('RiskThresholdConfig: highCutoff must be a finite number in [0,1]');
51
+ }
52
+ if (!isUnitFraction(stepUpThreshold)) {
53
+ throw new Error('RiskThresholdConfig: stepUpThreshold must be a finite number in [0,1]');
54
+ }
55
+ if (mediumCutoff > highCutoff) {
56
+ throw new Error('RiskThresholdConfig: mediumCutoff must be ≤ highCutoff (bands must ascend)');
57
+ }
58
+ return Object.freeze({ mediumCutoff, highCutoff, stepUpThreshold });
59
+ }
60
+ //# sourceMappingURL=thresholds.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"thresholds.js","sourceRoot":"","sources":["../../src/risk/thresholds.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAsBH;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAwB,MAAM,CAAC,MAAM,CAAC;IACxE,YAAY,EAAE,IAAI;IAClB,UAAU,EAAE,IAAI;IAChB,eAAe,EAAE,IAAI;CACtB,CAAC,CAAC;AAEH,+DAA+D;AAC/D,SAAS,cAAc,CAAC,CAAU;IAChC,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACzE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAC/B,OAAsC;IAEtC,MAAM,YAAY,GAAG,OAAO,EAAE,YAAY,IAAI,uBAAuB,CAAC,YAAY,CAAC;IACnF,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,uBAAuB,CAAC,UAAU,CAAC;IAC7E,MAAM,eAAe,GACnB,OAAO,EAAE,eAAe,IAAI,uBAAuB,CAAC,eAAe,CAAC;IAEtE,IAAI,CAAC,cAAc,CAAC,YAAY,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAC;IACJ,CAAC;IACD,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;IACtF,CAAC;IACD,IAAI,CAAC,cAAc,CAAC,eAAe,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CACb,uEAAuE,CACxE,CAAC;IACJ,CAAC;IACD,IAAI,YAAY,GAAG,UAAU,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CACb,4EAA4E,CAC7E,CAAC;IACJ,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,UAAU,EAAE,eAAe,EAAE,CAAC,CAAC;AACtE,CAAC"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Engine seam: **Session** — establishes/rotates/revokes the session.
3
+ *
4
+ * Two-tier storage (ADR-0013): durable session + refresh-token records live in
5
+ * MariaDB (lifecycle events only); the hot per-request session state + cached
6
+ * authorization facts live in Redis. This seam names the lifecycle contract only;
7
+ * the storage tiers, rotation, and fail-closed lookup are a later layer.
8
+ *
9
+ * TYPES / INTERFACES ONLY. ADR-0003: framework-agnostic.
10
+ */
11
+ import type { Principal } from '../identity/index.js';
12
+ /** Lifecycle state of a session. */
13
+ export type SessionStatus = 'active' | 'rotated' | 'revoked' | 'expired';
14
+ /** An established session bound to a principal. */
15
+ export interface Session {
16
+ readonly sessionId: string;
17
+ readonly principalId: string;
18
+ readonly tenantId: string;
19
+ readonly status: SessionStatus;
20
+ /** UTC epoch milliseconds — the platform sets UTC times explicitly. */
21
+ readonly issuedAtMs: number;
22
+ readonly expiresAtMs: number;
23
+ }
24
+ /** The token pair a session lifecycle event yields. */
25
+ export interface SessionTokens {
26
+ readonly accessToken: string;
27
+ readonly refreshToken: string;
28
+ }
29
+ /** A session plus the tokens minted for it (returned at lifecycle events). */
30
+ export interface SessionEstablishment {
31
+ readonly session: Session;
32
+ readonly tokens: SessionTokens;
33
+ }
34
+ /**
35
+ * Session Authority seam — the lifecycle operations (establish on login, rotate
36
+ * on refresh, revoke on logout). Implementation (hybrid storage, rotation,
37
+ * Redis-only hot lookup, NO durable fallback (fail-closed)) is a later layer.
38
+ */
39
+ export interface SessionAuthority {
40
+ establish(principal: Principal): Promise<SessionEstablishment>;
41
+ rotate(refreshToken: string): Promise<SessionEstablishment>;
42
+ revoke(sessionId: string): Promise<void>;
43
+ }
44
+ export * from './ports.js';
45
+ export * from './lifetimes.js';
46
+ export * from './session-authority.js';
47
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD,oCAAoC;AACpC,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;AAEzE,mDAAmD;AACnD,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,SAAS,E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