@platform-sec/sensitive-string-testing 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +329 -2
- package/dist/assertions.d.ts +81 -0
- package/dist/assertions.d.ts.map +1 -0
- package/dist/assertions.js +132 -0
- package/dist/assertions.js.map +1 -0
- package/dist/canary.d.ts +49 -0
- package/dist/canary.d.ts.map +1 -0
- package/dist/canary.js +116 -0
- package/dist/canary.js.map +1 -0
- package/dist/create-test-secret.d.ts +78 -0
- package/dist/create-test-secret.d.ts.map +1 -0
- package/dist/create-test-secret.js +91 -0
- package/dist/create-test-secret.js.map +1 -0
- package/dist/describe-type.d.ts +16 -0
- package/dist/describe-type.d.ts.map +1 -0
- package/dist/describe-type.js +35 -0
- package/dist/describe-type.js.map +1 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +77 -0
- package/dist/index.js.map +1 -0
- package/dist/leak-scan.d.ts +94 -0
- package/dist/leak-scan.d.ts.map +1 -0
- package/dist/leak-scan.js +309 -0
- package/dist/leak-scan.js.map +1 -0
- package/dist/matchers.d.ts +93 -0
- package/dist/matchers.d.ts.map +1 -0
- package/dist/matchers.js +170 -0
- package/dist/matchers.js.map +1 -0
- package/dist/mocks.d.ts +140 -0
- package/dist/mocks.d.ts.map +1 -0
- package/dist/mocks.js +146 -0
- package/dist/mocks.js.map +1 -0
- package/dist/serializer.d.ts +136 -0
- package/dist/serializer.d.ts.map +1 -0
- package/dist/serializer.js +210 -0
- package/dist/serializer.js.map +1 -0
- package/dist/vitest.d.ts +59 -0
- package/dist/vitest.d.ts.map +1 -0
- package/dist/vitest.js +3 -0
- package/dist/vitest.js.map +1 -0
- package/package.json +55 -3
package/dist/canary.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TESTING_CONTEXT_NAMES = exports.CONTAINS_CHECK_CONTEXT = exports.MATCHER_CONTEXT = exports.REDACTION_CHECK_CONTEXT = exports.LEAK_CHECK_CONTEXT = exports.UnreadableSecretError = void 0;
|
|
4
|
+
exports.rememberCanary = rememberCanary;
|
|
5
|
+
exports.recallCanary = recallCanary;
|
|
6
|
+
exports.rawValueOf = rawValueOf;
|
|
7
|
+
/**
|
|
8
|
+
* Canary values, keyed by the instance created from them.
|
|
9
|
+
*
|
|
10
|
+
* A leak check needs the raw value to search for. Reading it back through
|
|
11
|
+
* `reveal()` works, but it runs the class policy's authorization hook, fires
|
|
12
|
+
* the active audit context, and throws once the value has expired, so an
|
|
13
|
+
* assertion about output would fail for reasons that have nothing to do with
|
|
14
|
+
* the output. Because this package generated these values in the first place,
|
|
15
|
+
* it can simply remember them.
|
|
16
|
+
*
|
|
17
|
+
* The map holds values this package created, not values an application loaded.
|
|
18
|
+
* It is module private and keyed by object identity, so nothing outside these
|
|
19
|
+
* functions can enumerate it, and an entry becomes unreachable with the
|
|
20
|
+
* instance it belongs to. A value passed to `createTestSecret` through the
|
|
21
|
+
* `value` option is remembered the same way, which is a reason to keep real
|
|
22
|
+
* secrets out of that option.
|
|
23
|
+
*/
|
|
24
|
+
const canaries = new WeakMap();
|
|
25
|
+
/** Record the raw value this package generated for an instance. */
|
|
26
|
+
function rememberCanary(secret, raw) {
|
|
27
|
+
canaries.set(secret, raw);
|
|
28
|
+
}
|
|
29
|
+
/** The remembered raw value, or undefined for an instance from anywhere else. */
|
|
30
|
+
function recallCanary(secret) {
|
|
31
|
+
return canaries.get(secret);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The raw value of a secret, for a check that has to search for it.
|
|
35
|
+
*
|
|
36
|
+
* A value this package created is read from the canary registry. Anything else
|
|
37
|
+
* is revealed under an explicit, named context, so the reveal is attributable
|
|
38
|
+
* and does not silently inherit an ambient context the test set up for its own
|
|
39
|
+
* assertions.
|
|
40
|
+
*
|
|
41
|
+
* A class policy whose `checkRevealAllowed` rejects unknown contexts will
|
|
42
|
+
* refuse these reveals. Such a policy should allow the names exported as
|
|
43
|
+
* `TESTING_CONTEXT_NAMES`, or the test should build its secrets with
|
|
44
|
+
* `createTestSecret`, which needs no reveal at all.
|
|
45
|
+
*/
|
|
46
|
+
function rawValueOf(secret, contextName) {
|
|
47
|
+
const remembered = recallCanary(secret);
|
|
48
|
+
if (remembered !== undefined) {
|
|
49
|
+
return remembered;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return secret.reveal({ name: contextName });
|
|
53
|
+
}
|
|
54
|
+
catch (cause) {
|
|
55
|
+
// Without this the caller sees an ExpiredSensitiveValueError, or whatever
|
|
56
|
+
// the policy threw, reported from inside an assertion about output. That
|
|
57
|
+
// reads as an unrelated failure, and the dangerous reading of it is that
|
|
58
|
+
// the check ran and found nothing. It did not run at all.
|
|
59
|
+
throw new UnreadableSecretError(secret.klass, secret.label, contextName, cause);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Raised when a check could not read the value it was asked to look for.
|
|
64
|
+
*
|
|
65
|
+
* This is not a leak report and not a clean result. It means the assertion did
|
|
66
|
+
* not run, which matters because the alternative reading of a silent pass is
|
|
67
|
+
* that nothing leaked.
|
|
68
|
+
*/
|
|
69
|
+
class UnreadableSecretError extends Error {
|
|
70
|
+
klass;
|
|
71
|
+
label;
|
|
72
|
+
contextName;
|
|
73
|
+
constructor(klass, label,
|
|
74
|
+
/** The context the reveal was attempted under. */
|
|
75
|
+
contextName, cause) {
|
|
76
|
+
super([
|
|
77
|
+
'Cannot check this value: reading it for the check was refused.',
|
|
78
|
+
` Label: ${label ?? '(none)'}`,
|
|
79
|
+
` Class: ${klass}`,
|
|
80
|
+
` Context: ${contextName}`,
|
|
81
|
+
` Cause: ${describeCause(cause)}`,
|
|
82
|
+
' A value from createTestSecret is remembered when it is created and needs',
|
|
83
|
+
' no reveal, so this check would have run on one. This value came from',
|
|
84
|
+
' elsewhere, so the check had to reveal it, and its deadline or its class',
|
|
85
|
+
' policy refused. Build the value with createTestSecret, or allow the',
|
|
86
|
+
' context names in TESTING_CONTEXT_NAMES.',
|
|
87
|
+
].join('\n'), { cause });
|
|
88
|
+
this.klass = klass;
|
|
89
|
+
this.label = label;
|
|
90
|
+
this.contextName = contextName;
|
|
91
|
+
this.name = 'UnreadableSecretError';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.UnreadableSecretError = UnreadableSecretError;
|
|
95
|
+
function describeCause(cause) {
|
|
96
|
+
return cause instanceof Error ? `${cause.name}: ${cause.message}` : String(cause);
|
|
97
|
+
}
|
|
98
|
+
/** Context name used when a leak assertion has to reveal a foreign value. */
|
|
99
|
+
exports.LEAK_CHECK_CONTEXT = '__test-leak-check__';
|
|
100
|
+
/** Context name used when a redaction assertion has to reveal a foreign value. */
|
|
101
|
+
exports.REDACTION_CHECK_CONTEXT = '__test-redaction-check__';
|
|
102
|
+
/** Context name used when a matcher has to reveal a foreign value. */
|
|
103
|
+
exports.MATCHER_CONTEXT = '__test-matcher__';
|
|
104
|
+
/** Context name used when `toContainSensitive` has to reveal a foreign value. */
|
|
105
|
+
exports.CONTAINS_CHECK_CONTEXT = '__test-contains-check__';
|
|
106
|
+
/**
|
|
107
|
+
* Every context name this package reveals under, so a strict policy can
|
|
108
|
+
* allowlist them in one place.
|
|
109
|
+
*/
|
|
110
|
+
exports.TESTING_CONTEXT_NAMES = [
|
|
111
|
+
exports.LEAK_CHECK_CONTEXT,
|
|
112
|
+
exports.REDACTION_CHECK_CONTEXT,
|
|
113
|
+
exports.MATCHER_CONTEXT,
|
|
114
|
+
exports.CONTAINS_CHECK_CONTEXT,
|
|
115
|
+
];
|
|
116
|
+
//# sourceMappingURL=canary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"canary.js","sourceRoot":"","sources":["../src/canary.ts"],"names":[],"mappings":";;;AAsBA,wCAEC;AAGD,oCAEC;AAeD,gCAeC;AAzDD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,QAAQ,GAAG,IAAI,OAAO,EAA6C,CAAC;AAE1E,mEAAmE;AACnE,SAAgB,cAAc,CAAC,MAAyC,EAAE,GAAW;IACnF,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC5B,CAAC;AAED,iFAAiF;AACjF,SAAgB,YAAY,CAAC,MAAyC;IACpE,OAAO,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AAC9B,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,UAAU,CAAC,MAAyC,EAAE,WAAmB;IACvF,MAAM,UAAU,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;IACxC,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC7B,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;IAC9C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,0EAA0E;QAC1E,yEAAyE;QACzE,yEAAyE;QACzE,0DAA0D;QAC1D,MAAM,IAAI,qBAAqB,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC;IAClF,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAa,qBAAsB,SAAQ,KAAK;IAEnC;IACA;IAEA;IAJX,YACW,KAAuB,EACvB,KAAyB;IAClC,kDAAkD;IACzC,WAAmB,EAC5B,KAAc;QAEd,KAAK,CACH;YACE,gEAAgE;YAChE,cAAc,KAAK,IAAI,QAAQ,EAAE;YACjC,cAAc,KAAK,EAAE;YACrB,cAAc,WAAW,EAAE;YAC3B,cAAc,aAAa,CAAC,KAAK,CAAC,EAAE;YACpC,4EAA4E;YAC5E,wEAAwE;YACxE,2EAA2E;YAC3E,uEAAuE;YACvE,2CAA2C;SAC5C,CAAC,IAAI,CAAC,IAAI,CAAC,EACZ,EAAE,KAAK,EAAE,CACV,CAAC;QApBO,UAAK,GAAL,KAAK,CAAkB;QACvB,UAAK,GAAL,KAAK,CAAoB;QAEzB,gBAAW,GAAX,WAAW,CAAQ;QAkB5B,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACtC,CAAC;CACF;AAzBD,sDAyBC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACpF,CAAC;AAED,6EAA6E;AAChE,QAAA,kBAAkB,GAAG,qBAAqB,CAAC;AAExD,kFAAkF;AACrE,QAAA,uBAAuB,GAAG,0BAA0B,CAAC;AAElE,sEAAsE;AACzD,QAAA,eAAe,GAAG,kBAAkB,CAAC;AAElD,iFAAiF;AACpE,QAAA,sBAAsB,GAAG,yBAAyB,CAAC;AAEhE;;;GAGG;AACU,QAAA,qBAAqB,GAAsB;IACtD,0BAAkB;IAClB,+BAAuB;IACvB,uBAAe;IACf,8BAAsB;CACvB,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { SensitiveString } from '@platform-sec/sensitive-string-core';
|
|
2
|
+
import type { SensitivityClass, SensitiveStringOptions } from '@platform-sec/sensitive-string-core';
|
|
3
|
+
/**
|
|
4
|
+
* Options for `createTestSecret`.
|
|
5
|
+
*
|
|
6
|
+
* Every option of `SensitiveStringOptions` is accepted and passed through, so a
|
|
7
|
+
* feature that adds one to the core options object is usable here without a
|
|
8
|
+
* change to this package.
|
|
9
|
+
*/
|
|
10
|
+
export interface TestSecretOptions extends SensitiveStringOptions {
|
|
11
|
+
/** Leading marker of the generated value. Defaults to `__CANARY_`. */
|
|
12
|
+
prefix?: string;
|
|
13
|
+
/** Trailing marker of the generated value. Defaults to `__`. */
|
|
14
|
+
suffix?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Use this value instead of generating one.
|
|
17
|
+
*
|
|
18
|
+
* Useful when a test asserts on an exact value or needs a value of a specific
|
|
19
|
+
* shape, such as an address for the `email` mask strategy. It is remembered
|
|
20
|
+
* in the canary registry like a generated value, so it should be a fixture
|
|
21
|
+
* rather than a real secret.
|
|
22
|
+
*/
|
|
23
|
+
value?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create a `SensitiveString` whose value is a unique, conspicuous canary.
|
|
27
|
+
*
|
|
28
|
+
* The value is built to be searchable, so it stands out in a diff, a log
|
|
29
|
+
* capture, or a snapshot, and unique, so a leak found in one test cannot be
|
|
30
|
+
* confused with a value another test created. Nothing else about it is
|
|
31
|
+
* meaningful: it is not a realistic API key and is not meant to look like one.
|
|
32
|
+
*
|
|
33
|
+
* The raw value is recorded in this package's canary registry, so the leak and
|
|
34
|
+
* redaction assertions can search for it without revealing the instance.
|
|
35
|
+
*
|
|
36
|
+
* @param klass - Sensitivity classification. Defaults to `sensitive`.
|
|
37
|
+
* @param label - Optional label, carried onto the instance.
|
|
38
|
+
* @param options - Canary shape, plus any `SensitiveStringOptions`.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const apiKey = createTestSecret('credential', 'api_key');
|
|
43
|
+
* const otp = createTestSecret('credential', 'otp', { ttl: 30_000 });
|
|
44
|
+
* const email = createTestSecret('sensitive', 'email', { value: 'jane@example.com' });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function createTestSecret<C extends SensitivityClass = 'sensitive'>(klass?: C, label?: string, options?: TestSecretOptions): SensitiveString<C>;
|
|
48
|
+
/**
|
|
49
|
+
* Create a test secret whose deadline has already passed.
|
|
50
|
+
*
|
|
51
|
+
* `ttl: 0` is the deadline at construction, so the value is expired before the
|
|
52
|
+
* test does anything with it and no clock has to be advanced or faked.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* expect(() => createExpiredSecret('credential', 'otp').reveal()).toThrow(
|
|
57
|
+
* ExpiredSensitiveValueError
|
|
58
|
+
* );
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function createExpiredSecret<C extends SensitivityClass = 'sensitive'>(klass?: C, label?: string, options?: Omit<TestSecretOptions, 'ttl' | 'expiresAt'>): SensitiveString<C>;
|
|
62
|
+
/**
|
|
63
|
+
* Create a test secret that expires after `ttlMs`.
|
|
64
|
+
*
|
|
65
|
+
* A thin wrapper over `createTestSecret(klass, label, { ttl })` that reads as
|
|
66
|
+
* what the test is setting up. Pair it with fake timers rather than a real
|
|
67
|
+
* wait: the deadline is checked against the clock, not against a timer.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* vi.useFakeTimers();
|
|
72
|
+
* const session = createExpiringSecret('credential', 'session', 60_000);
|
|
73
|
+
* vi.advanceTimersByTime(60_001);
|
|
74
|
+
* expect(session.isExpired()).toBe(true);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function createExpiringSecret<C extends SensitivityClass = 'sensitive'>(klass: C | undefined, label: string | undefined, ttlMs: number, options?: Omit<TestSecretOptions, 'ttl' | 'expiresAt'>): SensitiveString<C>;
|
|
78
|
+
//# sourceMappingURL=create-test-secret.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-test-secret.d.ts","sourceRoot":"","sources":["../src/create-test-secret.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,KAAK,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAGpG;;;;;;GAMG;AACH,MAAM,WAAW,iBAAkB,SAAQ,sBAAsB;IAC/D,sEAAsE;IACtE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAWD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,gBAAgB,GAAG,WAAW,EACvE,KAAK,CAAC,EAAE,CAAC,EACT,KAAK,CAAC,EAAE,MAAM,EACd,OAAO,GAAE,iBAAsB,GAC9B,eAAe,CAAC,CAAC,CAAC,CAapB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,gBAAgB,GAAG,WAAW,EAC1E,KAAK,CAAC,EAAE,CAAC,EACT,KAAK,CAAC,EAAE,MAAM,EACd,OAAO,GAAE,IAAI,CAAC,iBAAiB,EAAE,KAAK,GAAG,WAAW,CAAM,GACzD,eAAe,CAAC,CAAC,CAAC,CAEpB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,GAAG,WAAW,EAC3E,KAAK,EAAE,CAAC,GAAG,SAAS,EACpB,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,IAAI,CAAC,iBAAiB,EAAE,KAAK,GAAG,WAAW,CAAM,GACzD,eAAe,CAAC,CAAC,CAAC,CAEpB"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTestSecret = createTestSecret;
|
|
4
|
+
exports.createExpiredSecret = createExpiredSecret;
|
|
5
|
+
exports.createExpiringSecret = createExpiringSecret;
|
|
6
|
+
const node_crypto_1 = require("node:crypto");
|
|
7
|
+
const sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
|
|
8
|
+
const canary_js_1 = require("./canary.js");
|
|
9
|
+
/**
|
|
10
|
+
* Counter component of the generated value.
|
|
11
|
+
*
|
|
12
|
+
* The timestamp alone is not enough: two secrets created in the same
|
|
13
|
+
* millisecond would collide, and under fake timers the clock does not move at
|
|
14
|
+
* all between them.
|
|
15
|
+
*/
|
|
16
|
+
let counter = 0;
|
|
17
|
+
/**
|
|
18
|
+
* Create a `SensitiveString` whose value is a unique, conspicuous canary.
|
|
19
|
+
*
|
|
20
|
+
* The value is built to be searchable, so it stands out in a diff, a log
|
|
21
|
+
* capture, or a snapshot, and unique, so a leak found in one test cannot be
|
|
22
|
+
* confused with a value another test created. Nothing else about it is
|
|
23
|
+
* meaningful: it is not a realistic API key and is not meant to look like one.
|
|
24
|
+
*
|
|
25
|
+
* The raw value is recorded in this package's canary registry, so the leak and
|
|
26
|
+
* redaction assertions can search for it without revealing the instance.
|
|
27
|
+
*
|
|
28
|
+
* @param klass - Sensitivity classification. Defaults to `sensitive`.
|
|
29
|
+
* @param label - Optional label, carried onto the instance.
|
|
30
|
+
* @param options - Canary shape, plus any `SensitiveStringOptions`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const apiKey = createTestSecret('credential', 'api_key');
|
|
35
|
+
* const otp = createTestSecret('credential', 'otp', { ttl: 30_000 });
|
|
36
|
+
* const email = createTestSecret('sensitive', 'email', { value: 'jane@example.com' });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function createTestSecret(klass, label, options = {}) {
|
|
40
|
+
const { prefix = '__CANARY_', suffix = '__', value, ...sensitiveStringOptions } = options;
|
|
41
|
+
const raw = value ?? generateCanary(prefix, suffix);
|
|
42
|
+
const secret = new sensitive_string_core_1.SensitiveString(raw, (klass ?? 'sensitive'), label, sensitiveStringOptions);
|
|
43
|
+
(0, canary_js_1.rememberCanary)(secret, raw);
|
|
44
|
+
return secret;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create a test secret whose deadline has already passed.
|
|
48
|
+
*
|
|
49
|
+
* `ttl: 0` is the deadline at construction, so the value is expired before the
|
|
50
|
+
* test does anything with it and no clock has to be advanced or faked.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* expect(() => createExpiredSecret('credential', 'otp').reveal()).toThrow(
|
|
55
|
+
* ExpiredSensitiveValueError
|
|
56
|
+
* );
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
function createExpiredSecret(klass, label, options = {}) {
|
|
60
|
+
return createTestSecret(klass, label, { ...options, ttl: 0 });
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create a test secret that expires after `ttlMs`.
|
|
64
|
+
*
|
|
65
|
+
* A thin wrapper over `createTestSecret(klass, label, { ttl })` that reads as
|
|
66
|
+
* what the test is setting up. Pair it with fake timers rather than a real
|
|
67
|
+
* wait: the deadline is checked against the clock, not against a timer.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* vi.useFakeTimers();
|
|
72
|
+
* const session = createExpiringSecret('credential', 'session', 60_000);
|
|
73
|
+
* vi.advanceTimersByTime(60_001);
|
|
74
|
+
* expect(session.isExpired()).toBe(true);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
function createExpiringSecret(klass, label, ttlMs, options = {}) {
|
|
78
|
+
return createTestSecret(klass, label, { ...options, ttl: ttlMs });
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* A value no application would produce, unique across a process.
|
|
82
|
+
*
|
|
83
|
+
* Randomness comes from `node:crypto` rather than `Math.random`, which test
|
|
84
|
+
* suites stub often enough that a stub would silently turn every canary in a
|
|
85
|
+
* file into the same string.
|
|
86
|
+
*/
|
|
87
|
+
function generateCanary(prefix, suffix) {
|
|
88
|
+
counter += 1;
|
|
89
|
+
return `${prefix}${Date.now()}_${counter}_${(0, node_crypto_1.randomBytes)(6).toString('hex')}${suffix}`;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=create-test-secret.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-test-secret.js","sourceRoot":"","sources":["../src/create-test-secret.ts"],"names":[],"mappings":";;AA2DA,4CAiBC;AAeD,kDAMC;AAiBD,oDAOC;AAzHD,6CAA0C;AAC1C,+EAAsE;AAEtE,2CAA6C;AAyB7C;;;;;;GAMG;AACH,IAAI,OAAO,GAAG,CAAC,CAAC;AAEhB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,gBAAgB,CAC9B,KAAS,EACT,KAAc,EACd,UAA6B,EAAE;IAE/B,MAAM,EAAE,MAAM,GAAG,WAAW,EAAE,MAAM,GAAG,IAAI,EAAE,KAAK,EAAE,GAAG,sBAAsB,EAAE,GAAG,OAAO,CAAC;IAC1F,MAAM,GAAG,GAAG,KAAK,IAAI,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpD,MAAM,MAAM,GAAG,IAAI,uCAAe,CAChC,GAAG,EACH,CAAC,KAAK,IAAI,WAAW,CAAM,EAC3B,KAAK,EACL,sBAAsB,CACvB,CAAC;IAEF,IAAA,0BAAc,EAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE5B,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,mBAAmB,CACjC,KAAS,EACT,KAAc,EACd,UAAwD,EAAE;IAE1D,OAAO,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,GAAG,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;AAChE,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,oBAAoB,CAClC,KAAoB,EACpB,KAAyB,EACzB,KAAa,EACb,UAAwD,EAAE;IAE1D,OAAO,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,GAAG,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;AACpE,CAAC;AAED;;;;;;GAMG;AACH,SAAS,cAAc,CAAC,MAAc,EAAE,MAAc;IACpD,OAAO,IAAI,CAAC,CAAC;IAEb,OAAO,GAAG,MAAM,GAAG,IAAI,CAAC,GAAG,EAAE,IAAI,OAAO,IAAI,IAAA,yBAAW,EAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,MAAM,EAAE,CAAC;AACxF,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Name a value by its type, never by its content.
|
|
3
|
+
*
|
|
4
|
+
* Every message this package produces runs through here rather than through
|
|
5
|
+
* template interpolation of the value itself. What reaches an assertion or a
|
|
6
|
+
* serializer may be a raw copy of a secret, and the string built from it ends
|
|
7
|
+
* up in a CI log, so the type is the most a message may say about it.
|
|
8
|
+
*
|
|
9
|
+
* It answers for any value, including the awkward ones. A message that explains
|
|
10
|
+
* what went wrong must not be the thing that throws: replacing a clear
|
|
11
|
+
* complaint about a bad argument with `Cannot read properties of undefined`
|
|
12
|
+
* loses the complaint and points the reader at this package instead of at their
|
|
13
|
+
* own call.
|
|
14
|
+
*/
|
|
15
|
+
export declare function describeType(received: unknown): string;
|
|
16
|
+
//# sourceMappingURL=describe-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"describe-type.d.ts","sourceRoot":"","sources":["../src/describe-type.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,CAoBtD"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.describeType = describeType;
|
|
4
|
+
/**
|
|
5
|
+
* Name a value by its type, never by its content.
|
|
6
|
+
*
|
|
7
|
+
* Every message this package produces runs through here rather than through
|
|
8
|
+
* template interpolation of the value itself. What reaches an assertion or a
|
|
9
|
+
* serializer may be a raw copy of a secret, and the string built from it ends
|
|
10
|
+
* up in a CI log, so the type is the most a message may say about it.
|
|
11
|
+
*
|
|
12
|
+
* It answers for any value, including the awkward ones. A message that explains
|
|
13
|
+
* what went wrong must not be the thing that throws: replacing a clear
|
|
14
|
+
* complaint about a bad argument with `Cannot read properties of undefined`
|
|
15
|
+
* loses the complaint and points the reader at this package instead of at their
|
|
16
|
+
* own call.
|
|
17
|
+
*/
|
|
18
|
+
function describeType(received) {
|
|
19
|
+
if (received === null) {
|
|
20
|
+
return 'null';
|
|
21
|
+
}
|
|
22
|
+
if (typeof received !== 'object') {
|
|
23
|
+
return typeof received;
|
|
24
|
+
}
|
|
25
|
+
if (Object.getPrototypeOf(received) === null) {
|
|
26
|
+
return 'an object with no prototype';
|
|
27
|
+
}
|
|
28
|
+
// `constructor` is an ordinary inherited property, so an object can reach
|
|
29
|
+
// here without one, or with one that is not a function: `Object.create` of a
|
|
30
|
+
// null-prototype object is the everyday way to get the first.
|
|
31
|
+
const constructor = received.constructor;
|
|
32
|
+
const name = typeof constructor === 'function' ? constructor.name : undefined;
|
|
33
|
+
return typeof name === 'string' && name !== '' ? name : 'an object';
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=describe-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"describe-type.js","sourceRoot":"","sources":["../src/describe-type.ts"],"names":[],"mappings":";;AAcA,oCAoBC;AAlCD;;;;;;;;;;;;;GAaG;AACH,SAAgB,YAAY,CAAC,QAAiB;IAC5C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACjC,OAAO,OAAO,QAAQ,CAAC;IACzB,CAAC;IAED,IAAI,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,KAAK,IAAI,EAAE,CAAC;QAC7C,OAAO,6BAA6B,CAAC;IACvC,CAAC;IAED,0EAA0E;IAC1E,6EAA6E;IAC7E,8DAA8D;IAC9D,MAAM,WAAW,GAAa,QAAsC,CAAC,WAAW,CAAC;IACjF,MAAM,IAAI,GAAY,OAAO,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IAEvF,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC;AACtE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @platform-sec/sensitive-string-testing
|
|
3
|
+
*
|
|
4
|
+
* Test utilities for code that handles `SensitiveString` values.
|
|
5
|
+
*
|
|
6
|
+
* - `createTestSecret` builds unique, conspicuous canary values.
|
|
7
|
+
* - `expectNotLeaked` and `expectRedacted` assert the core security property.
|
|
8
|
+
* - `sensitiveMatchers` adds matchers to Vitest or Jest.
|
|
9
|
+
* - `sensitiveSerializer` keeps snapshots readable without recording a value.
|
|
10
|
+
* - `mockRevealContext` and `createMockAuditLog` record what was accessed.
|
|
11
|
+
*
|
|
12
|
+
* The package depends on no test framework. The matchers are a plain object
|
|
13
|
+
* passed to `expect.extend`, and anything that needs a spy takes a factory such
|
|
14
|
+
* as `vi.fn` or `jest.fn` as an argument.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import {
|
|
19
|
+
* createTestSecret,
|
|
20
|
+
* expectNotLeaked,
|
|
21
|
+
* sensitiveMatchers,
|
|
22
|
+
* } from '@platform-sec/sensitive-string-testing';
|
|
23
|
+
*
|
|
24
|
+
* expect.extend(sensitiveMatchers);
|
|
25
|
+
*
|
|
26
|
+
* it('does not leak the key into the response', async () => {
|
|
27
|
+
* const apiKey = createTestSecret('credential', 'api_key');
|
|
28
|
+
*
|
|
29
|
+
* const response = await handler(apiKey);
|
|
30
|
+
*
|
|
31
|
+
* expect(apiKey).toHaveSensitivityClass('credential');
|
|
32
|
+
* expectNotLeaked(apiKey, response);
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @packageDocumentation
|
|
37
|
+
*/
|
|
38
|
+
export { createTestSecret, createExpiredSecret, createExpiringSecret, } from './create-test-secret.js';
|
|
39
|
+
export type { TestSecretOptions } from './create-test-secret.js';
|
|
40
|
+
export { expectNotLeaked, expectRedacted, LeakageDetectedError } from './assertions.js';
|
|
41
|
+
export type { RedactionOptions } from './assertions.js';
|
|
42
|
+
export { sensitiveMatchers, setupSensitiveMatchers } from './matchers.js';
|
|
43
|
+
export type { SensitiveMatchers, MatcherResult } from './matchers.js';
|
|
44
|
+
export { sensitiveSerializer, createSensitiveSerializer } from './serializer.js';
|
|
45
|
+
export type { SensitiveSerializerOptions, SnapshotSerializerPlugin } from './serializer.js';
|
|
46
|
+
export { mockRevealContext, createMockAuditLog } from './mocks.js';
|
|
47
|
+
export type { AuditCall, AuditEntry, AuditRevealFn, MockAuditLog, MockFunctionFactory, MockRevealContext, MockRevealContextOptions, } from './mocks.js';
|
|
48
|
+
export { collectFragments, findRawValue, findRawValueIn, joinFragments } from './leak-scan.js';
|
|
49
|
+
export type { Fragment, LeakLocation } from './leak-scan.js';
|
|
50
|
+
export { UnreadableSecretError, TESTING_CONTEXT_NAMES, LEAK_CHECK_CONTEXT, REDACTION_CHECK_CONTEXT, MATCHER_CONTEXT, CONTAINS_CHECK_CONTEXT, } from './canary.js';
|
|
51
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAGH,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,yBAAyB,CAAC;AACjC,YAAY,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAGjE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACxF,YAAY,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAGxD,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAC1E,YAAY,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGtE,OAAO,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACjF,YAAY,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAG5F,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACnE,YAAY,EACV,SAAS,EACT,UAAU,EACV,aAAa,EACb,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,wBAAwB,GACzB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/F,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG7D,OAAO,EACL,qBAAqB,EACrB,qBAAqB,EACrB,kBAAkB,EAClB,uBAAuB,EACvB,eAAe,EACf,sBAAsB,GACvB,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @platform-sec/sensitive-string-testing
|
|
4
|
+
*
|
|
5
|
+
* Test utilities for code that handles `SensitiveString` values.
|
|
6
|
+
*
|
|
7
|
+
* - `createTestSecret` builds unique, conspicuous canary values.
|
|
8
|
+
* - `expectNotLeaked` and `expectRedacted` assert the core security property.
|
|
9
|
+
* - `sensitiveMatchers` adds matchers to Vitest or Jest.
|
|
10
|
+
* - `sensitiveSerializer` keeps snapshots readable without recording a value.
|
|
11
|
+
* - `mockRevealContext` and `createMockAuditLog` record what was accessed.
|
|
12
|
+
*
|
|
13
|
+
* The package depends on no test framework. The matchers are a plain object
|
|
14
|
+
* passed to `expect.extend`, and anything that needs a spy takes a factory such
|
|
15
|
+
* as `vi.fn` or `jest.fn` as an argument.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import {
|
|
20
|
+
* createTestSecret,
|
|
21
|
+
* expectNotLeaked,
|
|
22
|
+
* sensitiveMatchers,
|
|
23
|
+
* } from '@platform-sec/sensitive-string-testing';
|
|
24
|
+
*
|
|
25
|
+
* expect.extend(sensitiveMatchers);
|
|
26
|
+
*
|
|
27
|
+
* it('does not leak the key into the response', async () => {
|
|
28
|
+
* const apiKey = createTestSecret('credential', 'api_key');
|
|
29
|
+
*
|
|
30
|
+
* const response = await handler(apiKey);
|
|
31
|
+
*
|
|
32
|
+
* expect(apiKey).toHaveSensitivityClass('credential');
|
|
33
|
+
* expectNotLeaked(apiKey, response);
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @packageDocumentation
|
|
38
|
+
*/
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.CONTAINS_CHECK_CONTEXT = exports.MATCHER_CONTEXT = exports.REDACTION_CHECK_CONTEXT = exports.LEAK_CHECK_CONTEXT = exports.TESTING_CONTEXT_NAMES = exports.UnreadableSecretError = exports.joinFragments = exports.findRawValueIn = exports.findRawValue = exports.collectFragments = exports.createMockAuditLog = exports.mockRevealContext = exports.createSensitiveSerializer = exports.sensitiveSerializer = exports.setupSensitiveMatchers = exports.sensitiveMatchers = exports.LeakageDetectedError = exports.expectRedacted = exports.expectNotLeaked = exports.createExpiringSecret = exports.createExpiredSecret = exports.createTestSecret = void 0;
|
|
41
|
+
// Test secret creation
|
|
42
|
+
var create_test_secret_js_1 = require("./create-test-secret.js");
|
|
43
|
+
Object.defineProperty(exports, "createTestSecret", { enumerable: true, get: function () { return create_test_secret_js_1.createTestSecret; } });
|
|
44
|
+
Object.defineProperty(exports, "createExpiredSecret", { enumerable: true, get: function () { return create_test_secret_js_1.createExpiredSecret; } });
|
|
45
|
+
Object.defineProperty(exports, "createExpiringSecret", { enumerable: true, get: function () { return create_test_secret_js_1.createExpiringSecret; } });
|
|
46
|
+
// Assertions
|
|
47
|
+
var assertions_js_1 = require("./assertions.js");
|
|
48
|
+
Object.defineProperty(exports, "expectNotLeaked", { enumerable: true, get: function () { return assertions_js_1.expectNotLeaked; } });
|
|
49
|
+
Object.defineProperty(exports, "expectRedacted", { enumerable: true, get: function () { return assertions_js_1.expectRedacted; } });
|
|
50
|
+
Object.defineProperty(exports, "LeakageDetectedError", { enumerable: true, get: function () { return assertions_js_1.LeakageDetectedError; } });
|
|
51
|
+
// Matchers
|
|
52
|
+
var matchers_js_1 = require("./matchers.js");
|
|
53
|
+
Object.defineProperty(exports, "sensitiveMatchers", { enumerable: true, get: function () { return matchers_js_1.sensitiveMatchers; } });
|
|
54
|
+
Object.defineProperty(exports, "setupSensitiveMatchers", { enumerable: true, get: function () { return matchers_js_1.setupSensitiveMatchers; } });
|
|
55
|
+
// Snapshot serializer
|
|
56
|
+
var serializer_js_1 = require("./serializer.js");
|
|
57
|
+
Object.defineProperty(exports, "sensitiveSerializer", { enumerable: true, get: function () { return serializer_js_1.sensitiveSerializer; } });
|
|
58
|
+
Object.defineProperty(exports, "createSensitiveSerializer", { enumerable: true, get: function () { return serializer_js_1.createSensitiveSerializer; } });
|
|
59
|
+
// Mocks
|
|
60
|
+
var mocks_js_1 = require("./mocks.js");
|
|
61
|
+
Object.defineProperty(exports, "mockRevealContext", { enumerable: true, get: function () { return mocks_js_1.mockRevealContext; } });
|
|
62
|
+
Object.defineProperty(exports, "createMockAuditLog", { enumerable: true, get: function () { return mocks_js_1.createMockAuditLog; } });
|
|
63
|
+
// Leak scanning, for a check this package does not already provide
|
|
64
|
+
var leak_scan_js_1 = require("./leak-scan.js");
|
|
65
|
+
Object.defineProperty(exports, "collectFragments", { enumerable: true, get: function () { return leak_scan_js_1.collectFragments; } });
|
|
66
|
+
Object.defineProperty(exports, "findRawValue", { enumerable: true, get: function () { return leak_scan_js_1.findRawValue; } });
|
|
67
|
+
Object.defineProperty(exports, "findRawValueIn", { enumerable: true, get: function () { return leak_scan_js_1.findRawValueIn; } });
|
|
68
|
+
Object.defineProperty(exports, "joinFragments", { enumerable: true, get: function () { return leak_scan_js_1.joinFragments; } });
|
|
69
|
+
// Reveal context names, so a strict policy can allow them
|
|
70
|
+
var canary_js_1 = require("./canary.js");
|
|
71
|
+
Object.defineProperty(exports, "UnreadableSecretError", { enumerable: true, get: function () { return canary_js_1.UnreadableSecretError; } });
|
|
72
|
+
Object.defineProperty(exports, "TESTING_CONTEXT_NAMES", { enumerable: true, get: function () { return canary_js_1.TESTING_CONTEXT_NAMES; } });
|
|
73
|
+
Object.defineProperty(exports, "LEAK_CHECK_CONTEXT", { enumerable: true, get: function () { return canary_js_1.LEAK_CHECK_CONTEXT; } });
|
|
74
|
+
Object.defineProperty(exports, "REDACTION_CHECK_CONTEXT", { enumerable: true, get: function () { return canary_js_1.REDACTION_CHECK_CONTEXT; } });
|
|
75
|
+
Object.defineProperty(exports, "MATCHER_CONTEXT", { enumerable: true, get: function () { return canary_js_1.MATCHER_CONTEXT; } });
|
|
76
|
+
Object.defineProperty(exports, "CONTAINS_CHECK_CONTEXT", { enumerable: true, get: function () { return canary_js_1.CONTAINS_CHECK_CONTEXT; } });
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;;;AAEH,uBAAuB;AACvB,iEAIiC;AAH/B,yHAAA,gBAAgB,OAAA;AAChB,4HAAA,mBAAmB,OAAA;AACnB,6HAAA,oBAAoB,OAAA;AAItB,aAAa;AACb,iDAAwF;AAA/E,gHAAA,eAAe,OAAA;AAAE,+GAAA,cAAc,OAAA;AAAE,qHAAA,oBAAoB,OAAA;AAG9D,WAAW;AACX,6CAA0E;AAAjE,gHAAA,iBAAiB,OAAA;AAAE,qHAAA,sBAAsB,OAAA;AAGlD,sBAAsB;AACtB,iDAAiF;AAAxE,oHAAA,mBAAmB,OAAA;AAAE,0HAAA,yBAAyB,OAAA;AAGvD,QAAQ;AACR,uCAAmE;AAA1D,6GAAA,iBAAiB,OAAA;AAAE,8GAAA,kBAAkB,OAAA;AAW9C,mEAAmE;AACnE,+CAA+F;AAAtF,gHAAA,gBAAgB,OAAA;AAAE,4GAAA,YAAY,OAAA;AAAE,8GAAA,cAAc,OAAA;AAAE,6GAAA,aAAa,OAAA;AAGtE,0DAA0D;AAC1D,yCAOqB;AANnB,kHAAA,qBAAqB,OAAA;AACrB,kHAAA,qBAAqB,OAAA;AACrB,+GAAA,kBAAkB,OAAA;AAClB,oHAAA,uBAAuB,OAAA;AACvB,4GAAA,eAAe,OAAA;AACf,mHAAA,sBAAsB,OAAA"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { SensitiveString } from '@platform-sec/sensitive-string-core';
|
|
2
|
+
import type { SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
3
|
+
/**
|
|
4
|
+
* `instanceof SensitiveString` narrows to `SensitiveString<any>`, because the
|
|
5
|
+
* class is generic in its sensitivity class and the check says nothing about
|
|
6
|
+
* which one. The predicate names the union instead, so nothing downstream of
|
|
7
|
+
* the narrowing is typed `any`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isSensitiveString(value: unknown): value is SensitiveString<SensitivityClass>;
|
|
10
|
+
/**
|
|
11
|
+
* One piece of text found while walking a value, and where it came from.
|
|
12
|
+
*/
|
|
13
|
+
export interface Fragment {
|
|
14
|
+
/** Where the text was found, such as `.body.headers['x-api-key']`. */
|
|
15
|
+
readonly path: string;
|
|
16
|
+
/** The text itself. */
|
|
17
|
+
readonly text: string;
|
|
18
|
+
/**
|
|
19
|
+
* True when the text is the redacted rendering of a `SensitiveString` that
|
|
20
|
+
* was still wrapped where it was found, rather than text the target held
|
|
21
|
+
* directly.
|
|
22
|
+
*
|
|
23
|
+
* A leak search still checks these. A wrapper is the correct state for a
|
|
24
|
+
* sensitive value to be in, and the raw value is unreachable through it, so
|
|
25
|
+
* under any policy `registerPolicy` accepts the rendering cannot contain the
|
|
26
|
+
* value and the wrapper is never reported. A rendering that does contain the
|
|
27
|
+
* whole value comes from a policy registered with `skipRedactValidation` or
|
|
28
|
+
* from a subclass that overrode `toString`, and that is a leak, not a false
|
|
29
|
+
* positive: passing it over is the one failure a leak assertion must not have.
|
|
30
|
+
*/
|
|
31
|
+
readonly wrapped: boolean;
|
|
32
|
+
}
|
|
33
|
+
/** Where a raw value was found, and enough context to locate it. */
|
|
34
|
+
export interface LeakLocation {
|
|
35
|
+
/** Path within the target, such as `.headers['authorization']`. */
|
|
36
|
+
readonly path: string;
|
|
37
|
+
/**
|
|
38
|
+
* The surrounding text, with every occurrence of the raw value replaced by
|
|
39
|
+
* `[LEAKED VALUE]`.
|
|
40
|
+
*
|
|
41
|
+
* The value is withheld on purpose: an assertion message ends up in CI logs,
|
|
42
|
+
* and a leak check that prints the secret to prove the secret leaked has only
|
|
43
|
+
* moved the leak.
|
|
44
|
+
*/
|
|
45
|
+
readonly context: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Walk a value and collect every piece of text it holds.
|
|
49
|
+
*
|
|
50
|
+
* This deliberately does not use `JSON.stringify`. `JSON.stringify` calls
|
|
51
|
+
* `toJSON`, and `toJSON` is exactly the method an object uses to decide what it
|
|
52
|
+
* shows: a response class whose `toJSON` omits a field hides a raw value that
|
|
53
|
+
* is still sitting on the object, and every `SensitiveString` renders as its
|
|
54
|
+
* redacted form whether or not the object also holds a leaked plain copy. This
|
|
55
|
+
* walk reads own properties instead, so what it reports is what the object
|
|
56
|
+
* actually holds.
|
|
57
|
+
*
|
|
58
|
+
* Covered: strings, other primitives, arrays and the properties attached to
|
|
59
|
+
* them, `Map`, `Set`, typed arrays and `Buffer` (decoded as UTF-8), `RegExp`
|
|
60
|
+
* sources, own property keys, and own properties whether enumerable or not.
|
|
61
|
+
* That last one is how an `Error`'s `message` and `stack` are reached, and the
|
|
62
|
+
* array properties are how a `String.prototype.match` result's `input`, which
|
|
63
|
+
* holds the whole subject string, is reached. Accessors are invoked, so a value
|
|
64
|
+
* behind a getter is found; a getter that throws is skipped.
|
|
65
|
+
*
|
|
66
|
+
* Not covered: closure variables, `WeakMap` and `WeakSet` contents, unsettled
|
|
67
|
+
* promises, and function bodies. A value reachable only from one of those is
|
|
68
|
+
* beyond what a synchronous walk can see.
|
|
69
|
+
*
|
|
70
|
+
* The traversal is depth first and pre-order, so the first match a search finds
|
|
71
|
+
* is the one nearest the front of the structure as it reads.
|
|
72
|
+
*/
|
|
73
|
+
export declare function collectFragments(root: unknown): Fragment[];
|
|
74
|
+
/**
|
|
75
|
+
* Find the first place a raw value appears in a target.
|
|
76
|
+
*
|
|
77
|
+
* An empty raw value is never reported: it is a substring of every string, and
|
|
78
|
+
* it carries no information to leak.
|
|
79
|
+
*/
|
|
80
|
+
export declare function findRawValue(target: unknown, raw: string): LeakLocation | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Find a raw value in fragments that have already been collected, for a caller
|
|
83
|
+
* that also needs the joined text and should not walk the target twice.
|
|
84
|
+
*/
|
|
85
|
+
export declare function findRawValueIn(fragments: readonly Fragment[], raw: string): LeakLocation | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* Render the collected text of a value as one string, for a check that needs to
|
|
88
|
+
* search the whole target rather than a single leaf.
|
|
89
|
+
*
|
|
90
|
+
* Fragments are joined with a newline, which no fragment contains a boundary
|
|
91
|
+
* across, so a match in the result is a match within one fragment.
|
|
92
|
+
*/
|
|
93
|
+
export declare function joinFragments(fragments: readonly Fragment[]): string;
|
|
94
|
+
//# sourceMappingURL=leak-scan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leak-scan.d.ts","sourceRoot":"","sources":["../src/leak-scan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAE5E;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,eAAe,CAAC,gBAAgB,CAAC,CAE5F;AA0BD;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,sEAAsE;IACtE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uBAAuB;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B;AAED,oEAAoE;AACpE,MAAM,WAAW,YAAY;IAC3B,mEAAmE;IACnE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;;;;;OAOG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,GAAG,QAAQ,EAAE,CAuB1D;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAEnF;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,SAAS,EAAE,SAAS,QAAQ,EAAE,EAC9B,GAAG,EAAE,MAAM,GACV,YAAY,GAAG,SAAS,CAa1B;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,SAAS,QAAQ,EAAE,GAAG,MAAM,CAEpE"}
|