@platform-sec/sensitive-string-core 0.6.0 → 0.6.1
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/bson.d.ts +28 -0
- package/dist/bson.d.ts.map +1 -0
- package/dist/bson.js +37 -0
- package/dist/bson.js.map +1 -0
- package/dist/classification.d.ts +69 -0
- package/dist/classification.d.ts.map +1 -0
- package/dist/classification.js +104 -0
- package/dist/classification.js.map +1 -0
- package/dist/compare.d.ts +36 -0
- package/dist/compare.d.ts.map +1 -0
- package/dist/compare.js +123 -0
- package/dist/compare.js.map +1 -0
- package/dist/context.d.ts +78 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +110 -0
- package/dist/context.js.map +1 -0
- package/dist/derive.d.ts +110 -0
- package/dist/derive.d.ts.map +1 -0
- package/dist/derive.js +153 -0
- package/dist/derive.js.map +1 -0
- package/dist/destination.d.ts +120 -0
- package/dist/destination.d.ts.map +1 -0
- package/dist/destination.js +352 -0
- package/dist/destination.js.map +1 -0
- package/dist/deterministic.d.ts +145 -0
- package/dist/deterministic.d.ts.map +1 -0
- package/dist/deterministic.js +352 -0
- package/dist/deterministic.js.map +1 -0
- package/dist/dev-mode.d.ts +177 -0
- package/dist/dev-mode.d.ts.map +1 -0
- package/dist/dev-mode.js +503 -0
- package/dist/dev-mode.js.map +1 -0
- package/dist/environment.d.ts +181 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +558 -0
- package/dist/environment.js.map +1 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +21 -0
- package/dist/errors.js.map +1 -0
- package/dist/global-state.d.ts +8 -0
- package/dist/global-state.d.ts.map +1 -0
- package/dist/global-state.js +22 -0
- package/dist/global-state.js.map +1 -0
- package/dist/hash.d.ts +74 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +253 -0
- package/dist/hash.js.map +1 -0
- package/dist/hygiene.d.ts +242 -0
- package/dist/hygiene.d.ts.map +1 -0
- package/dist/hygiene.js +257 -0
- package/dist/hygiene.js.map +1 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +233 -0
- package/dist/index.js.map +1 -0
- package/dist/mask.d.ts +51 -0
- package/dist/mask.d.ts.map +1 -0
- package/dist/mask.js +461 -0
- package/dist/mask.js.map +1 -0
- package/dist/own-property.d.ts +20 -0
- package/dist/own-property.d.ts.map +1 -0
- package/dist/own-property.js +34 -0
- package/dist/own-property.js.map +1 -0
- package/dist/patterns.d.ts +12 -0
- package/dist/patterns.d.ts.map +1 -0
- package/dist/patterns.js +19 -0
- package/dist/patterns.js.map +1 -0
- package/dist/policy.d.ts +51 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +181 -0
- package/dist/policy.js.map +1 -0
- package/dist/probe.d.ts +141 -0
- package/dist/probe.d.ts.map +1 -0
- package/dist/probe.js +213 -0
- package/dist/probe.js.map +1 -0
- package/dist/redact-copy.d.ts +61 -0
- package/dist/redact-copy.d.ts.map +1 -0
- package/dist/redact-copy.js +217 -0
- package/dist/redact-copy.js.map +1 -0
- package/dist/redact.d.ts +141 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +514 -0
- package/dist/redact.js.map +1 -0
- package/dist/redaction-marker.d.ts +48 -0
- package/dist/redaction-marker.d.ts.map +1 -0
- package/dist/redaction-marker.js +138 -0
- package/dist/redaction-marker.js.map +1 -0
- package/dist/redaction-observer.d.ts +108 -0
- package/dist/redaction-observer.d.ts.map +1 -0
- package/dist/redaction-observer.js +103 -0
- package/dist/redaction-observer.js.map +1 -0
- package/dist/redaction-scope.d.ts +21 -0
- package/dist/redaction-scope.d.ts.map +1 -0
- package/dist/redaction-scope.js +44 -0
- package/dist/redaction-scope.js.map +1 -0
- package/dist/reference.d.ts +101 -0
- package/dist/reference.d.ts.map +1 -0
- package/dist/reference.js +123 -0
- package/dist/reference.js.map +1 -0
- package/dist/regexp.d.ts +25 -0
- package/dist/regexp.d.ts.map +1 -0
- package/dist/regexp.js +61 -0
- package/dist/regexp.js.map +1 -0
- package/dist/release.d.ts +88 -0
- package/dist/release.d.ts.map +1 -0
- package/dist/release.js +108 -0
- package/dist/release.js.map +1 -0
- package/dist/reveal-budget.d.ts +148 -0
- package/dist/reveal-budget.d.ts.map +1 -0
- package/dist/reveal-budget.js +314 -0
- package/dist/reveal-budget.js.map +1 -0
- package/dist/revocation.d.ts +168 -0
- package/dist/revocation.d.ts.map +1 -0
- package/dist/revocation.js +541 -0
- package/dist/revocation.js.map +1 -0
- package/dist/sealing.d.ts +283 -0
- package/dist/sealing.d.ts.map +1 -0
- package/dist/sealing.js +523 -0
- package/dist/sealing.js.map +1 -0
- package/dist/sensitive-string.d.ts +919 -0
- package/dist/sensitive-string.d.ts.map +1 -0
- package/dist/sensitive-string.js +2260 -0
- package/dist/sensitive-string.js.map +1 -0
- package/dist/serialized.d.ts +33 -0
- package/dist/serialized.d.ts.map +1 -0
- package/dist/serialized.js +65 -0
- package/dist/serialized.js.map +1 -0
- package/dist/template.d.ts +69 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +158 -0
- package/dist/template.js.map +1 -0
- package/dist/ttl.d.ts +104 -0
- package/dist/ttl.d.ts.map +1 -0
- package/dist/ttl.js +330 -0
- package/dist/ttl.js.map +1 -0
- package/dist/types.d.ts +859 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -0
- package/dist/winston.d.ts +114 -0
- package/dist/winston.d.ts.map +1 -0
- package/dist/winston.js +170 -0
- package/dist/winston.js.map +1 -0
- package/dist/worker.d.ts +398 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +861 -0
- package/dist/worker.js.map +1 -0
- package/dist/wrap.d.ts +287 -0
- package/dist/wrap.d.ts.map +1 -0
- package/dist/wrap.js +553 -0
- package/dist/wrap.js.map +1 -0
- package/package.json +10 -10
package/dist/bson.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { SensitiveStringError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Raised when a `SensitiveString` reaches BSON serialisation unrevealed.
|
|
4
|
+
*
|
|
5
|
+
* BSON honours `toBSON()` on a value and ignores `toJSON()` (measured against
|
|
6
|
+
* `bson` 7.3.2). Without a `toBSON`, an instance handed to any MongoDB write is
|
|
7
|
+
* serialised by its own enumerable properties, which are `klass` and `label`:
|
|
8
|
+
* the document stores `{ klass: 'credential', label: 'token' }` and the value is
|
|
9
|
+
* gone. Nothing throws, nothing logs, and the column looks populated.
|
|
10
|
+
*
|
|
11
|
+
* That is worse than a leak in one specific way, which is why this throws where
|
|
12
|
+
* {@link SensitiveString.toJSON} redacts. A leak is loud once someone reads the
|
|
13
|
+
* log; this is silent until an authentication fails in production, and by then
|
|
14
|
+
* the original value is not recoverable from the document. It is also an
|
|
15
|
+
* authentication bypass on the read side: every classified value serialises to
|
|
16
|
+
* the same husk, so a filter built from one matches a document written from
|
|
17
|
+
* another.
|
|
18
|
+
*
|
|
19
|
+
* `toJSON` must not throw because logging must not throw, and a redacted log
|
|
20
|
+
* line is still useful. `toBSON` is only ever called when a value is about to be
|
|
21
|
+
* persisted or matched, and neither has a useful degraded outcome.
|
|
22
|
+
*/
|
|
23
|
+
export declare class SensitiveBsonError extends SensitiveStringError {
|
|
24
|
+
readonly name = "SensitiveBsonError";
|
|
25
|
+
readonly code = "bson-serialization-refused";
|
|
26
|
+
constructor(klass: string, label: string | undefined);
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=bson.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bson.d.ts","sourceRoot":"","sources":["../src/bson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,kBAAmB,SAAQ,oBAAoB;IAC1D,SAAkB,IAAI,wBAAwB;IAC9C,SAAkB,IAAI,gCAAgC;gBAE1C,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS;CAQrD"}
|
package/dist/bson.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SensitiveBsonError = void 0;
|
|
4
|
+
const errors_js_1 = require("./errors.js");
|
|
5
|
+
/**
|
|
6
|
+
* Raised when a `SensitiveString` reaches BSON serialisation unrevealed.
|
|
7
|
+
*
|
|
8
|
+
* BSON honours `toBSON()` on a value and ignores `toJSON()` (measured against
|
|
9
|
+
* `bson` 7.3.2). Without a `toBSON`, an instance handed to any MongoDB write is
|
|
10
|
+
* serialised by its own enumerable properties, which are `klass` and `label`:
|
|
11
|
+
* the document stores `{ klass: 'credential', label: 'token' }` and the value is
|
|
12
|
+
* gone. Nothing throws, nothing logs, and the column looks populated.
|
|
13
|
+
*
|
|
14
|
+
* That is worse than a leak in one specific way, which is why this throws where
|
|
15
|
+
* {@link SensitiveString.toJSON} redacts. A leak is loud once someone reads the
|
|
16
|
+
* log; this is silent until an authentication fails in production, and by then
|
|
17
|
+
* the original value is not recoverable from the document. It is also an
|
|
18
|
+
* authentication bypass on the read side: every classified value serialises to
|
|
19
|
+
* the same husk, so a filter built from one matches a document written from
|
|
20
|
+
* another.
|
|
21
|
+
*
|
|
22
|
+
* `toJSON` must not throw because logging must not throw, and a redacted log
|
|
23
|
+
* line is still useful. `toBSON` is only ever called when a value is about to be
|
|
24
|
+
* persisted or matched, and neither has a useful degraded outcome.
|
|
25
|
+
*/
|
|
26
|
+
class SensitiveBsonError extends errors_js_1.SensitiveStringError {
|
|
27
|
+
name = 'SensitiveBsonError';
|
|
28
|
+
code = 'bson-serialization-refused';
|
|
29
|
+
constructor(klass, label) {
|
|
30
|
+
super(`A SensitiveString<${label ? `${klass}:${label}` : klass}> reached BSON serialization ` +
|
|
31
|
+
'without being revealed. Storing it would persist { klass, label } and lose the value, ' +
|
|
32
|
+
'and matching on it would match every other value of the same class. Reveal it at the ' +
|
|
33
|
+
'write boundary, or use @platform-sec/sensitive-string-mongoose.');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.SensitiveBsonError = SensitiveBsonError;
|
|
37
|
+
//# sourceMappingURL=bson.js.map
|
package/dist/bson.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bson.js","sourceRoot":"","sources":["../src/bson.ts"],"names":[],"mappings":";;;AAAA,2CAAmD;AAEnD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,kBAAmB,SAAQ,gCAAoB;IACxC,IAAI,GAAG,oBAAoB,CAAC;IAC5B,IAAI,GAAG,4BAA4B,CAAC;IAEtD,YAAY,KAAa,EAAE,KAAyB;QAClD,KAAK,CACH,qBAAqB,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,+BAA+B;YACrF,wFAAwF;YACxF,uFAAuF;YACvF,iEAAiE,CACpE,CAAC;IACJ,CAAC;CACF;AAZD,gDAYC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { SensitivityClass } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A field's classification, or `'plain'` to leave it an ordinary string.
|
|
4
|
+
*
|
|
5
|
+
* `'plain'` is not an absence of a decision. It is the decision that a key
|
|
6
|
+
* carries configuration rather than a secret, and it is what keeps a wrapped
|
|
7
|
+
* `PORT` or `LOG_LEVEL` out of a config tree: an instance is an object, so
|
|
8
|
+
* `config.LOG_LEVEL === 'debug'` is permanently false once the value is
|
|
9
|
+
* wrapped, and the application misbehaves in a way that looks like a
|
|
10
|
+
* configuration bug rather than a security control.
|
|
11
|
+
*/
|
|
12
|
+
export type FieldClass = SensitivityClass | 'plain';
|
|
13
|
+
/** One pattern-to-class rule, tried in order for keys `fields` did not match. */
|
|
14
|
+
export interface ClassificationRule {
|
|
15
|
+
/** Matched against key names, never against values. */
|
|
16
|
+
readonly pattern: RegExp;
|
|
17
|
+
readonly klass: FieldClass;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Which keys of a decoded payload become which class.
|
|
21
|
+
*
|
|
22
|
+
* Precedence is `fields`, then `rules` in order, then `fallback`. The first
|
|
23
|
+
* match wins at every level, so a key named in `fields` is never reconsidered
|
|
24
|
+
* by a rule.
|
|
25
|
+
*
|
|
26
|
+
* Patterns run on key names. A key is a field name, not the secret, which is
|
|
27
|
+
* the same reasoning that keeps record keys in the clear in the Zod
|
|
28
|
+
* integration. Matching on values would mean reading them to decide whether to
|
|
29
|
+
* protect them.
|
|
30
|
+
*/
|
|
31
|
+
export interface ClassificationSpec {
|
|
32
|
+
/** Exact key or dot path matches. Highest precedence. */
|
|
33
|
+
readonly fields?: Readonly<Record<string, FieldClass>>;
|
|
34
|
+
/** Tried in order for keys `fields` did not match. */
|
|
35
|
+
readonly rules?: readonly ClassificationRule[];
|
|
36
|
+
/** Everything unmatched. Optional here, and each caller decides whether to require it. */
|
|
37
|
+
readonly fallback?: FieldClass;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A spec with its patterns compiled, so a rule list is prepared once per source
|
|
41
|
+
* rather than once per key of every load.
|
|
42
|
+
*/
|
|
43
|
+
export interface CompiledClassification {
|
|
44
|
+
/** The exact matches, in a map rather than an object so no key inherits one. */
|
|
45
|
+
readonly fields: ReadonlyMap<string, FieldClass>;
|
|
46
|
+
readonly rules: readonly ClassificationRule[];
|
|
47
|
+
readonly fallback: FieldClass | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* The class for one key, or `undefined` when nothing matched and the spec has
|
|
50
|
+
* no fallback.
|
|
51
|
+
*
|
|
52
|
+
* @param key - The authoritative name, which for a nested payload is the dot
|
|
53
|
+
* path from the root of the decoded object.
|
|
54
|
+
* @param aliases - Further names for the same field, tried after `key` at
|
|
55
|
+
* each precedence level. A nested field passes its leaf key here, so a rule
|
|
56
|
+
* written as `/password/` matches `db.password` without the caller having
|
|
57
|
+
* to anticipate the nesting.
|
|
58
|
+
*/
|
|
59
|
+
classify(key: string, aliases?: readonly string[]): FieldClass | undefined;
|
|
60
|
+
}
|
|
61
|
+
export declare function compileClassification(spec: ClassificationSpec): CompiledClassification;
|
|
62
|
+
/**
|
|
63
|
+
* The class for one key under a spec, compiling the spec for this call.
|
|
64
|
+
*
|
|
65
|
+
* Convenient for a one-off decision. Anything classifying more than a handful
|
|
66
|
+
* of keys compiles once with `compileClassification` instead.
|
|
67
|
+
*/
|
|
68
|
+
export declare function classifyField(key: string, spec: ClassificationSpec, aliases?: readonly string[]): FieldClass | undefined;
|
|
69
|
+
//# sourceMappingURL=classification.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classification.d.ts","sourceRoot":"","sources":["../src/classification.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD;;;;;;;;;GASG;AACH,MAAM,MAAM,UAAU,GAAG,gBAAgB,GAAG,OAAO,CAAC;AAEpD,iFAAiF;AACjF,MAAM,WAAW,kBAAkB;IACjC,uDAAuD;IACvD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;CAC5B;AAED;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,kBAAkB;IACjC,yDAAyD;IACzD,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;IACvD,sDAAsD;IACtD,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,kBAAkB,EAAE,CAAC;IAC/C,0FAA0F;IAC1F,QAAQ,CAAC,QAAQ,CAAC,EAAE,UAAU,CAAC;CAChC;AAED;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACrC,gFAAgF;IAChF,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACjD,QAAQ,CAAC,KAAK,EAAE,SAAS,kBAAkB,EAAE,CAAC;IAC9C,QAAQ,CAAC,QAAQ,EAAE,UAAU,GAAG,SAAS,CAAC;IAE1C;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,UAAU,GAAG,SAAS,CAAC;CAC5E;AA4CD,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,kBAAkB,GAAG,sBAAsB,CAsBtF;AAkDD;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,kBAAkB,EACxB,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,GAC1B,UAAU,GAAG,SAAS,CAExB"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compileClassification = compileClassification;
|
|
4
|
+
exports.classifyField = classifyField;
|
|
5
|
+
const FIELD_CLASSES = ['credential', 'sensitive', 'plain'];
|
|
6
|
+
function assertFieldClass(value, where) {
|
|
7
|
+
if (typeof value !== 'string' || !FIELD_CLASSES.includes(value)) {
|
|
8
|
+
throw new Error(`Invalid classification for ${where}: expected one of ${FIELD_CLASSES.join(', ')}.`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The same pattern without `g` or `y`.
|
|
13
|
+
*
|
|
14
|
+
* Both flags make a `RegExp` stateful through `lastIndex`, and a compiled spec
|
|
15
|
+
* is reused across every key of every load. A global pattern tested twice
|
|
16
|
+
* answers `true` then `false` for the same key, so the second secret of a
|
|
17
|
+
* payload would be classified differently from the first. Dropping the flags is
|
|
18
|
+
* the fix rather than resetting `lastIndex` at each call, because it also
|
|
19
|
+
* removes the shared mutable state a caller could observe on their own object.
|
|
20
|
+
*/
|
|
21
|
+
function stateless(pattern) {
|
|
22
|
+
const flags = pattern.flags.replace(/[gy]/g, '');
|
|
23
|
+
return flags === pattern.flags ? pattern : new RegExp(pattern.source, flags);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Prepare a spec for repeated matching.
|
|
27
|
+
*
|
|
28
|
+
* @throws Error if a class is not a `FieldClass`, or a rule has no `RegExp`.
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* Compiled forms, memoized per spec object.
|
|
32
|
+
*
|
|
33
|
+
* `classifyField` compiles the spec it is handed on every call, which made it
|
|
34
|
+
* an order of magnitude slower than a hoisted compiled classification over a
|
|
35
|
+
* payload of keys. A spec is `readonly` throughout its type, so reusing the
|
|
36
|
+
* compiled form for the same object is sound; a caller that mutates one after
|
|
37
|
+
* classifying with it gets the earlier compile.
|
|
38
|
+
*/
|
|
39
|
+
const compiledSpecs = new WeakMap();
|
|
40
|
+
function compileClassification(spec) {
|
|
41
|
+
// Checked before the memo, because `WeakMap.set` refuses a primitive key and
|
|
42
|
+
// would report this as "Invalid value used as weak map key", which names an
|
|
43
|
+
// implementation detail and not the caller's mistake. A primitive used to
|
|
44
|
+
// compile to a classification that matched nothing and said so to no one.
|
|
45
|
+
if (spec === null || typeof spec !== 'object') {
|
|
46
|
+
throw new TypeError(`Invalid classification specification: expected an object, received ${spec === null ? 'null' : typeof spec}.`);
|
|
47
|
+
}
|
|
48
|
+
const memoized = compiledSpecs.get(spec);
|
|
49
|
+
if (memoized !== undefined) {
|
|
50
|
+
return memoized;
|
|
51
|
+
}
|
|
52
|
+
const compiled = compileSpec(spec);
|
|
53
|
+
compiledSpecs.set(spec, compiled);
|
|
54
|
+
return compiled;
|
|
55
|
+
}
|
|
56
|
+
function compileSpec(spec) {
|
|
57
|
+
const fields = new Map();
|
|
58
|
+
for (const [key, klass] of Object.entries(spec.fields ?? {})) {
|
|
59
|
+
assertFieldClass(klass, `field '${key}'`);
|
|
60
|
+
fields.set(key, klass);
|
|
61
|
+
}
|
|
62
|
+
const rules = (spec.rules ?? []).map((rule, index) => {
|
|
63
|
+
if (!(rule.pattern instanceof RegExp)) {
|
|
64
|
+
throw new Error(`Invalid classification rule at index ${index}: pattern must be a RegExp.`);
|
|
65
|
+
}
|
|
66
|
+
assertFieldClass(rule.klass, `rule at index ${index}`);
|
|
67
|
+
return { pattern: stateless(rule.pattern), klass: rule.klass };
|
|
68
|
+
});
|
|
69
|
+
if (spec.fallback !== undefined) {
|
|
70
|
+
assertFieldClass(spec.fallback, 'fallback');
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
fields,
|
|
74
|
+
rules,
|
|
75
|
+
fallback: spec.fallback,
|
|
76
|
+
classify(key, aliases) {
|
|
77
|
+
const names = aliases === undefined ? [key] : [key, ...aliases];
|
|
78
|
+
for (const name of names) {
|
|
79
|
+
const declared = fields.get(name);
|
|
80
|
+
if (declared !== undefined) {
|
|
81
|
+
return declared;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (const rule of rules) {
|
|
85
|
+
for (const name of names) {
|
|
86
|
+
if (rule.pattern.test(name)) {
|
|
87
|
+
return rule.klass;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return spec.fallback;
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The class for one key under a spec, compiling the spec for this call.
|
|
97
|
+
*
|
|
98
|
+
* Convenient for a one-off decision. Anything classifying more than a handful
|
|
99
|
+
* of keys compiles once with `compileClassification` instead.
|
|
100
|
+
*/
|
|
101
|
+
function classifyField(key, spec, aliases) {
|
|
102
|
+
return compileClassification(spec).classify(key, aliases);
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=classification.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classification.js","sourceRoot":"","sources":["../src/classification.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether two raw values are equal, in time that does not depend on either
|
|
3
|
+
* value's contents, and that depends on the stored value's length only through
|
|
4
|
+
* the HMAC's block count, as described above.
|
|
5
|
+
*
|
|
6
|
+
* Two distinct values collide only on an HMAC-SHA256 collision, which is not a
|
|
7
|
+
* failure mode anyone has to plan around.
|
|
8
|
+
*
|
|
9
|
+
* @param a - One raw value.
|
|
10
|
+
* @param b - The other raw value.
|
|
11
|
+
* @returns true when the values are byte-for-byte equal.
|
|
12
|
+
*/
|
|
13
|
+
export declare function constantTimeEquals(a: string, b: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Whether two raw values held as UTF-8 bytes are equal, under the same
|
|
16
|
+
* guarantees `constantTimeEquals` gives and for the same reasons.
|
|
17
|
+
*
|
|
18
|
+
* It exists so a sealed value never has to become a string to be compared.
|
|
19
|
+
* Decrypting into a `Buffer`, comparing here, and zeroing both operands leaves
|
|
20
|
+
* nothing recoverable in process memory; the string path leaves one copy per
|
|
21
|
+
* comparison that garbage collection does not remove.
|
|
22
|
+
*
|
|
23
|
+
* The obvious byte comparison, `a.equals(b)`, is not used and is not an
|
|
24
|
+
* option: it is a length check followed by a `memcmp`, which is the exact
|
|
25
|
+
* variable-time shape `compare.ts` exists to avoid. Hashing first costs about
|
|
26
|
+
* 5us and removes both the length branch and the byte-position one.
|
|
27
|
+
*
|
|
28
|
+
* Neither operand is zeroed here. The caller owns both and knows which of them
|
|
29
|
+
* it may write to.
|
|
30
|
+
*
|
|
31
|
+
* @param a - One raw value, as UTF-8 bytes.
|
|
32
|
+
* @param b - The other raw value, as UTF-8 bytes.
|
|
33
|
+
* @returns true when the values are byte-for-byte equal.
|
|
34
|
+
*/
|
|
35
|
+
export declare function constantTimeEqualsBytes(a: Buffer, b: Buffer): boolean;
|
|
36
|
+
//# sourceMappingURL=compare.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":"AAwEA;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAEhE;AAcD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAErE"}
|
package/dist/compare.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.constantTimeEquals = constantTimeEquals;
|
|
4
|
+
exports.constantTimeEqualsBytes = constantTimeEqualsBytes;
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
/**
|
|
7
|
+
* Constant-time equality of two raw values, including their lengths.
|
|
8
|
+
*
|
|
9
|
+
* This module holds the comparison and nothing else. It does not read a
|
|
10
|
+
* `SensitiveString`, does not authorize and does not audit: the class reads
|
|
11
|
+
* both operands through the ordinary gate and hands the raw strings here, so a
|
|
12
|
+
* comparison costs exactly the exposures the gate recorded.
|
|
13
|
+
*
|
|
14
|
+
* The obvious implementation hands both values to `timingSafeEqual`, which
|
|
15
|
+
* requires equal lengths and therefore needs a length check in front of it.
|
|
16
|
+
* That check is a branch on the stored value's length, and it is observable:
|
|
17
|
+
* a comparand of the wrong length answers before any byte is examined, so an
|
|
18
|
+
* attacker who can time the call learns the length of the secret by finding
|
|
19
|
+
* the length that costs more. A length is not the secret, but it bounds the
|
|
20
|
+
* search for one.
|
|
21
|
+
*
|
|
22
|
+
* Comparing a wrong-length comparand against a zero-filled buffer of its own
|
|
23
|
+
* length is the usual repair and is not the one used here. It moves the branch
|
|
24
|
+
* rather than removing it: only the mismatch path allocates and fills that
|
|
25
|
+
* buffer, so that path becomes the slower one and the signal survives with its
|
|
26
|
+
* sign flipped.
|
|
27
|
+
*
|
|
28
|
+
* Hashing both sides first removes the branch instead of balancing it.
|
|
29
|
+
* `timingSafeEqual` then always receives two 32-byte digests, so there is no
|
|
30
|
+
* length to check and no path to choose between. What remains is the HMAC
|
|
31
|
+
* itself, whose cost grows with its input one 64-byte block at a time, over
|
|
32
|
+
* the stored value as well as over the comparand. That is not a branch and it
|
|
33
|
+
* is not nothing: measured independently, stored values of 8 to 256 bytes were
|
|
34
|
+
* indistinguishable within the timing analysis's noise floor, while 1024 bytes
|
|
35
|
+
* cost about 460ns more than 8 and 4096 about 2.5us more. For a credential-sized
|
|
36
|
+
* value the length is hidden to the resolution anyone has measured; for a value
|
|
37
|
+
* hundreds of bytes long it is hidden at 64-byte granularity, which is a
|
|
38
|
+
* weaker promise and is stated here rather than implied away.
|
|
39
|
+
*
|
|
40
|
+
* It is bought rather than free. Measured on Node 22.15 against 32-character
|
|
41
|
+
* operands, a comparison went from about 130ns to about 5us, because two
|
|
42
|
+
* `createHmac` calls dominate everything else in the function. Padding the
|
|
43
|
+
* stored value into a buffer sized by the comparand and folding in a
|
|
44
|
+
* branchless length check measures about 240ns and was not chosen: the copy
|
|
45
|
+
* runs for `min(stored, comparand)` bytes, so the stored length still shows
|
|
46
|
+
* through at memcpy granularity, and the argument that it does not matter is
|
|
47
|
+
* exactly the kind of argument this function exists so that nobody has to
|
|
48
|
+
* make. `equals()` is called once per authentication event, not per byte, and
|
|
49
|
+
* a caller who needs to find a credential among many wants `hash()` and an
|
|
50
|
+
* index rather than a linear scan of comparisons.
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* Ephemeral per-process key, never persisted, never exported and never
|
|
54
|
+
* reachable from a public API.
|
|
55
|
+
*
|
|
56
|
+
* The digests it produces exist for the length of one comparison, so it needs
|
|
57
|
+
* no agreement with any other process and must not have any: a key that
|
|
58
|
+
* survives a restart or is shared between them would make these digests
|
|
59
|
+
* durable identifiers of the values compared under it, which is the thing
|
|
60
|
+
* `derive.ts` exists to do deliberately and this module has no business doing
|
|
61
|
+
* as a side effect.
|
|
62
|
+
*
|
|
63
|
+
* It is keyed rather than a plain digest for the case where the operands are
|
|
64
|
+
* guessable. An unkeyed SHA-256 of a four-digit PIN is the PIN, so a digest
|
|
65
|
+
* that reached a heap dump or a core file would be worth attacking; with a key
|
|
66
|
+
* the attacker does not hold, it is worth nothing.
|
|
67
|
+
*/
|
|
68
|
+
const COMPARISON_KEY = (0, node_crypto_1.randomBytes)(32);
|
|
69
|
+
/** A fixed-width stand-in for a value, equal exactly when the values are. */
|
|
70
|
+
function probe(value) {
|
|
71
|
+
return (0, node_crypto_1.createHmac)('sha256', COMPARISON_KEY).update(value, 'utf8').digest();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Whether two raw values are equal, in time that does not depend on either
|
|
75
|
+
* value's contents, and that depends on the stored value's length only through
|
|
76
|
+
* the HMAC's block count, as described above.
|
|
77
|
+
*
|
|
78
|
+
* Two distinct values collide only on an HMAC-SHA256 collision, which is not a
|
|
79
|
+
* failure mode anyone has to plan around.
|
|
80
|
+
*
|
|
81
|
+
* @param a - One raw value.
|
|
82
|
+
* @param b - The other raw value.
|
|
83
|
+
* @returns true when the values are byte-for-byte equal.
|
|
84
|
+
*/
|
|
85
|
+
function constantTimeEquals(a, b) {
|
|
86
|
+
return (0, node_crypto_1.timingSafeEqual)(probe(a), probe(b));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The same stand-in, for a value already held as bytes.
|
|
90
|
+
*
|
|
91
|
+
* Identical to `probe` for the same value, because `update(text, 'utf8')` and
|
|
92
|
+
* `update(Buffer.from(text, 'utf8'))` feed the HMAC the same octets. That is
|
|
93
|
+
* what lets a sealed value be compared against an unsealed one without either
|
|
94
|
+
* side converting to the other's representation.
|
|
95
|
+
*/
|
|
96
|
+
function probeBytes(value) {
|
|
97
|
+
return (0, node_crypto_1.createHmac)('sha256', COMPARISON_KEY).update(value).digest();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Whether two raw values held as UTF-8 bytes are equal, under the same
|
|
101
|
+
* guarantees `constantTimeEquals` gives and for the same reasons.
|
|
102
|
+
*
|
|
103
|
+
* It exists so a sealed value never has to become a string to be compared.
|
|
104
|
+
* Decrypting into a `Buffer`, comparing here, and zeroing both operands leaves
|
|
105
|
+
* nothing recoverable in process memory; the string path leaves one copy per
|
|
106
|
+
* comparison that garbage collection does not remove.
|
|
107
|
+
*
|
|
108
|
+
* The obvious byte comparison, `a.equals(b)`, is not used and is not an
|
|
109
|
+
* option: it is a length check followed by a `memcmp`, which is the exact
|
|
110
|
+
* variable-time shape `compare.ts` exists to avoid. Hashing first costs about
|
|
111
|
+
* 5us and removes both the length branch and the byte-position one.
|
|
112
|
+
*
|
|
113
|
+
* Neither operand is zeroed here. The caller owns both and knows which of them
|
|
114
|
+
* it may write to.
|
|
115
|
+
*
|
|
116
|
+
* @param a - One raw value, as UTF-8 bytes.
|
|
117
|
+
* @param b - The other raw value, as UTF-8 bytes.
|
|
118
|
+
* @returns true when the values are byte-for-byte equal.
|
|
119
|
+
*/
|
|
120
|
+
function constantTimeEqualsBytes(a, b) {
|
|
121
|
+
return (0, node_crypto_1.timingSafeEqual)(probeBytes(a), probeBytes(b));
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=compare.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compare.js","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":";;AAoFA,gDAEC;AAmCD,0DAEC;AA3HD,6CAAuE;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,cAAc,GAAG,IAAA,yBAAW,EAAC,EAAE,CAAC,CAAC;AAEvC,6EAA6E;AAC7E,SAAS,KAAK,CAAC,KAAa;IAC1B,OAAO,IAAA,wBAAU,EAAC,QAAQ,EAAE,cAAc,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC;AAC7E,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,kBAAkB,CAAC,CAAS,EAAE,CAAS;IACrD,OAAO,IAAA,6BAAe,EAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,UAAU,CAAC,KAAa;IAC/B,OAAO,IAAA,wBAAU,EAAC,QAAQ,EAAE,cAAc,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;AACrE,CAAC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,uBAAuB,CAAC,CAAS,EAAE,CAAS;IAC1D,OAAO,IAAA,6BAAe,EAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;AACvD,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { AuditContext, ISensitiveString, RevealContext, RevealDetails } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Execute a function within a reveal context.
|
|
4
|
+
* The context will be available via getCurrentRevealContext() for the duration of fn.
|
|
5
|
+
*
|
|
6
|
+
* @param ctx - The reveal context to set
|
|
7
|
+
* @param fn - The function to execute within the context
|
|
8
|
+
* @returns The return value of fn
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const result = await withRevealContext(
|
|
13
|
+
* { name: 'api-handler', reason: 'processing payment' },
|
|
14
|
+
* async () => {
|
|
15
|
+
* // sensitiveString.reveal() will use this context
|
|
16
|
+
* return processPayment(sensitiveString);
|
|
17
|
+
* }
|
|
18
|
+
* );
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* Context does NOT propagate to worker threads or child processes: the store is
|
|
23
|
+
* per isolate. `toWorkerPayload()` carries the `name`, `reason` and `tags` of
|
|
24
|
+
* the active context across the boundary alongside the values, and
|
|
25
|
+
* `withWorkerPayload()` installs them on the far side with that side's own
|
|
26
|
+
* `auditReveal`, which is the one field that cannot cross.
|
|
27
|
+
*/
|
|
28
|
+
export declare function withRevealContext<T>(ctx: RevealContext, fn: () => T): T;
|
|
29
|
+
/**
|
|
30
|
+
* Get the current reveal context, if any.
|
|
31
|
+
*
|
|
32
|
+
* @returns The current RevealContext or null if none is set
|
|
33
|
+
*/
|
|
34
|
+
export declare function getCurrentRevealContext(): RevealContext | null;
|
|
35
|
+
/**
|
|
36
|
+
* Create a child context that inherits from the current context.
|
|
37
|
+
* Useful for adding context-specific tags while preserving parent context.
|
|
38
|
+
*
|
|
39
|
+
* @param overrides - Properties to override in the child context
|
|
40
|
+
* @returns A new RevealContext with merged properties
|
|
41
|
+
*/
|
|
42
|
+
export declare function createChildContext(overrides: Partial<RevealContext> & {
|
|
43
|
+
name: string;
|
|
44
|
+
}): RevealContext;
|
|
45
|
+
/**
|
|
46
|
+
* Convenience function to run with an audit callback.
|
|
47
|
+
* Creates a context that logs all reveal operations.
|
|
48
|
+
*
|
|
49
|
+
* @param name - Context name
|
|
50
|
+
* @param auditFn - Function called on each reveal
|
|
51
|
+
* @param fn - The function to execute
|
|
52
|
+
* @returns The return value of fn
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* await withAuditedReveals('payment-processing', (value, details, context) => {
|
|
57
|
+
* logger.info('Revealed sensitive value', {
|
|
58
|
+
* class: value.klass,
|
|
59
|
+
* op: details.operation,
|
|
60
|
+
* ...context.tags,
|
|
61
|
+
* });
|
|
62
|
+
* }, async () => {
|
|
63
|
+
* // All reveals in this scope will be audited
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @remarks
|
|
68
|
+
* `auditFn` receives the context each operation ran under, which is the one
|
|
69
|
+
* built here only while nothing nests a child over it. A two-parameter
|
|
70
|
+
* function stays valid; the third argument is there to be ignored.
|
|
71
|
+
*
|
|
72
|
+
* The value is the whole `ISensitiveString`, matching what `auditReveal`
|
|
73
|
+
* itself is handed. It used to be typed as `{ klass }` alone, which left a sink
|
|
74
|
+
* written against this wrapper unable to read `label` and so unable to say
|
|
75
|
+
* which credential was read.
|
|
76
|
+
*/
|
|
77
|
+
export declare function withAuditedReveals<T>(name: string, auditFn: (value: ISensitiveString, details: RevealDetails, context: AuditContext) => void, fn: () => T): T;
|
|
78
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAY/F;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAEvE;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,IAAI,aAAa,GAAG,IAAI,CAE9D;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,OAAO,CAAC,aAAa,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GACnD,aAAa,CAcf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,CAAC,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,KAAK,IAAI,EACzF,EAAE,EAAE,MAAM,CAAC,GACV,CAAC,CAQH"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withRevealContext = withRevealContext;
|
|
4
|
+
exports.getCurrentRevealContext = getCurrentRevealContext;
|
|
5
|
+
exports.createChildContext = createChildContext;
|
|
6
|
+
exports.withAuditedReveals = withAuditedReveals;
|
|
7
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
8
|
+
const global_state_js_1 = require("./global-state.js");
|
|
9
|
+
/**
|
|
10
|
+
* AsyncLocalStorage instance for reveal context propagation.
|
|
11
|
+
* Allows context to flow through async call chains without explicit threading.
|
|
12
|
+
*/
|
|
13
|
+
const revealContextStorage = (0, global_state_js_1.getGlobalSingleton)('reveal-context-storage', () => new node_async_hooks_1.AsyncLocalStorage());
|
|
14
|
+
/**
|
|
15
|
+
* Execute a function within a reveal context.
|
|
16
|
+
* The context will be available via getCurrentRevealContext() for the duration of fn.
|
|
17
|
+
*
|
|
18
|
+
* @param ctx - The reveal context to set
|
|
19
|
+
* @param fn - The function to execute within the context
|
|
20
|
+
* @returns The return value of fn
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const result = await withRevealContext(
|
|
25
|
+
* { name: 'api-handler', reason: 'processing payment' },
|
|
26
|
+
* async () => {
|
|
27
|
+
* // sensitiveString.reveal() will use this context
|
|
28
|
+
* return processPayment(sensitiveString);
|
|
29
|
+
* }
|
|
30
|
+
* );
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* Context does NOT propagate to worker threads or child processes: the store is
|
|
35
|
+
* per isolate. `toWorkerPayload()` carries the `name`, `reason` and `tags` of
|
|
36
|
+
* the active context across the boundary alongside the values, and
|
|
37
|
+
* `withWorkerPayload()` installs them on the far side with that side's own
|
|
38
|
+
* `auditReveal`, which is the one field that cannot cross.
|
|
39
|
+
*/
|
|
40
|
+
function withRevealContext(ctx, fn) {
|
|
41
|
+
return revealContextStorage.run(ctx, fn);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get the current reveal context, if any.
|
|
45
|
+
*
|
|
46
|
+
* @returns The current RevealContext or null if none is set
|
|
47
|
+
*/
|
|
48
|
+
function getCurrentRevealContext() {
|
|
49
|
+
return revealContextStorage.getStore() ?? null;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a child context that inherits from the current context.
|
|
53
|
+
* Useful for adding context-specific tags while preserving parent context.
|
|
54
|
+
*
|
|
55
|
+
* @param overrides - Properties to override in the child context
|
|
56
|
+
* @returns A new RevealContext with merged properties
|
|
57
|
+
*/
|
|
58
|
+
function createChildContext(overrides) {
|
|
59
|
+
const current = getCurrentRevealContext();
|
|
60
|
+
if (!current) {
|
|
61
|
+
return overrides;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
...current,
|
|
65
|
+
...overrides,
|
|
66
|
+
tags: {
|
|
67
|
+
...current.tags,
|
|
68
|
+
...overrides.tags,
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Convenience function to run with an audit callback.
|
|
74
|
+
* Creates a context that logs all reveal operations.
|
|
75
|
+
*
|
|
76
|
+
* @param name - Context name
|
|
77
|
+
* @param auditFn - Function called on each reveal
|
|
78
|
+
* @param fn - The function to execute
|
|
79
|
+
* @returns The return value of fn
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* await withAuditedReveals('payment-processing', (value, details, context) => {
|
|
84
|
+
* logger.info('Revealed sensitive value', {
|
|
85
|
+
* class: value.klass,
|
|
86
|
+
* op: details.operation,
|
|
87
|
+
* ...context.tags,
|
|
88
|
+
* });
|
|
89
|
+
* }, async () => {
|
|
90
|
+
* // All reveals in this scope will be audited
|
|
91
|
+
* });
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @remarks
|
|
95
|
+
* `auditFn` receives the context each operation ran under, which is the one
|
|
96
|
+
* built here only while nothing nests a child over it. A two-parameter
|
|
97
|
+
* function stays valid; the third argument is there to be ignored.
|
|
98
|
+
*
|
|
99
|
+
* The value is the whole `ISensitiveString`, matching what `auditReveal`
|
|
100
|
+
* itself is handed. It used to be typed as `{ klass }` alone, which left a sink
|
|
101
|
+
* written against this wrapper unable to read `label` and so unable to say
|
|
102
|
+
* which credential was read.
|
|
103
|
+
*/
|
|
104
|
+
function withAuditedReveals(name, auditFn, fn) {
|
|
105
|
+
return withRevealContext({
|
|
106
|
+
name,
|
|
107
|
+
auditReveal: auditFn,
|
|
108
|
+
}, fn);
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":";;AAuCA,8CAEC;AAOD,0DAEC;AASD,gDAgBC;AAkCD,gDAYC;AAzHD,uDAAqD;AAErD,uDAAuD;AAEvD;;;GAGG;AACH,MAAM,oBAAoB,GAAG,IAAA,oCAAkB,EAC7C,wBAAwB,EACxB,GAAG,EAAE,CAAC,IAAI,oCAAiB,EAAiB,CAC7C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,SAAgB,iBAAiB,CAAI,GAAkB,EAAE,EAAW;IAClE,OAAO,oBAAoB,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;AAC3C,CAAC;AAED;;;;GAIG;AACH,SAAgB,uBAAuB;IACrC,OAAO,oBAAoB,CAAC,QAAQ,EAAE,IAAI,IAAI,CAAC;AACjD,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAChC,SAAoD;IAEpD,MAAM,OAAO,GAAG,uBAAuB,EAAE,CAAC;IAC1C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO;QACL,GAAG,OAAO;QACV,GAAG,SAAS;QACZ,IAAI,EAAE;YACJ,GAAG,OAAO,CAAC,IAAI;YACf,GAAG,SAAS,CAAC,IAAI;SAClB;KACF,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,SAAgB,kBAAkB,CAChC,IAAY,EACZ,OAAyF,EACzF,EAAW;IAEX,OAAO,iBAAiB,CACtB;QACE,IAAI;QACJ,WAAW,EAAE,OAAO;KACrB,EACD,EAAE,CACH,CAAC;AACJ,CAAC"}
|