@platform-sec/sensitive-string-testing 0.2.0 → 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/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 +10 -10
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { SensitivityClass, SensitiveString } from '@platform-sec/sensitive-string-core';
|
|
2
|
+
/**
|
|
3
|
+
* Raised when a raw sensitive value was found somewhere it should not be.
|
|
4
|
+
*
|
|
5
|
+
* The message names the value by label and class and says where it was found.
|
|
6
|
+
* It never contains the value itself, so a failing security test does not put
|
|
7
|
+
* the secret it is guarding into a CI log.
|
|
8
|
+
*/
|
|
9
|
+
export declare class LeakageDetectedError extends Error {
|
|
10
|
+
readonly label: string | undefined;
|
|
11
|
+
readonly klass: SensitivityClass;
|
|
12
|
+
/** Index in the argument list of the target that leaked. */
|
|
13
|
+
readonly targetIndex: number;
|
|
14
|
+
/** Path within that target, such as `.headers['authorization']`. */
|
|
15
|
+
readonly path: string;
|
|
16
|
+
/** Surrounding text, with the value replaced by `[LEAKED VALUE]`. */
|
|
17
|
+
readonly context: string;
|
|
18
|
+
constructor(label: string | undefined, klass: SensitivityClass,
|
|
19
|
+
/** Index in the argument list of the target that leaked. */
|
|
20
|
+
targetIndex: number,
|
|
21
|
+
/** Path within that target, such as `.headers['authorization']`. */
|
|
22
|
+
path: string,
|
|
23
|
+
/** Surrounding text, with the value replaced by `[LEAKED VALUE]`. */
|
|
24
|
+
context: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Assert that the raw value of a secret appears in none of the targets.
|
|
28
|
+
*
|
|
29
|
+
* Targets are walked rather than serialized with `JSON.stringify`, because
|
|
30
|
+
* `JSON.stringify` asks each object what it wants to show. An object whose
|
|
31
|
+
* `toJSON` omits the field holding a leaked copy would pass a check built on
|
|
32
|
+
* it, and a `SensitiveString` would always render as its redacted form whether
|
|
33
|
+
* or not a plain copy sat next to it. See `collectFragments` for what the walk
|
|
34
|
+
* reaches and what it cannot.
|
|
35
|
+
*
|
|
36
|
+
* A value that is still wrapped in a `SensitiveString` is not a leak and is not
|
|
37
|
+
* reported as one.
|
|
38
|
+
*
|
|
39
|
+
* @throws LeakageDetectedError if the raw value is found.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const apiKey = createTestSecret('credential', 'api_key');
|
|
44
|
+
* const response = await handler(apiKey);
|
|
45
|
+
*
|
|
46
|
+
* expectNotLeaked(apiKey, response, logger.entries, JSON.stringify(response));
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function expectNotLeaked(secret: SensitiveString<SensitivityClass>, ...targets: unknown[]): void;
|
|
50
|
+
/**
|
|
51
|
+
* Options for `expectRedacted`.
|
|
52
|
+
*/
|
|
53
|
+
export interface RedactionOptions {
|
|
54
|
+
/**
|
|
55
|
+
* Pattern the output must match instead of the class policy's redaction.
|
|
56
|
+
*
|
|
57
|
+
* Use it when the output transforms the redacted form, such as a log format
|
|
58
|
+
* that truncates it or a template that wraps it in quotes.
|
|
59
|
+
*/
|
|
60
|
+
customPattern?: RegExp;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Assert that output holds a redacted rendering of a secret and not its value.
|
|
64
|
+
*
|
|
65
|
+
* The rendering that must be present is the one the class policy actually
|
|
66
|
+
* produces, read from the instance rather than assumed to be `[REDACTED:class]`.
|
|
67
|
+
* A custom policy, a per-label redaction, or a future deterministic redaction
|
|
68
|
+
* therefore passes this assertion without the test knowing the format, and a
|
|
69
|
+
* policy change that stops redacting fails it.
|
|
70
|
+
*
|
|
71
|
+
* @throws LeakageDetectedError if the raw value is present.
|
|
72
|
+
* @throws Error if the redacted rendering is absent.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const email = createTestSecret('sensitive', 'email');
|
|
77
|
+
* expectRedacted(formatUserLog({ email }), email);
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function expectRedacted(output: string | object, secret: SensitiveString<SensitivityClass>, options?: RedactionOptions): void;
|
|
81
|
+
//# sourceMappingURL=assertions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assertions.d.ts","sourceRoot":"","sources":["../src/assertions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAO7F;;;;;;GAMG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;IAE3C,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS;IAClC,QAAQ,CAAC,KAAK,EAAE,gBAAgB;IAChC,4DAA4D;IAC5D,QAAQ,CAAC,WAAW,EAAE,MAAM;IAC5B,oEAAoE;IACpE,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,qEAAqE;IACrE,QAAQ,CAAC,OAAO,EAAE,MAAM;gBAPf,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,KAAK,EAAE,gBAAgB;IAChC,4DAA4D;IACnD,WAAW,EAAE,MAAM;IAC5B,oEAAoE;IAC3D,IAAI,EAAE,MAAM;IACrB,qEAAqE;IAC5D,OAAO,EAAE,MAAM;CAc3B;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,EACzC,GAAG,OAAO,EAAE,OAAO,EAAE,GACpB,IAAI,CAgBN;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,EACzC,OAAO,GAAE,gBAAqB,GAC7B,IAAI,CAyBN"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LeakageDetectedError = void 0;
|
|
4
|
+
exports.expectNotLeaked = expectNotLeaked;
|
|
5
|
+
exports.expectRedacted = expectRedacted;
|
|
6
|
+
const canary_js_1 = require("./canary.js");
|
|
7
|
+
const leak_scan_js_1 = require("./leak-scan.js");
|
|
8
|
+
/** How much of a target is quoted when a redaction placeholder is missing. */
|
|
9
|
+
const OUTPUT_PREVIEW = 300;
|
|
10
|
+
/**
|
|
11
|
+
* Raised when a raw sensitive value was found somewhere it should not be.
|
|
12
|
+
*
|
|
13
|
+
* The message names the value by label and class and says where it was found.
|
|
14
|
+
* It never contains the value itself, so a failing security test does not put
|
|
15
|
+
* the secret it is guarding into a CI log.
|
|
16
|
+
*/
|
|
17
|
+
class LeakageDetectedError extends Error {
|
|
18
|
+
label;
|
|
19
|
+
klass;
|
|
20
|
+
targetIndex;
|
|
21
|
+
path;
|
|
22
|
+
context;
|
|
23
|
+
constructor(label, klass,
|
|
24
|
+
/** Index in the argument list of the target that leaked. */
|
|
25
|
+
targetIndex,
|
|
26
|
+
/** Path within that target, such as `.headers['authorization']`. */
|
|
27
|
+
path,
|
|
28
|
+
/** Surrounding text, with the value replaced by `[LEAKED VALUE]`. */
|
|
29
|
+
context) {
|
|
30
|
+
super([
|
|
31
|
+
'Sensitive value leaked.',
|
|
32
|
+
` Label: ${label ?? '(none)'}`,
|
|
33
|
+
` Class: ${klass}`,
|
|
34
|
+
` Target: [${targetIndex}]${path}`,
|
|
35
|
+
` Context: ${context}`,
|
|
36
|
+
' The value itself is withheld so this message is safe to log.',
|
|
37
|
+
].join('\n'));
|
|
38
|
+
this.label = label;
|
|
39
|
+
this.klass = klass;
|
|
40
|
+
this.targetIndex = targetIndex;
|
|
41
|
+
this.path = path;
|
|
42
|
+
this.context = context;
|
|
43
|
+
this.name = 'LeakageDetectedError';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.LeakageDetectedError = LeakageDetectedError;
|
|
47
|
+
/**
|
|
48
|
+
* Assert that the raw value of a secret appears in none of the targets.
|
|
49
|
+
*
|
|
50
|
+
* Targets are walked rather than serialized with `JSON.stringify`, because
|
|
51
|
+
* `JSON.stringify` asks each object what it wants to show. An object whose
|
|
52
|
+
* `toJSON` omits the field holding a leaked copy would pass a check built on
|
|
53
|
+
* it, and a `SensitiveString` would always render as its redacted form whether
|
|
54
|
+
* or not a plain copy sat next to it. See `collectFragments` for what the walk
|
|
55
|
+
* reaches and what it cannot.
|
|
56
|
+
*
|
|
57
|
+
* A value that is still wrapped in a `SensitiveString` is not a leak and is not
|
|
58
|
+
* reported as one.
|
|
59
|
+
*
|
|
60
|
+
* @throws LeakageDetectedError if the raw value is found.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const apiKey = createTestSecret('credential', 'api_key');
|
|
65
|
+
* const response = await handler(apiKey);
|
|
66
|
+
*
|
|
67
|
+
* expectNotLeaked(apiKey, response, logger.entries, JSON.stringify(response));
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function expectNotLeaked(secret, ...targets) {
|
|
71
|
+
const raw = (0, canary_js_1.rawValueOf)(secret, canary_js_1.LEAK_CHECK_CONTEXT);
|
|
72
|
+
for (let index = 0; index < targets.length; index += 1) {
|
|
73
|
+
const location = (0, leak_scan_js_1.findRawValue)(targets[index], raw);
|
|
74
|
+
if (location !== undefined) {
|
|
75
|
+
throw new LeakageDetectedError(secret.label, secret.klass, index, location.path, location.context);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Assert that output holds a redacted rendering of a secret and not its value.
|
|
81
|
+
*
|
|
82
|
+
* The rendering that must be present is the one the class policy actually
|
|
83
|
+
* produces, read from the instance rather than assumed to be `[REDACTED:class]`.
|
|
84
|
+
* A custom policy, a per-label redaction, or a future deterministic redaction
|
|
85
|
+
* therefore passes this assertion without the test knowing the format, and a
|
|
86
|
+
* policy change that stops redacting fails it.
|
|
87
|
+
*
|
|
88
|
+
* @throws LeakageDetectedError if the raw value is present.
|
|
89
|
+
* @throws Error if the redacted rendering is absent.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* const email = createTestSecret('sensitive', 'email');
|
|
94
|
+
* expectRedacted(formatUserLog({ email }), email);
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
function expectRedacted(output, secret, options = {}) {
|
|
98
|
+
const fragments = (0, leak_scan_js_1.collectFragments)(output);
|
|
99
|
+
const raw = (0, canary_js_1.rawValueOf)(secret, canary_js_1.REDACTION_CHECK_CONTEXT);
|
|
100
|
+
const location = (0, leak_scan_js_1.findRawValueIn)(fragments, raw);
|
|
101
|
+
if (location !== undefined) {
|
|
102
|
+
throw new LeakageDetectedError(secret.label, secret.klass, 0, location.path, location.context);
|
|
103
|
+
}
|
|
104
|
+
const text = (0, leak_scan_js_1.joinFragments)(fragments);
|
|
105
|
+
const expected = options.customPattern ?? redactionOf(secret);
|
|
106
|
+
if (matches(text, expected)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
throw new Error([
|
|
110
|
+
'Expected a redaction placeholder in the output, but found none.',
|
|
111
|
+
` Label: ${secret.label ?? '(none)'}`,
|
|
112
|
+
` Class: ${secret.klass}`,
|
|
113
|
+
` Expected: ${typeof expected === 'string' ? JSON.stringify(expected) : String(expected)}`,
|
|
114
|
+
` Output: ${preview(text)}`,
|
|
115
|
+
].join('\n'));
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The rendering the class policy produces for this value.
|
|
119
|
+
*
|
|
120
|
+
* `toString()` is the same call every redacting path in core makes, so this
|
|
121
|
+
* stays correct for a policy whose redaction depends on the value or the label.
|
|
122
|
+
*/
|
|
123
|
+
function redactionOf(secret) {
|
|
124
|
+
return secret.toString();
|
|
125
|
+
}
|
|
126
|
+
function matches(text, expected) {
|
|
127
|
+
return typeof expected === 'string' ? text.includes(expected) : expected.test(text);
|
|
128
|
+
}
|
|
129
|
+
function preview(text) {
|
|
130
|
+
return text.length > OUTPUT_PREVIEW ? `${text.slice(0, OUTPUT_PREVIEW)}...` : text;
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=assertions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ass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AAE;KAC/B,CAAC,IAAI,CAAC,IAAI,CAAC,CACb,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,WAAW,CAAC,MAAyC;IAC5D,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC3B,CAAC;AAED,SAAS,OAAO,CAAC,IAAY,EAAE,QAAyB;IACtD,OAAO,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACtF,CAAC;AAED,SAAS,OAAO,CAAC,IAAY;IAC3B,OAAO,IAAI,CAAC,MAAM,GAAG,cAAc,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AACrF,CAAC"}
|
package/dist/canary.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { SensitivityClass, SensitiveString } from '@platform-sec/sensitive-string-core';
|
|
2
|
+
/** Record the raw value this package generated for an instance. */
|
|
3
|
+
export declare function rememberCanary(secret: SensitiveString<SensitivityClass>, raw: string): void;
|
|
4
|
+
/** The remembered raw value, or undefined for an instance from anywhere else. */
|
|
5
|
+
export declare function recallCanary(secret: SensitiveString<SensitivityClass>): string | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* The raw value of a secret, for a check that has to search for it.
|
|
8
|
+
*
|
|
9
|
+
* A value this package created is read from the canary registry. Anything else
|
|
10
|
+
* is revealed under an explicit, named context, so the reveal is attributable
|
|
11
|
+
* and does not silently inherit an ambient context the test set up for its own
|
|
12
|
+
* assertions.
|
|
13
|
+
*
|
|
14
|
+
* A class policy whose `checkRevealAllowed` rejects unknown contexts will
|
|
15
|
+
* refuse these reveals. Such a policy should allow the names exported as
|
|
16
|
+
* `TESTING_CONTEXT_NAMES`, or the test should build its secrets with
|
|
17
|
+
* `createTestSecret`, which needs no reveal at all.
|
|
18
|
+
*/
|
|
19
|
+
export declare function rawValueOf(secret: SensitiveString<SensitivityClass>, contextName: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Raised when a check could not read the value it was asked to look for.
|
|
22
|
+
*
|
|
23
|
+
* This is not a leak report and not a clean result. It means the assertion did
|
|
24
|
+
* not run, which matters because the alternative reading of a silent pass is
|
|
25
|
+
* that nothing leaked.
|
|
26
|
+
*/
|
|
27
|
+
export declare class UnreadableSecretError extends Error {
|
|
28
|
+
readonly klass: SensitivityClass;
|
|
29
|
+
readonly label: string | undefined;
|
|
30
|
+
/** The context the reveal was attempted under. */
|
|
31
|
+
readonly contextName: string;
|
|
32
|
+
constructor(klass: SensitivityClass, label: string | undefined,
|
|
33
|
+
/** The context the reveal was attempted under. */
|
|
34
|
+
contextName: string, cause: unknown);
|
|
35
|
+
}
|
|
36
|
+
/** Context name used when a leak assertion has to reveal a foreign value. */
|
|
37
|
+
export declare const LEAK_CHECK_CONTEXT = "__test-leak-check__";
|
|
38
|
+
/** Context name used when a redaction assertion has to reveal a foreign value. */
|
|
39
|
+
export declare const REDACTION_CHECK_CONTEXT = "__test-redaction-check__";
|
|
40
|
+
/** Context name used when a matcher has to reveal a foreign value. */
|
|
41
|
+
export declare const MATCHER_CONTEXT = "__test-matcher__";
|
|
42
|
+
/** Context name used when `toContainSensitive` has to reveal a foreign value. */
|
|
43
|
+
export declare const CONTAINS_CHECK_CONTEXT = "__test-contains-check__";
|
|
44
|
+
/**
|
|
45
|
+
* Every context name this package reveals under, so a strict policy can
|
|
46
|
+
* allowlist them in one place.
|
|
47
|
+
*/
|
|
48
|
+
export declare const TESTING_CONTEXT_NAMES: readonly string[];
|
|
49
|
+
//# sourceMappingURL=canary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"canary.d.ts","sourceRoot":"","sources":["../src/canary.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAqB7F,mEAAmE;AACnE,wBAAgB,cAAc,CAAC,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAE3F;AAED,iFAAiF;AACjF,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,GAAG,MAAM,GAAG,SAAS,CAE1F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAejG;AAED;;;;;;GAMG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;IAE5C,QAAQ,CAAC,KAAK,EAAE,gBAAgB;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS;IAClC,kDAAkD;IAClD,QAAQ,CAAC,WAAW,EAAE,MAAM;gBAHnB,KAAK,EAAE,gBAAgB,EACvB,KAAK,EAAE,MAAM,GAAG,SAAS;IAClC,kDAAkD;IACzC,WAAW,EAAE,MAAM,EAC5B,KAAK,EAAE,OAAO;CAmBjB;AAMD,6EAA6E;AAC7E,eAAO,MAAM,kBAAkB,wBAAwB,CAAC;AAExD,kFAAkF;AAClF,eAAO,MAAM,uBAAuB,6BAA6B,CAAC;AAElE,sEAAsE;AACtE,eAAO,MAAM,eAAe,qBAAqB,CAAC;AAElD,iFAAiF;AACjF,eAAO,MAAM,sBAAsB,4BAA4B,CAAC;AAEhE;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,SAAS,MAAM,EAKlD,CAAC"}
|
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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AE,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":"","s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AAG,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"}
|