@astram/core 1.9.0 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/audit/audit-recorder.d.ts +38 -2
- package/dist/audit/audit-recorder.d.ts.map +1 -1
- package/dist/audit/audit-recorder.js +39 -12
- package/dist/audit/audit-recorder.js.map +1 -1
- package/dist/audit/audit-row.d.ts +63 -0
- package/dist/audit/audit-row.d.ts.map +1 -0
- package/dist/audit/audit-row.js +99 -0
- package/dist/audit/audit-row.js.map +1 -0
- package/dist/audit/errors.d.ts +22 -0
- package/dist/audit/errors.d.ts.map +1 -1
- package/dist/audit/errors.js +18 -0
- package/dist/audit/errors.js.map +1 -1
- package/dist/audit/index.d.ts +1 -0
- package/dist/audit/index.d.ts.map +1 -1
- package/dist/audit/index.js +3 -0
- package/dist/audit/index.js.map +1 -1
- package/dist/audit/ports.d.ts +13 -8
- package/dist/audit/ports.d.ts.map +1 -1
- package/dist/audit/ports.js +7 -5
- package/dist/audit/ports.js.map +1 -1
- package/dist/authentication/captcha.d.ts +58 -0
- package/dist/authentication/captcha.d.ts.map +1 -0
- package/dist/authentication/captcha.js +37 -0
- package/dist/authentication/captcha.js.map +1 -0
- package/dist/authentication/index.d.ts +3 -0
- package/dist/authentication/index.d.ts.map +1 -1
- package/dist/authentication/index.js +6 -0
- package/dist/authentication/index.js.map +1 -1
- package/dist/authentication/login-name.d.ts +22 -0
- package/dist/authentication/login-name.d.ts.map +1 -0
- package/dist/authentication/login-name.js +35 -0
- package/dist/authentication/login-name.js.map +1 -0
- package/dist/authentication/mfa-requirement.d.ts +156 -0
- package/dist/authentication/mfa-requirement.d.ts.map +1 -0
- package/dist/authentication/mfa-requirement.js +122 -0
- package/dist/authentication/mfa-requirement.js.map +1 -0
- package/dist/database/framework-tables.d.ts.map +1 -1
- package/dist/database/framework-tables.js +3 -1
- package/dist/database/framework-tables.js.map +1 -1
- package/dist-cjs/audit/audit-recorder.js +40 -12
- package/dist-cjs/audit/audit-row.js +102 -0
- package/dist-cjs/audit/errors.js +20 -1
- package/dist-cjs/audit/index.js +3 -0
- package/dist-cjs/audit/ports.js +7 -5
- package/dist-cjs/authentication/captcha.js +40 -0
- package/dist-cjs/authentication/index.js +6 -0
- package/dist-cjs/authentication/login-name.js +39 -0
- package/dist-cjs/authentication/mfa-requirement.js +127 -0
- package/dist-cjs/database/framework-tables.js +3 -1
- package/package.json +6 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../src/authentication/captcha.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH,mFAAmF;AACnF,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC7C,WAAW;IACX,cAAc;IACd,cAAc;CACN,CAAC,CAAC;AAGZ,6DAA6D;AAC7D,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC,MAAM,CAAC;IACnD,SAAS;IACT,UAAU;IACV,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,SAAS;CACD,CAAC,CAAC;AAwCZ,4CAA4C;AAC5C,MAAM,UAAU,oBAAoB,CAAC,MAA2B;IAC9D,OAAO,MAAM,CAAC,EAAE,KAAK,KAAK,IAAI,MAAM,CAAC,MAAM,KAAK,aAAa,CAAC;AAChE,CAAC"}
|
|
@@ -50,4 +50,7 @@ export * from './credential-resolver.js';
|
|
|
50
50
|
export * from './totp.js';
|
|
51
51
|
export * from './authenticator.js';
|
|
52
52
|
export * from './service-account-authenticator.js';
|
|
53
|
+
export * from './login-name.js';
|
|
54
|
+
export * from './mfa-requirement.js';
|
|
55
|
+
export * from './captcha.js';
|
|
53
56
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/authentication/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH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AG,UAAU,GAAG,MAAM,GAAG,UAAU,GAAG,eAAe,CAAC;AAE5E,0EAA0E;AAC1E,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACrD;AAED,4CAA4C;AAC5C,MAAM,MAAM,qBAAqB,GAAG,eAAe,GAAG,cAAc,GAAG,UAAU,CAAC;AAElF,mDAAmD;AACnD,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,OAAO,EAAE,qBAAqB,CAAC;IACxC,uDAAuD;IACvD,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;IAC/B,yEAAyE;IACzE,QAAQ,CAAC,gBAAgB,EAAE,SAAS,UAAU,EAAE,CAAC;IACjD,+EAA+E;IAC/E,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,YAAY,CACV,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,SAAS,UAAU,EAAE,GACjC,OAAO,CAAC,oBAAoB,CAAC,CAAC;CAClC;AAKD,cAAc,sBAAsB,CAAC;AAGrC,cAAc,0BAA0B,CAAC;AAEzC,cAAc,WAAW,CAAC;AAE1B,cAAc,oBAAoB,CAAC;AAEnC,cAAc,oCAAoC,CAAC;AAEnD,cAAc,iBAAiB,CAAC;AAEhC,cAAc,sBAAsB,CAAC;AAErC,cAAc,cAAc,CAAC"}
|
|
@@ -26,4 +26,10 @@ export * from './totp.js';
|
|
|
26
26
|
export * from './authenticator.js';
|
|
27
27
|
// Service-account API-key authentication (ADR-0033) — the machine/agent/MCP auth path:
|
|
28
28
|
export * from './service-account-authenticator.js';
|
|
29
|
+
// Login name (sign-in handle) rule: normalise + validate (auth capabilities R1.3, B-4):
|
|
30
|
+
export * from './login-name.js';
|
|
31
|
+
// Mandatory 2FA requirement rule + `MfaRequirementSource` port (auth capabilities R2, B-1, B-2):
|
|
32
|
+
export * from './mfa-requirement.js';
|
|
33
|
+
// CAPTCHA verifier port (auth capabilities R7.1; adapters live in guard, D-4):
|
|
34
|
+
export * from './captcha.js';
|
|
29
35
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/authentication/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AA2CH,iFAAiF;AACjF,iFAAiF;AACjF,aAAa;AACb,cAAc,sBAAsB,CAAC;AACrC,kFAAkF;AAClF,qBAAqB;AACrB,cAAc,0BAA0B,CAAC;AACzC,qFAAqF;AACrF,cAAc,WAAW,CAAC;AAC1B,iEAAiE;AACjE,cAAc,oBAAoB,CAAC;AACnC,uFAAuF;AACvF,cAAc,oCAAoC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/authentication/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AA2CH,iFAAiF;AACjF,iFAAiF;AACjF,aAAa;AACb,cAAc,sBAAsB,CAAC;AACrC,kFAAkF;AAClF,qBAAqB;AACrB,cAAc,0BAA0B,CAAC;AACzC,qFAAqF;AACrF,cAAc,WAAW,CAAC;AAC1B,iEAAiE;AACjE,cAAc,oBAAoB,CAAC;AACnC,uFAAuF;AACvF,cAAc,oCAAoC,CAAC;AACnD,wFAAwF;AACxF,cAAc,iBAAiB,CAAC;AAChC,iGAAiG;AACjG,cAAc,sBAAsB,CAAC;AACrC,+EAA+E;AAC/E,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Login name (the sign-in handle, `users.u_login_name`; auth capabilities R1.3, B-4).
|
|
3
|
+
*
|
|
4
|
+
* One rule shared by guard (login lookup), identity (admin create/update) and `bootstrap:org`:
|
|
5
|
+
* `[a-z0-9._-]`, 3–64 characters, stored lower-case. ASCII input is trimmed and lower-cased,
|
|
6
|
+
* nothing else. Input holding any non-ASCII character is returned as typed (not trimmed, not
|
|
7
|
+
* lower-cased) and so is never valid: the check runs BEFORE lower-casing, because Unicode case
|
|
8
|
+
* mapping can land on ASCII (KELVIN SIGN U+212A lower-cases to `k`). A valid login name never
|
|
9
|
+
* contains `@`, so it can never equal an email address.
|
|
10
|
+
*
|
|
11
|
+
* ADR-0003: framework-agnostic, no I/O.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Trim and lower-case typed ASCII input; no other transformation. Non-ASCII input comes back
|
|
15
|
+
* unchanged, so `isValidLoginName(normalizeLoginName(x))` is false for it.
|
|
16
|
+
*/
|
|
17
|
+
export declare function normalizeLoginName(input: string): string;
|
|
18
|
+
/** True when `value` (already normalized) is an allowed login name. Never throws. */
|
|
19
|
+
export declare function isValidLoginName(value: string): boolean;
|
|
20
|
+
/** True when a typed sign-in hint is an email form (contains `@`); otherwise it may be a handle. */
|
|
21
|
+
export declare function looksLikeEmail(hint: string): boolean;
|
|
22
|
+
//# sourceMappingURL=login-name.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"login-name.d.ts","sourceRoot":"","sources":["../../src/authentication/login-name.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAQH;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIxD;AAED,qFAAqF;AACrF,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEvD;AAED,oGAAoG;AACpG,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEpD"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Login name (the sign-in handle, `users.u_login_name`; auth capabilities R1.3, B-4).
|
|
3
|
+
*
|
|
4
|
+
* One rule shared by guard (login lookup), identity (admin create/update) and `bootstrap:org`:
|
|
5
|
+
* `[a-z0-9._-]`, 3–64 characters, stored lower-case. ASCII input is trimmed and lower-cased,
|
|
6
|
+
* nothing else. Input holding any non-ASCII character is returned as typed (not trimmed, not
|
|
7
|
+
* lower-cased) and so is never valid: the check runs BEFORE lower-casing, because Unicode case
|
|
8
|
+
* mapping can land on ASCII (KELVIN SIGN U+212A lower-cases to `k`). A valid login name never
|
|
9
|
+
* contains `@`, so it can never equal an email address.
|
|
10
|
+
*
|
|
11
|
+
* ADR-0003: framework-agnostic, no I/O.
|
|
12
|
+
*/
|
|
13
|
+
/** The stored form: lower-case `[a-z0-9._-]`, 3–64 characters (never `@`). */
|
|
14
|
+
const LOGIN_NAME_PATTERN = /^[a-z0-9._-]{3,64}$/;
|
|
15
|
+
/** Any character outside 7-bit ASCII (astral characters are surrogate pairs, in range). */
|
|
16
|
+
const NON_ASCII = /[\u0080-\uffff]/;
|
|
17
|
+
/**
|
|
18
|
+
* Trim and lower-case typed ASCII input; no other transformation. Non-ASCII input comes back
|
|
19
|
+
* unchanged, so `isValidLoginName(normalizeLoginName(x))` is false for it.
|
|
20
|
+
*/
|
|
21
|
+
export function normalizeLoginName(input) {
|
|
22
|
+
const typed = String(input);
|
|
23
|
+
if (NON_ASCII.test(typed))
|
|
24
|
+
return typed;
|
|
25
|
+
return typed.trim().toLowerCase();
|
|
26
|
+
}
|
|
27
|
+
/** True when `value` (already normalized) is an allowed login name. Never throws. */
|
|
28
|
+
export function isValidLoginName(value) {
|
|
29
|
+
return typeof value === 'string' && LOGIN_NAME_PATTERN.test(value);
|
|
30
|
+
}
|
|
31
|
+
/** True when a typed sign-in hint is an email form (contains `@`); otherwise it may be a handle. */
|
|
32
|
+
export function looksLikeEmail(hint) {
|
|
33
|
+
return typeof hint === 'string' && hint.includes('@');
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=login-name.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"login-name.js","sourceRoot":"","sources":["../../src/authentication/login-name.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,8EAA8E;AAC9E,MAAM,kBAAkB,GAAG,qBAAqB,CAAC;AAEjD,2FAA2F;AAC3F,MAAM,SAAS,GAAG,iBAAiB,CAAC;AAEpC;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC9C,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,IAAI,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACxC,OAAO,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AACpC,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACrE,CAAC;AAED,oGAAoG;AACpG,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AACxD,CAAC"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mandatory 2FA requirement rule (auth capabilities R2, B-2, D-11) — one pure decision reused at
|
|
3
|
+
* login, at the request-path recompute after a permission-version bump, and by identity's
|
|
4
|
+
* `mfaStatus`. No I/O: the facts come in through {@link MfaRequirementSource} (guard's MariaDB
|
|
5
|
+
* adapter, B-1) and the caller persists the clock the result asks for.
|
|
6
|
+
*
|
|
7
|
+
* Who is subject (R2.1, R2.2, R2.6):
|
|
8
|
+
* - A `tenant` policy at node N applies to everyone whose active scope node is N or below it.
|
|
9
|
+
* - A `role` policy at N applies to holders of that role, active at N or below.
|
|
10
|
+
* - A `permission` policy at N applies to holders of any key it covers (wildcards both ways, the
|
|
11
|
+
* grant forms `module`, `module:*`, `module:type:*`), active at N or below.
|
|
12
|
+
* - A platform-floor policy (`platform: true`, no node) applies everywhere.
|
|
13
|
+
* - Root accounts are subject only to platform-floor policies (D-11).
|
|
14
|
+
*
|
|
15
|
+
* The clock (R2.4) is per user: it starts when the user is first found required and not enrolled,
|
|
16
|
+
* and the deadline is that start plus the SHORTEST grace of the applying policies. Enrolled (a
|
|
17
|
+
* confirmed factor that counts, R2.8: TOTP only today) ⇒ the clock is cleared, so a later
|
|
18
|
+
* requirement starts a new one. Not required at the CURRENT node ⇒ the clock is kept (review round
|
|
19
|
+
* 1, item E): the requirement may still apply at another node of the user's, and clearing here
|
|
20
|
+
* would let a scope switch away and back restart the grace period.
|
|
21
|
+
*
|
|
22
|
+
* Fail closed: malformed input never loosens the requirement (an invalid grace counts as 0, a
|
|
23
|
+
* future clock is clamped to now, an unknown policy kind applies, flags must be strictly `true`).
|
|
24
|
+
*
|
|
25
|
+
* ADR-0003: framework-agnostic, no I/O.
|
|
26
|
+
*/
|
|
27
|
+
import type { AuthFactor } from './index.js';
|
|
28
|
+
/** What a security policy targets (R2.1). */
|
|
29
|
+
export type SecurityPolicyTargetKind = 'tenant' | 'role' | 'permission';
|
|
30
|
+
/** The target kinds, in their stable order. */
|
|
31
|
+
export declare const SECURITY_POLICY_TARGET_KINDS: readonly SecurityPolicyTargetKind[];
|
|
32
|
+
/** Longest grace a policy may give (R2.4). */
|
|
33
|
+
export declare const MAX_MFA_GRACE_DAYS = 14;
|
|
34
|
+
/** One day in milliseconds (grace days → deadline). */
|
|
35
|
+
export declare const MS_PER_DAY: number;
|
|
36
|
+
/**
|
|
37
|
+
* Factors that satisfy a mandatory-2FA policy (R2.8): TOTP today; passkey and email code join when
|
|
38
|
+
* they ship. Recovery codes never satisfy enrolment.
|
|
39
|
+
*/
|
|
40
|
+
export declare const MFA_REQUIREMENT_FACTORS: readonly AuthFactor[];
|
|
41
|
+
/** One stored policy, as the rule reads it (`security_policy` row, R2.9). */
|
|
42
|
+
export interface MfaPolicy {
|
|
43
|
+
/** Stable id, reported back in {@link MfaPolicyApplication.policyIds} (audit only, never UI). */
|
|
44
|
+
readonly id?: string;
|
|
45
|
+
/** The company node the policy covers, with its subtree. `null` only for a platform floor row. */
|
|
46
|
+
readonly scopeNodeId: string | null;
|
|
47
|
+
/** Platform floor (root-written, applies everywhere, R2.2). Must be strictly `true`. */
|
|
48
|
+
readonly platform: boolean;
|
|
49
|
+
readonly kind: SecurityPolicyTargetKind;
|
|
50
|
+
/** Role id (`role`), permission key or wildcard (`permission`); `null` for `tenant`. */
|
|
51
|
+
readonly target: string | null;
|
|
52
|
+
/** 0–14; anything else counts as 0. */
|
|
53
|
+
readonly graceDays: number;
|
|
54
|
+
}
|
|
55
|
+
/** The principal's facts the guard can supply without a new per-request read. */
|
|
56
|
+
export interface MfaSubject {
|
|
57
|
+
/** Strictly `true` for a root account (`SHIELD_ROOT_IDS`): only platform policies apply. */
|
|
58
|
+
readonly isRoot: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* The session's active scope node and its ancestors (scope closure). A non-platform policy
|
|
61
|
+
* applies only when its node is in this list.
|
|
62
|
+
*/
|
|
63
|
+
readonly nodeIds: readonly string[];
|
|
64
|
+
/** In-effect role ids at the active tenant (`CoarseGrants.roleIds`). */
|
|
65
|
+
readonly roleIds: readonly string[];
|
|
66
|
+
/** Effective permission keys (`CoarseGrants.grantedPermissionKeys`; wildcards allowed). */
|
|
67
|
+
readonly permissionKeys: readonly string[];
|
|
68
|
+
}
|
|
69
|
+
export interface MfaRequirementInput {
|
|
70
|
+
readonly policies: readonly MfaPolicy[];
|
|
71
|
+
readonly subject: MfaSubject;
|
|
72
|
+
/** A confirmed factor in {@link MFA_REQUIREMENT_FACTORS}. Must be strictly `true`. */
|
|
73
|
+
readonly hasConfirmedFactor: boolean;
|
|
74
|
+
/** The stored clock (`mfa_requirement_state.mrs_required_since_ms`), or `null`. */
|
|
75
|
+
readonly requiredSinceMs: number | null;
|
|
76
|
+
readonly nowMs: number;
|
|
77
|
+
}
|
|
78
|
+
/** Which policies applied (audit and UI wording; ids are for audit attributes only). */
|
|
79
|
+
export interface MfaPolicyApplication {
|
|
80
|
+
/** Kinds that applied, each once, sorted. */
|
|
81
|
+
readonly kinds: readonly SecurityPolicyTargetKind[];
|
|
82
|
+
/** True when a platform-floor policy applied. */
|
|
83
|
+
readonly platformFloor: boolean;
|
|
84
|
+
/** Ids of the applying policies that carry one, sorted. */
|
|
85
|
+
readonly policyIds: readonly string[];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* What the caller does with the stored clock:
|
|
89
|
+
* - `start`: none (or no usable value) stored: persist `requiredSinceMs` through
|
|
90
|
+
* {@link MfaRequirementSource.setRequiredSince} (keeps the earlier value);
|
|
91
|
+
* - `keep`: the stored value stays (also when not required at the current node);
|
|
92
|
+
* - `clear`: delete the stored row (enrolment only);
|
|
93
|
+
* - `none`: nothing stored, nothing to store.
|
|
94
|
+
*/
|
|
95
|
+
export type MfaClockAction = 'start' | 'keep' | 'clear' | 'none';
|
|
96
|
+
export type MfaRequirement = {
|
|
97
|
+
readonly status: 'not_required';
|
|
98
|
+
/** `keep` when a clock is stored (never cleared here, review round 1 item E), else `none`. */
|
|
99
|
+
readonly clock: 'clear' | 'keep' | 'none';
|
|
100
|
+
readonly applied: MfaPolicyApplication;
|
|
101
|
+
} | {
|
|
102
|
+
readonly status: 'enrolled';
|
|
103
|
+
readonly clock: 'clear' | 'none';
|
|
104
|
+
readonly applied: MfaPolicyApplication;
|
|
105
|
+
} | {
|
|
106
|
+
/** Required, not enrolled, deadline not reached: the session is NOT restricted (R2.4). */
|
|
107
|
+
readonly status: 'grace';
|
|
108
|
+
readonly requiredSinceMs: number;
|
|
109
|
+
readonly deadlineMs: number;
|
|
110
|
+
readonly clock: 'start' | 'keep';
|
|
111
|
+
readonly applied: MfaPolicyApplication;
|
|
112
|
+
} | {
|
|
113
|
+
/** Required, not enrolled, deadline reached: restricted session `mfa_enrollment` (R2.3). */
|
|
114
|
+
readonly status: 'required';
|
|
115
|
+
readonly requiredSinceMs: number;
|
|
116
|
+
readonly clock: 'start' | 'keep';
|
|
117
|
+
readonly applied: MfaPolicyApplication;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Port the guard's MariaDB adapter implements (B-1, task 14). Every method throws (the core
|
|
121
|
+
* `AuthorizationUnavailableError` or any error) when the store cannot answer; the caller then
|
|
122
|
+
* fails closed (denies), never treats an outage as "not required".
|
|
123
|
+
*/
|
|
124
|
+
export interface MfaRequirementSource {
|
|
125
|
+
/** The node and its ancestors from the scope closure, the node itself included. */
|
|
126
|
+
scopeChainOf(nodeId: string): Promise<readonly string[]>;
|
|
127
|
+
/** Policies whose scope node is in `nodeIds`, plus EVERY platform-floor policy. */
|
|
128
|
+
loadPolicies(nodeIds: readonly string[]): Promise<readonly MfaPolicy[]>;
|
|
129
|
+
/** Whether the principal has a confirmed device of a kind in {@link MFA_REQUIREMENT_FACTORS}. */
|
|
130
|
+
hasConfirmedFactor(principalId: string): Promise<boolean>;
|
|
131
|
+
/** The stored clock, or `null` when none. */
|
|
132
|
+
getRequiredSince(principalId: string): Promise<number | null>;
|
|
133
|
+
/**
|
|
134
|
+
* Store the clock: insert when none is stored, otherwise keep the EARLIER of the stored and given
|
|
135
|
+
* values (a concurrent first write keeps the first clock; a skewed future value is corrected).
|
|
136
|
+
* Returns the value now stored. Lifecycle write, never on an unchanged version.
|
|
137
|
+
*/
|
|
138
|
+
setRequiredSince(principalId: string, sinceMs: number): Promise<number>;
|
|
139
|
+
/** Delete the stored clock (enrolled, or no longer required). Idempotent. */
|
|
140
|
+
clearRequiredSince(principalId: string): Promise<void>;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Whether two permission keys overlap under the grant wildcard forms (`module`, `module:*`,
|
|
144
|
+
* `module:type:*`), in either direction: exact = exact, a wildcard covers keys under its prefix,
|
|
145
|
+
* two wildcards overlap when one prefix contains the other. Empty or `*` keys match nothing.
|
|
146
|
+
*/
|
|
147
|
+
export declare function permissionKeysOverlap(a: string, b: string): boolean;
|
|
148
|
+
/**
|
|
149
|
+
* True when `key` may be named by a `permission` policy: an exact `module:type:action` key or a
|
|
150
|
+
* grant wildcard (`module`, `module:*`, `module:type:*`), segments `^[a-z][a-z0-9_]*$`, at most 255
|
|
151
|
+
* characters (the column width).
|
|
152
|
+
*/
|
|
153
|
+
export declare function isSecurityPolicyPermissionTarget(key: string): boolean;
|
|
154
|
+
/** Evaluate the mandatory-2FA requirement for one principal at one instant (see file header). */
|
|
155
|
+
export declare function evaluateMfaRequirement(input: MfaRequirementInput): MfaRequirement;
|
|
156
|
+
//# sourceMappingURL=mfa-requirement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mfa-requirement.d.ts","sourceRoot":"","sources":["../../src/authentication/mfa-requirement.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,6CAA6C;AAC7C,MAAM,MAAM,wBAAwB,GAAG,QAAQ,GAAG,MAAM,GAAG,YAAY,CAAC;AAExE,+CAA+C;AAC/C,eAAO,MAAM,4BAA4B,EAAE,SAAS,wBAAwB,EAI1E,CAAC;AAEH,8CAA8C;AAC9C,eAAO,MAAM,kBAAkB,KAAK,CAAC;AAErC,uDAAuD;AACvD,eAAO,MAAM,UAAU,QAAsB,CAAC;AAE9C;;;GAGG;AACH,eAAO,MAAM,uBAAuB,EAAE,SAAS,UAAU,EAA4B,CAAC;AAEtF,6EAA6E;AAC7E,MAAM,WAAW,SAAS;IACxB,iGAAiG;IACjG,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;IACrB,kGAAkG;IAClG,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,wFAAwF;IACxF,QAAQ,CAAC,QAAQ,EAAE,OAAO,C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CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACxE,6EAA6E;IAC7E,kBAAkB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxD;AASD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CASnE;AAOD;;;;GAIG;AACH,wBAAgB,gCAAgC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAErE;AAqCD,iGAAiG;AACjG,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,mBAAmB,GAAG,cAAc,CA8CjF"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/** The target kinds, in their stable order. */
|
|
2
|
+
export const SECURITY_POLICY_TARGET_KINDS = Object.freeze([
|
|
3
|
+
'tenant',
|
|
4
|
+
'role',
|
|
5
|
+
'permission',
|
|
6
|
+
]);
|
|
7
|
+
/** Longest grace a policy may give (R2.4). */
|
|
8
|
+
export const MAX_MFA_GRACE_DAYS = 14;
|
|
9
|
+
/** One day in milliseconds (grace days → deadline). */
|
|
10
|
+
export const MS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
11
|
+
/**
|
|
12
|
+
* Factors that satisfy a mandatory-2FA policy (R2.8): TOTP today; passkey and email code join when
|
|
13
|
+
* they ship. Recovery codes never satisfy enrolment.
|
|
14
|
+
*/
|
|
15
|
+
export const MFA_REQUIREMENT_FACTORS = Object.freeze(['totp']);
|
|
16
|
+
/** The prefix a wildcard key covers (`identity:*` and bare `identity` → `identity:`), else null. */
|
|
17
|
+
function wildcardPrefix(key) {
|
|
18
|
+
if (key.endsWith(':*'))
|
|
19
|
+
return key.slice(0, -1);
|
|
20
|
+
if (!key.includes(':'))
|
|
21
|
+
return `${key}:`;
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether two permission keys overlap under the grant wildcard forms (`module`, `module:*`,
|
|
26
|
+
* `module:type:*`), in either direction: exact = exact, a wildcard covers keys under its prefix,
|
|
27
|
+
* two wildcards overlap when one prefix contains the other. Empty or `*` keys match nothing.
|
|
28
|
+
*/
|
|
29
|
+
export function permissionKeysOverlap(a, b) {
|
|
30
|
+
if (typeof a !== 'string' || typeof b !== 'string')
|
|
31
|
+
return false;
|
|
32
|
+
if (a === '' || b === '' || a === '*' || b === '*')
|
|
33
|
+
return false;
|
|
34
|
+
const pa = wildcardPrefix(a);
|
|
35
|
+
const pb = wildcardPrefix(b);
|
|
36
|
+
if (pa === null && pb === null)
|
|
37
|
+
return a === b;
|
|
38
|
+
if (pa !== null && pb === null)
|
|
39
|
+
return b.startsWith(pa);
|
|
40
|
+
if (pa === null && pb !== null)
|
|
41
|
+
return a.startsWith(pb);
|
|
42
|
+
return pa.startsWith(pb) || pb.startsWith(pa);
|
|
43
|
+
}
|
|
44
|
+
const SEGMENT = '[a-z][a-z0-9_]*';
|
|
45
|
+
const PERMISSION_TARGET = new RegExp(`^${SEGMENT}(?::\\*|:${SEGMENT}:\\*|:${SEGMENT}:${SEGMENT})?$`);
|
|
46
|
+
/**
|
|
47
|
+
* True when `key` may be named by a `permission` policy: an exact `module:type:action` key or a
|
|
48
|
+
* grant wildcard (`module`, `module:*`, `module:type:*`), segments `^[a-z][a-z0-9_]*$`, at most 255
|
|
49
|
+
* characters (the column width).
|
|
50
|
+
*/
|
|
51
|
+
export function isSecurityPolicyPermissionTarget(key) {
|
|
52
|
+
return typeof key === 'string' && key.length <= 255 && PERMISSION_TARGET.test(key);
|
|
53
|
+
}
|
|
54
|
+
function graceOf(p) {
|
|
55
|
+
const g = p.graceDays;
|
|
56
|
+
return typeof g === 'number' && Number.isInteger(g) && g >= 0 && g <= MAX_MFA_GRACE_DAYS ? g : 0;
|
|
57
|
+
}
|
|
58
|
+
function strings(xs) {
|
|
59
|
+
return Array.isArray(xs) ? xs.filter((x) => typeof x === 'string') : [];
|
|
60
|
+
}
|
|
61
|
+
function applies(p, subject, nodes) {
|
|
62
|
+
const platform = p.platform === true;
|
|
63
|
+
if (subject.isRoot === true && !platform)
|
|
64
|
+
return false; // D-11
|
|
65
|
+
if (!platform) {
|
|
66
|
+
if (typeof p.scopeNodeId !== 'string' || !nodes.has(p.scopeNodeId))
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
switch (p.kind) {
|
|
70
|
+
case 'tenant':
|
|
71
|
+
return true;
|
|
72
|
+
case 'role':
|
|
73
|
+
return (typeof p.target === 'string' &&
|
|
74
|
+
p.target !== '' &&
|
|
75
|
+
strings(subject.roleIds).includes(p.target));
|
|
76
|
+
case 'permission':
|
|
77
|
+
return (typeof p.target === 'string' &&
|
|
78
|
+
strings(subject.permissionKeys).some((k) => permissionKeysOverlap(p.target, k)));
|
|
79
|
+
default:
|
|
80
|
+
// An unknown kind is never silently ignored: it applies to its scope (fail closed).
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Evaluate the mandatory-2FA requirement for one principal at one instant (see file header). */
|
|
85
|
+
export function evaluateMfaRequirement(input) {
|
|
86
|
+
const { subject, nowMs } = input;
|
|
87
|
+
const nodes = new Set(strings(subject.nodeIds));
|
|
88
|
+
const applying = (Array.isArray(input.policies) ? input.policies : []).filter((p) => applies(p, subject, nodes));
|
|
89
|
+
const applied = Object.freeze({
|
|
90
|
+
kinds: Object.freeze([...new Set(applying.map((p) => p.kind))].sort()),
|
|
91
|
+
platformFloor: applying.some((p) => p.platform === true),
|
|
92
|
+
policyIds: Object.freeze([
|
|
93
|
+
...new Set(applying.map((p) => p.id).filter((id) => typeof id === 'string')),
|
|
94
|
+
].sort()),
|
|
95
|
+
});
|
|
96
|
+
const stored = typeof input.requiredSinceMs === 'number' &&
|
|
97
|
+
Number.isFinite(input.requiredSinceMs) &&
|
|
98
|
+
input.requiredSinceMs >= 0
|
|
99
|
+
? input.requiredSinceMs
|
|
100
|
+
: null;
|
|
101
|
+
const hadClock = input.requiredSinceMs !== null && input.requiredSinceMs !== undefined;
|
|
102
|
+
if (applying.length === 0) {
|
|
103
|
+
// Review round 1, item E: "no policy at the current node" never clears the clock (only
|
|
104
|
+
// enrolment does), so switching scope away and back cannot restart a grace period.
|
|
105
|
+
return Object.freeze({ status: 'not_required', clock: hadClock ? 'keep' : 'none', applied });
|
|
106
|
+
}
|
|
107
|
+
if (input.hasConfirmedFactor === true) {
|
|
108
|
+
return Object.freeze({ status: 'enrolled', clock: hadClock ? 'clear' : 'none', applied });
|
|
109
|
+
}
|
|
110
|
+
// A stored clock in the future (clock skew) cannot push the deadline out: it restarts at now,
|
|
111
|
+
// and `setRequiredSince` keeps the earlier of the two values.
|
|
112
|
+
const usable = stored !== null && stored <= nowMs;
|
|
113
|
+
const clock = usable ? 'keep' : 'start';
|
|
114
|
+
const requiredSinceMs = usable ? stored : nowMs;
|
|
115
|
+
const grace = Math.min(...applying.map(graceOf));
|
|
116
|
+
const deadlineMs = requiredSinceMs + grace * MS_PER_DAY;
|
|
117
|
+
if (nowMs < deadlineMs) {
|
|
118
|
+
return Object.freeze({ status: 'grace', requiredSinceMs, deadlineMs, clock, applied });
|
|
119
|
+
}
|
|
120
|
+
return Object.freeze({ status: 'required', requiredSinceMs, clock, applied });
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=mfa-requirement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mfa-requirement.js","sourceRoot":"","sources":["../../src/authentication/mfa-requirement.ts"],"names":[],"mappings":"AA+BA,+CAA+C;AAC/C,MAAM,CAAC,MAAM,4BAA4B,GAAwC,MAAM,CAAC,MAAM,CAAC;IAC7F,QAAQ;IACR,MAAM;IACN,YAAY;CACb,CAAC,CAAC;AAEH,8CAA8C;AAC9C,MAAM,CAAC,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAErC,uDAAuD;AACvD,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,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AAC,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IAC5F,CAAC;IAED,8FAA8F;IAC9F,8DAA8D;IAC9D,MAAM,MAAM,GAAG,MAAM,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,CAAC;IAClD,MAAM,KAAK,GAAqB,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;IAC1D,MAAM,eAAe,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;IAChD,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;IACjD,MAAM,UAAU,GAAG,eAAe,GAAG,KAAK,GAAG,UAAU,CAAC;IACxD,IAAI,KAAK,GAAG,UAAU,EAAE,CAAC;QACvB,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;IACzF,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;AAChF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"framework-tables.d.ts","sourceRoot":"","sources":["../../src/database/framework-tables.ts"],"names":[],"mappings":"AASA,yGAAyG;AACzG,eAAO,MAAM,gBAAgB;;;;;
|
|
1
|
+
{"version":3,"file":"framework-tables.d.ts","sourceRoot":"","sources":["../../src/database/framework-tables.ts"],"names":[],"mappings":"AASA,yGAAyG;AACzG,eAAO,MAAM,gBAAgB;;;;;EAuC3B,CAAC;AAEH,iCAAiC;AACjC,eAAO,MAAM,kBAAkB,EAAE,SAAS,MAAM,EAAmC,CAAC;AAIpF;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,MAAM,EACZ,WAAW,GAAE,MAAuC,GACnD,OAAO,CAGT"}
|
|
@@ -21,6 +21,7 @@ export const FRAMEWORK_TABLES = Object.freeze({
|
|
|
21
21
|
'shared_resource',
|
|
22
22
|
'service_account',
|
|
23
23
|
'stored_file',
|
|
24
|
+
'security_policy',
|
|
24
25
|
]),
|
|
25
26
|
identity: Object.freeze([
|
|
26
27
|
'identity_credential',
|
|
@@ -30,6 +31,7 @@ export const FRAMEWORK_TABLES = Object.freeze({
|
|
|
30
31
|
'password_reset_token',
|
|
31
32
|
'mfa_recovery_code',
|
|
32
33
|
'email_change_token',
|
|
34
|
+
'mfa_requirement_state',
|
|
33
35
|
]),
|
|
34
36
|
authz: Object.freeze([
|
|
35
37
|
'permission',
|
|
@@ -43,7 +45,7 @@ export const FRAMEWORK_TABLES = Object.freeze({
|
|
|
43
45
|
'service_account_key',
|
|
44
46
|
'cross_tenant_grant',
|
|
45
47
|
]),
|
|
46
|
-
audit: Object.freeze(['audit_log']),
|
|
48
|
+
audit: Object.freeze(['audit_log', 'audit_chain', 'audit_chain_checkpoint']),
|
|
47
49
|
});
|
|
48
50
|
/** Framework stored routines. */
|
|
49
51
|
export const FRAMEWORK_ROUTINES = Object.freeze(['prune_audit']);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"framework-tables.js","sourceRoot":"","sources":["../../src/database/framework-tables.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,8BAA8B,EAAE,MAAM,wBAAwB,CAAC;AAExE,yGAAyG;AACzG,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5C,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC;QACjB,WAAW;QACX,OAAO;QACP,OAAO;QACP,aAAa;QACb,UAAU;QACV,oBAAoB;QACpB,oBAAoB;QACpB,qBAAqB;QACrB,kBAAkB;QAClB,iBAAiB;QACjB,iBAAiB;QACjB,aAAa;
|
|
1
|
+
{"version":3,"file":"framework-tables.js","sourceRoot":"","sources":["../../src/database/framework-tables.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,8BAA8B,EAAE,MAAM,wBAAwB,CAAC;AAExE,yGAAyG;AACzG,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5C,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC;QACjB,WAAW;QACX,OAAO;QACP,OAAO;QACP,aAAa;QACb,UAAU;QACV,oBAAoB;QACpB,oBAAoB;QACpB,qBAAqB;QACrB,kBAAkB;QAClB,iBAAiB;QACjB,iBAAiB;QACjB,aAAa;QACb,iBAAiB;KAClB,CAAC;IACF,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC;QACtB,qBAAqB;QACrB,SAAS;QACT,eAAe;QACf,YAAY;QACZ,sBAAsB;QACtB,mBAAmB;QACnB,oBAAoB;QACpB,uBAAuB;KACxB,CAAC;IACF,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC;QACnB,YAAY;QACZ,iBAAiB;QACjB,0BAA0B;QAC1B,eAAe;QACf,0BAA0B;QAC1B,wBAAwB;QACxB,cAAc;QACd,eAAe;QACf,qBAAqB;QACrB,oBAAoB;KACrB,CAAC;IACF,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,aAAa,EAAE,wBAAwB,CAAC,CAAC;CAC7E,CAAC,CAAC;AAEH,iCAAiC;AACjC,MAAM,CAAC,MAAM,kBAAkB,GAAsB,MAAM,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC;AAEpF,MAAM,QAAQ,GAAwB,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;AAEtF;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CACtC,IAAY,EACZ,cAAsB,8BAA8B;IAEpD,MAAM,CAAC,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;IAC7B,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,WAAW,CAAC,WAAW,EAAE,CAAC;AAC5D,CAAC"}
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
*/
|
|
44
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
45
|
exports.GENESIS = void 0;
|
|
46
|
+
exports.linkEntry = linkEntry;
|
|
46
47
|
exports.createAuditRecorder = createAuditRecorder;
|
|
47
48
|
exports.verifyChain = verifyChain;
|
|
48
49
|
const canonicalize_js_1 = require("./canonicalize.js");
|
|
@@ -66,6 +67,23 @@ const CHAIN_SEP = '|';
|
|
|
66
67
|
function entryPreimage(contentHash, prevHash, sequenceNumber) {
|
|
67
68
|
return `${contentHash}${CHAIN_SEP}${prevHash}${CHAIN_SEP}${sequenceNumber}`;
|
|
68
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* The pure chain math for ONE event (§3): `contentHash = H(canonicalize(content))`,
|
|
72
|
+
* `sequenceNumber = prev.sequenceNumber + 1` (or `0`), `prevHash = prev.entryHash`
|
|
73
|
+
* (or {@link GENESIS}), `entryHash = H(contentHash ‖ prevHash ‖ sequenceNumber)`.
|
|
74
|
+
*
|
|
75
|
+
* `content` is hashed exactly as given: the caller passes the already-cleaned event
|
|
76
|
+
* (the recorder strips secrets first; the sealer builds it from the stored row with
|
|
77
|
+
* `auditEventFromRow`). Equal inputs give equal outputs. `prev.tenantId` is not read
|
|
78
|
+
* here; the caller keeps chains per tenant.
|
|
79
|
+
*/
|
|
80
|
+
function linkEntry(content, prev, hash) {
|
|
81
|
+
const sequenceNumber = prev !== undefined ? prev.sequenceNumber + 1 : 0;
|
|
82
|
+
const prevHash = prev !== undefined ? prev.entryHash : exports.GENESIS;
|
|
83
|
+
const contentHash = hash.hash((0, canonicalize_js_1.canonicalizeEvent)(content));
|
|
84
|
+
const entryHash = hash.hash(entryPreimage(contentHash, prevHash, sequenceNumber));
|
|
85
|
+
return { sequenceNumber, contentHash, prevHash, entryHash };
|
|
86
|
+
}
|
|
69
87
|
/**
|
|
70
88
|
* A non-blank string guard — an actor/tenant id must be a present, non-whitespace
|
|
71
89
|
* string. (`.trim()` catches `' '`, which a bare `length === 0` would miss.)
|
|
@@ -134,10 +152,13 @@ function createAuditRecorder(deps) {
|
|
|
134
152
|
// prevHash from it. Per-tenant: `readChain` only returns THIS tenant's records.
|
|
135
153
|
const chain = await store.readChain(event.tenantId);
|
|
136
154
|
const tail = chain.length > 0 ? chain[chain.length - 1] : undefined;
|
|
137
|
-
const sequenceNumber
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
155
|
+
const { sequenceNumber, contentHash, prevHash, entryHash } = linkEntry(cleanEvent, tail !== undefined
|
|
156
|
+
? {
|
|
157
|
+
tenantId: tail.tenantId,
|
|
158
|
+
sequenceNumber: tail.sequenceNumber,
|
|
159
|
+
entryHash: tail.entryHash,
|
|
160
|
+
}
|
|
161
|
+
: undefined, hash);
|
|
141
162
|
// ── (4) STAMP (AC-9). The engine's own clock — distinct from occurredAtMs.
|
|
142
163
|
const recordedAtMs = clock.nowMs();
|
|
143
164
|
const stored = {
|
|
@@ -205,17 +226,24 @@ function createAuditRecorder(deps) {
|
|
|
205
226
|
* - `content_mismatch` — the recomputed `contentHash`/`entryHash` ≠ the stored
|
|
206
227
|
* ones: a field of the record was mutated.
|
|
207
228
|
*
|
|
229
|
+
* With an `anchor` (a pruned chain's checkpoint, or the last record of the previous
|
|
230
|
+
* page), the chain is expected to continue from it: the expected tenant is
|
|
231
|
+
* `anchor.tenantId` (a foreign first record is reported at index 0), the first
|
|
232
|
+
* sequence `anchor.sequenceNumber + 1` and the first `prevHash` `anchor.entryHash`.
|
|
233
|
+
* Without one, the chain must start at sequence 0 from {@link GENESIS}.
|
|
234
|
+
*
|
|
208
235
|
* @param records An ordered, single-tenant chain (sequence ascending).
|
|
209
236
|
* @param hash The SAME deterministic hash used to write the chain.
|
|
237
|
+
* @param anchor Optional position the records continue from.
|
|
210
238
|
*/
|
|
211
|
-
function verifyChain(records, hash) {
|
|
212
|
-
let expectedPrevHash = exports.GENESIS;
|
|
213
|
-
let expectedSequence = 0;
|
|
214
|
-
// The chain is contractually SINGLE-tenant. Pin the expected tenant to the
|
|
215
|
-
// record's; any
|
|
216
|
-
// (a foreign record chained on at the right sequence can self-verify) and MUST
|
|
217
|
-
// rejected. An empty array
|
|
218
|
-
const expectedTenantId = records.length > 0 ? records[0]?.tenantId : undefined;
|
|
239
|
+
function verifyChain(records, hash, anchor) {
|
|
240
|
+
let expectedPrevHash = anchor !== undefined ? anchor.entryHash : exports.GENESIS;
|
|
241
|
+
let expectedSequence = anchor !== undefined ? anchor.sequenceNumber + 1 : 0;
|
|
242
|
+
// The chain is contractually SINGLE-tenant. Pin the expected tenant to the anchor's
|
|
243
|
+
// (else the first record's); any record with a different `tenantId` is a cross-tenant
|
|
244
|
+
// splice (a foreign record chained on at the right sequence can self-verify) and MUST
|
|
245
|
+
// be rejected. An empty array is vacuously intact (loop does not run).
|
|
246
|
+
const expectedTenantId = anchor !== undefined ? anchor.tenantId : records.length > 0 ? records[0]?.tenantId : undefined;
|
|
219
247
|
for (let i = 0; i < records.length; i++) {
|
|
220
248
|
const record = records[i];
|
|
221
249
|
if (record === undefined) {
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Stored-row mapping for the durable audit chain (audit-chain-store SPEC §5.D).
|
|
4
|
+
*
|
|
5
|
+
* `auditEventFromRow` is THE one mapping from an `audit_log` row back to the
|
|
6
|
+
* {@link AuditEvent} whose canonical form is hashed into the chain. The sealer (guard)
|
|
7
|
+
* and the verifier (`astram-migrate audit:verify`) both use it, so the content they
|
|
8
|
+
* hash is built the same way from the same stored columns. It is the inverse of the
|
|
9
|
+
* one INSERT shape (`@astram/engine` `MariaDbAuditSink.record`):
|
|
10
|
+
* - `al_type`, `al_actor_id`, `al_tenant_id` ⇒ `type`, `actorId`, `tenantId`;
|
|
11
|
+
* - `al_occurred_at_ms` (BIGINT) ⇒ `occurredAtMs` via {@link toSafeInteger};
|
|
12
|
+
* - `al_action_key` ⇒ `action: { key }`;
|
|
13
|
+
* - `al_resource_json`, `al_decision_json`, `al_attributes_json` ⇒ the parsed value
|
|
14
|
+
* (the driver may return JSON text or an already-parsed value);
|
|
15
|
+
* - SQL NULL ⇒ the field is absent (never `undefined`-valued, never `null`);
|
|
16
|
+
* - `al_uuid` is the row key, not content: it is never part of the event.
|
|
17
|
+
*
|
|
18
|
+
* BIGINT values never reach the hashed content as `bigint` (`canonicalizeEvent`
|
|
19
|
+
* rejects them): they are converted to a safe `number` or the row is rejected with a
|
|
20
|
+
* typed {@link AuditRowError}. A JSON column whose text does not parse is also rejected
|
|
21
|
+
* typed (`invalid_json`), so the sealer can tell a repeating row problem from a
|
|
22
|
+
* transient database error.
|
|
23
|
+
*
|
|
24
|
+
* Kept in its own file (not `audit-recorder.ts`): it is row/driver shape knowledge,
|
|
25
|
+
* not chain math, and the recorder stays independent of any storage layout.
|
|
26
|
+
*
|
|
27
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
28
|
+
*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.toSafeInteger = toSafeInteger;
|
|
31
|
+
exports.auditEventFromRow = auditEventFromRow;
|
|
32
|
+
const errors_js_1 = require("./errors.js");
|
|
33
|
+
const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
|
|
34
|
+
const MIN_SAFE = BigInt(Number.MIN_SAFE_INTEGER);
|
|
35
|
+
/** Optional sign, then decimal digits only; 17 digits already exceed the safe range. */
|
|
36
|
+
const DECIMAL_INTEGER = /^-?[0-9]{1,20}$/;
|
|
37
|
+
/**
|
|
38
|
+
* Convert a BIGINT column value (`number`, `bigint`, or decimal-integer string) to a
|
|
39
|
+
* JS `number`, refusing anything that is not exactly representable: a non-integer, a
|
|
40
|
+
* value outside `Number.MIN_SAFE_INTEGER..MAX_SAFE_INTEGER`, `NaN`/`±Infinity`, an
|
|
41
|
+
* empty or non-decimal string, `null`, `undefined` or any other type ⇒
|
|
42
|
+
* `AuditRowError('unsafe_integer')` (the value is never put in the message).
|
|
43
|
+
*
|
|
44
|
+
* Used for `al_occurred_at_ms`, `ac_sequence` and `acc_sequence`.
|
|
45
|
+
*/
|
|
46
|
+
function toSafeInteger(value) {
|
|
47
|
+
const v = value;
|
|
48
|
+
if (typeof v === 'number') {
|
|
49
|
+
if (!Number.isSafeInteger(v))
|
|
50
|
+
throw new errors_js_1.AuditRowError('unsafe_integer');
|
|
51
|
+
return v === 0 ? 0 : v; // normalize -0
|
|
52
|
+
}
|
|
53
|
+
if (typeof v === 'bigint') {
|
|
54
|
+
if (v > MAX_SAFE || v < MIN_SAFE)
|
|
55
|
+
throw new errors_js_1.AuditRowError('unsafe_integer');
|
|
56
|
+
return Number(v);
|
|
57
|
+
}
|
|
58
|
+
if (typeof v === 'string' && DECIMAL_INTEGER.test(v)) {
|
|
59
|
+
return toSafeInteger(BigInt(v));
|
|
60
|
+
}
|
|
61
|
+
throw new errors_js_1.AuditRowError('unsafe_integer');
|
|
62
|
+
}
|
|
63
|
+
/** A JSON column: SQL NULL (or a parsed JSON null) ⇒ absent; text ⇒ parsed; else as given. */
|
|
64
|
+
function jsonColumn(value) {
|
|
65
|
+
if (value === null || value === undefined)
|
|
66
|
+
return undefined;
|
|
67
|
+
if (typeof value !== 'string')
|
|
68
|
+
return value;
|
|
69
|
+
let parsed;
|
|
70
|
+
try {
|
|
71
|
+
parsed = JSON.parse(value);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
throw new errors_js_1.AuditRowError('invalid_json');
|
|
75
|
+
}
|
|
76
|
+
return parsed === null ? undefined : parsed;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Map one stored `audit_log` row to the {@link AuditEvent} that is hashed into the
|
|
80
|
+
* chain (see the file header for the column mapping). Throws {@link AuditRowError}
|
|
81
|
+
* for an unsafe BIGINT or unparseable JSON text.
|
|
82
|
+
*/
|
|
83
|
+
function auditEventFromRow(row) {
|
|
84
|
+
const occurredAtMs = toSafeInteger(row.al_occurred_at_ms);
|
|
85
|
+
const actionKey = row.al_action_key;
|
|
86
|
+
const resource = jsonColumn(row.al_resource_json);
|
|
87
|
+
const decision = jsonColumn(row.al_decision_json);
|
|
88
|
+
const attributes = jsonColumn(row.al_attributes_json);
|
|
89
|
+
const action = actionKey !== null && actionKey !== undefined ? { key: actionKey } : undefined;
|
|
90
|
+
return {
|
|
91
|
+
type: row.al_type,
|
|
92
|
+
occurredAtMs,
|
|
93
|
+
actorId: row.al_actor_id,
|
|
94
|
+
tenantId: row.al_tenant_id,
|
|
95
|
+
...(action !== undefined ? { action } : {}),
|
|
96
|
+
...(resource !== undefined ? { resource: resource } : {}),
|
|
97
|
+
...(decision !== undefined ? { decision: decision } : {}),
|
|
98
|
+
...(attributes !== undefined
|
|
99
|
+
? { attributes: attributes }
|
|
100
|
+
: {}),
|
|
101
|
+
};
|
|
102
|
+
}
|