@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
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isSensitiveString = isSensitiveString;
|
|
4
|
+
exports.collectFragments = collectFragments;
|
|
5
|
+
exports.findRawValue = findRawValue;
|
|
6
|
+
exports.findRawValueIn = findRawValueIn;
|
|
7
|
+
exports.joinFragments = joinFragments;
|
|
8
|
+
const sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
|
|
9
|
+
/**
|
|
10
|
+
* `instanceof SensitiveString` narrows to `SensitiveString<any>`, because the
|
|
11
|
+
* class is generic in its sensitivity class and the check says nothing about
|
|
12
|
+
* which one. The predicate names the union instead, so nothing downstream of
|
|
13
|
+
* the narrowing is typed `any`.
|
|
14
|
+
*/
|
|
15
|
+
function isSensitiveString(value) {
|
|
16
|
+
return value instanceof sensitive_string_core_1.SensitiveString;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Most values the walk will look at before it gives up.
|
|
20
|
+
*
|
|
21
|
+
* This is a guard against a hostile target, not a limit a real one reaches. A
|
|
22
|
+
* `Proxy` can fabricate a fresh object for every key forever, and a leak check
|
|
23
|
+
* that hangs the suite is worse than one that stops. Nothing built by ordinary
|
|
24
|
+
* code comes close to this count.
|
|
25
|
+
*
|
|
26
|
+
* There is deliberately no depth limit. A depth limit stops the walk in the
|
|
27
|
+
* middle of a structure it could otherwise finish, and a leak below the cut is
|
|
28
|
+
* then reported as no leak at all, which is the one answer a leak check must
|
|
29
|
+
* never give by accident. The walk uses an explicit stack instead of recursion,
|
|
30
|
+
* so depth costs heap rather than call frames and no limit is needed.
|
|
31
|
+
*/
|
|
32
|
+
const MAX_NODES = 200_000;
|
|
33
|
+
/** Characters of surrounding text kept on each side of a reported match. */
|
|
34
|
+
const CONTEXT_RADIUS = 60;
|
|
35
|
+
/** What a reported match stands in for, so a failure never reprints a secret. */
|
|
36
|
+
const LEAK_PLACEHOLDER = '[LEAKED VALUE]';
|
|
37
|
+
const utf8 = new TextDecoder('utf-8', { fatal: false });
|
|
38
|
+
/**
|
|
39
|
+
* Walk a value and collect every piece of text it holds.
|
|
40
|
+
*
|
|
41
|
+
* This deliberately does not use `JSON.stringify`. `JSON.stringify` calls
|
|
42
|
+
* `toJSON`, and `toJSON` is exactly the method an object uses to decide what it
|
|
43
|
+
* shows: a response class whose `toJSON` omits a field hides a raw value that
|
|
44
|
+
* is still sitting on the object, and every `SensitiveString` renders as its
|
|
45
|
+
* redacted form whether or not the object also holds a leaked plain copy. This
|
|
46
|
+
* walk reads own properties instead, so what it reports is what the object
|
|
47
|
+
* actually holds.
|
|
48
|
+
*
|
|
49
|
+
* Covered: strings, other primitives, arrays and the properties attached to
|
|
50
|
+
* them, `Map`, `Set`, typed arrays and `Buffer` (decoded as UTF-8), `RegExp`
|
|
51
|
+
* sources, own property keys, and own properties whether enumerable or not.
|
|
52
|
+
* That last one is how an `Error`'s `message` and `stack` are reached, and the
|
|
53
|
+
* array properties are how a `String.prototype.match` result's `input`, which
|
|
54
|
+
* holds the whole subject string, is reached. Accessors are invoked, so a value
|
|
55
|
+
* behind a getter is found; a getter that throws is skipped.
|
|
56
|
+
*
|
|
57
|
+
* Not covered: closure variables, `WeakMap` and `WeakSet` contents, unsettled
|
|
58
|
+
* promises, and function bodies. A value reachable only from one of those is
|
|
59
|
+
* beyond what a synchronous walk can see.
|
|
60
|
+
*
|
|
61
|
+
* The traversal is depth first and pre-order, so the first match a search finds
|
|
62
|
+
* is the one nearest the front of the structure as it reads.
|
|
63
|
+
*/
|
|
64
|
+
function collectFragments(root) {
|
|
65
|
+
const fragments = [];
|
|
66
|
+
const seen = new WeakSet();
|
|
67
|
+
const stack = [{ kind: 'value', value: root, path: '' }];
|
|
68
|
+
let budget = MAX_NODES;
|
|
69
|
+
while (stack.length > 0 && budget > 0) {
|
|
70
|
+
budget -= 1;
|
|
71
|
+
const item = stack.pop();
|
|
72
|
+
if (item === undefined) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
if (item.kind === 'text') {
|
|
76
|
+
push(fragments, item.path, item.text, item.wrapped);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
expand(item.value, item.path, fragments, seen, stack);
|
|
80
|
+
}
|
|
81
|
+
return fragments;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Find the first place a raw value appears in a target.
|
|
85
|
+
*
|
|
86
|
+
* An empty raw value is never reported: it is a substring of every string, and
|
|
87
|
+
* it carries no information to leak.
|
|
88
|
+
*/
|
|
89
|
+
function findRawValue(target, raw) {
|
|
90
|
+
return findRawValueIn(collectFragments(target), raw);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Find a raw value in fragments that have already been collected, for a caller
|
|
94
|
+
* that also needs the joined text and should not walk the target twice.
|
|
95
|
+
*/
|
|
96
|
+
function findRawValueIn(fragments, raw) {
|
|
97
|
+
if (raw.length === 0) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
for (const fragment of fragments) {
|
|
101
|
+
const index = fragment.text.indexOf(raw);
|
|
102
|
+
if (index !== -1) {
|
|
103
|
+
return { path: fragment.path, context: describeContext(fragment.text, raw, index) };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Render the collected text of a value as one string, for a check that needs to
|
|
110
|
+
* search the whole target rather than a single leaf.
|
|
111
|
+
*
|
|
112
|
+
* Fragments are joined with a newline, which no fragment contains a boundary
|
|
113
|
+
* across, so a match in the result is a match within one fragment.
|
|
114
|
+
*/
|
|
115
|
+
function joinFragments(fragments) {
|
|
116
|
+
return fragments.map((fragment) => fragment.text).join('\n');
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The text around a match, with the matched value replaced.
|
|
120
|
+
*/
|
|
121
|
+
function describeContext(text, raw, index) {
|
|
122
|
+
const start = Math.max(0, index - CONTEXT_RADIUS);
|
|
123
|
+
const end = Math.min(text.length, index + raw.length + CONTEXT_RADIUS);
|
|
124
|
+
// split/join replaces every occurrence without building a RegExp from the
|
|
125
|
+
// raw value, which would need escaping and would put the value in a pattern.
|
|
126
|
+
const window = text.slice(start, end).split(raw).join(LEAK_PLACEHOLDER);
|
|
127
|
+
return `${start > 0 ? '...' : ''}${window}${end < text.length ? '...' : ''}`;
|
|
128
|
+
}
|
|
129
|
+
function push(fragments, path, text, wrapped = false) {
|
|
130
|
+
if (text.length > 0) {
|
|
131
|
+
fragments.push({ path, text, wrapped });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function valueItem(value, path) {
|
|
135
|
+
return { kind: 'value', value, path };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Queue children so the stack pops them in source order.
|
|
139
|
+
*
|
|
140
|
+
* Reversing here is what keeps the traversal depth first and pre-order, which
|
|
141
|
+
* is the order the reported path assumes.
|
|
142
|
+
*/
|
|
143
|
+
function queue(stack, children) {
|
|
144
|
+
for (let index = children.length - 1; index >= 0; index -= 1) {
|
|
145
|
+
const child = children[index];
|
|
146
|
+
if (child !== undefined) {
|
|
147
|
+
stack.push(child);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Record what a value contributes directly, and queue what it contains. */
|
|
152
|
+
function expand(value, path, fragments, seen, stack) {
|
|
153
|
+
if (value === null || value === undefined) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
switch (typeof value) {
|
|
157
|
+
case 'string':
|
|
158
|
+
push(fragments, path, value);
|
|
159
|
+
return;
|
|
160
|
+
case 'number':
|
|
161
|
+
case 'boolean':
|
|
162
|
+
case 'bigint':
|
|
163
|
+
push(fragments, path, String(value));
|
|
164
|
+
return;
|
|
165
|
+
case 'symbol':
|
|
166
|
+
push(fragments, path, value.description ?? '');
|
|
167
|
+
return;
|
|
168
|
+
case 'function':
|
|
169
|
+
// The name only. A function body can hold a literal secret, but reading
|
|
170
|
+
// every function source in a response object costs more than it finds.
|
|
171
|
+
push(fragments, `${path}.name`, value.name);
|
|
172
|
+
return;
|
|
173
|
+
default:
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
expandObject(value, path, fragments, seen, stack);
|
|
177
|
+
}
|
|
178
|
+
function expandObject(obj, path, fragments, seen, stack) {
|
|
179
|
+
if (isSensitiveString(obj)) {
|
|
180
|
+
// The raw value is unreachable from here, which is the point: a value that
|
|
181
|
+
// is still wrapped has nothing for a leak search to find. What is recorded
|
|
182
|
+
// is the rendering the class policy produces, so a redaction check can look
|
|
183
|
+
// for it and so a policy that renders the value itself is still caught.
|
|
184
|
+
push(fragments, path, safeRedaction(obj), true);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (seen.has(obj)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
seen.add(obj);
|
|
191
|
+
const children = [];
|
|
192
|
+
if (Array.isArray(obj)) {
|
|
193
|
+
obj.forEach((item, index) => {
|
|
194
|
+
children.push(valueItem(item, `${path}[${index}]`));
|
|
195
|
+
});
|
|
196
|
+
// Arrays carry properties beyond their elements often enough to matter: a
|
|
197
|
+
// `String.prototype.match` result is an array whose `input` holds the whole
|
|
198
|
+
// subject string, which is the value the match came from.
|
|
199
|
+
collectOwnProperties(obj, path, children, isElementKey);
|
|
200
|
+
queue(stack, children);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (obj instanceof Map) {
|
|
204
|
+
let index = 0;
|
|
205
|
+
for (const [key, value] of obj) {
|
|
206
|
+
children.push(valueItem(key, `${path}<map key ${index}>`));
|
|
207
|
+
children.push(valueItem(value, `${path}<map value ${index}>`));
|
|
208
|
+
index += 1;
|
|
209
|
+
}
|
|
210
|
+
queue(stack, children);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (obj instanceof Set) {
|
|
214
|
+
let index = 0;
|
|
215
|
+
for (const value of obj) {
|
|
216
|
+
children.push(valueItem(value, `${path}<set ${index}>`));
|
|
217
|
+
index += 1;
|
|
218
|
+
}
|
|
219
|
+
queue(stack, children);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (obj instanceof RegExp) {
|
|
223
|
+
push(fragments, `${path}.source`, obj.source);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (ArrayBuffer.isView(obj) || obj instanceof ArrayBuffer) {
|
|
227
|
+
push(fragments, path, decodeBytes(obj));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
collectOwnProperties(obj, path, children);
|
|
231
|
+
queue(stack, children);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Queue own properties, enumerable or not.
|
|
235
|
+
*
|
|
236
|
+
* Non-enumerable properties are included because hiding a value behind
|
|
237
|
+
* `enumerable: false` is precisely how a copy escapes `JSON.stringify`, and
|
|
238
|
+
* because an `Error`'s `message` and `stack` are non-enumerable own properties.
|
|
239
|
+
*/
|
|
240
|
+
function collectOwnProperties(obj, path, out, skip) {
|
|
241
|
+
for (const key of Reflect.ownKeys(obj)) {
|
|
242
|
+
if (skip?.(key)) {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const keyPath = `${path}${accessorFor(key)}`;
|
|
246
|
+
if (typeof key === 'string') {
|
|
247
|
+
// A raw value used as an object key, such as a cache keyed by token.
|
|
248
|
+
out.push({ kind: 'text', path: `${keyPath} (key)`, text: key, wrapped: false });
|
|
249
|
+
}
|
|
250
|
+
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
|
|
251
|
+
if (descriptor === undefined) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if ('value' in descriptor) {
|
|
255
|
+
out.push(valueItem(descriptor.value, keyPath));
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method -- read to test for presence, then invoked as getter.call(obj) below
|
|
259
|
+
const getter = descriptor.get;
|
|
260
|
+
if (getter === undefined) {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
// A value reachable only through a getter is still a value the object
|
|
264
|
+
// holds. A getter that throws tells us nothing, so it is skipped.
|
|
265
|
+
try {
|
|
266
|
+
out.push(valueItem(getter.call(obj), keyPath));
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
// Intentionally ignored.
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/** Keys an array walk has already covered as elements, plus its length. */
|
|
274
|
+
function isElementKey(key) {
|
|
275
|
+
return typeof key === 'string' && (key === 'length' || ARRAY_INDEX.test(key));
|
|
276
|
+
}
|
|
277
|
+
const ARRAY_INDEX = /^(?:0|[1-9]\d*)$/;
|
|
278
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
279
|
+
function accessorFor(key) {
|
|
280
|
+
if (typeof key === 'symbol') {
|
|
281
|
+
return `[Symbol(${key.description ?? ''})]`;
|
|
282
|
+
}
|
|
283
|
+
return IDENTIFIER.test(key) ? `.${key}` : `[${JSON.stringify(key)}]`;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* The redacted rendering of a wrapped value, or a marker when the class policy
|
|
287
|
+
* refuses to produce one. A policy that throws must not turn a leak check into
|
|
288
|
+
* an error about the policy.
|
|
289
|
+
*/
|
|
290
|
+
function safeRedaction(value) {
|
|
291
|
+
try {
|
|
292
|
+
return value.toString();
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return `[SensitiveString<${value.klass}> redaction threw]`;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function decodeBytes(source) {
|
|
299
|
+
try {
|
|
300
|
+
const bytes = source instanceof ArrayBuffer
|
|
301
|
+
? new Uint8Array(source)
|
|
302
|
+
: new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
303
|
+
return utf8.decode(bytes);
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
return '';
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
//# sourceMappingURL=leak-scan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":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AC;QACP,OAAO,oBAAoB,KAAK,CAAC,KAAK,oBAAoB,CAAC;IAC7D,CAAC;AACH,CAAC;AAED,SAAS,WAAW,CAAC,MAAqC;IACxD,IAAI,CAAC;QACH,MAAM,KAAK,GACT,MAAM,YAAY,WAAW;YAC3B,CAAC,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC;YACxB,CAAC,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;QAE1E,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { SensitiveString } from '@platform-sec/sensitive-string-core';
|
|
2
|
+
import type { SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
3
|
+
/**
|
|
4
|
+
* What a matcher returns. Structurally the result type Vitest and Jest both
|
|
5
|
+
* expect, declared here so this package depends on neither.
|
|
6
|
+
*/
|
|
7
|
+
export interface MatcherResult {
|
|
8
|
+
pass: boolean;
|
|
9
|
+
message: () => string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The matchers this package adds, as an interface a consumer can use to
|
|
13
|
+
* augment their test framework's assertion types.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* // vitest.d.ts, when not importing the ready-made augmentation
|
|
18
|
+
* import type { SensitiveMatchers } from '@platform-sec/sensitive-string-testing';
|
|
19
|
+
*
|
|
20
|
+
* declare module 'vitest' {
|
|
21
|
+
* interface Assertion<T = unknown> extends SensitiveMatchers<T> {}
|
|
22
|
+
* interface AsymmetricMatchersContaining extends SensitiveMatchers<unknown> {}
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export interface SensitiveMatchers<R = unknown> {
|
|
27
|
+
/** The value is a `SensitiveString`. */
|
|
28
|
+
toBeSensitiveString(): R;
|
|
29
|
+
/** The value is a `SensitiveString` of this sensitivity class. */
|
|
30
|
+
toHaveSensitivityClass(expected: SensitivityClass): R;
|
|
31
|
+
/** The value is a `SensitiveString` with this label. */
|
|
32
|
+
toHaveLabel(expected: string | undefined): R;
|
|
33
|
+
/** Revealing the value yields exactly this string. */
|
|
34
|
+
toRevealAs(expected: string): R;
|
|
35
|
+
/** The value has a deadline and that deadline has passed. */
|
|
36
|
+
toBeExpired(): R;
|
|
37
|
+
/** The received output contains the raw value of the given secret. */
|
|
38
|
+
toContainSensitive(expected: SensitiveString<SensitivityClass>): R;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Custom matchers for Vitest and Jest.
|
|
42
|
+
*
|
|
43
|
+
* Register them once, in a setup file:
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { expect } from 'vitest';
|
|
47
|
+
* import { sensitiveMatchers } from '@platform-sec/sensitive-string-testing';
|
|
48
|
+
*
|
|
49
|
+
* expect.extend(sensitiveMatchers);
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* Both frameworks also expose a registered matcher asymmetrically, so
|
|
53
|
+
* `expect.toBeSensitiveString()` works inside `toEqual` and
|
|
54
|
+
* `expect.objectContaining` without any further registration.
|
|
55
|
+
*
|
|
56
|
+
* Every matcher is read only: none of them changes the value under assertion,
|
|
57
|
+
* and none of them puts a raw value into a failure message. `toRevealAs` is the
|
|
58
|
+
* one that could, and it reports the redacted rendering and the length instead,
|
|
59
|
+
* because an assertion message ends up in a CI log.
|
|
60
|
+
*
|
|
61
|
+
* `toRevealAs` reveals through the instance, so an expired value or a policy
|
|
62
|
+
* denial fails the assertion rather than passing on a remembered copy. The leak
|
|
63
|
+
* matcher `toContainSensitive` reads the canary registry instead, since what it
|
|
64
|
+
* needs is a string to search output for, not permission to use the value.
|
|
65
|
+
*/
|
|
66
|
+
export declare const sensitiveMatchers: {
|
|
67
|
+
toBeSensitiveString(received: unknown): MatcherResult;
|
|
68
|
+
toHaveSensitivityClass(received: unknown, expected: SensitivityClass): MatcherResult;
|
|
69
|
+
toHaveLabel(received: unknown, expected: string | undefined): MatcherResult;
|
|
70
|
+
toRevealAs(received: unknown, expected: string): MatcherResult;
|
|
71
|
+
toBeExpired(received: unknown): MatcherResult;
|
|
72
|
+
toContainSensitive(received: unknown, expected: SensitiveString<SensitivityClass>): MatcherResult;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Register the matchers with a test framework's `expect`.
|
|
76
|
+
*
|
|
77
|
+
* Taking `expect` as an argument rather than importing it keeps this package
|
|
78
|
+
* free of a dependency on either framework, which is what lets the same build
|
|
79
|
+
* serve Vitest and Jest.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* // vitest.setup.ts
|
|
84
|
+
* import { expect } from 'vitest';
|
|
85
|
+
* import { setupSensitiveMatchers } from '@platform-sec/sensitive-string-testing';
|
|
86
|
+
*
|
|
87
|
+
* setupSensitiveMatchers(expect);
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function setupSensitiveMatchers(expect: {
|
|
91
|
+
extend(matchers: typeof sensitiveMatchers): unknown;
|
|
92
|
+
}): void;
|
|
93
|
+
//# sourceMappingURL=matchers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matchers.d.ts","sourceRoot":"","sources":["../src/matchers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAK5E;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,MAAM,CAAC;CACvB;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,GAAG,OAAO;IAC5C,wCAAwC;IACxC,mBAAmB,IAAI,CAAC,CAAC;IACzB,kEAAkE;IAClE,sBAAsB,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,CAAC;IACtD,wDAAwD;IACxD,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,CAAC,CAAC;IAC7C,sDAAsD;IACtD,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,CAAC,CAAC;IAChC,6DAA6D;IAC7D,WAAW,IAAI,CAAC,CAAC;IACjB,sEAAsE;IACtE,kBAAkB,CAAC,QAAQ,EAAE,eAAe,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;CACpE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,iBAAiB;kCACE,OAAO,GAAG,aAAa;qCAYpB,OAAO,YAAY,gBAAgB,GAAG,aAAa;0BAgB9D,OAAO,YAAY,MAAM,GAAG,SAAS,GAAG,aAAa;yBAgBtD,OAAO,YAAY,MAAM,GAAG,aAAa;0BAwCxC,OAAO,GAAG,aAAa;iCAyBjC,OAAO,YACP,eAAe,CAAC,gBAAgB,CAAC,GAC1C,aAAa;CAgBjB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE;IAC7C,MAAM,CAAC,QAAQ,EAAE,OAAO,iBAAiB,GAAG,OAAO,CAAC;CACrD,GAAG,IAAI,CAEP"}
|
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;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACU,QAAA,iBAAiB,GAAG;IAC/B,mBAAmB,CAAC,QAAiB;QACnC,MAAM,IAAI,GAAG,QAAQ,YAAY,uCAAe,CAAC;QAEjD,OAAO;YACL,IAAI;YACJ,OAAO,EAAE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