@platform-sec/sensitive-string-core 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bson.d.ts +28 -0
- package/dist/bson.d.ts.map +1 -0
- package/dist/bson.js +37 -0
- package/dist/bson.js.map +1 -0
- package/dist/classification.d.ts +69 -0
- package/dist/classification.d.ts.map +1 -0
- package/dist/classification.js +104 -0
- package/dist/classification.js.map +1 -0
- package/dist/compare.d.ts +36 -0
- package/dist/compare.d.ts.map +1 -0
- package/dist/compare.js +123 -0
- package/dist/compare.js.map +1 -0
- package/dist/context.d.ts +78 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +110 -0
- package/dist/context.js.map +1 -0
- package/dist/derive.d.ts +110 -0
- package/dist/derive.d.ts.map +1 -0
- package/dist/derive.js +153 -0
- package/dist/derive.js.map +1 -0
- package/dist/destination.d.ts +120 -0
- package/dist/destination.d.ts.map +1 -0
- package/dist/destination.js +352 -0
- package/dist/destination.js.map +1 -0
- package/dist/deterministic.d.ts +145 -0
- package/dist/deterministic.d.ts.map +1 -0
- package/dist/deterministic.js +352 -0
- package/dist/deterministic.js.map +1 -0
- package/dist/dev-mode.d.ts +177 -0
- package/dist/dev-mode.d.ts.map +1 -0
- package/dist/dev-mode.js +503 -0
- package/dist/dev-mode.js.map +1 -0
- package/dist/environment.d.ts +181 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +558 -0
- package/dist/environment.js.map +1 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +21 -0
- package/dist/errors.js.map +1 -0
- package/dist/global-state.d.ts +8 -0
- package/dist/global-state.d.ts.map +1 -0
- package/dist/global-state.js +22 -0
- package/dist/global-state.js.map +1 -0
- package/dist/hash.d.ts +74 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +253 -0
- package/dist/hash.js.map +1 -0
- package/dist/hygiene.d.ts +242 -0
- package/dist/hygiene.d.ts.map +1 -0
- package/dist/hygiene.js +257 -0
- package/dist/hygiene.js.map +1 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +233 -0
- package/dist/index.js.map +1 -0
- package/dist/mask.d.ts +51 -0
- package/dist/mask.d.ts.map +1 -0
- package/dist/mask.js +461 -0
- package/dist/mask.js.map +1 -0
- package/dist/own-property.d.ts +20 -0
- package/dist/own-property.d.ts.map +1 -0
- package/dist/own-property.js +34 -0
- package/dist/own-property.js.map +1 -0
- package/dist/patterns.d.ts +12 -0
- package/dist/patterns.d.ts.map +1 -0
- package/dist/patterns.js +19 -0
- package/dist/patterns.js.map +1 -0
- package/dist/policy.d.ts +51 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +181 -0
- package/dist/policy.js.map +1 -0
- package/dist/probe.d.ts +141 -0
- package/dist/probe.d.ts.map +1 -0
- package/dist/probe.js +213 -0
- package/dist/probe.js.map +1 -0
- package/dist/redact-copy.d.ts +61 -0
- package/dist/redact-copy.d.ts.map +1 -0
- package/dist/redact-copy.js +217 -0
- package/dist/redact-copy.js.map +1 -0
- package/dist/redact.d.ts +141 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +514 -0
- package/dist/redact.js.map +1 -0
- package/dist/redaction-marker.d.ts +48 -0
- package/dist/redaction-marker.d.ts.map +1 -0
- package/dist/redaction-marker.js +138 -0
- package/dist/redaction-marker.js.map +1 -0
- package/dist/redaction-observer.d.ts +108 -0
- package/dist/redaction-observer.d.ts.map +1 -0
- package/dist/redaction-observer.js +103 -0
- package/dist/redaction-observer.js.map +1 -0
- package/dist/redaction-scope.d.ts +21 -0
- package/dist/redaction-scope.d.ts.map +1 -0
- package/dist/redaction-scope.js +44 -0
- package/dist/redaction-scope.js.map +1 -0
- package/dist/reference.d.ts +101 -0
- package/dist/reference.d.ts.map +1 -0
- package/dist/reference.js +123 -0
- package/dist/reference.js.map +1 -0
- package/dist/regexp.d.ts +25 -0
- package/dist/regexp.d.ts.map +1 -0
- package/dist/regexp.js +61 -0
- package/dist/regexp.js.map +1 -0
- package/dist/release.d.ts +88 -0
- package/dist/release.d.ts.map +1 -0
- package/dist/release.js +108 -0
- package/dist/release.js.map +1 -0
- package/dist/reveal-budget.d.ts +148 -0
- package/dist/reveal-budget.d.ts.map +1 -0
- package/dist/reveal-budget.js +314 -0
- package/dist/reveal-budget.js.map +1 -0
- package/dist/revocation.d.ts +168 -0
- package/dist/revocation.d.ts.map +1 -0
- package/dist/revocation.js +541 -0
- package/dist/revocation.js.map +1 -0
- package/dist/sealing.d.ts +283 -0
- package/dist/sealing.d.ts.map +1 -0
- package/dist/sealing.js +523 -0
- package/dist/sealing.js.map +1 -0
- package/dist/sensitive-string.d.ts +919 -0
- package/dist/sensitive-string.d.ts.map +1 -0
- package/dist/sensitive-string.js +2260 -0
- package/dist/sensitive-string.js.map +1 -0
- package/dist/serialized.d.ts +33 -0
- package/dist/serialized.d.ts.map +1 -0
- package/dist/serialized.js +65 -0
- package/dist/serialized.js.map +1 -0
- package/dist/template.d.ts +69 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +158 -0
- package/dist/template.js.map +1 -0
- package/dist/ttl.d.ts +104 -0
- package/dist/ttl.d.ts.map +1 -0
- package/dist/ttl.js +330 -0
- package/dist/ttl.js.map +1 -0
- package/dist/types.d.ts +859 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -0
- package/dist/winston.d.ts +114 -0
- package/dist/winston.d.ts.map +1 -0
- package/dist/winston.js +170 -0
- package/dist/winston.js.map +1 -0
- package/dist/worker.d.ts +398 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +861 -0
- package/dist/worker.js.map +1 -0
- package/dist/wrap.d.ts +287 -0
- package/dist/wrap.d.ts.map +1 -0
- package/dist/wrap.js +553 -0
- package/dist/wrap.js.map +1 -0
- package/package.json +10 -10
package/dist/redact.js
ADDED
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.redactInStructure = redactInStructure;
|
|
4
|
+
exports.safeLog = safeLog;
|
|
5
|
+
exports.safeStringify = safeStringify;
|
|
6
|
+
exports.createSensitiveSerializer = createSensitiveSerializer;
|
|
7
|
+
exports.containsSensitive = containsSensitive;
|
|
8
|
+
exports.sensitiveClassesIn = sensitiveClassesIn;
|
|
9
|
+
const sensitive_string_js_1 = require("./sensitive-string.js");
|
|
10
|
+
const serialized_js_1 = require("./serialized.js");
|
|
11
|
+
const own_property_js_1 = require("./own-property.js");
|
|
12
|
+
const redaction_scope_js_1 = require("./redaction-scope.js");
|
|
13
|
+
const probe_js_1 = require("./probe.js");
|
|
14
|
+
/**
|
|
15
|
+
* Identify ordinary objects: plain objects, null-prototype objects, and class
|
|
16
|
+
* instances. Exotic objects such as typed arrays, promises, boxed primitives,
|
|
17
|
+
* and anything with a custom Symbol.toStringTag report a different tag and are
|
|
18
|
+
* handled by the string fallback instead.
|
|
19
|
+
*
|
|
20
|
+
* The tag is read through a guard, because reading it goes through a
|
|
21
|
+
* `Symbol.toStringTag` getter the object controls. An object that refuses is
|
|
22
|
+
* treated as ordinary, which sends it to be walked and redacted rather than to
|
|
23
|
+
* `String()`, which would ask the same object another question.
|
|
24
|
+
*/
|
|
25
|
+
function isOrdinaryObject(obj) {
|
|
26
|
+
return (0, probe_js_1.probeTag)(obj) === '[object Object]';
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Copy own enumerable properties into a new plain object, redacting as we go.
|
|
30
|
+
* Used for plain objects and class instances alike. The result is always a
|
|
31
|
+
* plain object: methods, prototype getters, and private fields are not carried
|
|
32
|
+
* over, which keeps the output inert for logging.
|
|
33
|
+
*
|
|
34
|
+
* An object that cannot be enumerated at all becomes the {@link UNREADABLE}
|
|
35
|
+
* marker rather than an empty object. Both are safe; only one is honest. An
|
|
36
|
+
* empty object in a log line reads as "this had no properties", which is a
|
|
37
|
+
* different and more misleading claim than "this could not be read".
|
|
38
|
+
*
|
|
39
|
+
* A key named `__proto__` stays a key in the copy rather than becoming its
|
|
40
|
+
* prototype, which would both drop that subtree from the log line and hand
|
|
41
|
+
* the copy inherited properties the input never had: see `assignOwn`.
|
|
42
|
+
*/
|
|
43
|
+
function redactOwnProperties(obj, seen, omit) {
|
|
44
|
+
const entries = (0, probe_js_1.probeEntries)(obj);
|
|
45
|
+
if (entries === undefined) {
|
|
46
|
+
return probe_js_1.UNREADABLE;
|
|
47
|
+
}
|
|
48
|
+
const result = {};
|
|
49
|
+
for (const [key, value] of entries) {
|
|
50
|
+
if (key === omit) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
(0, own_property_js_1.assignOwn)(result, key, redactWalk(value, seen));
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Recursively redact SensitiveString instances in a data structure.
|
|
59
|
+
*
|
|
60
|
+
* Handles:
|
|
61
|
+
* - Primitive values (returned as-is)
|
|
62
|
+
* - SensitiveString instances (redacted via toString())
|
|
63
|
+
* - Serialized sensitive values, the `{ __sensitive: true, klass, value }` shape
|
|
64
|
+
* `serialize()` produces (replaced by the redaction of the class they name,
|
|
65
|
+
* or '[REDACTED]' for a class that is not registered)
|
|
66
|
+
* - Arrays, Map, and Set (recursively processed)
|
|
67
|
+
* - Date (preserved as-is)
|
|
68
|
+
* - Error (name, message, and stack copied into a plain object)
|
|
69
|
+
* - Plain objects and class instances (own enumerable properties recursively
|
|
70
|
+
* processed into a new plain object)
|
|
71
|
+
* - Objects with a toJSON() method (their JSON form, recursively processed; a
|
|
72
|
+
* reveal() from inside the method is refused, and the object's own
|
|
73
|
+
* properties are walked instead)
|
|
74
|
+
* - Other exotic objects such as typed arrays or promises (string form)
|
|
75
|
+
* - Circular references (replaced with '[Circular]')
|
|
76
|
+
*
|
|
77
|
+
* @param obj - The value to process
|
|
78
|
+
* @returns A new structure with all SensitiveString instances redacted
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const data = {
|
|
83
|
+
* user: 'alice',
|
|
84
|
+
* token: new SensitiveString('secret123', 'credential', 'token'),
|
|
85
|
+
* nested: {
|
|
86
|
+
* email: new SensitiveString('alice@example.com', 'sensitive', 'email')
|
|
87
|
+
* }
|
|
88
|
+
* };
|
|
89
|
+
*
|
|
90
|
+
* const safe = redactInStructure(data);
|
|
91
|
+
* // {
|
|
92
|
+
* // user: 'alice',
|
|
93
|
+
* // token: '[REDACTED:credential]',
|
|
94
|
+
* // nested: { email: '[REDACTED:sensitive]' }
|
|
95
|
+
* // }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
function redactInStructure(obj) {
|
|
99
|
+
return redactWalk(obj, new WeakSet());
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The walk itself, carrying the set that catches a cycle.
|
|
103
|
+
*
|
|
104
|
+
* Private so the accumulator stays out of the exported signature. It used to be
|
|
105
|
+
* an optional second parameter documented as internal, which is not a control:
|
|
106
|
+
* a caller sharing one `seen` across a graph reaches an object the walk has
|
|
107
|
+
* already visited and is told '[Circular]' for the whole payload, and the same
|
|
108
|
+
* mistake on `containsSensitive` answers `false` about a payload that does hold
|
|
109
|
+
* a secret. Neither is a mistake a safety helper should let a caller make.
|
|
110
|
+
*/
|
|
111
|
+
function redactWalk(obj, seen) {
|
|
112
|
+
// Handle primitives and null
|
|
113
|
+
if (obj === null || typeof obj !== 'object') {
|
|
114
|
+
return obj;
|
|
115
|
+
}
|
|
116
|
+
// Read once, used twice: this answers both "will every `instanceof` below
|
|
117
|
+
// throw" and, forty lines down, "is this a plain object". An object that
|
|
118
|
+
// refuses to name its prototype has to be caught before the first
|
|
119
|
+
// `instanceof`, which walks the chain and would throw.
|
|
120
|
+
const prototype = (0, probe_js_1.probePrototype)(obj);
|
|
121
|
+
if (prototype === undefined) {
|
|
122
|
+
return probe_js_1.UNREADABLE;
|
|
123
|
+
}
|
|
124
|
+
// A SensitiveString redacts itself, through the class's own rendering so an
|
|
125
|
+
// observer sees it under the 'structure' surface rather than as a coercion.
|
|
126
|
+
// Guarded inside the binding, because a Proxy around an instance answers
|
|
127
|
+
// `instanceof` through the prototype it forwards and then fails the brand
|
|
128
|
+
// check: the private field is on the target, not the proxy. The marker
|
|
129
|
+
// loses nothing, since the redaction is the whole of what the walk keeps.
|
|
130
|
+
if (obj instanceof sensitive_string_js_1.SensitiveString) {
|
|
131
|
+
return (0, sensitive_string_js_1.renderStructureRedaction)(obj) ?? probe_js_1.UNREADABLE;
|
|
132
|
+
}
|
|
133
|
+
// A serialize() result carries the raw value by design, and it is a plain
|
|
134
|
+
// object, so JSON.stringify would emit it. In a log line that is the one
|
|
135
|
+
// leak toJSON() cannot catch; it takes the class's redaction here instead.
|
|
136
|
+
if ((0, serialized_js_1.carriesSerializedMarker)(obj)) {
|
|
137
|
+
return (0, serialized_js_1.redactSerialized)(obj);
|
|
138
|
+
}
|
|
139
|
+
// Circular reference protection
|
|
140
|
+
if (seen.has(obj)) {
|
|
141
|
+
return '[Circular]';
|
|
142
|
+
}
|
|
143
|
+
seen.add(obj);
|
|
144
|
+
// Arrays. `map` reads `length` and every index through the object, and a
|
|
145
|
+
// Proxy around an array answers `Array.isArray` and may refuse both.
|
|
146
|
+
if (Array.isArray(obj)) {
|
|
147
|
+
return (0, probe_js_1.probeBuiltin)(() => obj.map((item) => redactWalk(item, seen))) ?? probe_js_1.UNREADABLE;
|
|
148
|
+
}
|
|
149
|
+
// Date, preserved as-is for the logger to format. A Proxy around a Date
|
|
150
|
+
// answers `instanceof` and then throws from every method, including the
|
|
151
|
+
// `toJSON()` that `JSON.stringify` calls on it later, so the walk asks it
|
|
152
|
+
// one question now rather than leaving the throw for the serializer.
|
|
153
|
+
if (obj instanceof Date) {
|
|
154
|
+
return (0, probe_js_1.probeBuiltin)(() => obj.getTime()) === undefined ? probe_js_1.UNREADABLE : obj;
|
|
155
|
+
}
|
|
156
|
+
// Error: name, message and stack, each read through a guard, because any of
|
|
157
|
+
// the three may be a getter the object controls. A property that refuses
|
|
158
|
+
// reads as absent while the others still read, which is the rule
|
|
159
|
+
// `probeEntries` applies to every other object.
|
|
160
|
+
if (obj instanceof Error) {
|
|
161
|
+
return {
|
|
162
|
+
name: (0, probe_js_1.probeProperty)(obj, 'name'),
|
|
163
|
+
message: (0, probe_js_1.probeProperty)(obj, 'message'),
|
|
164
|
+
stack: (0, probe_js_1.probeProperty)(obj, 'stack'),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
// Map and Set, entry by entry. Iteration is the collection's own code: a
|
|
168
|
+
// Proxy around either throws from `entries()` or `values()`, and a subclass
|
|
169
|
+
// may throw from its iterator. A partial copy would claim to be the whole.
|
|
170
|
+
if (obj instanceof Map) {
|
|
171
|
+
return ((0, probe_js_1.probeBuiltin)(() => {
|
|
172
|
+
const result = new Map();
|
|
173
|
+
for (const [key, value] of obj) {
|
|
174
|
+
result.set(redactWalk(key, seen), redactWalk(value, seen));
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
}) ?? probe_js_1.UNREADABLE);
|
|
178
|
+
}
|
|
179
|
+
if (obj instanceof Set) {
|
|
180
|
+
return ((0, probe_js_1.probeBuiltin)(() => {
|
|
181
|
+
const result = new Set();
|
|
182
|
+
for (const value of obj) {
|
|
183
|
+
result.add(redactWalk(value, seen));
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}) ?? probe_js_1.UNREADABLE);
|
|
187
|
+
}
|
|
188
|
+
// Objects that define their own JSON form (URL, Buffer, user classes, and a
|
|
189
|
+
// plain object carrying its own toJSON): redact that form. It has to come
|
|
190
|
+
// before the plain-object copy, because a copy that keeps the function hands
|
|
191
|
+
// JSON.stringify an unredacted form to call later. A toJSON() that returns
|
|
192
|
+
// the object itself is walked like any other object.
|
|
193
|
+
//
|
|
194
|
+
// Both the lookup and the call are guarded. `'toJSON' in obj` goes through a
|
|
195
|
+
// `has` trap, and the method itself is caller code that may throw. A refusal
|
|
196
|
+
// from either falls through to the own-property walk, which is the same path
|
|
197
|
+
// an object with no toJSON takes and still redacts everything it holds.
|
|
198
|
+
const toJson = (0, probe_js_1.probeMethod)(obj, 'toJSON');
|
|
199
|
+
if (toJson !== undefined) {
|
|
200
|
+
// With the redaction mark set: a toJSON() that calls reveal() would hand
|
|
201
|
+
// the walk a plain string it cannot tell from one that was always plain,
|
|
202
|
+
// so the gate refuses the reveal for the duration of the call. The refusal
|
|
203
|
+
// is a throw, which lands in the fallback below like any other refusal,
|
|
204
|
+
// and the instance is redacted as an instance there. See `redaction-scope`.
|
|
205
|
+
const called = (0, redaction_scope_js_1.runInsideRedaction)(() => (0, probe_js_1.probeCall)(toJson, obj));
|
|
206
|
+
if (called.ok) {
|
|
207
|
+
return called.value === obj ? redactOwnProperties(obj, seen) : redactWalk(called.value, seen);
|
|
208
|
+
}
|
|
209
|
+
// The method refused, so the walk falls through to own properties. It also
|
|
210
|
+
// drops the method itself: an own enumerable `toJSON` would otherwise be
|
|
211
|
+
// copied into the result, and the next serializer to touch that result
|
|
212
|
+
// would call it and get the same throw. The output of this function is
|
|
213
|
+
// supposed to be inert.
|
|
214
|
+
return redactOwnProperties(obj, seen, 'toJSON');
|
|
215
|
+
}
|
|
216
|
+
// Handle plain objects
|
|
217
|
+
if (prototype === Object.prototype) {
|
|
218
|
+
return redactOwnProperties(obj, seen);
|
|
219
|
+
}
|
|
220
|
+
// Class instances (entities, DTOs, view models) and null-prototype objects:
|
|
221
|
+
// walk own enumerable properties. Stringifying these would hide any
|
|
222
|
+
// SensitiveString they hold behind "[object Object]".
|
|
223
|
+
if (isOrdinaryObject(obj)) {
|
|
224
|
+
return redactOwnProperties(obj, seen);
|
|
225
|
+
}
|
|
226
|
+
// Exotic objects (typed arrays, promises, boxed primitives, RegExp, ...):
|
|
227
|
+
// fall back to the string representation.
|
|
228
|
+
//
|
|
229
|
+
// Guarded, because an object that reaches here has its own toString by
|
|
230
|
+
// definition, and its own toString is caller code. A refusal leaves nothing
|
|
231
|
+
// to keep, so the marker stands in for it.
|
|
232
|
+
return (0, probe_js_1.probeString)(obj) ?? probe_js_1.UNREADABLE;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Prepare a value for safe logging.
|
|
236
|
+
* Wrapper around redactInStructure with a clearer name for log contexts.
|
|
237
|
+
*
|
|
238
|
+
* @param obj - The value to make safe for logging
|
|
239
|
+
* @returns A new structure with all SensitiveString instances redacted
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* console.log('Processing request:', safeLog(payload));
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
function safeLog(obj) {
|
|
247
|
+
return redactInStructure(obj);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Convert the values JSON.stringify rejects outright.
|
|
251
|
+
*
|
|
252
|
+
* A BigInt is the one primitive it throws a TypeError on rather than skipping,
|
|
253
|
+
* so a single `id: 1n` anywhere in a payload takes down the logging call that
|
|
254
|
+
* was meant to be the safe one. The decimal string is the conventional
|
|
255
|
+
* substitute and is lossless, unlike Number(), which silently rounds anything
|
|
256
|
+
* past 2^53. A BigInt has no source text to leak the way a function does.
|
|
257
|
+
*/
|
|
258
|
+
function toJsonSafe(value) {
|
|
259
|
+
return typeof value === 'bigint' ? value.toString() : value;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Wrap the caller's replacer, if any, so the conversion runs on whatever that
|
|
263
|
+
* replacer returned.
|
|
264
|
+
*
|
|
265
|
+
* Order matters: a caller's replacer is free to hand back a BigInt of its own,
|
|
266
|
+
* so the conversion has to come last to hold. Running it first would also hide
|
|
267
|
+
* the real value from a replacer that wanted to inspect it.
|
|
268
|
+
*
|
|
269
|
+
* Installing a replacer unconditionally is safe: an identity replacer produces
|
|
270
|
+
* byte-identical output to passing null, and JSON.stringify applies it to the
|
|
271
|
+
* root value too, so a bare BigInt is covered as well as a nested one. The
|
|
272
|
+
* `function` expression, rather than an arrow, preserves the holder object as
|
|
273
|
+
* `this`, which is what a replacer passed straight to JSON.stringify receives.
|
|
274
|
+
*/
|
|
275
|
+
function jsonSafeReplacer(replacer) {
|
|
276
|
+
if (!replacer) {
|
|
277
|
+
return (_key, value) => toJsonSafe(value);
|
|
278
|
+
}
|
|
279
|
+
return function (key, value) {
|
|
280
|
+
return toJsonSafe(replacer.call(this, key, value));
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Safely stringify a value to JSON with all sensitive data redacted.
|
|
285
|
+
*
|
|
286
|
+
* Always returns a string and never throws on the input's shape: a BigInt
|
|
287
|
+
* becomes its decimal string, and a value JSON cannot represent at all becomes
|
|
288
|
+
* 'null'. Both are cases where JSON.stringify alone would take a logging call
|
|
289
|
+
* down or hand back an undefined that its own type declaration denies.
|
|
290
|
+
*
|
|
291
|
+
* @param obj - The value to stringify
|
|
292
|
+
* @param options - Stringify options
|
|
293
|
+
* @returns JSON string with redacted sensitive values, or the string 'null'
|
|
294
|
+
* for a value JSON cannot represent
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```ts
|
|
298
|
+
* const json = safeStringify({
|
|
299
|
+
* user: 'alice',
|
|
300
|
+
* token: new SensitiveString('secret', 'credential', 'token')
|
|
301
|
+
* });
|
|
302
|
+
* // '{"user":"alice","token":"[REDACTED:credential]"}'
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
function safeStringify(obj, options = {}) {
|
|
306
|
+
const redacted = redactInStructure(obj);
|
|
307
|
+
const space = options.pretty ? 2 : undefined;
|
|
308
|
+
const json = JSON.stringify(redacted, jsonSafeReplacer(options.replacer), space);
|
|
309
|
+
// JSON.stringify returns undefined, not a string, for the values JSON cannot
|
|
310
|
+
// represent: undefined itself, functions (a class constructor among them),
|
|
311
|
+
// and symbols. A replacer that drops the root does the same. TypeScript's
|
|
312
|
+
// own lib declares the return as `string` and hides this, so a caller that
|
|
313
|
+
// believed the declaration would fail on the result rather than at the call.
|
|
314
|
+
//
|
|
315
|
+
// 'null' is what JSON.stringify already substitutes for these same values in
|
|
316
|
+
// array position, so the fallback keeps the return parseable as JSON and
|
|
317
|
+
// leaks nothing. Deliberately not String(redacted): a function's string form
|
|
318
|
+
// is its source text, which would put a static field's initializer, secret
|
|
319
|
+
// literals and all, into output this function exists to keep safe.
|
|
320
|
+
return json ?? 'null';
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Create a logger-compatible serializer that redacts sensitive values.
|
|
324
|
+
* Can be used with pino, winston, or other logging libraries that support
|
|
325
|
+
* custom serializers.
|
|
326
|
+
*
|
|
327
|
+
* @returns A serializer function compatible with most logging libraries
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```ts
|
|
331
|
+
* // With pino
|
|
332
|
+
* const logger = pino({
|
|
333
|
+
* serializers: {
|
|
334
|
+
* req: createSensitiveSerializer(),
|
|
335
|
+
* payload: createSensitiveSerializer(),
|
|
336
|
+
* }
|
|
337
|
+
* });
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
function createSensitiveSerializer() {
|
|
341
|
+
return (value) => redactInStructure(value);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Check if a value contains any sensitive value: a SensitiveString instance,
|
|
345
|
+
* or a serialized one, which holds the raw value in the open.
|
|
346
|
+
* Useful for validation or debugging.
|
|
347
|
+
*
|
|
348
|
+
* Walks arrays, Map and Set entries, plain objects, and the own enumerable
|
|
349
|
+
* properties of class instances.
|
|
350
|
+
*
|
|
351
|
+
* @param obj - The value to check
|
|
352
|
+
* @returns true if any SensitiveString or serialized sensitive value is found
|
|
353
|
+
*/
|
|
354
|
+
function containsSensitive(obj) {
|
|
355
|
+
return containsWalk(obj, new WeakSet());
|
|
356
|
+
}
|
|
357
|
+
/** The walk itself. Private for the reason `redactWalk` is. */
|
|
358
|
+
function containsWalk(obj, seen) {
|
|
359
|
+
if (obj === null || typeof obj !== 'object') {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
// Before the first `instanceof`, which walks the prototype chain and throws
|
|
363
|
+
// for an object that refuses to report it. Unwalkable, so unprovable: a leak
|
|
364
|
+
// detector answers `true` for a value it could not read, for the reason given
|
|
365
|
+
// at the ordinary-object branch below.
|
|
366
|
+
if ((0, probe_js_1.isUnreadable)(obj)) {
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
if (obj instanceof sensitive_string_js_1.SensitiveString || (0, serialized_js_1.carriesSerializedMarker)(obj)) {
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
if (seen.has(obj)) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
seen.add(obj);
|
|
376
|
+
// Iteration is the collection's own code, and a collection that refuses it
|
|
377
|
+
// answers `true`, for the reason given at the ordinary-object branch below.
|
|
378
|
+
if (Array.isArray(obj)) {
|
|
379
|
+
return (0, probe_js_1.probeBuiltin)(() => obj.some((item) => containsWalk(item, seen))) ?? true;
|
|
380
|
+
}
|
|
381
|
+
if (obj instanceof Map) {
|
|
382
|
+
return ((0, probe_js_1.probeBuiltin)(() => {
|
|
383
|
+
for (const [key, value] of obj) {
|
|
384
|
+
if (containsWalk(key, seen) || containsWalk(value, seen)) {
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return false;
|
|
389
|
+
}) ?? true);
|
|
390
|
+
}
|
|
391
|
+
if (obj instanceof Set) {
|
|
392
|
+
return ((0, probe_js_1.probeBuiltin)(() => {
|
|
393
|
+
for (const value of obj) {
|
|
394
|
+
if (containsWalk(value, seen)) {
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return false;
|
|
399
|
+
}) ?? true);
|
|
400
|
+
}
|
|
401
|
+
if (isOrdinaryObject(obj)) {
|
|
402
|
+
const entries = (0, probe_js_1.probeEntries)(obj);
|
|
403
|
+
// An object that cannot be enumerated answers `true`, not `false`. This is
|
|
404
|
+
// a leak detector, and the two wrong answers are not symmetric: reporting a
|
|
405
|
+
// value it could not read as clean is how a caller ships a payload
|
|
406
|
+
// believing it was checked. `sensitiveClassesIn` deliberately answers the
|
|
407
|
+
// other way and says why.
|
|
408
|
+
if (entries === undefined) {
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
return entries.some(([, value]) => containsWalk(value, seen));
|
|
412
|
+
}
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Collect the sensitivity classes present anywhere in a value.
|
|
417
|
+
*
|
|
418
|
+
* The traversal matches {@link containsSensitive} exactly: arrays, Map keys and
|
|
419
|
+
* values, Set values, plain objects, and the own enumerable properties of class
|
|
420
|
+
* instances. It answers "which classes are in here" where `containsSensitive`
|
|
421
|
+
* answers "is anything in here", so a caller can act on `credential` without
|
|
422
|
+
* acting on `sensitive`. A serialized value contributes the class it names when
|
|
423
|
+
* that class is registered; one naming an unregistered class is counted by
|
|
424
|
+
* `containsSensitive` but has no class to contribute here.
|
|
425
|
+
*
|
|
426
|
+
* @param obj - The value to inspect
|
|
427
|
+
* @returns The set of sensitivity classes found, empty when there are none
|
|
428
|
+
*
|
|
429
|
+
* @example
|
|
430
|
+
* ```ts
|
|
431
|
+
* const classes = sensitiveClassesIn({ token: apiKey, email: userEmail });
|
|
432
|
+
* classes.has('credential'); // true
|
|
433
|
+
* ```
|
|
434
|
+
*/
|
|
435
|
+
function sensitiveClassesIn(obj) {
|
|
436
|
+
return classesWalk(obj, new Set(), new WeakSet());
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* The walk itself. Private for the reason `redactWalk` is, and with one more:
|
|
440
|
+
* a caller who primes `found` gets back a set naming a class the payload does
|
|
441
|
+
* not contain, which is a wrong answer that reads exactly like a right one.
|
|
442
|
+
*/
|
|
443
|
+
function classesWalk(obj, found, seen) {
|
|
444
|
+
if (obj === null || typeof obj !== 'object') {
|
|
445
|
+
return found;
|
|
446
|
+
}
|
|
447
|
+
// Unwalkable, and there is no class to name for something that was never
|
|
448
|
+
// read. `containsSensitive` answers `true` for the same object; see the note
|
|
449
|
+
// at its ordinary-object branch for why the two differ here.
|
|
450
|
+
if ((0, probe_js_1.isUnreadable)(obj)) {
|
|
451
|
+
return found;
|
|
452
|
+
}
|
|
453
|
+
if (obj instanceof sensitive_string_js_1.SensitiveString) {
|
|
454
|
+
// Through a guard: a Proxy around an instance forwards the read, and a
|
|
455
|
+
// hostile one may refuse it. Nothing to name then, as for any other object
|
|
456
|
+
// that was never read.
|
|
457
|
+
const klass = (0, probe_js_1.probeProperty)(obj, 'klass');
|
|
458
|
+
if (typeof klass === 'string') {
|
|
459
|
+
found.add(klass);
|
|
460
|
+
}
|
|
461
|
+
return found;
|
|
462
|
+
}
|
|
463
|
+
if ((0, serialized_js_1.carriesSerializedMarker)(obj)) {
|
|
464
|
+
const klass = (0, serialized_js_1.serializedClass)(obj);
|
|
465
|
+
if (klass !== undefined) {
|
|
466
|
+
found.add(klass);
|
|
467
|
+
}
|
|
468
|
+
return found;
|
|
469
|
+
}
|
|
470
|
+
if (seen.has(obj)) {
|
|
471
|
+
return found;
|
|
472
|
+
}
|
|
473
|
+
seen.add(obj);
|
|
474
|
+
// Iteration is the collection's own code. A collection that refuses it
|
|
475
|
+
// contributes nothing beyond what was read before the refusal, which is the
|
|
476
|
+
// answer this walk gives for an object it cannot enumerate.
|
|
477
|
+
if (Array.isArray(obj)) {
|
|
478
|
+
(0, probe_js_1.probeBuiltin)(() => {
|
|
479
|
+
for (const item of obj) {
|
|
480
|
+
classesWalk(item, found, seen);
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
return found;
|
|
484
|
+
}
|
|
485
|
+
if (obj instanceof Map) {
|
|
486
|
+
(0, probe_js_1.probeBuiltin)(() => {
|
|
487
|
+
for (const [key, value] of obj) {
|
|
488
|
+
classesWalk(key, found, seen);
|
|
489
|
+
classesWalk(value, found, seen);
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
return found;
|
|
493
|
+
}
|
|
494
|
+
if (obj instanceof Set) {
|
|
495
|
+
(0, probe_js_1.probeBuiltin)(() => {
|
|
496
|
+
for (const value of obj) {
|
|
497
|
+
classesWalk(value, found, seen);
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
return found;
|
|
501
|
+
}
|
|
502
|
+
if (isOrdinaryObject(obj)) {
|
|
503
|
+
// An object that cannot be enumerated contributes no classes, because there
|
|
504
|
+
// is no class to name. That is the one place this traversal and
|
|
505
|
+
// `containsSensitive` disagree, and the disagreement is the honest one:
|
|
506
|
+
// `containsSensitive` can say "there may be something in here" without
|
|
507
|
+
// knowing what, and this function cannot.
|
|
508
|
+
for (const [, value] of (0, probe_js_1.probeEntries)(obj) ?? []) {
|
|
509
|
+
classesWalk(value, found, seen);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return found;
|
|
513
|
+
}
|
|
514
|
+
//# sourceMappingURL=redact.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redact.js","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":";;AA8GA,8CAEC;AAwKD,0BAEC;AAyED,sCAkBC;AAoBD,8DAEC;AAaD,8CAEC;AA+FD,gDAEC;AA3fD,+DAAkF;AAElF,mDAA6F;AAC7F,uDAA8C;AAC9C,6DAA0D;AAC1D,yCAWoB;AAEpB;;;;;;;;;;GAUG;AACH,SAAS,gBAAgB,CAAC,GAAW;IACnC,OAAO,IAAA,mBAAQ,EAAC,GAAG,CAAC,KAAK,iBAAiB,CAAC;AAC7C,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAS,mBAAmB,CAC1B,GAAW,EACX,IAAqB,EACrB,IAAa;IAEb,MAAM,OAAO,GAAG,IAAA,uBAAY,EAAC,GAAG,C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,CAAC;QACvB,IAAA,uBAAY,EAAC,GAAG,EAAE;YAChB,KAAK,MAAM,KAAK,IAAI,GAAG,EAAE,CAAC;gBACxB,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;YAClC,CAAC;QACH,CAAC,CAAC,CAAC;QACH,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1B,4EAA4E;QAC5E,gEAAgE;QAChE,wEAAwE;QACxE,uEAAuE;QACvE,0CAA0C;QAC1C,KAAK,MAAM,CAAC,EAAE,KAAK,CAAC,IAAI,IAAA,uBAAY,EAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;YAChD,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** The prefix every default policy in core produces. */
|
|
2
|
+
export declare const MARKER_PREFIX = "[REDACTED:";
|
|
3
|
+
/**
|
|
4
|
+
* Every matcher that recognises a redaction the policy registry can produce.
|
|
5
|
+
*
|
|
6
|
+
* Derived from the registry rather than hardcoded, because `registerPolicy()`
|
|
7
|
+
* accepts a `redact` of the caller's own and a guard that only knew the default
|
|
8
|
+
* shape would miss exactly the deployment that customised it. Three sources,
|
|
9
|
+
* in the order they are cheapest to be sure of:
|
|
10
|
+
*
|
|
11
|
+
* {@link MARKER_PREFIX} unconditionally, which covers every default policy and
|
|
12
|
+
* every class this process has never registered. That last part is what a
|
|
13
|
+
* persistence guard needs and a transport guard does not: a column written by
|
|
14
|
+
* an older release, or by a service that registered a class this one does not
|
|
15
|
+
* have, still holds `[REDACTED:whatever]`, and the reader has to recognise it.
|
|
16
|
+
*
|
|
17
|
+
* Each policy's `markerPattern`, for a value-dependent redaction that declares
|
|
18
|
+
* its own shape. `deterministicRedaction()` sets it. Read through `search()`
|
|
19
|
+
* rather than `test()`, because a caller's pattern may carry `g` despite the
|
|
20
|
+
* warning on the field, and `test()` on a global pattern advances `lastIndex`
|
|
21
|
+
* so the next call starts mid-string and misses.
|
|
22
|
+
*
|
|
23
|
+
* Each policy's constant redaction, discovered with the two probes above.
|
|
24
|
+
*
|
|
25
|
+
* Memoized against {@link policyRegistryVersion}, so the probes run once per
|
|
26
|
+
* registry change rather than once per bound parameter. The version moves on an
|
|
27
|
+
* `overwrite` re-registration too, which a memo keyed on the set of class names
|
|
28
|
+
* would not see.
|
|
29
|
+
*/
|
|
30
|
+
export declare function redactionMatchers(): readonly (string | RegExp)[];
|
|
31
|
+
/**
|
|
32
|
+
* Whether a string carries a redaction marker anywhere in it.
|
|
33
|
+
*
|
|
34
|
+
* Containment rather than equality, because the ways a marker reaches a column
|
|
35
|
+
* or a request body are concatenation and interpolation as often as a bare
|
|
36
|
+
* assignment: a template string built over a wrapped value produces a marker
|
|
37
|
+
* embedded in a longer one, and that value is corrupt in exactly the same way.
|
|
38
|
+
*/
|
|
39
|
+
export declare function containsRedactionMarker(value: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Drop the memo.
|
|
42
|
+
*
|
|
43
|
+
* Rarely needed: {@link policyRegistryVersion} already moves on every
|
|
44
|
+
* registration and every reset. It exists for a test that mutates a policy
|
|
45
|
+
* object in place, which the registry cannot see.
|
|
46
|
+
*/
|
|
47
|
+
export declare function resetRedactionMarkerCache(): void;
|
|
48
|
+
//# sourceMappingURL=redaction-marker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redaction-marker.d.ts","sourceRoot":"","sources":["../src/redaction-marker.ts"],"names":[],"mappings":"AAoCA,wDAAwD;AACxD,eAAO,MAAM,aAAa,eAAe,CAAC;AAK1C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,iBAAiB,IAAI,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAqChE;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAU9D;AAED;;;;;;GAMG;AACH,wBAAgB,yBAAyB,IAAI,IAAI,CAGhD"}
|