flare-redact 1.10.4 → 1.11.0
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/CHANGELOG.md +82 -3
- package/README.md +37 -0
- package/dist/audit.d.ts +82 -0
- package/dist/audit.d.ts.map +1 -0
- package/dist/audit.js +146 -0
- package/dist/audit.js.map +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +20 -3
- package/dist/cli.js.map +1 -1
- package/dist/db.d.ts +67 -0
- package/dist/db.d.ts.map +1 -0
- package/dist/db.js +402 -0
- package/dist/db.js.map +1 -0
- package/dist/help.d.ts +1 -1
- package/dist/help.d.ts.map +1 -1
- package/dist/help.js +4 -2
- package/dist/help.js.map +1 -1
- package/dist/recording.d.ts +82 -0
- package/dist/recording.d.ts.map +1 -0
- package/dist/recording.js +195 -0
- package/dist/recording.js.map +1 -0
- package/dist/register.d.ts +22 -4
- package/dist/register.d.ts.map +1 -1
- package/dist/register.js +51 -4
- package/dist/register.js.map +1 -1
- package/dist/replay.d.ts +54 -0
- package/dist/replay.d.ts.map +1 -0
- package/dist/replay.js +87 -0
- package/dist/replay.js.map +1 -0
- package/dist/snapshot.d.ts +25 -0
- package/dist/snapshot.d.ts.map +1 -0
- package/dist/snapshot.js +49 -0
- package/dist/snapshot.js.map +1 -0
- package/docs/guide.md +290 -6
- package/package.json +21 -1
- package/spec/detectors.json +1 -1
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redaction for recorded HTTP traffic: HAR files (browser devtools, Playwright
|
|
3
|
+
* `recordHar`, Polly.js) and anything shaped like a request or response that a
|
|
4
|
+
* test suite writes to disk and commits.
|
|
5
|
+
*
|
|
6
|
+
* A recording leaks in places a log line does not: credential headers whose
|
|
7
|
+
* names carry an `x-` prefix, cookies whose values are opaque session ids, and
|
|
8
|
+
* bodies stored as JSON text, form text or base64. Each is handled here.
|
|
9
|
+
*/
|
|
10
|
+
import { type RedactOptions } from './index.js';
|
|
11
|
+
/**
|
|
12
|
+
* Whether a header, query parameter or form field name carries a credential.
|
|
13
|
+
* `X-Api-Key`, `Proxy-Authorization`, `Set-Cookie` and `access_token` all do;
|
|
14
|
+
* the plain sensitive-key list matches whole names only and would miss them.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isCredentialName(name: string): boolean;
|
|
17
|
+
interface NameValue {
|
|
18
|
+
name: string;
|
|
19
|
+
value: string;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
interface HarContent {
|
|
23
|
+
text?: string;
|
|
24
|
+
encoding?: string;
|
|
25
|
+
mimeType?: string;
|
|
26
|
+
size?: number;
|
|
27
|
+
[key: string]: unknown;
|
|
28
|
+
}
|
|
29
|
+
interface HarMessage {
|
|
30
|
+
url?: string;
|
|
31
|
+
headers?: NameValue[];
|
|
32
|
+
cookies?: NameValue[];
|
|
33
|
+
queryString?: NameValue[];
|
|
34
|
+
postData?: {
|
|
35
|
+
text?: string;
|
|
36
|
+
mimeType?: string;
|
|
37
|
+
params?: NameValue[];
|
|
38
|
+
[key: string]: unknown;
|
|
39
|
+
};
|
|
40
|
+
content?: HarContent;
|
|
41
|
+
[key: string]: unknown;
|
|
42
|
+
}
|
|
43
|
+
export interface HarEntry {
|
|
44
|
+
request?: HarMessage;
|
|
45
|
+
response?: HarMessage;
|
|
46
|
+
[key: string]: unknown;
|
|
47
|
+
}
|
|
48
|
+
export interface Har {
|
|
49
|
+
log: {
|
|
50
|
+
entries?: HarEntry[];
|
|
51
|
+
[key: string]: unknown;
|
|
52
|
+
};
|
|
53
|
+
[key: string]: unknown;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A redactor for recordings. Bodies are redacted as JSON when they parse as
|
|
57
|
+
* JSON (so field names count), as form fields when they are form-encoded, and
|
|
58
|
+
* as text otherwise; a body that needs no change keeps its exact bytes.
|
|
59
|
+
*/
|
|
60
|
+
export declare function createRecordingRedactor(opts?: RedactOptions): {
|
|
61
|
+
/** A redacted copy of one HAR entry (or Polly.js recording). */
|
|
62
|
+
entry: (input: HarEntry) => HarEntry;
|
|
63
|
+
/** A redacted copy of a whole HAR document. */
|
|
64
|
+
har: (input: Har) => Har;
|
|
65
|
+
/** Redact a body held as text; JSON and form bodies keep their field names. */
|
|
66
|
+
body: (text: string, mimeType?: string) => string;
|
|
67
|
+
/** Redact a URL; credential query parameters are masked by name. */
|
|
68
|
+
url: (raw: string) => string;
|
|
69
|
+
/** Redact a header value, masking it whole when the name carries a credential. */
|
|
70
|
+
header: (name: string, raw: string) => string;
|
|
71
|
+
};
|
|
72
|
+
/** A redacted copy of a HAR document. */
|
|
73
|
+
export declare function redactHar(har: Har, opts?: RedactOptions): Har;
|
|
74
|
+
/** A redacted copy of one HAR entry. */
|
|
75
|
+
export declare function redactHarEntry(entry: HarEntry, opts?: RedactOptions): HarEntry;
|
|
76
|
+
/**
|
|
77
|
+
* A Polly.js `beforePersist` handler: `server.any().on('beforePersist', pollyBeforePersist())`.
|
|
78
|
+
* Polly persists the recording object it passes in, so it is updated in place.
|
|
79
|
+
*/
|
|
80
|
+
export declare function pollyBeforePersist(opts?: RedactOptions): (_request: unknown, recording: HarEntry) => void;
|
|
81
|
+
export {};
|
|
82
|
+
//# sourceMappingURL=recording.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recording.d.ts","sourceRoot":"","sources":["../src/recording.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AA0B/D;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAGtD;AAED,UAAU,SAAS;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,UAAU,UAAU;IAClB,IAAI,CAAC,EAAE,MAA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/B,kFAAkF;mBArH/D,MAAM,OAAO,MAAM,KAAG,MAAM;EAwHlD;AAED,yCAAyC;AACzC,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,GAAE,aAAkB,GAAG,GAAG,CAEjE;AAED,wCAAwC;AACxC,wBAAgB,cAAc,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,GAAE,aAAkB,GAAG,QAAQ,CAElF;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,GAAE,aAAkB,IAEjD,UAAU,OAAO,EAAE,WAAW,QAAQ,KAAG,IAAI,CAGtD"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redaction for recorded HTTP traffic: HAR files (browser devtools, Playwright
|
|
3
|
+
* `recordHar`, Polly.js) and anything shaped like a request or response that a
|
|
4
|
+
* test suite writes to disk and commits.
|
|
5
|
+
*
|
|
6
|
+
* A recording leaks in places a log line does not: credential headers whose
|
|
7
|
+
* names carry an `x-` prefix, cookies whose values are opaque session ids, and
|
|
8
|
+
* bodies stored as JSON text, form text or base64. Each is handled here.
|
|
9
|
+
*/
|
|
10
|
+
import { compilePolicy } from './index.js';
|
|
11
|
+
import { Buffer } from 'node:buffer';
|
|
12
|
+
import { SENSITIVE_KEY_RE } from './detectors.js';
|
|
13
|
+
const MASK = '***';
|
|
14
|
+
/** Header and parameter names that always carry a credential, after dropping `x-`. */
|
|
15
|
+
const CREDENTIAL_NAMES = new Set([
|
|
16
|
+
'access_token',
|
|
17
|
+
'api_token',
|
|
18
|
+
'apikey',
|
|
19
|
+
'auth_token',
|
|
20
|
+
'csrf_token',
|
|
21
|
+
'id_token',
|
|
22
|
+
'private_token',
|
|
23
|
+
'proxy_authorization',
|
|
24
|
+
'set_cookie',
|
|
25
|
+
'vault_token',
|
|
26
|
+
'xsrf_token',
|
|
27
|
+
'amz_security_token',
|
|
28
|
+
'amz_credential',
|
|
29
|
+
'amz_signature',
|
|
30
|
+
'goog_api_key',
|
|
31
|
+
'signature',
|
|
32
|
+
]);
|
|
33
|
+
/**
|
|
34
|
+
* Whether a header, query parameter or form field name carries a credential.
|
|
35
|
+
* `X-Api-Key`, `Proxy-Authorization`, `Set-Cookie` and `access_token` all do;
|
|
36
|
+
* the plain sensitive-key list matches whole names only and would miss them.
|
|
37
|
+
*/
|
|
38
|
+
export function isCredentialName(name) {
|
|
39
|
+
const normal = name.trim().toLowerCase().replace(/^x-/, '').replace(/-/g, '_');
|
|
40
|
+
return SENSITIVE_KEY_RE.test(normal) || CREDENTIAL_NAMES.has(normal);
|
|
41
|
+
}
|
|
42
|
+
const TEXTUAL = /^(?:text\/|application\/(?:[\w.+-]*\+)?(?:json|xml|javascript|x-www-form-urlencoded|graphql))/i;
|
|
43
|
+
/**
|
|
44
|
+
* A redactor for recordings. Bodies are redacted as JSON when they parse as
|
|
45
|
+
* JSON (so field names count), as form fields when they are form-encoded, and
|
|
46
|
+
* as text otherwise; a body that needs no change keeps its exact bytes.
|
|
47
|
+
*/
|
|
48
|
+
export function createRecordingRedactor(opts = {}) {
|
|
49
|
+
const policy = compilePolicy(opts);
|
|
50
|
+
const value = (name, raw) => isCredentialName(name) ? MASK : policy.redact({ [name]: raw })[name];
|
|
51
|
+
const pairs = (list, always = false) => list?.map((pair) => ({ ...pair, value: always ? MASK : value(pair.name, String(pair.value ?? '')) }));
|
|
52
|
+
const decode = (part) => {
|
|
53
|
+
try {
|
|
54
|
+
return decodeURIComponent(part.replace(/\+/g, ' '));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return part;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/** Form-encoded text, one field at a time; untouched fields keep their bytes. */
|
|
61
|
+
const form = (text) => {
|
|
62
|
+
let changed = false;
|
|
63
|
+
const parts = text.split('&').map((part) => {
|
|
64
|
+
if (!part)
|
|
65
|
+
return part;
|
|
66
|
+
const eq = part.indexOf('=');
|
|
67
|
+
const rawName = eq === -1 ? part : part.slice(0, eq);
|
|
68
|
+
const raw = eq === -1 ? '' : decode(part.slice(eq + 1));
|
|
69
|
+
const next = value(decode(rawName), raw);
|
|
70
|
+
if (next === raw)
|
|
71
|
+
return part;
|
|
72
|
+
changed = true;
|
|
73
|
+
return `${rawName}=${encodeURIComponent(next).replace(/%20/g, '+')}`;
|
|
74
|
+
});
|
|
75
|
+
return changed ? parts.join('&') : text;
|
|
76
|
+
};
|
|
77
|
+
/** Redact a body held as text, given its media type when known. */
|
|
78
|
+
const body = (text, mimeType = '') => {
|
|
79
|
+
if (/x-www-form-urlencoded/i.test(mimeType))
|
|
80
|
+
return form(text);
|
|
81
|
+
const trimmed = text.trimStart();
|
|
82
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
83
|
+
try {
|
|
84
|
+
const parsed = JSON.parse(text);
|
|
85
|
+
if (policy.isClean(parsed))
|
|
86
|
+
return text;
|
|
87
|
+
return JSON.stringify(policy.redact(parsed));
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
// Not JSON after all: fall through to text.
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return policy.redact(text);
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* The query string is redacted one parameter at a time: run over the whole
|
|
97
|
+
* URL, an assignment detector would read `token=x&page=2` as one value and
|
|
98
|
+
* swallow the parameters after it.
|
|
99
|
+
*/
|
|
100
|
+
const url = (raw) => {
|
|
101
|
+
const hashAt = raw.indexOf('#');
|
|
102
|
+
const beforeHash = hashAt === -1 ? raw : raw.slice(0, hashAt);
|
|
103
|
+
const hash = hashAt === -1 ? '' : policy.redact(raw.slice(hashAt));
|
|
104
|
+
const queryAt = beforeHash.indexOf('?');
|
|
105
|
+
if (queryAt === -1)
|
|
106
|
+
return policy.redact(beforeHash) + hash;
|
|
107
|
+
const base = policy.redact(beforeHash.slice(0, queryAt));
|
|
108
|
+
return `${base}?${form(beforeHash.slice(queryAt + 1))}${hash}`;
|
|
109
|
+
};
|
|
110
|
+
const content = (input) => {
|
|
111
|
+
if (!input || typeof input.text !== 'string')
|
|
112
|
+
return input;
|
|
113
|
+
const mimeType = input.mimeType ?? '';
|
|
114
|
+
if (input.encoding === 'base64') {
|
|
115
|
+
if (!TEXTUAL.test(mimeType))
|
|
116
|
+
return input;
|
|
117
|
+
const decoded = Buffer.from(input.text, 'base64').toString('utf8');
|
|
118
|
+
const next = body(decoded, mimeType);
|
|
119
|
+
if (next === decoded)
|
|
120
|
+
return input;
|
|
121
|
+
return { ...input, text: Buffer.from(next, 'utf8').toString('base64'), size: Buffer.byteLength(next) };
|
|
122
|
+
}
|
|
123
|
+
const next = body(input.text, mimeType);
|
|
124
|
+
return next === input.text ? input : { ...input, text: next, size: Buffer.byteLength(next) };
|
|
125
|
+
};
|
|
126
|
+
const fixLength = (headers, before, after) => {
|
|
127
|
+
if (!headers || before === after || typeof after?.size !== 'number')
|
|
128
|
+
return headers;
|
|
129
|
+
return headers.map((header) => header.name.toLowerCase() === 'content-length' ? { ...header, value: String(after.size) } : header);
|
|
130
|
+
};
|
|
131
|
+
const message = (input) => {
|
|
132
|
+
if (!input)
|
|
133
|
+
return input;
|
|
134
|
+
const out = { ...input };
|
|
135
|
+
if (typeof input.url === 'string')
|
|
136
|
+
out.url = url(input.url);
|
|
137
|
+
if (input.headers)
|
|
138
|
+
out.headers = pairs(input.headers);
|
|
139
|
+
if (input.cookies)
|
|
140
|
+
out.cookies = pairs(input.cookies, true);
|
|
141
|
+
if (input.queryString)
|
|
142
|
+
out.queryString = pairs(input.queryString);
|
|
143
|
+
if (input.postData) {
|
|
144
|
+
const postData = { ...input.postData };
|
|
145
|
+
if (typeof postData.text === 'string')
|
|
146
|
+
postData.text = body(postData.text, postData.mimeType);
|
|
147
|
+
if (postData.params)
|
|
148
|
+
postData.params = pairs(postData.params);
|
|
149
|
+
out.postData = postData;
|
|
150
|
+
}
|
|
151
|
+
if (input.content) {
|
|
152
|
+
out.content = content(input.content);
|
|
153
|
+
out.headers = fixLength(out.headers, input.content, out.content);
|
|
154
|
+
}
|
|
155
|
+
if (typeof input.redirectURL === 'string' && input.redirectURL)
|
|
156
|
+
out.redirectURL = url(input.redirectURL);
|
|
157
|
+
return out;
|
|
158
|
+
};
|
|
159
|
+
const entry = (input) => ({
|
|
160
|
+
...input,
|
|
161
|
+
request: message(input.request),
|
|
162
|
+
response: message(input.response),
|
|
163
|
+
});
|
|
164
|
+
return {
|
|
165
|
+
/** A redacted copy of one HAR entry (or Polly.js recording). */
|
|
166
|
+
entry,
|
|
167
|
+
/** A redacted copy of a whole HAR document. */
|
|
168
|
+
har: (input) => ({ ...input, log: { ...input.log, entries: input.log.entries?.map(entry) } }),
|
|
169
|
+
/** Redact a body held as text; JSON and form bodies keep their field names. */
|
|
170
|
+
body,
|
|
171
|
+
/** Redact a URL; credential query parameters are masked by name. */
|
|
172
|
+
url,
|
|
173
|
+
/** Redact a header value, masking it whole when the name carries a credential. */
|
|
174
|
+
header: value,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/** A redacted copy of a HAR document. */
|
|
178
|
+
export function redactHar(har, opts = {}) {
|
|
179
|
+
return createRecordingRedactor(opts).har(har);
|
|
180
|
+
}
|
|
181
|
+
/** A redacted copy of one HAR entry. */
|
|
182
|
+
export function redactHarEntry(entry, opts = {}) {
|
|
183
|
+
return createRecordingRedactor(opts).entry(entry);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* A Polly.js `beforePersist` handler: `server.any().on('beforePersist', pollyBeforePersist())`.
|
|
187
|
+
* Polly persists the recording object it passes in, so it is updated in place.
|
|
188
|
+
*/
|
|
189
|
+
export function pollyBeforePersist(opts = {}) {
|
|
190
|
+
const redactor = createRecordingRedactor(opts);
|
|
191
|
+
return (_request, recording) => {
|
|
192
|
+
Object.assign(recording, redactor.entry(recording));
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=recording.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1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IACtD,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/register.d.ts
CHANGED
|
@@ -6,12 +6,30 @@
|
|
|
6
6
|
* node --import flare-redact/register app.js
|
|
7
7
|
* NODE_OPTIONS="--import flare-redact/register" npm start
|
|
8
8
|
*
|
|
9
|
-
* FLARE_REDACT_PRESET picks a preset (logs, llm, gdpr, strict).
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* FLARE_REDACT_PRESET picks a preset (logs, llm, gdpr, strict).
|
|
10
|
+
*
|
|
11
|
+
* FLARE_REDACT_MODE picks what happens:
|
|
12
|
+
* enforce redact the output (the default)
|
|
13
|
+
* shadow leave the output as it is and count what would have been redacted
|
|
14
|
+
* audit redact the output and count what was redacted
|
|
15
|
+
* In shadow and audit mode the counts go to stderr when the process exits, and
|
|
16
|
+
* every FLARE_REDACT_REPORT_EVERY seconds when that is set. They never include
|
|
17
|
+
* a matched value.
|
|
18
|
+
*
|
|
19
|
+
* An unknown preset or mode stops the process at startup: a typo should fail
|
|
20
|
+
* the deploy, not leave the logs unredacted.
|
|
12
21
|
*/
|
|
13
22
|
import { type RedactOptions } from './index.js';
|
|
14
23
|
export declare const PRESET_ENV = "FLARE_REDACT_PRESET";
|
|
24
|
+
export declare const MODE_ENV = "FLARE_REDACT_MODE";
|
|
25
|
+
export declare const REPORT_EVERY_ENV = "FLARE_REDACT_REPORT_EVERY";
|
|
26
|
+
export type RegisterMode = 'enforce' | 'shadow' | 'audit';
|
|
27
|
+
type Env = Record<string, string | undefined>;
|
|
15
28
|
/** The options the environment asks for. Throws on an unknown preset. */
|
|
16
|
-
export declare function registerOptions(env:
|
|
29
|
+
export declare function registerOptions(env: Env): RedactOptions;
|
|
30
|
+
/** The mode the environment asks for. Throws on an unknown one. */
|
|
31
|
+
export declare function registerMode(env: Env): RegisterMode;
|
|
32
|
+
/** Seconds between reports, 0 for only at exit. Throws on anything but a whole number. */
|
|
33
|
+
export declare function reportEvery(env: Env): number;
|
|
34
|
+
export {};
|
|
17
35
|
//# sourceMappingURL=register.d.ts.map
|
package/dist/register.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAA8B,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAG5E,eAAO,MAAM,UAAU,wBAAwB,CAAC;AAChD,eAAO,MAAM,QAAQ,sBAAsB,CAAC;AAC5C,eAAO,MAAM,gBAAgB,8BAA8B,CAAC;AAE5D,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,QAAQ,GAAG,OAAO,CAAC;AAE1D,KAAK,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;AAE9C,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,GAAG,EAAE,GAAG,GAAG,aAAa,CAKvD;AAED,mEAAmE;AACnE,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,YAAY,CAMnD;AAED,0FAA0F;AAC1F,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAK5C"}
|
package/dist/register.js
CHANGED
|
@@ -6,12 +6,24 @@
|
|
|
6
6
|
* node --import flare-redact/register app.js
|
|
7
7
|
* NODE_OPTIONS="--import flare-redact/register" npm start
|
|
8
8
|
*
|
|
9
|
-
* FLARE_REDACT_PRESET picks a preset (logs, llm, gdpr, strict).
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* FLARE_REDACT_PRESET picks a preset (logs, llm, gdpr, strict).
|
|
10
|
+
*
|
|
11
|
+
* FLARE_REDACT_MODE picks what happens:
|
|
12
|
+
* enforce redact the output (the default)
|
|
13
|
+
* shadow leave the output as it is and count what would have been redacted
|
|
14
|
+
* audit redact the output and count what was redacted
|
|
15
|
+
* In shadow and audit mode the counts go to stderr when the process exits, and
|
|
16
|
+
* every FLARE_REDACT_REPORT_EVERY seconds when that is set. They never include
|
|
17
|
+
* a matched value.
|
|
18
|
+
*
|
|
19
|
+
* An unknown preset or mode stops the process at startup: a typo should fail
|
|
20
|
+
* the deploy, not leave the logs unredacted.
|
|
12
21
|
*/
|
|
13
22
|
import { compilePolicy, wrapConsole } from './index.js';
|
|
23
|
+
import { createAudit, shadowConsole } from './audit.js';
|
|
14
24
|
export const PRESET_ENV = 'FLARE_REDACT_PRESET';
|
|
25
|
+
export const MODE_ENV = 'FLARE_REDACT_MODE';
|
|
26
|
+
export const REPORT_EVERY_ENV = 'FLARE_REDACT_REPORT_EVERY';
|
|
15
27
|
/** The options the environment asks for. Throws on an unknown preset. */
|
|
16
28
|
export function registerOptions(env) {
|
|
17
29
|
const preset = env[PRESET_ENV]?.trim();
|
|
@@ -19,6 +31,41 @@ export function registerOptions(env) {
|
|
|
19
31
|
compilePolicy(opts);
|
|
20
32
|
return opts;
|
|
21
33
|
}
|
|
34
|
+
/** The mode the environment asks for. Throws on an unknown one. */
|
|
35
|
+
export function registerMode(env) {
|
|
36
|
+
const mode = (env[MODE_ENV]?.trim().toLowerCase() || 'enforce');
|
|
37
|
+
if (mode !== 'enforce' && mode !== 'shadow' && mode !== 'audit') {
|
|
38
|
+
throw new Error(`${MODE_ENV} must be enforce, shadow or audit, not ${JSON.stringify(env[MODE_ENV])}`);
|
|
39
|
+
}
|
|
40
|
+
return mode;
|
|
41
|
+
}
|
|
42
|
+
/** Seconds between reports, 0 for only at exit. Throws on anything but a whole number. */
|
|
43
|
+
export function reportEvery(env) {
|
|
44
|
+
const raw = env[REPORT_EVERY_ENV]?.trim();
|
|
45
|
+
if (!raw)
|
|
46
|
+
return 0;
|
|
47
|
+
if (!/^\d+$/.test(raw))
|
|
48
|
+
throw new Error(`${REPORT_EVERY_ENV} must be a number of seconds, not ${JSON.stringify(raw)}`);
|
|
49
|
+
return Number(raw);
|
|
50
|
+
}
|
|
22
51
|
const env = typeof process === 'undefined' ? {} : process.env;
|
|
23
|
-
|
|
52
|
+
const opts = registerOptions(env);
|
|
53
|
+
const mode = registerMode(env);
|
|
54
|
+
if (mode === 'enforce') {
|
|
55
|
+
wrapConsole(opts);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
const every = reportEvery(env);
|
|
59
|
+
const audit = createAudit(opts);
|
|
60
|
+
shadowConsole(audit, console, { enforce: mode === 'audit' });
|
|
61
|
+
const print = () => {
|
|
62
|
+
if (audit.report().scanned)
|
|
63
|
+
process.stderr.write(`[flare-redact ${mode}] ${audit.format()}\n`);
|
|
64
|
+
};
|
|
65
|
+
if (typeof process !== 'undefined' && typeof process.on === 'function') {
|
|
66
|
+
process.on('exit', print);
|
|
67
|
+
if (every > 0)
|
|
68
|
+
setInterval(print, every * 1000).unref?.();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
24
71
|
//# sourceMappingURL=register.js.map
|
package/dist/register.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"register.js","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"register.js","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,aAAa,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAExD,MAAM,CAAC,MAAM,UAAU,GAAG,qBAAqB,CAAC;AAChD,MAAM,CAAC,MAAM,QAAQ,GAAG,mBAAmB,CAAC;AAC5C,MAAM,CAAC,MAAM,gBAAgB,GAAG,2BAA2B,CAAC;AAM5D,yEAAyE;AACzE,MAAM,UAAU,eAAe,CAAC,GAAQ;IACtC,MAAM,MAAM,GAAG,GAAG,CAAC,UAAU,CAAC,EAA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,CAAC,CAAC;AAE/B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;IACvB,WAAW,CAAC,IAAI,CAAC,CAAC;AACpB,CAAC;KAAM,CAAC;IACN,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;IAC/B,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAChC,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,KAAK,OAAO,EAAE,CAAC,CAAC;IAC7D,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC,OAAO;YAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,iBAAiB,IAAI,KAAK,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACjG,CAAC,CAAC;IACF,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,OAAO,CAAC,EAAE,KAAK,UAAU,EAAE,CAAC;QACvE,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAC1B,IAAI,KAAK,GAAG,CAAC;YAAE,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC;IAC5D,CAAC;AACH,CAAC"}
|
package/dist/replay.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Masking for session replay: rrweb, and the tools built on it (Sentry Session
|
|
3
|
+
* Replay, PostHog, Highlight, OpenReplay-style recorders).
|
|
4
|
+
*
|
|
5
|
+
* A replay tool either masks every character, which makes replays hard to use,
|
|
6
|
+
* or masks only what a CSS selector names, which misses the email typed into a
|
|
7
|
+
* search box and the API key pasted into a support form. These functions mask
|
|
8
|
+
* what is sensitive and keep the rest readable. Each masked character becomes
|
|
9
|
+
* `*` by default, as rrweb's own masking does, so the replayed layout does not
|
|
10
|
+
* shift.
|
|
11
|
+
*
|
|
12
|
+
* Browser-safe: no Node built-ins.
|
|
13
|
+
*/
|
|
14
|
+
import { type RedactOptions } from './index.js';
|
|
15
|
+
export interface ReplayMaskOptions extends RedactOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Replace each masked character with `*`, keeping spaces, so text keeps its
|
|
18
|
+
* length and the replayed layout does not shift. Default `true`; set `false`
|
|
19
|
+
* to use the usual masks (`a***@***`) or the chosen `mode`.
|
|
20
|
+
*/
|
|
21
|
+
preserveLength?: boolean;
|
|
22
|
+
/** Distinct texts remembered between calls, since a page repeats them. Default 2000. */
|
|
23
|
+
cacheSize?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A mask function `(text, element?) => string` for rrweb's `maskTextFn` and
|
|
27
|
+
* `maskInputFn`, or Sentry's `maskFn`. Text that holds nothing sensitive is
|
|
28
|
+
* returned as it is.
|
|
29
|
+
*
|
|
30
|
+
* A form field is masked whole when it is a password field, when its
|
|
31
|
+
* `autocomplete` names a password, one-time code or card, or when its `name`
|
|
32
|
+
* or `id` is a credential name such as `password` or `api_key`; anything else
|
|
33
|
+
* typed into it is masked where a detector finds something.
|
|
34
|
+
*
|
|
35
|
+
* Without detector options the `pii` group is on, so phone numbers count too.
|
|
36
|
+
*/
|
|
37
|
+
export declare function replayMaskFn(opts?: ReplayMaskOptions): (text: string, element?: unknown) => string;
|
|
38
|
+
/**
|
|
39
|
+
* Options to spread into rrweb's `record()` (or PostHog's `session_recording`):
|
|
40
|
+
* every text node and every input goes through {@link replayMaskFn}, which
|
|
41
|
+
* keeps clean text readable.
|
|
42
|
+
*
|
|
43
|
+
* ```js
|
|
44
|
+
* rrweb.record({ emit, ...rrwebMasking() });
|
|
45
|
+
* posthog.init(key, { session_recording: rrwebMasking() });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function rrwebMasking(opts?: ReplayMaskOptions): {
|
|
49
|
+
maskAllInputs: boolean;
|
|
50
|
+
maskTextSelector: string;
|
|
51
|
+
maskTextFn: (text: string, element?: unknown) => string;
|
|
52
|
+
maskInputFn: (text: string, element?: unknown) => string;
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=replay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAE/D,MAAM,WAAW,iBAAkB,SAAQ,aAAa;IACtD;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,wFAAwF;IACxF,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAkBD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,IAAI,GAAE,iBAAsB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,MAAM,CA+BtG;AAED;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,IAAI,GAAE,iBAAsB;;;uBAKlC,MAAM,YAAY,OAAO;wBACxB,MAAM,YAAY,OAAO;EAEhD"}
|
package/dist/replay.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Masking for session replay: rrweb, and the tools built on it (Sentry Session
|
|
3
|
+
* Replay, PostHog, Highlight, OpenReplay-style recorders).
|
|
4
|
+
*
|
|
5
|
+
* A replay tool either masks every character, which makes replays hard to use,
|
|
6
|
+
* or masks only what a CSS selector names, which misses the email typed into a
|
|
7
|
+
* search box and the API key pasted into a support form. These functions mask
|
|
8
|
+
* what is sensitive and keep the rest readable. Each masked character becomes
|
|
9
|
+
* `*` by default, as rrweb's own masking does, so the replayed layout does not
|
|
10
|
+
* shift.
|
|
11
|
+
*
|
|
12
|
+
* Browser-safe: no Node built-ins.
|
|
13
|
+
*/
|
|
14
|
+
import { compilePolicy } from './index.js';
|
|
15
|
+
/** `autocomplete` tokens of fields that hold a credential or a card in full. */
|
|
16
|
+
const CREDENTIAL_AUTOCOMPLETE = /(?:^|\s)(?:current-password|new-password|one-time-code|cc-number|cc-csc|cc-exp|cc-exp-month|cc-exp-year)(?:\s|$)/i;
|
|
17
|
+
const FORM_FIELD = /^(?:input|textarea|select)$/i;
|
|
18
|
+
const hide = (value) => value.replace(/\S/g, '*');
|
|
19
|
+
/**
|
|
20
|
+
* A mask function `(text, element?) => string` for rrweb's `maskTextFn` and
|
|
21
|
+
* `maskInputFn`, or Sentry's `maskFn`. Text that holds nothing sensitive is
|
|
22
|
+
* returned as it is.
|
|
23
|
+
*
|
|
24
|
+
* A form field is masked whole when it is a password field, when its
|
|
25
|
+
* `autocomplete` names a password, one-time code or card, or when its `name`
|
|
26
|
+
* or `id` is a credential name such as `password` or `api_key`; anything else
|
|
27
|
+
* typed into it is masked where a detector finds something.
|
|
28
|
+
*
|
|
29
|
+
* Without detector options the `pii` group is on, so phone numbers count too.
|
|
30
|
+
*/
|
|
31
|
+
export function replayMaskFn(opts = {}) {
|
|
32
|
+
const { preserveLength = true, cacheSize = 2000, ...rest } = opts;
|
|
33
|
+
const chooses = rest.only !== undefined || rest.enable !== undefined || rest.preset !== undefined;
|
|
34
|
+
const options = { ...(chooses ? {} : { enable: ['pii'] }), ...rest };
|
|
35
|
+
if (preserveLength && options.mask === undefined && (options.mode ?? 'mask') === 'mask') {
|
|
36
|
+
options.mask = ({ value }) => hide(value);
|
|
37
|
+
}
|
|
38
|
+
const policy = compilePolicy(options);
|
|
39
|
+
const whole = (value) => (preserveLength ? hide(value) : '***');
|
|
40
|
+
const cache = new Map();
|
|
41
|
+
const text = (value) => {
|
|
42
|
+
const known = cache.get(value);
|
|
43
|
+
if (known !== undefined)
|
|
44
|
+
return known;
|
|
45
|
+
const out = policy.redact(value);
|
|
46
|
+
if (cache.size >= cacheSize)
|
|
47
|
+
cache.delete(cache.keys().next().value);
|
|
48
|
+
cache.set(value, out);
|
|
49
|
+
return out;
|
|
50
|
+
};
|
|
51
|
+
return (value, element) => {
|
|
52
|
+
if (typeof value !== 'string' || value === '')
|
|
53
|
+
return value;
|
|
54
|
+
const el = element;
|
|
55
|
+
if (!el || typeof el.tagName !== 'string' || !FORM_FIELD.test(el.tagName))
|
|
56
|
+
return text(value);
|
|
57
|
+
const attr = (name) => el.getAttribute?.(name) ?? '';
|
|
58
|
+
if ((el.type ?? attr('type')).toLowerCase() === 'password')
|
|
59
|
+
return whole(value);
|
|
60
|
+
if (CREDENTIAL_AUTOCOMPLETE.test(attr('autocomplete')))
|
|
61
|
+
return whole(value);
|
|
62
|
+
const field = attr('name') || el.id || '';
|
|
63
|
+
if (field)
|
|
64
|
+
return policy.redact({ [field]: value })[field];
|
|
65
|
+
return text(value);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Options to spread into rrweb's `record()` (or PostHog's `session_recording`):
|
|
70
|
+
* every text node and every input goes through {@link replayMaskFn}, which
|
|
71
|
+
* keeps clean text readable.
|
|
72
|
+
*
|
|
73
|
+
* ```js
|
|
74
|
+
* rrweb.record({ emit, ...rrwebMasking() });
|
|
75
|
+
* posthog.init(key, { session_recording: rrwebMasking() });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export function rrwebMasking(opts = {}) {
|
|
79
|
+
const mask = replayMaskFn(opts);
|
|
80
|
+
return {
|
|
81
|
+
maskAllInputs: true,
|
|
82
|
+
maskTextSelector: '*',
|
|
83
|
+
maskTextFn: (text, element) => mask(text, element),
|
|
84
|
+
maskInputFn: (text, element) => mask(text, element),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=replay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.js","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,aAAa,EAAsB,MAAM,YAAY,CAAC;AAqB/D,gFAAg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O,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,CAAW,CAAC;QACrE,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,OAA0B,EAAE;IACvD,MAAM,IAAI,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IAChC,OAAO;QACL,aAAa,EAAE,IAAI;QACnB,gBAAgB,EAAE,GAAG;QACrB,UAAU,EAAE,CAAC,IAAY,EAAE,OAAiB,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC;QACpE,WAAW,EAAE,CAAC,IAAY,EAAE,OAAiB,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC;KACtE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A snapshot serializer for Vitest and Jest: secrets and personal data are
|
|
3
|
+
* masked before a snapshot is written, so a token that reached a rendered
|
|
4
|
+
* component, an error message or a recorded response is never committed.
|
|
5
|
+
*
|
|
6
|
+
* Vitest: `snapshotSerializers: ['flare-redact/snapshot']` in the config, or
|
|
7
|
+
* `expect.addSnapshotSerializer(createSnapshotSerializer(options))` in a test.
|
|
8
|
+
*/
|
|
9
|
+
import { type RedactOptions } from './index.js';
|
|
10
|
+
type Printer = (value: unknown, ...rest: unknown[]) => string;
|
|
11
|
+
export interface SnapshotSerializer {
|
|
12
|
+
test(value: unknown): boolean;
|
|
13
|
+
serialize(value: unknown, config: unknown, indentation: string, depth: number, refs: unknown, printer: Printer): string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A serializer that takes over only values that need masking, and hands the
|
|
17
|
+
* masked copy back to the snapshot printer, so the usual formatting is kept
|
|
18
|
+
* and clean values are untouched.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createSnapshotSerializer(opts?: RedactOptions): SnapshotSerializer;
|
|
21
|
+
declare const serializer: SnapshotSerializer;
|
|
22
|
+
export declare const test: (value: unknown) => boolean;
|
|
23
|
+
export declare const serialize: (value: unknown, config: unknown, indentation: string, depth: number, refs: unknown, printer: Printer) => string;
|
|
24
|
+
export default serializer;
|
|
25
|
+
//# sourceMappingURL=snapshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAE/D,KAAK,OAAO,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,MAAM,CAAC;AAE9D,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;IAC9B,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC;CACzH;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,GAAE,aAAkB,GAAG,kBAAkB,CAuBrF;AAED,QAAA,MAAM,UAAU,oBAA6B,CAAC;AAE9C,eAAO,MAAM,IAAI,UApCH,OAAO,KAAG,OAoCW,CAAC;AACpC,eAAO,MAAM,SAAS,UApCH,OAAO,UAAU,OAAO,eAAe,MAAM,SAAS,MAAM,QAAQ,OAAO,WAAW,OAAO,KAAG,MAoCtE,CAAC;AAC9C,eAAe,UAAU,CAAC"}
|
package/dist/snapshot.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A snapshot serializer for Vitest and Jest: secrets and personal data are
|
|
3
|
+
* masked before a snapshot is written, so a token that reached a rendered
|
|
4
|
+
* component, an error message or a recorded response is never committed.
|
|
5
|
+
*
|
|
6
|
+
* Vitest: `snapshotSerializers: ['flare-redact/snapshot']` in the config, or
|
|
7
|
+
* `expect.addSnapshotSerializer(createSnapshotSerializer(options))` in a test.
|
|
8
|
+
*/
|
|
9
|
+
import { compilePolicy } from './index.js';
|
|
10
|
+
/**
|
|
11
|
+
* A serializer that takes over only values that need masking, and hands the
|
|
12
|
+
* masked copy back to the snapshot printer, so the usual formatting is kept
|
|
13
|
+
* and clean values are untouched.
|
|
14
|
+
*/
|
|
15
|
+
export function createSnapshotSerializer(opts = {}) {
|
|
16
|
+
const policy = compilePolicy(opts);
|
|
17
|
+
// Masked output goes back through the printer, which asks `test` again; a
|
|
18
|
+
// mask that still looks like a finding must not be masked in a loop.
|
|
19
|
+
const masked = new WeakSet();
|
|
20
|
+
const maskedText = new Set();
|
|
21
|
+
return {
|
|
22
|
+
test: (value) => {
|
|
23
|
+
if (typeof value === 'string')
|
|
24
|
+
return !maskedText.has(value) && !policy.isClean(value);
|
|
25
|
+
if (typeof value !== 'object' || value === null || masked.has(value))
|
|
26
|
+
return false;
|
|
27
|
+
return !policy.isClean(value);
|
|
28
|
+
},
|
|
29
|
+
serialize: (value, config, indentation, depth, refs, printer) => {
|
|
30
|
+
const out = policy.redact(value);
|
|
31
|
+
if (typeof out === 'string')
|
|
32
|
+
maskedText.add(out);
|
|
33
|
+
else if (typeof out === 'object' && out !== null)
|
|
34
|
+
masked.add(out);
|
|
35
|
+
try {
|
|
36
|
+
return printer(out, config, indentation, depth, refs);
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
if (typeof out === 'string')
|
|
40
|
+
maskedText.delete(out);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const serializer = createSnapshotSerializer();
|
|
46
|
+
export const test = serializer.test;
|
|
47
|
+
export const serialize = serializer.serialize;
|
|
48
|
+
export default serializer;
|
|
49
|
+
//# sourceMappingURL=snapshot.js.map
|