@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
package/dist/matchers.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sensitiveMatchers = void 0;
|
|
4
|
+
exports.setupSensitiveMatchers = setupSensitiveMatchers;
|
|
5
|
+
const sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
|
|
6
|
+
const canary_js_1 = require("./canary.js");
|
|
7
|
+
const describe_type_js_1 = require("./describe-type.js");
|
|
8
|
+
const leak_scan_js_1 = require("./leak-scan.js");
|
|
9
|
+
/**
|
|
10
|
+
* Custom matchers for Vitest and Jest.
|
|
11
|
+
*
|
|
12
|
+
* Register them once, in a setup file:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { expect } from 'vitest';
|
|
16
|
+
* import { sensitiveMatchers } from '@platform-sec/sensitive-string-testing';
|
|
17
|
+
*
|
|
18
|
+
* expect.extend(sensitiveMatchers);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* Both frameworks also expose a registered matcher asymmetrically, so
|
|
22
|
+
* `expect.toBeSensitiveString()` works inside `toEqual` and
|
|
23
|
+
* `expect.objectContaining` without any further registration.
|
|
24
|
+
*
|
|
25
|
+
* Every matcher is read only: none of them changes the value under assertion,
|
|
26
|
+
* and none of them puts a raw value into a failure message. `toRevealAs` is the
|
|
27
|
+
* one that could, and it reports the redacted rendering and the length instead,
|
|
28
|
+
* because an assertion message ends up in a CI log.
|
|
29
|
+
*
|
|
30
|
+
* `toRevealAs` reveals through the instance, so an expired value or a policy
|
|
31
|
+
* denial fails the assertion rather than passing on a remembered copy. The leak
|
|
32
|
+
* matcher `toContainSensitive` reads the canary registry instead, since what it
|
|
33
|
+
* needs is a string to search output for, not permission to use the value.
|
|
34
|
+
*/
|
|
35
|
+
exports.sensitiveMatchers = {
|
|
36
|
+
toBeSensitiveString(received) {
|
|
37
|
+
const pass = received instanceof sensitive_string_core_1.SensitiveString;
|
|
38
|
+
return {
|
|
39
|
+
pass,
|
|
40
|
+
message: () => pass
|
|
41
|
+
? 'Expected value not to be a SensitiveString, but it was.'
|
|
42
|
+
: `Expected value to be a SensitiveString, but got ${(0, describe_type_js_1.describeType)(received)}.`,
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
toHaveSensitivityClass(received, expected) {
|
|
46
|
+
if (!(0, leak_scan_js_1.isSensitiveString)(received)) {
|
|
47
|
+
return notASensitiveString(received);
|
|
48
|
+
}
|
|
49
|
+
const actual = received.klass;
|
|
50
|
+
return {
|
|
51
|
+
pass: actual === expected,
|
|
52
|
+
message: () => actual === expected
|
|
53
|
+
? `Expected sensitivity class not to be '${expected}'.`
|
|
54
|
+
: `Expected sensitivity class '${expected}', but got '${actual}'.`,
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
toHaveLabel(received, expected) {
|
|
58
|
+
if (!(received instanceof sensitive_string_core_1.SensitiveString)) {
|
|
59
|
+
return notASensitiveString(received);
|
|
60
|
+
}
|
|
61
|
+
const actual = received.label;
|
|
62
|
+
return {
|
|
63
|
+
pass: actual === expected,
|
|
64
|
+
message: () => actual === expected
|
|
65
|
+
? `Expected label not to be ${quote(expected)}.`
|
|
66
|
+
: `Expected label ${quote(expected)}, but got ${quote(actual)}.`,
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
toRevealAs(received, expected) {
|
|
70
|
+
if (!(received instanceof sensitive_string_core_1.SensitiveString)) {
|
|
71
|
+
return notASensitiveString(received);
|
|
72
|
+
}
|
|
73
|
+
const secret = received;
|
|
74
|
+
let actual;
|
|
75
|
+
try {
|
|
76
|
+
// Deliberately `reveal()`, not the canary registry the leak checks use.
|
|
77
|
+
// This matcher asserts on what revealing the value yields, so it has to
|
|
78
|
+
// go through the same gate the code under test would: a value whose
|
|
79
|
+
// deadline has passed, or whose policy denies the context, does not
|
|
80
|
+
// reveal, and reading a remembered copy would report that it does.
|
|
81
|
+
actual = secret.reveal({ name: canary_js_1.MATCHER_CONTEXT });
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
// An expired value, or one a policy refuses to reveal, is not equal to
|
|
85
|
+
// anything. Reporting it as a failed assertion keeps `.not` usable, which
|
|
86
|
+
// a thrown error would not.
|
|
87
|
+
return {
|
|
88
|
+
pass: false,
|
|
89
|
+
message: () => `Expected the value to reveal, but the reveal failed: ${reason(error)}.`,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const pass = actual === expected;
|
|
93
|
+
return {
|
|
94
|
+
pass,
|
|
95
|
+
message: () => pass
|
|
96
|
+
? `Expected the revealed value not to equal ${quote(expected)}.`
|
|
97
|
+
: [
|
|
98
|
+
`Expected the revealed value to equal ${quote(expected)} `,
|
|
99
|
+
`(${expected.length} characters), but got ${secret.toString()} `,
|
|
100
|
+
`(${actual.length} characters). The actual value is withheld so a `,
|
|
101
|
+
'failing assertion cannot print a secret into a log.',
|
|
102
|
+
].join(''),
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
toBeExpired(received) {
|
|
106
|
+
if (!(received instanceof sensitive_string_core_1.SensitiveString)) {
|
|
107
|
+
return notASensitiveString(received);
|
|
108
|
+
}
|
|
109
|
+
const secret = received;
|
|
110
|
+
const pass = secret.isExpired();
|
|
111
|
+
return {
|
|
112
|
+
pass,
|
|
113
|
+
message: () => {
|
|
114
|
+
if (pass) {
|
|
115
|
+
return 'Expected the value not to be expired, but its deadline has passed.';
|
|
116
|
+
}
|
|
117
|
+
const remaining = secret.ttlRemaining();
|
|
118
|
+
return remaining === undefined
|
|
119
|
+
? 'Expected the value to be expired, but it has no deadline.'
|
|
120
|
+
: `Expected the value to be expired, but it has ${remaining}ms left.`;
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
toContainSensitive(received, expected) {
|
|
125
|
+
const raw = (0, canary_js_1.rawValueOf)(expected, canary_js_1.CONTAINS_CHECK_CONTEXT);
|
|
126
|
+
const location = (0, leak_scan_js_1.findRawValue)(received, raw);
|
|
127
|
+
return {
|
|
128
|
+
pass: location !== undefined,
|
|
129
|
+
message: () => location === undefined
|
|
130
|
+
? 'Expected the output to contain the raw sensitive value, but it does not.'
|
|
131
|
+
: [
|
|
132
|
+
'Expected the output not to contain the raw sensitive value, but found it at ',
|
|
133
|
+
`${location.path === '' ? '(the target itself)' : location.path}: `,
|
|
134
|
+
location.context,
|
|
135
|
+
].join(''),
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Register the matchers with a test framework's `expect`.
|
|
141
|
+
*
|
|
142
|
+
* Taking `expect` as an argument rather than importing it keeps this package
|
|
143
|
+
* free of a dependency on either framework, which is what lets the same build
|
|
144
|
+
* serve Vitest and Jest.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* // vitest.setup.ts
|
|
149
|
+
* import { expect } from 'vitest';
|
|
150
|
+
* import { setupSensitiveMatchers } from '@platform-sec/sensitive-string-testing';
|
|
151
|
+
*
|
|
152
|
+
* setupSensitiveMatchers(expect);
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
function setupSensitiveMatchers(expect) {
|
|
156
|
+
expect.extend(exports.sensitiveMatchers);
|
|
157
|
+
}
|
|
158
|
+
function notASensitiveString(received) {
|
|
159
|
+
return {
|
|
160
|
+
pass: false,
|
|
161
|
+
message: () => `Expected a SensitiveString, but got ${(0, describe_type_js_1.describeType)(received)}.`,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function quote(value) {
|
|
165
|
+
return value === undefined ? '(none)' : `'${value}'`;
|
|
166
|
+
}
|
|
167
|
+
function reason(error) {
|
|
168
|
+
return error instanceof Error ? `${error.name}: ${error.message}` : String(error);
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=matchers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matchers.js","sourceRoot":"","sources":["../src/matchers.ts"],"names":[],"mappings":";;;AAyNA,wDAIC;AA7ND,+EAAsE;AAEtE,2CAAkF;AAClF,yDAAkD;AAClD,iDAAiE;AAyCjE;;;;;;;;;;;;;;;;;;;;;;;;;GAyB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,KAAK,CAAC,KAAyB;IACtC,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,KAAK,GAAG,CAAC;AACvD,CAAC;AAED,SAAS,MAAM,CAAC,KAAc;IAC5B,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"}
|
package/dist/mocks.d.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { AuditContext, ISensitiveString, RevealContext, RevealDetails, SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
2
|
+
/**
|
|
3
|
+
* The audit callback shape core invokes, named so a mock factory can be typed
|
|
4
|
+
* against it.
|
|
5
|
+
*/
|
|
6
|
+
export type AuditRevealFn = (value: ISensitiveString, details: RevealDetails, context: AuditContext) => void;
|
|
7
|
+
/**
|
|
8
|
+
* A factory that wraps a function in the host framework's spy, such as `vi.fn`
|
|
9
|
+
* or `jest.fn`.
|
|
10
|
+
*
|
|
11
|
+
* The factory is passed in rather than detected at runtime. Detection would
|
|
12
|
+
* mean reading a `vi` or `jest` global that only exists under one framework and
|
|
13
|
+
* only when globals are enabled, and importing either framework would make this
|
|
14
|
+
* package depend on the one the consumer might not be using.
|
|
15
|
+
*/
|
|
16
|
+
export type MockFunctionFactory = (implementation: AuditRevealFn) => AuditRevealFn;
|
|
17
|
+
/** One recorded call to a mock context's audit hook. */
|
|
18
|
+
export interface AuditCall {
|
|
19
|
+
readonly timestamp: Date;
|
|
20
|
+
/**
|
|
21
|
+
* The value that was accessed.
|
|
22
|
+
*
|
|
23
|
+
* The instance, not its raw value: an assertion can read `klass` and `label`
|
|
24
|
+
* from it, and reading anything more still has to go through `reveal()`.
|
|
25
|
+
*/
|
|
26
|
+
readonly value: ISensitiveString;
|
|
27
|
+
/** What the operation was, and its parameters. */
|
|
28
|
+
readonly details: RevealDetails;
|
|
29
|
+
/**
|
|
30
|
+
* The context the operation ran under, copied rather than referenced.
|
|
31
|
+
*
|
|
32
|
+
* Not necessarily the mock context itself: an integration that nests a child
|
|
33
|
+
* over it to tag a reveal is audited under the child, and the child's name
|
|
34
|
+
* and tags are what appear here.
|
|
35
|
+
*
|
|
36
|
+
* A copy because the live context of a mock holds `calls`, and a record
|
|
37
|
+
* pointing back at it would make the call log a cycle that neither
|
|
38
|
+
* `JSON.stringify` nor a snapshot assertion could walk. `tags` is copied one
|
|
39
|
+
* level, so a later reveal under the same context cannot rewrite what an
|
|
40
|
+
* earlier record says.
|
|
41
|
+
*
|
|
42
|
+
* A caller that forwards the hook with two arguments names no context, and
|
|
43
|
+
* the record then carries this context's own name and no tags.
|
|
44
|
+
*/
|
|
45
|
+
readonly context: AuditContext;
|
|
46
|
+
}
|
|
47
|
+
/** Options for `mockRevealContext`. */
|
|
48
|
+
export interface MockRevealContextOptions {
|
|
49
|
+
/** Spy factory, such as `vi.fn` or `jest.fn`. */
|
|
50
|
+
fn?: MockFunctionFactory;
|
|
51
|
+
/** Reason recorded on the context. */
|
|
52
|
+
reason?: string;
|
|
53
|
+
/** Tags recorded on the context. */
|
|
54
|
+
tags?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
/** A `RevealContext` that records what was accessed through it. */
|
|
57
|
+
export interface MockRevealContext extends RevealContext {
|
|
58
|
+
readonly name: string;
|
|
59
|
+
/** The audit hook. A spy when a factory was given, a plain function otherwise. */
|
|
60
|
+
auditReveal: AuditRevealFn;
|
|
61
|
+
/** Calls in order. Recorded whether or not a spy factory was given. */
|
|
62
|
+
readonly calls: readonly AuditCall[];
|
|
63
|
+
/** Forget every recorded call, and clear the spy if there is one. */
|
|
64
|
+
reset(): void;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Create a `RevealContext` that records every access made through it.
|
|
68
|
+
*
|
|
69
|
+
* Pass it to `reveal()`, `mask()`, or `withRevealContext()` and assert on
|
|
70
|
+
* `calls`. Passing a spy factory additionally makes `auditReveal` a spy, so the
|
|
71
|
+
* framework's own call assertions work on it.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const ctx = mockRevealContext('payment-handler', { fn: vi.fn });
|
|
76
|
+
* const token = createTestSecret('credential', 'api_key');
|
|
77
|
+
*
|
|
78
|
+
* token.reveal(ctx);
|
|
79
|
+
*
|
|
80
|
+
* expect(ctx.calls).toHaveLength(1);
|
|
81
|
+
* expect(ctx.calls[0]?.details.operation).toBe('reveal');
|
|
82
|
+
* expect(ctx.auditReveal).toHaveBeenCalledTimes(1);
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function mockRevealContext(name: string, options?: MockRevealContextOptions): MockRevealContext;
|
|
86
|
+
/**
|
|
87
|
+
* One recorded access, reduced to what an audit sink would keep.
|
|
88
|
+
*
|
|
89
|
+
* There is deliberately no raw value and no reference to the instance here: an
|
|
90
|
+
* audit log is the thing most likely to be printed by a failing assertion or
|
|
91
|
+
* dumped into a snapshot, so it holds classification and nothing else.
|
|
92
|
+
*/
|
|
93
|
+
export interface AuditEntry {
|
|
94
|
+
readonly timestamp: Date;
|
|
95
|
+
readonly klass: SensitivityClass;
|
|
96
|
+
readonly label: string | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Name of the context the access was made under.
|
|
99
|
+
*
|
|
100
|
+
* Read from the context core reports, so a reveal made under a child context
|
|
101
|
+
* is recorded under the child's name rather than this log's. A caller that
|
|
102
|
+
* forwards the hook with two arguments names no context, and the entry then
|
|
103
|
+
* carries this log's own name.
|
|
104
|
+
*/
|
|
105
|
+
readonly contextName: string;
|
|
106
|
+
/** `reveal`, `mask`, or `renew`. */
|
|
107
|
+
readonly operation: RevealDetails['operation'];
|
|
108
|
+
readonly details: RevealDetails;
|
|
109
|
+
}
|
|
110
|
+
/** A recording audit sink, plus the context that feeds it. */
|
|
111
|
+
export interface MockAuditLog {
|
|
112
|
+
readonly entries: readonly AuditEntry[];
|
|
113
|
+
/** Pass this to `withRevealContext()` or to an individual operation. */
|
|
114
|
+
readonly context: RevealContext;
|
|
115
|
+
/** Entries whose value carried this label. */
|
|
116
|
+
getEntriesFor(label: string): AuditEntry[];
|
|
117
|
+
/** Entries whose value had this sensitivity class. */
|
|
118
|
+
getEntriesByClass(klass: SensitivityClass): AuditEntry[];
|
|
119
|
+
/** Forget every entry. */
|
|
120
|
+
clear(): void;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Create a context whose audit hook records classification only.
|
|
124
|
+
*
|
|
125
|
+
* Use it to assert that a code path audited what it accessed, without the test
|
|
126
|
+
* holding a second reference to the values.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const audit = createMockAuditLog();
|
|
131
|
+
*
|
|
132
|
+
* await withRevealContext(audit.context, async () => {
|
|
133
|
+
* await chargeCard(card);
|
|
134
|
+
* });
|
|
135
|
+
*
|
|
136
|
+
* expect(audit.getEntriesFor('card_number')).toHaveLength(1);
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export declare function createMockAuditLog(name?: string): MockAuditLog;
|
|
140
|
+
//# sourceMappingURL=mocks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mocks.d.ts","sourceRoot":"","sources":["../src/mocks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,gBAAgB,EACjB,MAAM,qCAAqC,CAAC;AAE7C;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,CAC1B,KAAK,EAAE,gBAAgB,EACvB,OAAO,EAAE,aAAa,EACtB,OAAO,EAAE,YAAY,KAClB,IAAI,CAAC;AAEV;;;;;;;;GAQG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,cAAc,EAAE,aAAa,KAAK,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;IAC/C,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;CACjC;AAED,8DAA8D;AAC9D,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,SAAS,UAAU,EAAE,CAAC;IACxC,wEAAwE;IACxE,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;IAChC,8CAA8C;IAC9C,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,EAAE,CAAC;IAC3C,sDAAsD;IACtD,iBAAiB,CAAC,KAAK,EAAE,gBAAgB,GAAG,UAAU,EAAE,CAAC;IACzD,0BAA0B;IAC1B,KAAK,IAAI,IAAI,CAAC;CACf;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,SAAmB,GAAG,YAAY,CAqCxE"}
|
package/dist/mocks.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mockRevealContext = mockRevealContext;
|
|
4
|
+
exports.createMockAuditLog = createMockAuditLog;
|
|
5
|
+
/**
|
|
6
|
+
* Create a `RevealContext` that records every access made through it.
|
|
7
|
+
*
|
|
8
|
+
* Pass it to `reveal()`, `mask()`, or `withRevealContext()` and assert on
|
|
9
|
+
* `calls`. Passing a spy factory additionally makes `auditReveal` a spy, so the
|
|
10
|
+
* framework's own call assertions work on it.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const ctx = mockRevealContext('payment-handler', { fn: vi.fn });
|
|
15
|
+
* const token = createTestSecret('credential', 'api_key');
|
|
16
|
+
*
|
|
17
|
+
* token.reveal(ctx);
|
|
18
|
+
*
|
|
19
|
+
* expect(ctx.calls).toHaveLength(1);
|
|
20
|
+
* expect(ctx.calls[0]?.details.operation).toBe('reveal');
|
|
21
|
+
* expect(ctx.auditReveal).toHaveBeenCalledTimes(1);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function mockRevealContext(name, options = {}) {
|
|
25
|
+
const calls = [];
|
|
26
|
+
const record = (value, details, context) => {
|
|
27
|
+
calls.push({ timestamp: new Date(), value, details, context: snapshot(context, name) });
|
|
28
|
+
};
|
|
29
|
+
const auditReveal = options.fn ? options.fn(record) : record;
|
|
30
|
+
const context = {
|
|
31
|
+
name,
|
|
32
|
+
auditReveal,
|
|
33
|
+
calls,
|
|
34
|
+
reset() {
|
|
35
|
+
calls.length = 0;
|
|
36
|
+
clearSpy(auditReveal);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
if (options.reason !== undefined) {
|
|
40
|
+
context.reason = options.reason;
|
|
41
|
+
}
|
|
42
|
+
if (options.tags !== undefined) {
|
|
43
|
+
context.tags = options.tags;
|
|
44
|
+
}
|
|
45
|
+
return context;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create a context whose audit hook records classification only.
|
|
49
|
+
*
|
|
50
|
+
* Use it to assert that a code path audited what it accessed, without the test
|
|
51
|
+
* holding a second reference to the values.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const audit = createMockAuditLog();
|
|
56
|
+
*
|
|
57
|
+
* await withRevealContext(audit.context, async () => {
|
|
58
|
+
* await chargeCard(card);
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* expect(audit.getEntriesFor('card_number')).toHaveLength(1);
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function createMockAuditLog(name = 'mock-audit-log') {
|
|
65
|
+
const entries = [];
|
|
66
|
+
const context = {
|
|
67
|
+
name,
|
|
68
|
+
// An arrow rather than a method, deliberately: this hook exists to be
|
|
69
|
+
// handed to `withRevealContext()` and forwarded on, and a method that is
|
|
70
|
+
// detached from its object loses `this`. It reads none, and this way it
|
|
71
|
+
// cannot start to.
|
|
72
|
+
auditReveal: (value, details, auditedUnder) => {
|
|
73
|
+
entries.push({
|
|
74
|
+
timestamp: new Date(),
|
|
75
|
+
klass: value.klass,
|
|
76
|
+
label: value.label,
|
|
77
|
+
// Falls back for the same reason `snapshot()` does: a caller
|
|
78
|
+
// forwarding this hook with two arguments names no context, and this
|
|
79
|
+
// one is then the context the operation ran under.
|
|
80
|
+
contextName: contextNameOr(auditedUnder, name),
|
|
81
|
+
operation: details.operation,
|
|
82
|
+
details,
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
return {
|
|
87
|
+
entries,
|
|
88
|
+
context,
|
|
89
|
+
getEntriesFor(label) {
|
|
90
|
+
return entries.filter((entry) => entry.label === label);
|
|
91
|
+
},
|
|
92
|
+
getEntriesByClass(klass) {
|
|
93
|
+
return entries.filter((entry) => entry.klass === klass);
|
|
94
|
+
},
|
|
95
|
+
clear() {
|
|
96
|
+
entries.length = 0;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* What a record keeps of the context an operation ran under.
|
|
102
|
+
*
|
|
103
|
+
* The descriptive fields only, one level deep. See `AuditCall.context` for why
|
|
104
|
+
* this is a copy.
|
|
105
|
+
*
|
|
106
|
+
* `undefined` is not a shape core produces, and it is the shape a caller
|
|
107
|
+
* forwarding the hook itself produces: a two-argument call, which is what core
|
|
108
|
+
* made before it carried the context and what a consumer's own forwarder still
|
|
109
|
+
* makes. This runs inside the raw-value gate, where a `TypeError` would surface
|
|
110
|
+
* as a failing reveal in somebody's test rather than as a missing tag, so the
|
|
111
|
+
* answer is the context the hook was installed on. For a two-argument caller
|
|
112
|
+
* that is the truthful one: it is the context the operation ran under, and the
|
|
113
|
+
* argument exists only because a nested child can now say otherwise.
|
|
114
|
+
*/
|
|
115
|
+
function snapshot(context, installedOn) {
|
|
116
|
+
if (context === undefined)
|
|
117
|
+
return { name: installedOn };
|
|
118
|
+
const copy = {
|
|
119
|
+
name: context.name,
|
|
120
|
+
};
|
|
121
|
+
if (context.reason !== undefined)
|
|
122
|
+
copy.reason = context.reason;
|
|
123
|
+
if (context.tags !== undefined)
|
|
124
|
+
copy.tags = { ...context.tags };
|
|
125
|
+
return copy;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The name of the context an operation ran under, falling back to the context
|
|
129
|
+
* holding the hook when a two-argument caller named none. See `snapshot()`.
|
|
130
|
+
*/
|
|
131
|
+
function contextNameOr(context, installedOn) {
|
|
132
|
+
return context === undefined ? installedOn : context.name;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Clear a spy's recorded calls when the function turns out to be one.
|
|
136
|
+
*
|
|
137
|
+
* Both frameworks expose `mockClear`, so this needs no knowledge of which one
|
|
138
|
+
* produced the spy.
|
|
139
|
+
*/
|
|
140
|
+
function clearSpy(fn) {
|
|
141
|
+
const candidate = fn;
|
|
142
|
+
if (typeof candidate.mockClear === 'function') {
|
|
143
|
+
candidate.mockClear();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=mocks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mocks.js","sourceRoot":"","sources":["../src/mocks.ts"],"names":[],"mappings":";;AAoGA,8CA+BC;AAyDD,gDAqCC;AAhJD;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,iBAAiB,CAC/B,IAAY,EACZ,UAAoC,EAAE;IAEtC,MAAM,KAAK,GAAgB,EAAE,CAAC;IAE9B,MAAM,MAAM,GAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;QACxD,KAAK,CAAC,IAAI,CAAC,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1F,CAAC,CAAC;IAEF,MAAM,WAAW,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,M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4
|
+
* This is the shape of `pretty-format`'s `NewPlugin`, written out here so the
|
|
5
|
+
* package keeps its rule that nothing in it depends on a test framework. The
|
|
6
|
+
* serializer is handed to Vitest's or Jest's `expect.addSnapshotSerializer`,
|
|
7
|
+
* both of which type that parameter as `NewPlugin`, and a structural match is
|
|
8
|
+
* what lets one object satisfy both without importing either.
|
|
9
|
+
*
|
|
10
|
+
* `pretty-format` passes `serialize` five more arguments: the print config, the
|
|
11
|
+
* current indentation, the depth, the values already visited, and a printer for
|
|
12
|
+
* children. This serializer needs none of them, because it renders a leaf: it
|
|
13
|
+
* never prints a child and never recurses, so there is nothing to indent, no
|
|
14
|
+
* depth to track, and no cycle to break. They are declared as optional
|
|
15
|
+
* `unknown` rather than imported so no `pretty-format` version pins this
|
|
16
|
+
* package.
|
|
17
|
+
*/
|
|
18
|
+
export interface SnapshotSerializerPlugin {
|
|
19
|
+
/** Whether this plugin renders the value. Called for every value printed. */
|
|
20
|
+
test(value: unknown): boolean;
|
|
21
|
+
/** The rendering. Called only for values `test` accepted. */
|
|
22
|
+
serialize(value: unknown, config?: unknown, indentation?: string, depth?: number, refs?: unknown, printer?: unknown): string;
|
|
23
|
+
}
|
|
24
|
+
/** Options for {@link createSensitiveSerializer}. */
|
|
25
|
+
export interface SensitiveSerializerOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Add `{expired}` to the default format, so a value whose deadline has passed
|
|
28
|
+
* renders as `SensitiveString<credential:otp:EXPIRED>`.
|
|
29
|
+
*
|
|
30
|
+
* Off by default, and worth leaving off for a stored snapshot: whether a
|
|
31
|
+
* value is expired depends on when the test ran, so a snapshot that records
|
|
32
|
+
* it holds only while the deadline stays on the same side of the clock. It
|
|
33
|
+
* earns its place in an inline snapshot of a value built with `ttl: 0`, which
|
|
34
|
+
* is expired at construction and stays that way.
|
|
35
|
+
*
|
|
36
|
+
* @defaultValue false
|
|
37
|
+
*/
|
|
38
|
+
readonly showExpiration?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Add `{length}` to the default format, so a value renders as
|
|
41
|
+
* `SensitiveString<credential:api_key:len=32>`.
|
|
42
|
+
*
|
|
43
|
+
* Off by default. The length is not the value, and core treats it as
|
|
44
|
+
* metadata, but a snapshot is committed to version control and the length of
|
|
45
|
+
* a credential says more about it than its class and label do.
|
|
46
|
+
*
|
|
47
|
+
* @defaultValue false
|
|
48
|
+
*/
|
|
49
|
+
readonly showLength?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* The rendering, with `{class}`, `{label}`, `{length}` and `{expired}` as
|
|
52
|
+
* placeholders. There is deliberately no placeholder for the value.
|
|
53
|
+
*
|
|
54
|
+
* Each placeholder carries its own separator, so a format is a template for
|
|
55
|
+
* the whole rendering rather than a list of fields to join: `{label}` expands
|
|
56
|
+
* to `:api_key` or to nothing, and the three optional placeholders collapse
|
|
57
|
+
* cleanly on a value with no label, no length shown and no deadline.
|
|
58
|
+
*
|
|
59
|
+
* A format supersedes `showLength` and `showExpiration`: the placeholders
|
|
60
|
+
* written here decide what appears, so the two ways of asking for a field
|
|
61
|
+
* cannot disagree. An unknown placeholder is rejected rather than passed
|
|
62
|
+
* through, so a `{value}` someone expected to work fails at registration
|
|
63
|
+
* instead of printing itself into every snapshot in the suite.
|
|
64
|
+
*
|
|
65
|
+
* @defaultValue `'SensitiveString<{class}{label}>'`, plus `{length}` and
|
|
66
|
+
* `{expired}` when the options above ask for them
|
|
67
|
+
*/
|
|
68
|
+
readonly format?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Build a snapshot serializer with a custom rendering.
|
|
72
|
+
*
|
|
73
|
+
* @param options - The format, and the fields to include.
|
|
74
|
+
* @returns A plugin for `expect.addSnapshotSerializer` or for a runner's config.
|
|
75
|
+
* @throws TypeError If an option has the wrong type, or the format names a placeholder that does not exist.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const verbose = createSensitiveSerializer({ showLength: true, showExpiration: true });
|
|
80
|
+
*
|
|
81
|
+
* expect.addSnapshotSerializer(verbose);
|
|
82
|
+
* // SensitiveString<credential:api_key:len=32>
|
|
83
|
+
* // SensitiveString<credential:otp:len=6:EXPIRED>
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function createSensitiveSerializer(options?: SensitiveSerializerOptions): SnapshotSerializerPlugin;
|
|
87
|
+
/**
|
|
88
|
+
* A snapshot serializer for `SensitiveString`, with the default format.
|
|
89
|
+
*
|
|
90
|
+
* Register it once, in a setup file:
|
|
91
|
+
*
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { expect } from 'vitest';
|
|
94
|
+
* import { sensitiveSerializer } from '@platform-sec/sensitive-string-testing';
|
|
95
|
+
*
|
|
96
|
+
* expect.addSnapshotSerializer(sensitiveSerializer);
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* A snapshot of a structure holding sensitive values then keeps the shape of
|
|
100
|
+
* the structure and says what each sensitive field is, without recording what
|
|
101
|
+
* it holds:
|
|
102
|
+
*
|
|
103
|
+
* ```
|
|
104
|
+
* {
|
|
105
|
+
* "id": 1,
|
|
106
|
+
* "email": SensitiveString<sensitive:email>,
|
|
107
|
+
* "apiKey": SensitiveString<credential:api_key>,
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* The rendering is built from the class and the label, which are public
|
|
112
|
+
* metadata, and never from the value: nothing here calls `reveal()`, and no
|
|
113
|
+
* format placeholder prints a value. It also does not use the class policy's
|
|
114
|
+
* `redact()`, so a project that registered a custom redaction format gets the
|
|
115
|
+
* same snapshots as one that did not, and a policy registered with
|
|
116
|
+
* `skipRedactValidation` cannot route a value into a snapshot through it.
|
|
117
|
+
*/
|
|
118
|
+
export declare const sensitiveSerializer: SnapshotSerializerPlugin;
|
|
119
|
+
/**
|
|
120
|
+
* The plugin again, as a default export, for a runner that loads it by path
|
|
121
|
+
* rather than by importing it:
|
|
122
|
+
*
|
|
123
|
+
* ```js
|
|
124
|
+
* // jest.config.js
|
|
125
|
+
* module.exports = {
|
|
126
|
+
* snapshotSerializers: ['@platform-sec/sensitive-string-testing/serializer'],
|
|
127
|
+
* };
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* That path is loaded with `require`, which is what this export is for. Code
|
|
131
|
+
* that imports the module should use the named `sensitiveSerializer` instead:
|
|
132
|
+
* this package builds to CommonJS, and a default import of a CommonJS module
|
|
133
|
+
* from ES module code yields the module's exports rather than this value.
|
|
134
|
+
*/
|
|
135
|
+
export default sensitiveSerializer;
|
|
136
|
+
//# sourceMappingURL=serializer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../src/serializer.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,wBAAwB;IACvC,6EAA6E;IAC7E,IAAI,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;IAC9B,6DAA6D;IAC7D,SAAS,CACP,KAAK,EAAE,OAAO,EACd,MAAM,CAAC,EAAE,OAAO,EAChB,WAAW,CAAC,EAAE,MAAM,EACpB,KAAK,CAAC,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,OAAO,GAChB,MAAM,CAAC;CACX;AAED,qDAAqD;AACrD,MAAM,WAAW,0BAA0B;IACzC;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAElC;;;;;;;;;OASG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;IAE9B;;;;;;;;;;;;;;;;;OAiBG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AA6BD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,GAAE,0BAA+B,GACvC,wBAAwB,CAyB1B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,mBAAmB,EAAE,wBAAsD,CAAC;AAEzF;;;;;;;;;;;;;;;GAeG;AACH,eAAe,mBAAmB,CAAC"}
|