@adaptic/backend-legacy 0.0.1057 → 0.0.1059
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/auth/authorization-map.cjs +306 -0
- package/auth/authorization-map.d.ts +143 -0
- package/auth/cortex-auth-checker.cjs +172 -0
- package/auth/cortex-auth-checker.d.ts +114 -0
- package/auth/credential-fields.cjs +51 -0
- package/auth/credential-fields.d.ts +19 -0
- package/auth/credential-redaction.cjs +134 -0
- package/auth/credential-redaction.d.ts +60 -0
- package/auth/graphql-auth-shadow.cjs +317 -0
- package/auth/graphql-auth-shadow.d.ts +194 -0
- package/auth/membership-write-authority.cjs +131 -0
- package/auth/membership-write-authority.d.ts +110 -0
- package/auth/mutation-authorization.cjs +376 -0
- package/auth/mutation-authorization.d.ts +261 -0
- package/auth/nested-write-inspector.cjs +189 -0
- package/auth/nested-write-inspector.d.ts +94 -0
- package/auth/nested-write-policy.cjs +189 -0
- package/auth/nested-write-policy.d.ts +99 -0
- package/auth/service-token.cjs +339 -0
- package/auth/service-token.d.ts +106 -0
- package/auth/tenancy-scope.cjs +736 -0
- package/auth/tenancy-scope.d.ts +347 -0
- package/auth/token-verifier.cjs +451 -0
- package/auth/token-verifier.d.ts +153 -0
- package/auth/totp.cjs +265 -0
- package/auth/totp.d.ts +111 -0
- package/auth/user-write-content.cjs +116 -0
- package/auth/user-write-content.d.ts +47 -0
- package/auth/ws-auth-context.cjs +75 -0
- package/auth/ws-auth-context.d.ts +69 -0
- package/config/jwtConfig.cjs +57 -0
- package/config/jwtConfig.d.ts +16 -0
- package/config/metrics.cjs +356 -0
- package/config/metrics.d.ts +128 -0
- package/config/persisted-queries.cjs +122 -0
- package/config/persisted-queries.d.ts +40 -0
- package/config/server-only-models.cjs +166 -0
- package/config/server-only-models.d.ts +89 -0
- package/config/tracing.d.ts +24 -0
- package/esm/auth/service-token.d.ts +12 -0
- package/esm/auth/service-token.mjs +27 -5
- package/esm/config/jwtConfig.mjs +5 -1
- package/generated/typegraphql-prisma/enhance.cjs +3 -3
- package/generated/typegraphql-prisma/index.cjs +6 -6
- package/generated/typegraphql-prisma/resolvers/crud/args.index.cjs +95 -95
- package/generated/typegraphql-prisma/resolvers/outputs/index.cjs +1 -1
- package/generated/typegraphql-prisma/resolvers/relations/args.index.cjs +32 -32
- package/middleware/audit-log-write-redaction.cjs +143 -0
- package/middleware/audit-log-write-redaction.d.ts +82 -0
- package/middleware/audit-logger.cjs +378 -0
- package/middleware/audit-logger.d.ts +158 -0
- package/middleware/audit-payload-guard.cjs +107 -0
- package/middleware/audit-payload-guard.d.ts +85 -0
- package/middleware/auth.cjs +66 -0
- package/middleware/auth.d.ts +41 -0
- package/middleware/credential-field-guard.cjs +514 -0
- package/middleware/credential-field-guard.d.ts +173 -0
- package/middleware/generated-type-names.cjs +77 -0
- package/middleware/generated-type-names.d.ts +44 -0
- package/middleware/graphql-validation-plugin.cjs +310 -0
- package/middleware/graphql-validation-plugin.d.ts +98 -0
- package/middleware/guard-log-throttle.cjs +89 -0
- package/middleware/guard-log-throttle.d.ts +80 -0
- package/middleware/index.cjs +57 -0
- package/middleware/index.d.ts +14 -0
- package/middleware/input-validator.cjs +220 -0
- package/middleware/input-validator.d.ts +63 -0
- package/middleware/mandate-version-validation.cjs +131 -0
- package/middleware/mandate-version-validation.d.ts +41 -0
- package/middleware/mutation-account-facts.cjs +223 -0
- package/middleware/mutation-account-facts.d.ts +143 -0
- package/middleware/mutation-auth-guard.cjs +536 -0
- package/middleware/mutation-auth-guard.d.ts +100 -0
- package/middleware/mutation-membership-facts.cjs +276 -0
- package/middleware/mutation-membership-facts.d.ts +133 -0
- package/middleware/mutation-tenant-facts.cjs +132 -0
- package/middleware/mutation-tenant-facts.d.ts +81 -0
- package/middleware/query-complexity.cjs +182 -0
- package/middleware/query-complexity.d.ts +56 -0
- package/middleware/rate-limiter.cjs +223 -0
- package/middleware/rate-limiter.d.ts +93 -0
- package/middleware/rate-tier.cjs +111 -0
- package/middleware/rate-tier.d.ts +56 -0
- package/middleware/soft-delete.cjs +175 -0
- package/middleware/soft-delete.d.ts +146 -0
- package/middleware/tenancy-scoping.cjs +229 -0
- package/middleware/tenancy-scoping.d.ts +67 -0
- package/middleware/trading-policy-audit.cjs +353 -0
- package/middleware/trading-policy-audit.d.ts +184 -0
- package/middleware/types.cjs +17 -0
- package/middleware/types.d.ts +87 -0
- package/middleware/validation-examples.cjs +398 -0
- package/middleware/validation-examples.d.ts +76 -0
- package/package.json +10 -2
- package/readers/index.cjs +15 -0
- package/readers/index.d.ts +10 -0
- package/readers/intraday-lane-prefs.cjs +76 -0
- package/readers/intraday-lane-prefs.d.ts +45 -0
- package/esm/config/__tests__/server-only-surface.harness.d.ts +0 -12
- package/esm/config/__tests__/server-only-surface.harness.mjs +0 -41
- package/esm/middleware/__tests__/audit-log-write-redaction.harness.d.ts +0 -144
- package/esm/middleware/__tests__/audit-log-write-redaction.harness.mjs +0 -920
- package/esm/middleware/__tests__/credential-field-coverage.harness.d.ts +0 -55
- package/esm/middleware/__tests__/credential-field-coverage.harness.mjs +0 -440
- package/esm/middleware/__tests__/credential-field-guard.harness.d.ts +0 -34
- package/esm/middleware/__tests__/credential-field-guard.harness.mjs +0 -212
- package/esm/middleware/__tests__/mutation-auth-guard.harness.d.ts +0 -56
- package/esm/middleware/__tests__/mutation-auth-guard.harness.mjs +0 -575
- package/esm/middleware/__tests__/rate-limit-test-utils.d.ts +0 -60
- package/esm/middleware/__tests__/rate-limit-test-utils.mjs +0 -85
- package/esm/middleware/__tests__/ts-node-harness.d.ts +0 -10
- package/esm/middleware/__tests__/ts-node-harness.mjs +0 -71
- package/health.cjs +0 -274
- package/server.cjs +0 -607
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CORTEX-P0-001 — resolver-level authorization AuthChecker (SHADOW-FIRST).
|
|
3
|
+
*
|
|
4
|
+
* A TypeGraphQL {@link AuthChecker} that evaluates whether an operation WOULD be
|
|
5
|
+
* allowed under resolver-level authorization, closing the residual vector called
|
|
6
|
+
* out in the tenancy-scoping middleware docs: a governed model reached
|
|
7
|
+
* transitively through a non-governed root, and — more broadly — any
|
|
8
|
+
* `@Authorized()`-decorated field executed without a verified principal.
|
|
9
|
+
*
|
|
10
|
+
* CRITICAL SAFETY CONTRACT — this change is additive and default-OFF. When
|
|
11
|
+
* enforcement is OFF (the default), the checker NEVER denies: it returns `true`
|
|
12
|
+
* for every operation, emitting a structured warn log and incrementing a
|
|
13
|
+
* Prometheus counter for any operation that enforcement WOULD deny. Live
|
|
14
|
+
* behaviour is byte-identical to before this change until
|
|
15
|
+
* `CORTEX_AUTHCHECKER_ENFORCE` is flipped on. Additionally, TypeGraphQL only
|
|
16
|
+
* invokes an AuthChecker for fields decorated with `@Authorized()`; the
|
|
17
|
+
* auto-generated typegraphql-prisma resolvers carry no such decorator, so
|
|
18
|
+
* registering this checker via `buildSchema({ authChecker })` is inert at
|
|
19
|
+
* runtime until decorators are added in the separate enforcement phase.
|
|
20
|
+
*
|
|
21
|
+
* Decision model (mirrors the token-verifier principal kinds):
|
|
22
|
+
*
|
|
23
|
+
* - `server` — trusted server-to-server caller. ALWAYS allowed; the
|
|
24
|
+
* engine + utils call the CRUD resolvers with a server principal and must
|
|
25
|
+
* retain full access, bypassing any role gate.
|
|
26
|
+
* - `admin` / `user` — verified end-user. Allowed when the field requires no
|
|
27
|
+
* specific role, or when the principal carries one of the required roles;
|
|
28
|
+
* otherwise a would-deny with reason `insufficient_role`.
|
|
29
|
+
* - `null` (no verified principal) — a genuinely unauthenticated caller on a
|
|
30
|
+
* non-public (i.e. `@Authorized()`) operation. A would-deny with reason
|
|
31
|
+
* `unauthenticated`.
|
|
32
|
+
*
|
|
33
|
+
* @see src/auth/tenancy-scope.ts for the row-level (data-scope) counterpart.
|
|
34
|
+
* @see src/auth/token-verifier.ts for `BackendPrincipal`.
|
|
35
|
+
*/
|
|
36
|
+
import { Counter } from 'prom-client';
|
|
37
|
+
import type { AuthChecker } from 'type-graphql';
|
|
38
|
+
import type { BackendPrincipal } from './token-verifier';
|
|
39
|
+
/**
|
|
40
|
+
* Environment variable gating enforcement. When set to `true`/`1`, the checker
|
|
41
|
+
* fails closed (returns `false`) for any would-deny operation. Unset or any
|
|
42
|
+
* other value keeps the checker in SHADOW mode (observe + count + allow).
|
|
43
|
+
*/
|
|
44
|
+
export declare const CORTEX_AUTHCHECKER_ENFORCE_ENV = "CORTEX_AUTHCHECKER_ENFORCE";
|
|
45
|
+
/**
|
|
46
|
+
* Whether resolver-level authorization enforcement is ON. Read fresh on each
|
|
47
|
+
* call (a cheap env read) so the shadow→enforce graduation can be flipped
|
|
48
|
+
* operationally without a process restart — the same pattern the tenancy-scoping
|
|
49
|
+
* middleware uses. Defaults to OFF (shadow) for any unset/unrecognised value.
|
|
50
|
+
*
|
|
51
|
+
* @param env - Environment source (defaults to `process.env`); injectable for tests.
|
|
52
|
+
* @returns `true` only when the flag is explicitly `true`/`1`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function isAuthCheckerEnforced(env?: Record<string, string | undefined>): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Finite set of reasons enforcement WOULD deny an operation. Used as the sole
|
|
57
|
+
* label on {@link cortexAuthCheckerWouldDenyTotal}.
|
|
58
|
+
*/
|
|
59
|
+
export type WouldDenyReason = 'unauthenticated' | 'insufficient_role';
|
|
60
|
+
/**
|
|
61
|
+
* Counts EVERY AuthChecker invocation, regardless of outcome (audit
|
|
62
|
+
* B01-backend-legacy-07). A flat-zero {@link cortexAuthCheckerWouldDenyTotal}
|
|
63
|
+
* is ambiguous on its own: it can mean "no traffic would be denied" OR "the
|
|
64
|
+
* checker never ran" (no `@Authorized()` field was ever executed) — exactly the
|
|
65
|
+
* false-confidence pattern that preceded the beta-net-cap fail-open. Graduation
|
|
66
|
+
* rule: enforce only when this counter is > 0 and the would-deny series has
|
|
67
|
+
* been observed over a full trading week.
|
|
68
|
+
*/
|
|
69
|
+
export declare const cortexAuthCheckerEvaluationsTotal: Counter<string>;
|
|
70
|
+
/**
|
|
71
|
+
* Counts operations that resolver-level authorization WOULD deny. In shadow mode
|
|
72
|
+
* this is the primary signal proving whether flipping `CORTEX_AUTHCHECKER_ENFORCE`
|
|
73
|
+
* to `enforce` would reject any legitimate platform or engine traffic. Labelled
|
|
74
|
+
* by the discriminated {@link WouldDenyReason}.
|
|
75
|
+
*/
|
|
76
|
+
export declare const cortexAuthCheckerWouldDenyTotal: Counter<"reason">;
|
|
77
|
+
/** The outcome of evaluating whether an operation would be allowed. */
|
|
78
|
+
interface AuthEvaluation {
|
|
79
|
+
readonly wouldAllow: boolean;
|
|
80
|
+
/** Populated only when `wouldAllow` is `false`. */
|
|
81
|
+
readonly reason?: WouldDenyReason;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Pure evaluation of whether a principal would satisfy the required roles for an
|
|
85
|
+
* operation. Performs no I/O and no logging.
|
|
86
|
+
*
|
|
87
|
+
* @param principal - The verified principal, or `null` for an unauthenticated caller.
|
|
88
|
+
* @param requiredRoles - Roles declared on the field's `@Authorized(...)` decorator
|
|
89
|
+
* (empty when the field only requires authentication).
|
|
90
|
+
* @returns The {@link AuthEvaluation}.
|
|
91
|
+
*/
|
|
92
|
+
export declare function evaluateWouldAllow(principal: BackendPrincipal | null, requiredRoles: readonly string[]): AuthEvaluation;
|
|
93
|
+
/**
|
|
94
|
+
* GraphQL context shape this checker reads. `principal` is the verified
|
|
95
|
+
* `BackendPrincipal` attached to the context in `server.ts` (`null` for an
|
|
96
|
+
* unauthenticated caller).
|
|
97
|
+
*/
|
|
98
|
+
export interface CortexAuthContext {
|
|
99
|
+
principal?: BackendPrincipal | null;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The CORTEX-P0-001 AuthChecker. Register via
|
|
103
|
+
* `buildSchema({ authChecker: cortexAuthChecker })`.
|
|
104
|
+
*
|
|
105
|
+
* SHADOW (default): a would-deny operation increments
|
|
106
|
+
* {@link cortexAuthCheckerWouldDenyTotal}, emits a warn log, and RETURNS TRUE —
|
|
107
|
+
* no behaviour change. ENFORCE (`CORTEX_AUTHCHECKER_ENFORCE=true`): a would-deny
|
|
108
|
+
* operation is logged, the counter is incremented, and the checker RETURNS FALSE
|
|
109
|
+
* (fail closed). Operations that would be allowed always return `true` with no
|
|
110
|
+
* metric or log emission.
|
|
111
|
+
*/
|
|
112
|
+
export declare const cortexAuthChecker: AuthChecker<CortexAuthContext>;
|
|
113
|
+
export {};
|
|
114
|
+
//# sourceMappingURL=cortex-auth-checker.d.ts.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The stored credential columns, by Prisma model.
|
|
4
|
+
*
|
|
5
|
+
* One vocabulary for every place that has to know what a credential is: the
|
|
6
|
+
* credential-field guard (what may be read, filtered or grouped on), the
|
|
7
|
+
* redaction applied to audit payloads on write, and the redaction applied to
|
|
8
|
+
* audit payloads on read. It lives in a module that imports nothing, so each
|
|
9
|
+
* of those can depend on it without depending on the others.
|
|
10
|
+
*
|
|
11
|
+
* @module auth/credential-fields
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.CREDENTIAL_FIELDS = void 0;
|
|
15
|
+
/**
|
|
16
|
+
* Every stored credential column, by Prisma model. A column belongs here when
|
|
17
|
+
* its value lets the holder act as someone: a broker or vendor API secret, an
|
|
18
|
+
* OAuth access / refresh / id token, a session token, a one-time verification
|
|
19
|
+
* or invite token.
|
|
20
|
+
*/
|
|
21
|
+
exports.CREDENTIAL_FIELDS = new Map([
|
|
22
|
+
['AlpacaAccount', new Set(['APIKey', 'APISecret'])],
|
|
23
|
+
['BrokerageAccount', new Set(['apiKey', 'apiSecret'])],
|
|
24
|
+
[
|
|
25
|
+
'LlmConfiguration',
|
|
26
|
+
new Set([
|
|
27
|
+
'openaiApiKey',
|
|
28
|
+
'anthropicApiKey',
|
|
29
|
+
'deepseekApiKey',
|
|
30
|
+
'kimiApiKey',
|
|
31
|
+
'qwenApiKey',
|
|
32
|
+
'xaiApiKey',
|
|
33
|
+
'geminiApiKey',
|
|
34
|
+
'deepinfraApiKey',
|
|
35
|
+
]),
|
|
36
|
+
],
|
|
37
|
+
['User', new Set(['openaiAPIKey'])],
|
|
38
|
+
['Account', new Set(['refresh_token', 'access_token', 'id_token'])],
|
|
39
|
+
['LinkedProvider', new Set(['accessToken', 'refreshToken'])],
|
|
40
|
+
['Session', new Set(['sessionToken'])],
|
|
41
|
+
['VerificationToken', new Set(['token'])],
|
|
42
|
+
['AccountLinkingRequest', new Set(['verificationToken'])],
|
|
43
|
+
['InviteToken', new Set(['token'])],
|
|
44
|
+
// The TOTP secret (encrypted, but decryptable by whoever holds the key) and
|
|
45
|
+
// the recovery-code hashes are a second factor. The schema already omits
|
|
46
|
+
// them from every output and predicate type; listing them here also keeps
|
|
47
|
+
// them out of `groupBy` / `distinct` for non-service callers and redacts
|
|
48
|
+
// them from audit payloads copied from mutation variables.
|
|
49
|
+
['UserTotp', new Set(['secretEnc', 'recoveryCodesHash'])],
|
|
50
|
+
]);
|
|
51
|
+
//# sourceMappingURL=credential-fields.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The stored credential columns, by Prisma model.
|
|
3
|
+
*
|
|
4
|
+
* One vocabulary for every place that has to know what a credential is: the
|
|
5
|
+
* credential-field guard (what may be read, filtered or grouped on), the
|
|
6
|
+
* redaction applied to audit payloads on write, and the redaction applied to
|
|
7
|
+
* audit payloads on read. It lives in a module that imports nothing, so each
|
|
8
|
+
* of those can depend on it without depending on the others.
|
|
9
|
+
*
|
|
10
|
+
* @module auth/credential-fields
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Every stored credential column, by Prisma model. A column belongs here when
|
|
14
|
+
* its value lets the holder act as someone: a broker or vendor API secret, an
|
|
15
|
+
* OAuth access / refresh / id token, a session token, a one-time verification
|
|
16
|
+
* or invite token.
|
|
17
|
+
*/
|
|
18
|
+
export declare const CREDENTIAL_FIELDS: ReadonlyMap<string, ReadonlySet<string>>;
|
|
19
|
+
//# sourceMappingURL=credential-fields.d.ts.map
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Redact stored-credential values from a payload before it is persisted or
|
|
4
|
+
* logged.
|
|
5
|
+
*
|
|
6
|
+
* The audit trail records what a mutation asked for, and a mutation that
|
|
7
|
+
* writes a broker key carries the key in its arguments. An audit row is read
|
|
8
|
+
* by far more people and processes than the credential column itself, so a
|
|
9
|
+
* credential copied into one is a second, unguarded copy of the secret. Every
|
|
10
|
+
* `AuditLog` write passes through here before it is stored, whoever makes it:
|
|
11
|
+
* the server's Prisma client applies it to each row an `AuditLog` operation
|
|
12
|
+
* writes (`middleware/audit-log-write-redaction`), so a writer that forgets to
|
|
13
|
+
* redact cannot store a key. The audit payload guard
|
|
14
|
+
* (`middleware/audit-payload-guard`) applies the same rule when a row is read,
|
|
15
|
+
* which also covers rows stored before any writer redacted.
|
|
16
|
+
*
|
|
17
|
+
* Key-directed rather than type-directed on purpose: audit payloads are
|
|
18
|
+
* persisted as JSON with no GraphQL type attached, and over-redacting a
|
|
19
|
+
* non-secret that shares a credential column's name costs an audit reader one
|
|
20
|
+
* field, while under-redacting leaks a key. The key set is exactly the
|
|
21
|
+
* credential columns {@link CREDENTIAL_FIELDS} guards on the read side.
|
|
22
|
+
*
|
|
23
|
+
* @module auth/credential-redaction
|
|
24
|
+
*/
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.CREDENTIAL_KEY_NAMES = exports.REDACTED = void 0;
|
|
27
|
+
exports.redactCredentials = redactCredentials;
|
|
28
|
+
exports.credentialKeyPaths = credentialKeyPaths;
|
|
29
|
+
// From the leaf module, not the guard: the guard redacts audit payloads on
|
|
30
|
+
// read, so importing it here would make the two modules import each other.
|
|
31
|
+
const credential_fields_1 = require("./credential-fields.cjs");
|
|
32
|
+
/** The placeholder a redacted value is replaced with. */
|
|
33
|
+
exports.REDACTED = '[REDACTED]';
|
|
34
|
+
/** Deepest nesting walked; anything deeper is replaced wholesale. */
|
|
35
|
+
const MAX_DEPTH = 12;
|
|
36
|
+
/** Every credential column name across all models. */
|
|
37
|
+
exports.CREDENTIAL_KEY_NAMES = new Set([...credential_fields_1.CREDENTIAL_FIELDS.values()].flatMap((fields) => [...fields]));
|
|
38
|
+
/**
|
|
39
|
+
* Whether `value` is Prisma's field-update form of an absent value: an object
|
|
40
|
+
* whose only key is `set`, holding `null` or nothing. `{ set: null }` is how a
|
|
41
|
+
* Prisma update clears a nullable column, and the generated CRUD functions
|
|
42
|
+
* send every clear in that form (`removeUndefinedProps` in `src/utils.ts`
|
|
43
|
+
* keeps it for that reason), so it is the same statement as a bare `null`.
|
|
44
|
+
*/
|
|
45
|
+
function isClearingUpdate(value) {
|
|
46
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
47
|
+
return false;
|
|
48
|
+
const keys = Object.keys(value);
|
|
49
|
+
if (keys.length !== 1 || keys[0] !== 'set')
|
|
50
|
+
return false;
|
|
51
|
+
const assigned = value.set;
|
|
52
|
+
return assigned === null || assigned === undefined;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Whether a credential key's value holds nothing: absent, `null`, or a
|
|
56
|
+
* clearing update ({@link isClearingUpdate}). Such a value discloses nothing,
|
|
57
|
+
* and replacing it would make a revoke read like a rotation.
|
|
58
|
+
*/
|
|
59
|
+
function holdsNoValue(value) {
|
|
60
|
+
if (value === null || value === undefined)
|
|
61
|
+
return true;
|
|
62
|
+
return isClearingUpdate(value);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A deep copy of `value` with every credential-named key's value replaced by
|
|
66
|
+
* {@link REDACTED}. A credential value that holds nothing (absent, `null`, or
|
|
67
|
+
* an update's `{ set: null }`) is kept as-is, so the audit row still shows
|
|
68
|
+
* whether a credential was set, cleared or untouched.
|
|
69
|
+
*
|
|
70
|
+
* @param value - Any JSON-shaped value.
|
|
71
|
+
* @returns The redacted copy.
|
|
72
|
+
*/
|
|
73
|
+
function redactCredentials(value, depth = 0) {
|
|
74
|
+
if (value === null || value === undefined)
|
|
75
|
+
return value;
|
|
76
|
+
if (depth >= MAX_DEPTH)
|
|
77
|
+
return exports.REDACTED;
|
|
78
|
+
if (Array.isArray(value))
|
|
79
|
+
return value.map((item) => redactCredentials(item, depth + 1));
|
|
80
|
+
if (typeof value !== 'object')
|
|
81
|
+
return value;
|
|
82
|
+
if (value instanceof Date)
|
|
83
|
+
return value.toISOString();
|
|
84
|
+
const out = {};
|
|
85
|
+
for (const [key, child] of Object.entries(value)) {
|
|
86
|
+
out[key] =
|
|
87
|
+
exports.CREDENTIAL_KEY_NAMES.has(key) && !holdsNoValue(child)
|
|
88
|
+
? exports.REDACTED
|
|
89
|
+
: redactCredentials(child, depth + 1);
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Every place {@link redactCredentials} replaces a value in `value`, in
|
|
95
|
+
* traversal order. It walks by the same rules (credential key names, the
|
|
96
|
+
* exemption for a value that holds nothing, the depth limit), so it is empty
|
|
97
|
+
* exactly when redaction leaves the payload as it was. Paths name keys only,
|
|
98
|
+
* never values.
|
|
99
|
+
*
|
|
100
|
+
* @param value - Any JSON-shaped value.
|
|
101
|
+
* @returns The replaced locations.
|
|
102
|
+
*/
|
|
103
|
+
function credentialKeyPaths(value, depth = 0, path = '') {
|
|
104
|
+
if (value === null || value === undefined)
|
|
105
|
+
return [];
|
|
106
|
+
if (depth >= MAX_DEPTH)
|
|
107
|
+
return [{ path: path || '<root>', carriesValue: carriesValue(value) }];
|
|
108
|
+
if (Array.isArray(value)) {
|
|
109
|
+
return value.flatMap((item, index) => credentialKeyPaths(item, depth + 1, joinPath(path, String(index))));
|
|
110
|
+
}
|
|
111
|
+
if (typeof value !== 'object' || value instanceof Date)
|
|
112
|
+
return [];
|
|
113
|
+
return Object.entries(value).flatMap(([key, child]) => exports.CREDENTIAL_KEY_NAMES.has(key) && !holdsNoValue(child)
|
|
114
|
+
? [{ path: joinPath(path, key), carriesValue: carriesValue(child) }]
|
|
115
|
+
: credentialKeyPaths(child, depth + 1, joinPath(path, key)));
|
|
116
|
+
}
|
|
117
|
+
function joinPath(parent, key) {
|
|
118
|
+
return parent === '' ? key : `${parent}.${key}`;
|
|
119
|
+
}
|
|
120
|
+
/** Whether `value` holds a non-empty string, a number or a boolean anywhere. */
|
|
121
|
+
function carriesValue(value, depth = 0) {
|
|
122
|
+
if (value === null || value === undefined)
|
|
123
|
+
return false;
|
|
124
|
+
if (typeof value === 'string')
|
|
125
|
+
return value.length > 0;
|
|
126
|
+
if (typeof value !== 'object')
|
|
127
|
+
return true;
|
|
128
|
+
// Too deep to inspect: assume it does, as the redaction assumes it must.
|
|
129
|
+
if (depth >= MAX_DEPTH)
|
|
130
|
+
return true;
|
|
131
|
+
const children = Array.isArray(value) ? value : Object.values(value);
|
|
132
|
+
return children.some((child) => carriesValue(child, depth + 1));
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=credential-redaction.js.map
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redact stored-credential values from a payload before it is persisted or
|
|
3
|
+
* logged.
|
|
4
|
+
*
|
|
5
|
+
* The audit trail records what a mutation asked for, and a mutation that
|
|
6
|
+
* writes a broker key carries the key in its arguments. An audit row is read
|
|
7
|
+
* by far more people and processes than the credential column itself, so a
|
|
8
|
+
* credential copied into one is a second, unguarded copy of the secret. Every
|
|
9
|
+
* `AuditLog` write passes through here before it is stored, whoever makes it:
|
|
10
|
+
* the server's Prisma client applies it to each row an `AuditLog` operation
|
|
11
|
+
* writes (`middleware/audit-log-write-redaction`), so a writer that forgets to
|
|
12
|
+
* redact cannot store a key. The audit payload guard
|
|
13
|
+
* (`middleware/audit-payload-guard`) applies the same rule when a row is read,
|
|
14
|
+
* which also covers rows stored before any writer redacted.
|
|
15
|
+
*
|
|
16
|
+
* Key-directed rather than type-directed on purpose: audit payloads are
|
|
17
|
+
* persisted as JSON with no GraphQL type attached, and over-redacting a
|
|
18
|
+
* non-secret that shares a credential column's name costs an audit reader one
|
|
19
|
+
* field, while under-redacting leaks a key. The key set is exactly the
|
|
20
|
+
* credential columns {@link CREDENTIAL_FIELDS} guards on the read side.
|
|
21
|
+
*
|
|
22
|
+
* @module auth/credential-redaction
|
|
23
|
+
*/
|
|
24
|
+
/** The placeholder a redacted value is replaced with. */
|
|
25
|
+
export declare const REDACTED = "[REDACTED]";
|
|
26
|
+
/** Every credential column name across all models. */
|
|
27
|
+
export declare const CREDENTIAL_KEY_NAMES: ReadonlySet<string>;
|
|
28
|
+
/**
|
|
29
|
+
* A deep copy of `value` with every credential-named key's value replaced by
|
|
30
|
+
* {@link REDACTED}. A credential value that holds nothing (absent, `null`, or
|
|
31
|
+
* an update's `{ set: null }`) is kept as-is, so the audit row still shows
|
|
32
|
+
* whether a credential was set, cleared or untouched.
|
|
33
|
+
*
|
|
34
|
+
* @param value - Any JSON-shaped value.
|
|
35
|
+
* @returns The redacted copy.
|
|
36
|
+
*/
|
|
37
|
+
export declare function redactCredentials(value: unknown, depth?: number): unknown;
|
|
38
|
+
/** One value {@link redactCredentials} replaces, located by key path. */
|
|
39
|
+
export interface CredentialKeyPath {
|
|
40
|
+
/** Dotted key path from the payload root, array indices included. */
|
|
41
|
+
path: string;
|
|
42
|
+
/**
|
|
43
|
+
* Whether the replaced value held anything: a non-empty string, a number or
|
|
44
|
+
* a boolean somewhere inside it. A key set to `""` or `{ set: "" }` is
|
|
45
|
+
* replaced all the same, but it disclosed nothing.
|
|
46
|
+
*/
|
|
47
|
+
carriesValue: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Every place {@link redactCredentials} replaces a value in `value`, in
|
|
51
|
+
* traversal order. It walks by the same rules (credential key names, the
|
|
52
|
+
* exemption for a value that holds nothing, the depth limit), so it is empty
|
|
53
|
+
* exactly when redaction leaves the payload as it was. Paths name keys only,
|
|
54
|
+
* never values.
|
|
55
|
+
*
|
|
56
|
+
* @param value - Any JSON-shaped value.
|
|
57
|
+
* @returns The replaced locations.
|
|
58
|
+
*/
|
|
59
|
+
export declare function credentialKeyPaths(value: unknown, depth?: number, path?: string): CredentialKeyPath[];
|
|
60
|
+
//# sourceMappingURL=credential-redaction.d.ts.map
|