@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,56 @@
1
+ /**
2
+ * Audit engine ERRORS — the typed validation signal for a REJECTED event
3
+ * (attribution / tenant-scope / UTC-time enforcement, §2 / AC-2).
4
+ *
5
+ * Unlike the Session/Authorization engines (which fail CLOSED with a single
6
+ * GENERIC anti-enumeration reason), an audit REJECTION is a caller CONTRACT
7
+ * violation, not a security oracle — there is no resource-existence to protect by
8
+ * genericizing it. So this error names WHICH invariant failed (missing actor /
9
+ * tenant / invalid time) to help the emitting adapter fix its call site, WITHOUT
10
+ * ever echoing an attribute VALUE (a secret could ride in there).
11
+ *
12
+ * ⛔ This is thrown BEFORE anything is persisted — a rejected event is never
13
+ * stored under a blank actor (AC-2). It is distinct from a STORE-APPEND failure,
14
+ * which the engine lets PROPAGATE unchanged (AC-11) so the caller sees the real
15
+ * cause.
16
+ *
17
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
18
+ */
19
+ /** Which attribution/scope/time/serializability invariant a rejected event violated. */
20
+ export type AuditValidationReason = 'missing_actor' | 'missing_tenant' | 'invalid_occurred_at'
21
+ /**
22
+ * An attribute value is NON-SERIALIZABLE for the content hash — a `bigint`,
23
+ * `symbol`, or `function` that cannot be deterministically fingerprinted without
24
+ * ambiguity. Rejected with THIS typed reason (never a raw `TypeError` mid-hash),
25
+ * so the emitting adapter fixes the call site. Legitimate JSON-ish values
26
+ * (`Date`, `-0`, `NaN`/`±Infinity`, `null`, `undefined`) are normalized, not
27
+ * rejected — see `./canonicalize.ts`.
28
+ */
29
+ | 'unserializable_attribute'
30
+ /**
31
+ * An attribute graph is CYCLIC (or pathologically deep): a value references
32
+ * itself directly or transitively (`c.self = c`), so a naive structural walk
33
+ * would recurse forever and overflow the stack with an untyped `RangeError`
34
+ * mid-hash. Both the secret stripper and the canonicalizer carry a shared
35
+ * visited-`WeakSet` / depth cap (`./walk.ts`) and surface such a graph as THIS
36
+ * TYPED rejection instead (never an untyped throw), so the emitting adapter fixes
37
+ * the call site. Carries NO value (a secret could ride in the bag).
38
+ */
39
+ | 'cyclic_attribute';
40
+ /**
41
+ * Thrown by `record()` when an {@link import('./index.js').AuditEvent} fails the
42
+ * attribution / tenant-scope / UTC-time gate (AC-2): a blank/absent `actorId`, a
43
+ * blank/absent `tenantId`, or a non-finite `occurredAtMs`; ALSO thrown when an
44
+ * attribute value is non-serializable for the content hash (`bigint`/`symbol`/
45
+ * `function` → `unserializable_attribute`) or CYCLIC/over-deep (`cyclic_attribute`),
46
+ * so a bad value surfaces as a TYPED rejection rather than a raw `TypeError` /
47
+ * `RangeError` mid-hash. Names the failing invariant
48
+ * ({@link reason}) for the emitting adapter; NEVER carries an attribute value (no
49
+ * secret leak).
50
+ */
51
+ export declare class AuditValidationError extends Error {
52
+ /** The specific invariant that failed (for the emitting adapter to fix). */
53
+ readonly reason: AuditValidationReason;
54
+ constructor(reason: AuditValidationReason);
55
+ }
56
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/audit/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,wFAAwF;AACxF,MAAM,MAAM,qBAAqB,GAC7B,eAAe,GACf,gBAAgB,GAChB,qBAAqB;AACvB;;;;;;;GAOG;GACD,0BAA0B;AAC5B;;;;;;;;GAQG;GACD,kBAAkB,CAAC;AAEvB;;;;;;;;;;GAUG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,4EAA4E;IAC5E,QAAQ,CAAC,MAAM,EAAE,qBAAqB,CAAC;gBAE3B,MAAM,EAAE,qBAAqB;CAO1C"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Audit engine ERRORS — the typed validation signal for a REJECTED event
3
+ * (attribution / tenant-scope / UTC-time enforcement, §2 / AC-2).
4
+ *
5
+ * Unlike the Session/Authorization engines (which fail CLOSED with a single
6
+ * GENERIC anti-enumeration reason), an audit REJECTION is a caller CONTRACT
7
+ * violation, not a security oracle — there is no resource-existence to protect by
8
+ * genericizing it. So this error names WHICH invariant failed (missing actor /
9
+ * tenant / invalid time) to help the emitting adapter fix its call site, WITHOUT
10
+ * ever echoing an attribute VALUE (a secret could ride in there).
11
+ *
12
+ * ⛔ This is thrown BEFORE anything is persisted — a rejected event is never
13
+ * stored under a blank actor (AC-2). It is distinct from a STORE-APPEND failure,
14
+ * which the engine lets PROPAGATE unchanged (AC-11) so the caller sees the real
15
+ * cause.
16
+ *
17
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
18
+ */
19
+ /**
20
+ * Thrown by `record()` when an {@link import('./index.js').AuditEvent} fails the
21
+ * attribution / tenant-scope / UTC-time gate (AC-2): a blank/absent `actorId`, a
22
+ * blank/absent `tenantId`, or a non-finite `occurredAtMs`; ALSO thrown when an
23
+ * attribute value is non-serializable for the content hash (`bigint`/`symbol`/
24
+ * `function` → `unserializable_attribute`) or CYCLIC/over-deep (`cyclic_attribute`),
25
+ * so a bad value surfaces as a TYPED rejection rather than a raw `TypeError` /
26
+ * `RangeError` mid-hash. Names the failing invariant
27
+ * ({@link reason}) for the emitting adapter; NEVER carries an attribute value (no
28
+ * secret leak).
29
+ */
30
+ export class AuditValidationError extends Error {
31
+ /** The specific invariant that failed (for the emitting adapter to fix). */
32
+ reason;
33
+ constructor(reason) {
34
+ super(`audit_event_rejected:${reason}`);
35
+ this.name = 'AuditValidationError';
36
+ this.reason = reason;
37
+ // Preserve the prototype chain when compiled down-level so `instanceof` works.
38
+ Object.setPrototypeOf(this, AuditValidationError.prototype);
39
+ }
40
+ }
41
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/audit/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AA2BH;;;;;;;;;;GAUG;AACH,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC7C,4EAA4E;IACnE,MAAM,CAAwB;IAEvC,YAAY,MAA6B;QACvC,KAAK,CAAC,wBAAwB,MAAM,EAAE,CAAC,CAAC;QACxC,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,+EAA+E;QAC/E,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;IAC9D,CAAC;CACF"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Engine seam: **Audit** — records every decision that matters.
3
+ *
4
+ * Immutably and attributably records decisions and lifecycle events
5
+ * (010_Architecture §6). This seam names the sink contract only; the immutable
6
+ * store and retention are a later layer.
7
+ *
8
+ * TYPES / INTERFACES ONLY. ADR-0003: framework-agnostic.
9
+ */
10
+ import type { Decision, Action, Resource } from '../authorization/index.js';
11
+ /** Categories of auditable event. */
12
+ export type AuditEventType = 'authentication' | 'session_lifecycle' | 'authorization_decision' | 'permission_change' | 'admin_action';
13
+ /**
14
+ * One immutable, attributable audit record. Actor is referenced by stable id;
15
+ * the presentation layer resolves it to a display label (never surfaces raw ids
16
+ * to end users).
17
+ */
18
+ export interface AuditEvent {
19
+ readonly type: AuditEventType;
20
+ /** UTC epoch milliseconds — times are set explicitly in UTC. */
21
+ readonly occurredAtMs: number;
22
+ /** Stable id of the acting principal (resolved to a label for display). */
23
+ readonly actorId: string;
24
+ readonly tenantId: string;
25
+ /** Present for `authorization_decision` events. */
26
+ readonly action?: Action;
27
+ readonly resource?: Resource;
28
+ readonly decision?: Decision;
29
+ /** Non-sensitive supplementary attributes. */
30
+ readonly attributes?: Readonly<Record<string, unknown>>;
31
+ }
32
+ /**
33
+ * Audit Sink seam — the append-only destination for audit events.
34
+ * Implementation (immutable store, integrity, retention) is a later layer.
35
+ */
36
+ export interface AuditSink {
37
+ /** Append one event. Append-only: events are never mutated or deleted. */
38
+ record(event: AuditEvent): Promise<void>;
39
+ }
40
+ export * from './ports.js';
41
+ export * from './canonicalize.js';
42
+ export * from './sensitive.js';
43
+ export * from './errors.js';
44
+ export * from './audit-recorder.js';
45
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE5E,qCAAqC;AACrC,MAAM,MAAM,cAAc,GACtB,gBAAgB,GAChB,mBAAmB,GACnB,wBAAwB,GACxB,mBAAmB,GACnB,cAAc,CAAC;AAEnB;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,gEAAgE;IAChE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,mDAAmD;IACnD,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,8CAA8C;IAC9C,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACzD;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,0EAA0E;IAC1E,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1C;AAMD,cAAc,YAAY,CAAC;AAE3B,cAAc,mBAAmB,CAAC;AAElC,cAAc,gBAAgB,CAAC;AAE/B,cAAc,aAAa,CAAC;AAG5B,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Engine seam: **Audit** — records every decision that matters.
3
+ *
4
+ * Immutably and attributably records decisions and lifecycle events
5
+ * (010_Architecture §6). This seam names the sink contract only; the immutable
6
+ * store and retention are a later layer.
7
+ *
8
+ * TYPES / INTERFACES ONLY. ADR-0003: framework-agnostic.
9
+ */
10
+ // --- Engine implementation behind the FROZEN seam above (Layer 9) -------------
11
+ // APPEND-ONLY re-exports (mirrors identity/session/authorization barrels).
12
+ // Nothing above this line changes; the engine is added as SIBLING files.
13
+ // - the injected PORTs (hash / append-only store / clock) + the persisted record shape:
14
+ export * from './ports.js';
15
+ // - the pure, key-ordered canonical serialization (determinism = tamper-evidence):
16
+ export * from './canonicalize.js';
17
+ // - the default sensitive-key denylist + the no-secret-leak stripper:
18
+ export * from './sensitive.js';
19
+ // - the typed validation-rejection error (attribution/tenant/UTC-time gate):
20
+ export * from './errors.js';
21
+ // - the tamper-evident recorder `createAuditRecorder` (IS an AuditSink) + the pure
22
+ // `verifyChain` primitive + the GENESIS chain constant:
23
+ export * from './audit-recorder.js';
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAyCH,iFAAiF;AACjF,2EAA2E;AAC3E,yEAAyE;AACzE,0FAA0F;AAC1F,cAAc,YAAY,CAAC;AAC3B,qFAAqF;AACrF,cAAc,mBAAmB,CAAC;AAClC,wEAAwE;AACxE,cAAc,gBAAgB,CAAC;AAC/B,+EAA+E;AAC/E,cAAc,aAAa,CAAC;AAC5B,qFAAqF;AACrF,4DAA4D;AAC5D,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,131 @@
1
+ /**
2
+ * The Audit engine **PORTS** — the framework-agnostic contracts the
3
+ * `createAuditRecorder` engine (`./audit-recorder.ts`) uses to HASH content,
4
+ * PERSIST records append-only, and READ the clock, WITHOUT the engine ever
5
+ * knowing which crypto / DB backs them.
6
+ *
7
+ * ADR-0003 (zero-dep Core): every concrete adapter (`node:crypto` SHA-256, the
8
+ * append-only MariaDB `shield_audit` table) is a real runtime dependency and
9
+ * CANNOT live here. The Core defines only these interfaces; `@astram/guard`
10
+ * ships the concretes and INJECTS them into `createAuditRecorder(...)`. This keeps
11
+ * `@astram/core` at ZERO runtime dependencies (the load-bearing invariant) while
12
+ * the substrate stays swappable.
13
+ *
14
+ * ⛔ SECURITY — the audit engine records for INTEGRITY, not confidentiality. It
15
+ * NEVER persists a secret value (a denylist strips secret-bearing keys before a
16
+ * record is built — see `./sensitive.ts`), and no port here is permitted to LOG
17
+ * its input. The hash is a fingerprint for tamper-detection, never a place to
18
+ * stash a secret.
19
+ *
20
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
21
+ */
22
+ import type { Clock } from '../session/ports.js';
23
+ import type { AuditEvent } from './index.js';
24
+ export type { Clock };
25
+ /**
26
+ * A deterministic one-way hash of a string — the tamper-evidence primitive.
27
+ * Concrete: SHA-256 (`node:crypto`) in `@astram/guard`.
28
+ *
29
+ * ⛔ DETERMINISM IS LOAD-BEARING: the same input MUST always yield the same
30
+ * output, on the write path AND on the independent verification path
31
+ * ({@link import('./audit-recorder.js').verifyChain}). A non-deterministic hash
32
+ * (salted/keyed per-call, time-varying) would make every stored chain fail
33
+ * verification and DEFEAT tamper-detection — so this port is a PLAIN digest, not
34
+ * a keyed/HMAC hash (this is INTEGRITY, not confidentiality; no secret is
35
+ * required to re-verify).
36
+ *
37
+ * ⛔ The engine may feed a canonicalized event into this; the port MUST NOT log
38
+ * its input (an attribute bag, though already secret-stripped, is not for logs).
39
+ */
40
+ export interface HashFunction {
41
+ /** Deterministic digest of `input` (hex/base64 string — the engine treats it opaquely). */
42
+ hash(input: string): string;
43
+ }
44
+ /**
45
+ * The persisted shape of ONE audit record: the caller's original
46
+ * {@link AuditEvent} fields PLUS the engine's chain + stamping metadata. This is
47
+ * what the {@link AuditEventStore} durably holds and what
48
+ * {@link import('./audit-recorder.js').verifyChain} re-verifies.
49
+ *
50
+ * ⛔ The `attributes` carried here are ALREADY secret-stripped by the engine (see
51
+ * `./sensitive.ts`) — a raw secret value NEVER reaches this record. `strippedKeys`
52
+ * records only the NAMES of any keys that were removed (for forensic
53
+ * completeness), never their values.
54
+ */
55
+ export interface StoredAuditRecord {
56
+ readonly type: AuditEvent['type'];
57
+ /** UTC epoch ms the event OCCURRED — the caller's time (distinct from `recordedAtMs`). */
58
+ readonly occurredAtMs: number;
59
+ readonly actorId: string;
60
+ readonly tenantId: string;
61
+ readonly action?: AuditEvent['action'];
62
+ readonly resource?: AuditEvent['resource'];
63
+ readonly decision?: AuditEvent['decision'];
64
+ /** The CLEANED (secret-stripped) non-sensitive attributes actually persisted. */
65
+ readonly attributes?: Readonly<Record<string, unknown>>;
66
+ /** NAMES (never values) of denylisted attribute keys that were stripped, if any. */
67
+ readonly strippedKeys?: readonly string[];
68
+ /**
69
+ * Per-tenant, zero-based monotonic position in THIS tenant's chain (the 1st
70
+ * record for a tenant is `0`). Binds order into {@link entryHash} so a deletion
71
+ * or reorder breaks every downstream hash.
72
+ */
73
+ readonly sequenceNumber: number;
74
+ /**
75
+ * UTC epoch ms the engine RECORDED the event (its own {@link Clock} stamp) —
76
+ * distinct from the caller-supplied {@link occurredAtMs} (AC-9). It is NOT part
77
+ * of the content hash (the caller's `occurredAtMs` is the authoritative event
78
+ * time; `recordedAtMs` is engine provenance).
79
+ */
80
+ readonly recordedAtMs: number;
81
+ /** `hash(canonicalize(cleanEvent))` — fingerprints the event content (AC-3/6). */
82
+ readonly contentHash: string;
83
+ /**
84
+ * The prior record's {@link entryHash} for THIS tenant, or {@link GENESIS} for
85
+ * the first record of the tenant. Links the chain (AC-4/5).
86
+ */
87
+ readonly prevHash: string;
88
+ /**
89
+ * `hash(contentHash ‖ prevHash ‖ sequenceNumber)` — binds content + link +
90
+ * order into one value. Any mutation/deletion/reorder breaks a downstream
91
+ * `entryHash` (§3). The next record's {@link prevHash} points at this.
92
+ */
93
+ readonly entryHash: string;
94
+ }
95
+ /**
96
+ * The **append-only audit record store** PORT (MariaDB `shield_audit` adapter).
97
+ * Written ONCE per event and read back ordered for verification. Concrete:
98
+ * `MariaDbAuditEventStore` in `@astram/guard`.
99
+ *
100
+ * ⛔ APPEND-ONLY IS STRUCTURAL: this port intentionally exposes NO update / delete
101
+ * method — the adapter's table forbids UPDATE/DELETE (Infra, ADR-0014). Even the
102
+ * engine cannot rewrite history.
103
+ *
104
+ * ⛔ CONCURRENCY: the engine reads the tenant's current chain tail, then appends
105
+ * the successor; correctness of the per-tenant sequence + `prevHash` link assumes
106
+ * the store SERIALIZES appends per tenant (the adapter enforces this with the
107
+ * table's per-tenant unique `(tenantId, sequenceNumber)` key — a duplicate
108
+ * sequence violates the constraint and the append fails, surfacing to the caller
109
+ * rather than silently forking the chain).
110
+ */
111
+ export interface AuditEventStore {
112
+ /**
113
+ * Append ONE record (the engine has already computed sequence/hashes/stamp).
114
+ *
115
+ * ⛔ On failure this MUST reject (throw) — the engine lets that PROPAGATE to the
116
+ * caller (AC-11); an event is NEVER silently dropped. The adapter never
117
+ * "swallows and returns" a failed write.
118
+ */
119
+ append(record: StoredAuditRecord): Promise<void>;
120
+ /**
121
+ * Read a tenant's FULL chain ordered by {@link StoredAuditRecord.sequenceNumber}
122
+ * ascending — used to compute the next `prevHash`/sequence on write and to feed
123
+ * {@link import('./audit-recorder.js').verifyChain} on the read path.
124
+ *
125
+ * Returns `[]` for an unknown/empty tenant (a legitimate "no chain yet", not an
126
+ * error). Records are scoped to `tenantId` ONLY — cross-tenant records never
127
+ * appear (per-tenant isolation, §3.2).
128
+ */
129
+ readChain(tenantId: string): Promise<readonly StoredAuditRecord[]>;
130
+ }
131
+ //# sourceMappingURL=ports.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ports.d.ts","sourceRoot":"","sources":["../../src/audit/ports.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAO7C,YAAY,EAAE,KAAK,EAAE,CAAC;AAMtB;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,YAAY;IAC3B,2FAA2F;IAC3F,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;CAC7B;AAMD;;;;;;;;;;GAUG;AACH,MAAM,WAAW,iBAAiB;IAEhC,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;IAClC,0FAA0F;IAC1F,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,CAAC,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;IACvC,QAAQ,CAAC,QAAQ,CAAC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC;IAC3C,QAAQ,CAAC,QAAQ,CAAC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC;IAC3C,iFAAiF;IACjF,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACxD,oFAAoF;IACpF,QAAQ,CAAC,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAG1C;;;;OAIG;IACH,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC;;;;;OAKG;IACH,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,kFAAkF;IAClF,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B;;;;OAIG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjD;;;;;;;;OAQG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,iBAAiB,EAAE,CAAC,CAAC;CACpE"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The Audit engine **PORTS** — the framework-agnostic contracts the
3
+ * `createAuditRecorder` engine (`./audit-recorder.ts`) uses to HASH content,
4
+ * PERSIST records append-only, and READ the clock, WITHOUT the engine ever
5
+ * knowing which crypto / DB backs them.
6
+ *
7
+ * ADR-0003 (zero-dep Core): every concrete adapter (`node:crypto` SHA-256, the
8
+ * append-only MariaDB `shield_audit` table) is a real runtime dependency and
9
+ * CANNOT live here. The Core defines only these interfaces; `@astram/guard`
10
+ * ships the concretes and INJECTS them into `createAuditRecorder(...)`. This keeps
11
+ * `@astram/core` at ZERO runtime dependencies (the load-bearing invariant) while
12
+ * the substrate stays swappable.
13
+ *
14
+ * ⛔ SECURITY — the audit engine records for INTEGRITY, not confidentiality. It
15
+ * NEVER persists a secret value (a denylist strips secret-bearing keys before a
16
+ * record is built — see `./sensitive.ts`), and no port here is permitted to LOG
17
+ * its input. The hash is a fingerprint for tamper-detection, never a place to
18
+ * stash a secret.
19
+ *
20
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
21
+ */
22
+ export {};
23
+ // The Clock PORT (UTC epoch ms) — injected so `recordedAtMs` stamping is
24
+ // deterministic in tests; re-exported from `../session/ports.ts` at the top of
25
+ // this file (a single shared `Clock` contract, no duplicate barrel export).
26
+ // Concrete: `SystemClock` (`Date.now`) in `@astram/guard`.
27
+ //# sourceMappingURL=ports.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ports.js","sourceRoot":"","sources":["../../src/audit/ports.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAqIH,yEAAyE;AACzE,+EAA+E;AAC/E,4EAA4E;AAC5E,2DAA2D"}
@@ -0,0 +1,68 @@
1
+ /**
2
+ * **Sensitive-attribute stripping** — the no-secret-leak guard (§2, AC-7).
3
+ *
4
+ * The audit engine records for INTEGRITY, and it MUST NEVER persist a secret
5
+ * value. A caller may accidentally include a token / password / hash in an
6
+ * event's `attributes`; this module removes any attribute whose KEY matches a
7
+ * denylisted secret-bearing term BEFORE the record is built or hashed, so a raw
8
+ * secret never reaches the store, the content hash, or a log.
9
+ *
10
+ * Matching is by KEY (case-insensitive substring): a key CONTAINING a denylist
11
+ * term is stripped (e.g. `access_token`, `userPassword`, `x-api-key` all match).
12
+ * Values are never inspected (a value scan is unreliable and could itself leak).
13
+ * Only the stripped key NAMES are surfaced (`strippedKeys`) — never the values.
14
+ *
15
+ * Stripping RECURSES through nested objects AND arrays at every depth: a
16
+ * denylisted key found under a benign parent — `{ meta: { password } }` or
17
+ * `{ changes: [{ secret }] }` — is stripped just as a top-level one is, so a raw
18
+ * secret never survives inside a diff-bag / `meta:{}` / request snapshot either.
19
+ * Recorded `strippedKeys` carry the dotted PATH to each removed key (names only,
20
+ * never values), so a nested strip is forensically visible.
21
+ *
22
+ * The denylist is DEFAULT-ON and overridable per recorder (a caller may pass a
23
+ * REPLACEMENT list) — but there is no way to disable stripping entirely; passing
24
+ * an empty list is the explicit opt-out and is the caller's documented risk.
25
+ *
26
+ * PURE — no I/O, no state. ADR-0003: framework-agnostic — no framework / HTTP /
27
+ * DB / node imports.
28
+ */
29
+ /**
30
+ * The DEFAULT denylist of secret-bearing key fragments (lower-case; matched as a
31
+ * case-insensitive SUBSTRING of an attribute key). Covers tokens, passwords,
32
+ * password/refresh hashes, API keys, credentials, session/OTP/MFA secrets. A key
33
+ * CONTAINING any of these is stripped — e.g. `refresh_token`, `refreshToken`,
34
+ * `api_key`, `apiKey`, `session_token`, `mfa_secret`, `pwd`.
35
+ *
36
+ * ⛔ Extend, don't shrink: adding fragments only makes the guard stricter.
37
+ * Overriding with a SHORTER list is a deliberate (documented) risk the caller owns.
38
+ */
39
+ export declare const DEFAULT_SENSITIVE_DENYLIST: readonly string[];
40
+ /** The result of stripping: the cleaned bag + the NAMES/PATHS of removed keys. */
41
+ export interface StripSensitiveResult {
42
+ /**
43
+ * The attributes with every denylisted key removed, RECURSIVELY (nested objects
44
+ * and arrays walked at every depth). `undefined` when the input was `undefined`
45
+ * (no attributes) — the caller then persists no `attributes`.
46
+ */
47
+ readonly cleaned: Readonly<Record<string, unknown>> | undefined;
48
+ /**
49
+ * PATHS (never values) of the keys that were stripped. A top-level strip is the
50
+ * bare key name; a nested strip is a dotted path (e.g. `meta.password`,
51
+ * `changes.1.secret` for an array index). Order follows the depth-first walk.
52
+ */
53
+ readonly strippedKeys: readonly string[];
54
+ }
55
+ /**
56
+ * Strip every attribute whose KEY is denylisted (AC-7). The engine calls this on
57
+ * an event's `attributes` before building/hashing the stored record, so NO raw
58
+ * secret value ever reaches the store, the `contentHash`, or a log.
59
+ *
60
+ * @param attributes The caller's raw attribute bag (or `undefined`).
61
+ * @param denylist Optional REPLACEMENT denylist; defaults to
62
+ * {@link DEFAULT_SENSITIVE_DENYLIST}. Matching is case-insensitive
63
+ * substring on the key. An empty list disables stripping (a
64
+ * documented, caller-owned risk).
65
+ * @returns The `cleaned` bag (secret keys removed) + `strippedKeys` names.
66
+ */
67
+ export declare function stripSensitive(attributes: Readonly<Record<string, unknown>> | undefined, denylist?: readonly string[]): StripSensitiveResult;
68
+ //# sourceMappingURL=sensitive.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sensitive.d.ts","sourceRoot":"","sources":["../../src/audit/sensitive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAIH;;;;;;;;;GASG;AACH,eAAO,MAAM,0BAA0B,EAAE,SAAS,MAAM,EAmBtD,CAAC;AAEH,kFAAkF;AAClF,MAAM,WAAW,oBAAoB;IACnC;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAChE;;;;OAIG;IACH,QAAQ,CAAC,YAAY,EAAE,SAAS,MAAM,EAAE,CAAC;CAC1C;AA0FD;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,EACzD,QAAQ,GAAE,SAAS,MAAM,EAA+B,GACvD,oBAAoB,CAiBtB"}
@@ -0,0 +1,159 @@
1
+ /**
2
+ * **Sensitive-attribute stripping** — the no-secret-leak guard (§2, AC-7).
3
+ *
4
+ * The audit engine records for INTEGRITY, and it MUST NEVER persist a secret
5
+ * value. A caller may accidentally include a token / password / hash in an
6
+ * event's `attributes`; this module removes any attribute whose KEY matches a
7
+ * denylisted secret-bearing term BEFORE the record is built or hashed, so a raw
8
+ * secret never reaches the store, the content hash, or a log.
9
+ *
10
+ * Matching is by KEY (case-insensitive substring): a key CONTAINING a denylist
11
+ * term is stripped (e.g. `access_token`, `userPassword`, `x-api-key` all match).
12
+ * Values are never inspected (a value scan is unreliable and could itself leak).
13
+ * Only the stripped key NAMES are surfaced (`strippedKeys`) — never the values.
14
+ *
15
+ * Stripping RECURSES through nested objects AND arrays at every depth: a
16
+ * denylisted key found under a benign parent — `{ meta: { password } }` or
17
+ * `{ changes: [{ secret }] }` — is stripped just as a top-level one is, so a raw
18
+ * secret never survives inside a diff-bag / `meta:{}` / request snapshot either.
19
+ * Recorded `strippedKeys` carry the dotted PATH to each removed key (names only,
20
+ * never values), so a nested strip is forensically visible.
21
+ *
22
+ * The denylist is DEFAULT-ON and overridable per recorder (a caller may pass a
23
+ * REPLACEMENT list) — but there is no way to disable stripping entirely; passing
24
+ * an empty list is the explicit opt-out and is the caller's documented risk.
25
+ *
26
+ * PURE — no I/O, no state. ADR-0003: framework-agnostic — no framework / HTTP /
27
+ * DB / node imports.
28
+ */
29
+ import { guardDescent, isDescendableObject } from './walk.js';
30
+ /**
31
+ * The DEFAULT denylist of secret-bearing key fragments (lower-case; matched as a
32
+ * case-insensitive SUBSTRING of an attribute key). Covers tokens, passwords,
33
+ * password/refresh hashes, API keys, credentials, session/OTP/MFA secrets. A key
34
+ * CONTAINING any of these is stripped — e.g. `refresh_token`, `refreshToken`,
35
+ * `api_key`, `apiKey`, `session_token`, `mfa_secret`, `pwd`.
36
+ *
37
+ * ⛔ Extend, don't shrink: adding fragments only makes the guard stricter.
38
+ * Overriding with a SHORTER list is a deliberate (documented) risk the caller owns.
39
+ */
40
+ export const DEFAULT_SENSITIVE_DENYLIST = Object.freeze([
41
+ 'token',
42
+ 'password',
43
+ 'passwd',
44
+ 'pwd',
45
+ 'secret',
46
+ 'refresh_token',
47
+ 'refreshtoken',
48
+ 'access_token',
49
+ 'authorization',
50
+ 'api_key',
51
+ 'api-key',
52
+ 'apikey',
53
+ 'private_key',
54
+ 'credential',
55
+ 'session_token',
56
+ 'otp',
57
+ 'mfa_secret',
58
+ 'hash',
59
+ ]);
60
+ /**
61
+ * Return whether `key` matches any denylist fragment (case-insensitive substring).
62
+ * A denylist fragment is compared already-lower-cased against the lower-cased key.
63
+ */
64
+ function isSensitiveKey(key, denylistLower) {
65
+ const k = key.toLowerCase();
66
+ return denylistLower.some((frag) => k.includes(frag));
67
+ }
68
+ /**
69
+ * Recursively strip denylisted keys from an arbitrary attribute VALUE. Objects are
70
+ * walked key-by-key (a denylisted key at ANY depth → its value is dropped and the
71
+ * dotted path recorded); arrays are walked element-by-element (order preserved,
72
+ * indices form part of the path). Primitives pass through unchanged. Deterministic:
73
+ * key iteration follows the object's own order and array order is retained, so the
74
+ * downstream key-sorted canonicalization stays reproducible.
75
+ *
76
+ * ⛔ DESCEND-SET PARITY: an OBJECT is descended iff {@link isDescendableObject} — the
77
+ * SAME shared predicate the canonicalizer (`./canonicalize.ts`) uses — so the
78
+ * stripper and the canonicalizer recurse into the EXACT same objects, REGARDLESS of
79
+ * prototype (a null-proto `Object.create(null)` bag or a class instance is descended
80
+ * by both). This closes the round-2 secret-leak regression: previously the stripper
81
+ * descended only `Object.prototype`-proto objects while the canonicalizer hashed
82
+ * ANY object, so a secret on a non-plain object was hashed+stored but NOT stripped.
83
+ * Own ENUMERABLE keys are read directly (`Object.keys`), which works for any
84
+ * prototype — so a denylisted key on a null-proto bag is matched too.
85
+ *
86
+ * A special leaf (Date) is NOT descendable ({@link isDescendableObject} excludes it)
87
+ * → passes through untouched here; the canonicalizer owns its value normalization.
88
+ *
89
+ * The shared {@link guardDescent} (visited-`WeakSet` + depth cap) rejects a cyclic /
90
+ * pathologically-deep graph with a TYPED `AuditValidationError('cyclic_attribute')`
91
+ * — never an untyped `RangeError` from stack overflow.
92
+ *
93
+ * `path` is the dotted prefix of the current value (`''` at the top level).
94
+ */
95
+ function stripValue(value, denylistLower, path, strippedKeys, visited, depth) {
96
+ if (Array.isArray(value)) {
97
+ guardDescent(value, visited, depth);
98
+ visited.add(value);
99
+ try {
100
+ return value.map((element, index) => stripValue(element, denylistLower, path === '' ? String(index) : `${path}.${index}`, strippedKeys, visited, depth + 1));
101
+ }
102
+ finally {
103
+ visited.delete(value);
104
+ }
105
+ }
106
+ // Descend by the SHARED predicate (any non-null, non-array, non-special-leaf
107
+ // object — REGARDLESS of prototype). Own enumerable keys are read directly, so a
108
+ // denylisted key on a null-proto / class-instance object is stripped too.
109
+ if (isDescendableObject(value)) {
110
+ guardDescent(value, visited, depth);
111
+ visited.add(value);
112
+ try {
113
+ const obj = value;
114
+ const cleaned = {};
115
+ for (const key of Object.keys(obj)) {
116
+ const keyPath = path === '' ? key : `${path}.${key}`;
117
+ if (isSensitiveKey(key, denylistLower)) {
118
+ // Record only the PATH — never the value (that is the secret).
119
+ strippedKeys.push(keyPath);
120
+ continue;
121
+ }
122
+ cleaned[key] = stripValue(obj[key], denylistLower, keyPath, strippedKeys, visited, depth + 1);
123
+ }
124
+ return cleaned;
125
+ }
126
+ finally {
127
+ visited.delete(value);
128
+ }
129
+ }
130
+ return value;
131
+ }
132
+ /**
133
+ * Strip every attribute whose KEY is denylisted (AC-7). The engine calls this on
134
+ * an event's `attributes` before building/hashing the stored record, so NO raw
135
+ * secret value ever reaches the store, the `contentHash`, or a log.
136
+ *
137
+ * @param attributes The caller's raw attribute bag (or `undefined`).
138
+ * @param denylist Optional REPLACEMENT denylist; defaults to
139
+ * {@link DEFAULT_SENSITIVE_DENYLIST}. Matching is case-insensitive
140
+ * substring on the key. An empty list disables stripping (a
141
+ * documented, caller-owned risk).
142
+ * @returns The `cleaned` bag (secret keys removed) + `strippedKeys` names.
143
+ */
144
+ export function stripSensitive(attributes, denylist = DEFAULT_SENSITIVE_DENYLIST) {
145
+ if (attributes === undefined || attributes === null) {
146
+ return { cleaned: undefined, strippedKeys: [] };
147
+ }
148
+ const denylistLower = denylist.map((d) => d.toLowerCase());
149
+ const strippedKeys = [];
150
+ // Walk the top-level bag recursively: a denylisted key at ANY depth (nested
151
+ // object or array element) is stripped and its dotted path recorded. The bag is
152
+ // descended by the SHARED predicate REGARDLESS of prototype, so a top-level
153
+ // denylist key on a null-proto `Object.create(null)` bag is stripped too (round-2
154
+ // Finding A). A fresh visited-`WeakSet` (depth 0) backs the shared cycle guard.
155
+ const visited = new WeakSet();
156
+ const cleaned = stripValue(attributes, denylistLower, '', strippedKeys, visited, 0);
157
+ return { cleaned, strippedKeys };
158
+ }
159
+ //# sourceMappingURL=sensitive.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sensitive.js","sourceRoot":"","sources":["../../src/audit/sensitive.ts"],"names":[],"mappings":"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"}