@logtape/redaction 1.2.2 → 1.2.3
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/package.json +5 -2
- package/deno.json +0 -34
- package/src/field.test.ts +0 -548
- package/src/field.ts +0 -428
- package/src/mod.ts +0 -8
- package/src/pattern.test.ts +0 -351
- package/src/pattern.ts +0 -209
- package/tsdown.config.ts +0 -11
package/src/field.ts
DELETED
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
import type { LogRecord, Sink } from "@logtape/logtape";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The type for a field pattern used in redaction. A string or a regular
|
|
5
|
-
* expression that matches field names.
|
|
6
|
-
* @since 0.10.0
|
|
7
|
-
*/
|
|
8
|
-
export type FieldPattern = string | RegExp;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* An array of field patterns used for redaction. Each pattern can be
|
|
12
|
-
* a string or a regular expression that matches field names.
|
|
13
|
-
* @since 0.10.0
|
|
14
|
-
*/
|
|
15
|
-
export type FieldPatterns = FieldPattern[];
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Default field patterns for redaction. These patterns will match
|
|
19
|
-
* common sensitive fields such as passwords, tokens, and personal
|
|
20
|
-
* information.
|
|
21
|
-
* @since 0.10.0
|
|
22
|
-
*/
|
|
23
|
-
export const DEFAULT_REDACT_FIELDS: FieldPatterns = [
|
|
24
|
-
/pass(?:code|phrase|word)/i,
|
|
25
|
-
/secret/i,
|
|
26
|
-
/token/i,
|
|
27
|
-
/key/i,
|
|
28
|
-
/credential/i,
|
|
29
|
-
/auth/i,
|
|
30
|
-
/signature/i,
|
|
31
|
-
/sensitive/i,
|
|
32
|
-
/private/i,
|
|
33
|
-
/ssn/i,
|
|
34
|
-
/email/i,
|
|
35
|
-
/phone/i,
|
|
36
|
-
/address/i,
|
|
37
|
-
];
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Options for redacting fields in a {@link LogRecord}. Used by
|
|
41
|
-
* the {@link redactByField} function.
|
|
42
|
-
* @since 0.10.0
|
|
43
|
-
*/
|
|
44
|
-
export interface FieldRedactionOptions {
|
|
45
|
-
/**
|
|
46
|
-
* The field patterns to match against. This can be an array of
|
|
47
|
-
* strings or regular expressions. If a field matches any of the
|
|
48
|
-
* patterns, it will be redacted.
|
|
49
|
-
* @defaultValue {@link DEFAULT_REDACT_FIELDS}
|
|
50
|
-
*/
|
|
51
|
-
readonly fieldPatterns: FieldPatterns;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* The action to perform on the matched fields. If not provided,
|
|
55
|
-
* the default action is to delete the field from the properties.
|
|
56
|
-
* If a function is provided, it will be called with the
|
|
57
|
-
* value of the field, and the return value will be used to replace
|
|
58
|
-
* the field in the properties.
|
|
59
|
-
* If the action is `"delete"`, the field will be removed from the
|
|
60
|
-
* properties.
|
|
61
|
-
* @default `"delete"`
|
|
62
|
-
*/
|
|
63
|
-
readonly action?: "delete" | ((value: unknown) => unknown);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Redacts properties and message values in a {@link LogRecord} based on the
|
|
68
|
-
* provided field patterns and action.
|
|
69
|
-
*
|
|
70
|
-
* Note that it is a decorator which wraps the sink and redacts properties
|
|
71
|
-
* and message values before passing them to the sink.
|
|
72
|
-
*
|
|
73
|
-
* For string templates (e.g., `"Hello, {name}!"`), placeholder names are
|
|
74
|
-
* matched against the field patterns to determine which values to redact.
|
|
75
|
-
*
|
|
76
|
-
* For tagged template literals (e.g., `` `Hello, ${name}!` ``), redaction
|
|
77
|
-
* is performed by comparing message values with redacted property values.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* import { getConsoleSink } from "@logtape/logtape";
|
|
82
|
-
* import { redactByField } from "@logtape/redaction";
|
|
83
|
-
*
|
|
84
|
-
* const sink = redactByField(getConsoleSink());
|
|
85
|
-
* ```
|
|
86
|
-
*
|
|
87
|
-
* @param sink The sink to wrap.
|
|
88
|
-
* @param options The redaction options.
|
|
89
|
-
* @returns The wrapped sink.
|
|
90
|
-
* @since 0.10.0
|
|
91
|
-
*/
|
|
92
|
-
export function redactByField(
|
|
93
|
-
sink: Sink | Sink & Disposable | Sink & AsyncDisposable,
|
|
94
|
-
options: FieldRedactionOptions | FieldPatterns = DEFAULT_REDACT_FIELDS,
|
|
95
|
-
): Sink | Sink & Disposable | Sink & AsyncDisposable {
|
|
96
|
-
const opts = Array.isArray(options) ? { fieldPatterns: options } : options;
|
|
97
|
-
const wrapped = (record: LogRecord) => {
|
|
98
|
-
const redactedProperties = redactProperties(record.properties, opts);
|
|
99
|
-
let redactedMessage = record.message;
|
|
100
|
-
|
|
101
|
-
if (typeof record.rawMessage === "string") {
|
|
102
|
-
// String template: redact by placeholder names
|
|
103
|
-
const placeholders = extractPlaceholderNames(record.rawMessage);
|
|
104
|
-
const { redactedIndices, wildcardIndices } =
|
|
105
|
-
getRedactedPlaceholderIndices(
|
|
106
|
-
placeholders,
|
|
107
|
-
opts.fieldPatterns,
|
|
108
|
-
);
|
|
109
|
-
if (redactedIndices.size > 0 || wildcardIndices.size > 0) {
|
|
110
|
-
redactedMessage = redactMessageArray(
|
|
111
|
-
record.message,
|
|
112
|
-
redactedIndices,
|
|
113
|
-
wildcardIndices,
|
|
114
|
-
redactedProperties,
|
|
115
|
-
opts.action,
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
} else {
|
|
119
|
-
// Tagged template: redact by comparing values
|
|
120
|
-
const redactedValues = getRedactedValues(
|
|
121
|
-
record.properties,
|
|
122
|
-
redactedProperties,
|
|
123
|
-
);
|
|
124
|
-
if (redactedValues.size > 0) {
|
|
125
|
-
redactedMessage = redactMessageByValues(record.message, redactedValues);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
sink({
|
|
130
|
-
...record,
|
|
131
|
-
message: redactedMessage,
|
|
132
|
-
properties: redactedProperties,
|
|
133
|
-
});
|
|
134
|
-
};
|
|
135
|
-
if (Symbol.dispose in sink) wrapped[Symbol.dispose] = sink[Symbol.dispose];
|
|
136
|
-
if (Symbol.asyncDispose in sink) {
|
|
137
|
-
wrapped[Symbol.asyncDispose] = sink[Symbol.asyncDispose];
|
|
138
|
-
}
|
|
139
|
-
return wrapped;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Redacts properties from an object based on specified field patterns.
|
|
144
|
-
*
|
|
145
|
-
* This function creates a shallow copy of the input object and applies
|
|
146
|
-
* redaction rules to its properties. For properties that match the redaction
|
|
147
|
-
* patterns, the function either removes them or transforms their values based
|
|
148
|
-
* on the provided action.
|
|
149
|
-
*
|
|
150
|
-
* The redaction process is recursive and will be applied to nested objects
|
|
151
|
-
* as well, allowing for deep redaction of sensitive data in complex object
|
|
152
|
-
* structures.
|
|
153
|
-
* @param properties The properties to redact.
|
|
154
|
-
* @param options The redaction options.
|
|
155
|
-
* @returns The redacted properties.
|
|
156
|
-
* @since 0.10.0
|
|
157
|
-
*/
|
|
158
|
-
export function redactProperties(
|
|
159
|
-
properties: Record<string, unknown>,
|
|
160
|
-
options: FieldRedactionOptions,
|
|
161
|
-
): Record<string, unknown> {
|
|
162
|
-
const copy = { ...properties };
|
|
163
|
-
for (const field in copy) {
|
|
164
|
-
if (shouldFieldRedacted(field, options.fieldPatterns)) {
|
|
165
|
-
if (options.action == null || options.action === "delete") {
|
|
166
|
-
delete copy[field];
|
|
167
|
-
} else {
|
|
168
|
-
copy[field] = options.action(copy[field]);
|
|
169
|
-
}
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
const value = copy[field];
|
|
173
|
-
// Check if value is an array:
|
|
174
|
-
if (Array.isArray(value)) {
|
|
175
|
-
copy[field] = value.map((item) => {
|
|
176
|
-
if (
|
|
177
|
-
typeof item === "object" && item !== null &&
|
|
178
|
-
(Object.getPrototypeOf(item) === Object.prototype ||
|
|
179
|
-
Object.getPrototypeOf(item) === null)
|
|
180
|
-
) {
|
|
181
|
-
// @ts-ignore: item is always Record<string, unknown>
|
|
182
|
-
return redactProperties(item, options);
|
|
183
|
-
}
|
|
184
|
-
return item;
|
|
185
|
-
});
|
|
186
|
-
// Check if value is a vanilla object:
|
|
187
|
-
} else if (
|
|
188
|
-
typeof value === "object" && value !== null &&
|
|
189
|
-
(Object.getPrototypeOf(value) === Object.prototype ||
|
|
190
|
-
Object.getPrototypeOf(value) === null)
|
|
191
|
-
) {
|
|
192
|
-
// @ts-ignore: value is always Record<string, unknown>
|
|
193
|
-
copy[field] = redactProperties(value, options);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return copy;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Checks if a field should be redacted based on the provided field patterns.
|
|
201
|
-
* @param field The field name to check.
|
|
202
|
-
* @param fieldPatterns The field patterns to match against.
|
|
203
|
-
* @returns `true` if the field should be redacted, `false` otherwise.
|
|
204
|
-
* @since 0.10.0
|
|
205
|
-
*/
|
|
206
|
-
export function shouldFieldRedacted(
|
|
207
|
-
field: string,
|
|
208
|
-
fieldPatterns: FieldPatterns,
|
|
209
|
-
): boolean {
|
|
210
|
-
for (const fieldPattern of fieldPatterns) {
|
|
211
|
-
if (typeof fieldPattern === "string") {
|
|
212
|
-
if (fieldPattern === field) return true;
|
|
213
|
-
} else {
|
|
214
|
-
if (fieldPattern.test(field)) return true;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return false;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Extracts placeholder names from a message template string in order.
|
|
222
|
-
* @param template The message template string.
|
|
223
|
-
* @returns An array of placeholder names in the order they appear.
|
|
224
|
-
*/
|
|
225
|
-
function extractPlaceholderNames(template: string): string[] {
|
|
226
|
-
const placeholders: string[] = [];
|
|
227
|
-
for (let i = 0; i < template.length; i++) {
|
|
228
|
-
if (template[i] === "{") {
|
|
229
|
-
// Check for escaped brace
|
|
230
|
-
if (i + 1 < template.length && template[i + 1] === "{") {
|
|
231
|
-
i++;
|
|
232
|
-
continue;
|
|
233
|
-
}
|
|
234
|
-
const closeIndex = template.indexOf("}", i + 1);
|
|
235
|
-
if (closeIndex === -1) continue;
|
|
236
|
-
const key = template.slice(i + 1, closeIndex).trim();
|
|
237
|
-
placeholders.push(key);
|
|
238
|
-
i = closeIndex;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
return placeholders;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Parses a property path into its segments.
|
|
246
|
-
* @param path The property path (e.g., "user.password" or "users[0].email").
|
|
247
|
-
* @returns An array of path segments.
|
|
248
|
-
*/
|
|
249
|
-
function parsePathSegments(path: string): string[] {
|
|
250
|
-
const segments: string[] = [];
|
|
251
|
-
let current = "";
|
|
252
|
-
for (const char of path) {
|
|
253
|
-
if (char === "." || char === "[") {
|
|
254
|
-
if (current) segments.push(current);
|
|
255
|
-
current = "";
|
|
256
|
-
} else if (char === "]" || char === "?") {
|
|
257
|
-
// Skip these characters
|
|
258
|
-
} else {
|
|
259
|
-
current += char;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
if (current) segments.push(current);
|
|
263
|
-
return segments;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Determines which placeholder indices should be redacted based on field
|
|
268
|
-
* patterns, and which are wildcard placeholders.
|
|
269
|
-
* @param placeholders Array of placeholder names from the template.
|
|
270
|
-
* @param fieldPatterns Field patterns to match against.
|
|
271
|
-
* @returns Object with redactedIndices and wildcardIndices.
|
|
272
|
-
*/
|
|
273
|
-
function getRedactedPlaceholderIndices(
|
|
274
|
-
placeholders: string[],
|
|
275
|
-
fieldPatterns: FieldPatterns,
|
|
276
|
-
): { redactedIndices: Set<number>; wildcardIndices: Set<number> } {
|
|
277
|
-
const redactedIndices = new Set<number>();
|
|
278
|
-
const wildcardIndices = new Set<number>();
|
|
279
|
-
|
|
280
|
-
for (let i = 0; i < placeholders.length; i++) {
|
|
281
|
-
const placeholder = placeholders[i];
|
|
282
|
-
|
|
283
|
-
// Track wildcard {*} separately
|
|
284
|
-
if (placeholder === "*") {
|
|
285
|
-
wildcardIndices.add(i);
|
|
286
|
-
continue;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// Check the full placeholder name
|
|
290
|
-
if (shouldFieldRedacted(placeholder, fieldPatterns)) {
|
|
291
|
-
redactedIndices.add(i);
|
|
292
|
-
continue;
|
|
293
|
-
}
|
|
294
|
-
// For nested paths, check each segment
|
|
295
|
-
const segments = parsePathSegments(placeholder);
|
|
296
|
-
for (const segment of segments) {
|
|
297
|
-
if (shouldFieldRedacted(segment, fieldPatterns)) {
|
|
298
|
-
redactedIndices.add(i);
|
|
299
|
-
break;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
return { redactedIndices, wildcardIndices };
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Redacts values in the message array based on the redacted placeholder
|
|
308
|
-
* indices and wildcard indices.
|
|
309
|
-
* @param message The original message array.
|
|
310
|
-
* @param redactedIndices Set of placeholder indices to redact.
|
|
311
|
-
* @param wildcardIndices Set of wildcard placeholder indices.
|
|
312
|
-
* @param redactedProperties The redacted properties object.
|
|
313
|
-
* @param action The redaction action.
|
|
314
|
-
* @returns New message array with redacted values.
|
|
315
|
-
*/
|
|
316
|
-
function redactMessageArray(
|
|
317
|
-
message: readonly unknown[],
|
|
318
|
-
redactedIndices: Set<number>,
|
|
319
|
-
wildcardIndices: Set<number>,
|
|
320
|
-
redactedProperties: Record<string, unknown>,
|
|
321
|
-
action: "delete" | ((value: unknown) => unknown) | undefined,
|
|
322
|
-
): readonly unknown[] {
|
|
323
|
-
if (redactedIndices.size === 0 && wildcardIndices.size === 0) return message;
|
|
324
|
-
|
|
325
|
-
const result: unknown[] = [];
|
|
326
|
-
let placeholderIndex = 0;
|
|
327
|
-
|
|
328
|
-
for (let i = 0; i < message.length; i++) {
|
|
329
|
-
if (i % 2 === 0) {
|
|
330
|
-
// Even index: text segment
|
|
331
|
-
result.push(message[i]);
|
|
332
|
-
} else {
|
|
333
|
-
// Odd index: value/placeholder
|
|
334
|
-
if (wildcardIndices.has(placeholderIndex)) {
|
|
335
|
-
// Wildcard {*}: replace with redacted properties
|
|
336
|
-
result.push(redactedProperties);
|
|
337
|
-
} else if (redactedIndices.has(placeholderIndex)) {
|
|
338
|
-
if (action == null || action === "delete") {
|
|
339
|
-
result.push("");
|
|
340
|
-
} else {
|
|
341
|
-
result.push(action(message[i]));
|
|
342
|
-
}
|
|
343
|
-
} else {
|
|
344
|
-
result.push(message[i]);
|
|
345
|
-
}
|
|
346
|
-
placeholderIndex++;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
return result;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Collects redacted value mappings from original to redacted properties.
|
|
354
|
-
* @param original The original properties.
|
|
355
|
-
* @param redacted The redacted properties.
|
|
356
|
-
* @param map The map to populate with original -> redacted value pairs.
|
|
357
|
-
*/
|
|
358
|
-
function collectRedactedValues(
|
|
359
|
-
original: Record<string, unknown>,
|
|
360
|
-
redacted: Record<string, unknown>,
|
|
361
|
-
map: Map<unknown, unknown>,
|
|
362
|
-
): void {
|
|
363
|
-
for (const key in original) {
|
|
364
|
-
const origVal = original[key];
|
|
365
|
-
const redVal = redacted[key];
|
|
366
|
-
|
|
367
|
-
if (origVal !== redVal) {
|
|
368
|
-
map.set(origVal, redVal);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// Recurse into nested objects
|
|
372
|
-
if (
|
|
373
|
-
typeof origVal === "object" && origVal !== null &&
|
|
374
|
-
typeof redVal === "object" && redVal !== null &&
|
|
375
|
-
!Array.isArray(origVal)
|
|
376
|
-
) {
|
|
377
|
-
collectRedactedValues(
|
|
378
|
-
origVal as Record<string, unknown>,
|
|
379
|
-
redVal as Record<string, unknown>,
|
|
380
|
-
map,
|
|
381
|
-
);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Gets a map of original values to their redacted replacements.
|
|
388
|
-
* @param original The original properties.
|
|
389
|
-
* @param redacted The redacted properties.
|
|
390
|
-
* @returns A map of original -> redacted values.
|
|
391
|
-
*/
|
|
392
|
-
function getRedactedValues(
|
|
393
|
-
original: Record<string, unknown>,
|
|
394
|
-
redacted: Record<string, unknown>,
|
|
395
|
-
): Map<unknown, unknown> {
|
|
396
|
-
const map = new Map<unknown, unknown>();
|
|
397
|
-
collectRedactedValues(original, redacted, map);
|
|
398
|
-
return map;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Redacts message array values by comparing with redacted property values.
|
|
403
|
-
* Used for tagged template literals where placeholder names are not available.
|
|
404
|
-
* @param message The original message array.
|
|
405
|
-
* @param redactedValues Map of original -> redacted values.
|
|
406
|
-
* @returns New message array with redacted values.
|
|
407
|
-
*/
|
|
408
|
-
function redactMessageByValues(
|
|
409
|
-
message: readonly unknown[],
|
|
410
|
-
redactedValues: Map<unknown, unknown>,
|
|
411
|
-
): readonly unknown[] {
|
|
412
|
-
if (redactedValues.size === 0) return message;
|
|
413
|
-
|
|
414
|
-
const result: unknown[] = [];
|
|
415
|
-
for (let i = 0; i < message.length; i++) {
|
|
416
|
-
if (i % 2 === 0) {
|
|
417
|
-
result.push(message[i]);
|
|
418
|
-
} else {
|
|
419
|
-
const val = message[i];
|
|
420
|
-
if (redactedValues.has(val)) {
|
|
421
|
-
result.push(redactedValues.get(val));
|
|
422
|
-
} else {
|
|
423
|
-
result.push(val);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
return result;
|
|
428
|
-
}
|