@logtape/redaction 1.2.1 → 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/src/field.ts DELETED
@@ -1,412 +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 = getRedactedPlaceholderIndices(
105
- placeholders,
106
- opts.fieldPatterns,
107
- );
108
- if (redactedIndices.size > 0) {
109
- redactedMessage = redactMessageArray(
110
- record.message,
111
- redactedIndices,
112
- opts.action,
113
- );
114
- }
115
- } else {
116
- // Tagged template: redact by comparing values
117
- const redactedValues = getRedactedValues(
118
- record.properties,
119
- redactedProperties,
120
- );
121
- if (redactedValues.size > 0) {
122
- redactedMessage = redactMessageByValues(record.message, redactedValues);
123
- }
124
- }
125
-
126
- sink({
127
- ...record,
128
- message: redactedMessage,
129
- properties: redactedProperties,
130
- });
131
- };
132
- if (Symbol.dispose in sink) wrapped[Symbol.dispose] = sink[Symbol.dispose];
133
- if (Symbol.asyncDispose in sink) {
134
- wrapped[Symbol.asyncDispose] = sink[Symbol.asyncDispose];
135
- }
136
- return wrapped;
137
- }
138
-
139
- /**
140
- * Redacts properties from an object based on specified field patterns.
141
- *
142
- * This function creates a shallow copy of the input object and applies
143
- * redaction rules to its properties. For properties that match the redaction
144
- * patterns, the function either removes them or transforms their values based
145
- * on the provided action.
146
- *
147
- * The redaction process is recursive and will be applied to nested objects
148
- * as well, allowing for deep redaction of sensitive data in complex object
149
- * structures.
150
- * @param properties The properties to redact.
151
- * @param options The redaction options.
152
- * @returns The redacted properties.
153
- * @since 0.10.0
154
- */
155
- export function redactProperties(
156
- properties: Record<string, unknown>,
157
- options: FieldRedactionOptions,
158
- ): Record<string, unknown> {
159
- const copy = { ...properties };
160
- for (const field in copy) {
161
- if (shouldFieldRedacted(field, options.fieldPatterns)) {
162
- if (options.action == null || options.action === "delete") {
163
- delete copy[field];
164
- } else {
165
- copy[field] = options.action(copy[field]);
166
- }
167
- continue;
168
- }
169
- const value = copy[field];
170
- // Check if value is an array:
171
- if (Array.isArray(value)) {
172
- copy[field] = value.map((item) => {
173
- if (
174
- typeof item === "object" && item !== null &&
175
- (Object.getPrototypeOf(item) === Object.prototype ||
176
- Object.getPrototypeOf(item) === null)
177
- ) {
178
- // @ts-ignore: item is always Record<string, unknown>
179
- return redactProperties(item, options);
180
- }
181
- return item;
182
- });
183
- // Check if value is a vanilla object:
184
- } else if (
185
- typeof value === "object" && value !== null &&
186
- (Object.getPrototypeOf(value) === Object.prototype ||
187
- Object.getPrototypeOf(value) === null)
188
- ) {
189
- // @ts-ignore: value is always Record<string, unknown>
190
- copy[field] = redactProperties(value, options);
191
- }
192
- }
193
- return copy;
194
- }
195
-
196
- /**
197
- * Checks if a field should be redacted based on the provided field patterns.
198
- * @param field The field name to check.
199
- * @param fieldPatterns The field patterns to match against.
200
- * @returns `true` if the field should be redacted, `false` otherwise.
201
- * @since 0.10.0
202
- */
203
- export function shouldFieldRedacted(
204
- field: string,
205
- fieldPatterns: FieldPatterns,
206
- ): boolean {
207
- for (const fieldPattern of fieldPatterns) {
208
- if (typeof fieldPattern === "string") {
209
- if (fieldPattern === field) return true;
210
- } else {
211
- if (fieldPattern.test(field)) return true;
212
- }
213
- }
214
- return false;
215
- }
216
-
217
- /**
218
- * Extracts placeholder names from a message template string in order.
219
- * @param template The message template string.
220
- * @returns An array of placeholder names in the order they appear.
221
- */
222
- function extractPlaceholderNames(template: string): string[] {
223
- const placeholders: string[] = [];
224
- for (let i = 0; i < template.length; i++) {
225
- if (template[i] === "{") {
226
- // Check for escaped brace
227
- if (i + 1 < template.length && template[i + 1] === "{") {
228
- i++;
229
- continue;
230
- }
231
- const closeIndex = template.indexOf("}", i + 1);
232
- if (closeIndex === -1) continue;
233
- const key = template.slice(i + 1, closeIndex).trim();
234
- placeholders.push(key);
235
- i = closeIndex;
236
- }
237
- }
238
- return placeholders;
239
- }
240
-
241
- /**
242
- * Parses a property path into its segments.
243
- * @param path The property path (e.g., "user.password" or "users[0].email").
244
- * @returns An array of path segments.
245
- */
246
- function parsePathSegments(path: string): string[] {
247
- const segments: string[] = [];
248
- let current = "";
249
- for (const char of path) {
250
- if (char === "." || char === "[") {
251
- if (current) segments.push(current);
252
- current = "";
253
- } else if (char === "]" || char === "?") {
254
- // Skip these characters
255
- } else {
256
- current += char;
257
- }
258
- }
259
- if (current) segments.push(current);
260
- return segments;
261
- }
262
-
263
- /**
264
- * Determines which placeholder indices should be redacted based on field
265
- * patterns.
266
- * @param placeholders Array of placeholder names from the template.
267
- * @param fieldPatterns Field patterns to match against.
268
- * @returns Set of indices that should be redacted.
269
- */
270
- function getRedactedPlaceholderIndices(
271
- placeholders: string[],
272
- fieldPatterns: FieldPatterns,
273
- ): Set<number> {
274
- const indices = new Set<number>();
275
- for (let i = 0; i < placeholders.length; i++) {
276
- const placeholder = placeholders[i];
277
- // Skip wildcard {*}
278
- if (placeholder === "*") continue;
279
-
280
- // Check the full placeholder name
281
- if (shouldFieldRedacted(placeholder, fieldPatterns)) {
282
- indices.add(i);
283
- continue;
284
- }
285
- // For nested paths, check each segment
286
- const segments = parsePathSegments(placeholder);
287
- for (const segment of segments) {
288
- if (shouldFieldRedacted(segment, fieldPatterns)) {
289
- indices.add(i);
290
- break;
291
- }
292
- }
293
- }
294
- return indices;
295
- }
296
-
297
- /**
298
- * Redacts values in the message array based on the redacted placeholder
299
- * indices.
300
- * @param message The original message array.
301
- * @param redactedIndices Set of placeholder indices to redact.
302
- * @param action The redaction action.
303
- * @returns New message array with redacted values.
304
- */
305
- function redactMessageArray(
306
- message: readonly unknown[],
307
- redactedIndices: Set<number>,
308
- action: "delete" | ((value: unknown) => unknown) | undefined,
309
- ): readonly unknown[] {
310
- if (redactedIndices.size === 0) return message;
311
-
312
- const result: unknown[] = [];
313
- let placeholderIndex = 0;
314
-
315
- for (let i = 0; i < message.length; i++) {
316
- if (i % 2 === 0) {
317
- // Even index: text segment
318
- result.push(message[i]);
319
- } else {
320
- // Odd index: value/placeholder
321
- if (redactedIndices.has(placeholderIndex)) {
322
- if (action == null || action === "delete") {
323
- result.push("");
324
- } else {
325
- result.push(action(message[i]));
326
- }
327
- } else {
328
- result.push(message[i]);
329
- }
330
- placeholderIndex++;
331
- }
332
- }
333
- return result;
334
- }
335
-
336
- /**
337
- * Collects redacted value mappings from original to redacted properties.
338
- * @param original The original properties.
339
- * @param redacted The redacted properties.
340
- * @param map The map to populate with original -> redacted value pairs.
341
- */
342
- function collectRedactedValues(
343
- original: Record<string, unknown>,
344
- redacted: Record<string, unknown>,
345
- map: Map<unknown, unknown>,
346
- ): void {
347
- for (const key in original) {
348
- const origVal = original[key];
349
- const redVal = redacted[key];
350
-
351
- if (origVal !== redVal) {
352
- map.set(origVal, redVal);
353
- }
354
-
355
- // Recurse into nested objects
356
- if (
357
- typeof origVal === "object" && origVal !== null &&
358
- typeof redVal === "object" && redVal !== null &&
359
- !Array.isArray(origVal)
360
- ) {
361
- collectRedactedValues(
362
- origVal as Record<string, unknown>,
363
- redVal as Record<string, unknown>,
364
- map,
365
- );
366
- }
367
- }
368
- }
369
-
370
- /**
371
- * Gets a map of original values to their redacted replacements.
372
- * @param original The original properties.
373
- * @param redacted The redacted properties.
374
- * @returns A map of original -> redacted values.
375
- */
376
- function getRedactedValues(
377
- original: Record<string, unknown>,
378
- redacted: Record<string, unknown>,
379
- ): Map<unknown, unknown> {
380
- const map = new Map<unknown, unknown>();
381
- collectRedactedValues(original, redacted, map);
382
- return map;
383
- }
384
-
385
- /**
386
- * Redacts message array values by comparing with redacted property values.
387
- * Used for tagged template literals where placeholder names are not available.
388
- * @param message The original message array.
389
- * @param redactedValues Map of original -> redacted values.
390
- * @returns New message array with redacted values.
391
- */
392
- function redactMessageByValues(
393
- message: readonly unknown[],
394
- redactedValues: Map<unknown, unknown>,
395
- ): readonly unknown[] {
396
- if (redactedValues.size === 0) return message;
397
-
398
- const result: unknown[] = [];
399
- for (let i = 0; i < message.length; i++) {
400
- if (i % 2 === 0) {
401
- result.push(message[i]);
402
- } else {
403
- const val = message[i];
404
- if (redactedValues.has(val)) {
405
- result.push(redactedValues.get(val));
406
- } else {
407
- result.push(val);
408
- }
409
- }
410
- }
411
- return result;
412
- }
package/src/mod.ts DELETED
@@ -1,8 +0,0 @@
1
- export {
2
- DEFAULT_REDACT_FIELDS,
3
- type FieldPattern,
4
- type FieldPatterns,
5
- type FieldRedactionOptions,
6
- redactByField,
7
- } from "./field.ts";
8
- export * from "./pattern.ts";