@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/policy.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { SensitivityClass, SensitivityPolicy } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Determine the more restrictive class when combining two sensitive values.
|
|
4
|
+
* Uses SENSITIVITY_PRECEDENCE ordering (higher index = more restrictive).
|
|
5
|
+
*/
|
|
6
|
+
export declare function defaultCombineClasses(a: SensitivityClass, b: SensitivityClass): SensitivityClass;
|
|
7
|
+
/** The registry's current version. Increments on every registration and reset. */
|
|
8
|
+
export declare function policyRegistryVersion(): number;
|
|
9
|
+
/**
|
|
10
|
+
* Options for registering a policy.
|
|
11
|
+
*/
|
|
12
|
+
export interface RegisterPolicyOptions {
|
|
13
|
+
/** Allow overwriting an existing policy */
|
|
14
|
+
overwrite?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Skip validation of the redact function.
|
|
17
|
+
* ⚠️ DANGEROUS - only use for testing malicious policy behavior.
|
|
18
|
+
*/
|
|
19
|
+
skipRedactValidation?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Register a custom sensitivity policy.
|
|
23
|
+
*
|
|
24
|
+
* @throws Error if policy already exists and overwrite is not set
|
|
25
|
+
* @throws Error if redact function does not actually redact values
|
|
26
|
+
*/
|
|
27
|
+
export declare function registerPolicy(policy: SensitivityPolicy, opts?: RegisterPolicyOptions): void;
|
|
28
|
+
/**
|
|
29
|
+
* Get the policy for a sensitivity class.
|
|
30
|
+
*
|
|
31
|
+
* @throws Error if no policy is registered for the class
|
|
32
|
+
*/
|
|
33
|
+
export declare function getPolicy(name: SensitivityClass): SensitivityPolicy;
|
|
34
|
+
/**
|
|
35
|
+
* Get the default policy (for 'sensitive' class).
|
|
36
|
+
*/
|
|
37
|
+
export declare function getDefaultPolicy(): SensitivityPolicy;
|
|
38
|
+
/**
|
|
39
|
+
* Check if a policy is registered for a sensitivity class.
|
|
40
|
+
*/
|
|
41
|
+
export declare function hasPolicy(name: SensitivityClass): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Get all registered sensitivity classes.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getRegisteredClasses(): SensitivityClass[];
|
|
46
|
+
/**
|
|
47
|
+
* Reset policies to defaults.
|
|
48
|
+
* Primarily useful for testing.
|
|
49
|
+
*/
|
|
50
|
+
export declare function resetPolicies(): void;
|
|
51
|
+
//# sourceMappingURL=policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAA0B,MAAM,YAAY,CAAC;AAGzF;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,gBAAgB,EAAE,CAAC,EAAE,gBAAgB,GAAG,gBAAgB,CAIhG;AAgFD,kFAAkF;AAClF,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,2CAA2C;IAC3C,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC;AAgCD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,iBAAiB,EAAE,IAAI,GAAE,qBAA0B,GAAG,IAAI,CAchG;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,gBAAgB,GAAG,iBAAiB,CAMnE;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,iBAAiB,CAEpD;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAEzD;AAED;;GAEG;AACH,wBAAgB,oBAAoB,IAAI,gBAAgB,EAAE,CAEzD;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAMpC"}
|
package/dist/policy.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultCombineClasses = defaultCombineClasses;
|
|
4
|
+
exports.policyRegistryVersion = policyRegistryVersion;
|
|
5
|
+
exports.registerPolicy = registerPolicy;
|
|
6
|
+
exports.getPolicy = getPolicy;
|
|
7
|
+
exports.getDefaultPolicy = getDefaultPolicy;
|
|
8
|
+
exports.hasPolicy = hasPolicy;
|
|
9
|
+
exports.getRegisteredClasses = getRegisteredClasses;
|
|
10
|
+
exports.resetPolicies = resetPolicies;
|
|
11
|
+
const types_js_1 = require("./types.js");
|
|
12
|
+
const global_state_js_1 = require("./global-state.js");
|
|
13
|
+
/**
|
|
14
|
+
* Determine the more restrictive class when combining two sensitive values.
|
|
15
|
+
* Uses SENSITIVITY_PRECEDENCE ordering (higher index = more restrictive).
|
|
16
|
+
*/
|
|
17
|
+
function defaultCombineClasses(a, b) {
|
|
18
|
+
const indexA = types_js_1.SENSITIVITY_PRECEDENCE.indexOf(a);
|
|
19
|
+
const indexB = types_js_1.SENSITIVITY_PRECEDENCE.indexOf(b);
|
|
20
|
+
return indexA >= indexB ? a : b;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Create a default redaction string for a sensitivity class.
|
|
24
|
+
*/
|
|
25
|
+
function createDefaultRedact(klass) {
|
|
26
|
+
return (_value) => `[REDACTED:${klass}]`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Default policies.
|
|
30
|
+
* Two classes based on the only behavioral difference that matters:
|
|
31
|
+
* - credential: constant-time comparison (for timing attack prevention)
|
|
32
|
+
* - sensitive: regular comparison (for everything else)
|
|
33
|
+
*/
|
|
34
|
+
const DEFAULT_POLICIES = [
|
|
35
|
+
{
|
|
36
|
+
name: 'sensitive',
|
|
37
|
+
redact: createDefaultRedact('sensitive'),
|
|
38
|
+
requireConstantTimeEquals: false,
|
|
39
|
+
allowPartialQueries: true,
|
|
40
|
+
allowMask: true,
|
|
41
|
+
advisory: {
|
|
42
|
+
allowPersistPlain: false,
|
|
43
|
+
allowPersistEncrypted: true,
|
|
44
|
+
allowSendToLog: true,
|
|
45
|
+
// Written explicitly, though `undefined` is a refusal wherever this flag
|
|
46
|
+
// is read. Absent and `false` behave identically and read completely
|
|
47
|
+
// differently to somebody reviewing a policy: absent looks like an
|
|
48
|
+
// oversight. `sensitive` starts closed because the class is deliberately
|
|
49
|
+
// coarse and covers a customer email as readily as an internal note. A
|
|
50
|
+
// caller who wants either in a prompt registers a policy saying so, in
|
|
51
|
+
// one place, in code that shows up in review.
|
|
52
|
+
allowSendToExternal: false,
|
|
53
|
+
criticality: 'medium',
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'credential',
|
|
58
|
+
redact: createDefaultRedact('credential'),
|
|
59
|
+
requireConstantTimeEquals: true,
|
|
60
|
+
allowPartialQueries: false,
|
|
61
|
+
allowMask: false,
|
|
62
|
+
advisory: {
|
|
63
|
+
allowPersistPlain: false,
|
|
64
|
+
allowSendToLog: true,
|
|
65
|
+
allowHash: true, // credentials can typically be hashed for comparison
|
|
66
|
+
// See the note on `sensitive` above. A credential bound for a third
|
|
67
|
+
// party is the case this flag exists for.
|
|
68
|
+
allowSendToExternal: false,
|
|
69
|
+
criticality: 'high',
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
/**
|
|
74
|
+
* Internal registry of sensitivity policies.
|
|
75
|
+
*
|
|
76
|
+
* The Symbol.for()-backed singleton keeps policy changes visible when an
|
|
77
|
+
* application loads multiple physical copies of core.
|
|
78
|
+
*/
|
|
79
|
+
const policyRegistry = (0, global_state_js_1.getGlobalSingleton)('policy-registry', () => new Map(DEFAULT_POLICIES.map((policy) => [policy.name, policy])));
|
|
80
|
+
/**
|
|
81
|
+
* How many times the registry has changed.
|
|
82
|
+
*
|
|
83
|
+
* A cache over the registry cannot be keyed on the set of class *names*: an
|
|
84
|
+
* `overwrite` re-registration replaces a policy's `redact` while leaving the
|
|
85
|
+
* name set identical, so anything memoized against the names keeps answering
|
|
86
|
+
* from the previous policy. This counter moves on every mutation, including
|
|
87
|
+
* that one, which is what {@link redactionMatchers} keys its memo on.
|
|
88
|
+
*
|
|
89
|
+
* A singleton for the same reason the registry is one: two physical copies of
|
|
90
|
+
* core share the registry, so they must share its version too.
|
|
91
|
+
*/
|
|
92
|
+
const registryVersion = (0, global_state_js_1.getGlobalSingleton)('policy-registry-version', () => ({ value: 0 }));
|
|
93
|
+
/** The registry's current version. Increments on every registration and reset. */
|
|
94
|
+
function policyRegistryVersion() {
|
|
95
|
+
return registryVersion.value;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Validate that a redact function actually redacts the value.
|
|
99
|
+
* Tests with multiple canary values to catch identity functions and partial leaks.
|
|
100
|
+
*
|
|
101
|
+
* @throws Error if the redact function returns or contains the original value
|
|
102
|
+
*/
|
|
103
|
+
function validateRedactFunction(redact) {
|
|
104
|
+
// Test with multiple canary values to catch various bypass attempts
|
|
105
|
+
const canaries = [
|
|
106
|
+
'VALIDATION_CANARY_' + Math.random().toString(36).slice(2),
|
|
107
|
+
'test-secret-value-12345',
|
|
108
|
+
'<script>alert(1)</script>', // XSS-like payload
|
|
109
|
+
];
|
|
110
|
+
for (const canary of canaries) {
|
|
111
|
+
const result = redact(canary);
|
|
112
|
+
// Check for identity function
|
|
113
|
+
if (result === canary) {
|
|
114
|
+
throw new Error('Invalid redact function: must not return the original value unchanged');
|
|
115
|
+
}
|
|
116
|
+
// Check for partial inclusion (value embedded in output)
|
|
117
|
+
// Only check if canary is long enough to be meaningful
|
|
118
|
+
if (canary.length >= 8 && result.includes(canary)) {
|
|
119
|
+
throw new Error('Invalid redact function: must not include the original value in output');
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Register a custom sensitivity policy.
|
|
125
|
+
*
|
|
126
|
+
* @throws Error if policy already exists and overwrite is not set
|
|
127
|
+
* @throws Error if redact function does not actually redact values
|
|
128
|
+
*/
|
|
129
|
+
function registerPolicy(policy, opts = {}) {
|
|
130
|
+
if (policyRegistry.has(policy.name) && !opts.overwrite) {
|
|
131
|
+
throw new Error(`Policy '${policy.name}' already registered. Use { overwrite: true } to replace.`);
|
|
132
|
+
}
|
|
133
|
+
// Validate the redact function unless explicitly skipped
|
|
134
|
+
if (!opts.skipRedactValidation) {
|
|
135
|
+
validateRedactFunction((value) => policy.redact(value));
|
|
136
|
+
}
|
|
137
|
+
policyRegistry.set(policy.name, policy);
|
|
138
|
+
registryVersion.value += 1;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get the policy for a sensitivity class.
|
|
142
|
+
*
|
|
143
|
+
* @throws Error if no policy is registered for the class
|
|
144
|
+
*/
|
|
145
|
+
function getPolicy(name) {
|
|
146
|
+
const policy = policyRegistry.get(name);
|
|
147
|
+
if (!policy) {
|
|
148
|
+
throw new Error(`No policy registered for sensitivity class '${name}'`);
|
|
149
|
+
}
|
|
150
|
+
return policy;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get the default policy (for 'sensitive' class).
|
|
154
|
+
*/
|
|
155
|
+
function getDefaultPolicy() {
|
|
156
|
+
return getPolicy('sensitive');
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Check if a policy is registered for a sensitivity class.
|
|
160
|
+
*/
|
|
161
|
+
function hasPolicy(name) {
|
|
162
|
+
return policyRegistry.has(name);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get all registered sensitivity classes.
|
|
166
|
+
*/
|
|
167
|
+
function getRegisteredClasses() {
|
|
168
|
+
return Array.from(policyRegistry.keys());
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Reset policies to defaults.
|
|
172
|
+
* Primarily useful for testing.
|
|
173
|
+
*/
|
|
174
|
+
function resetPolicies() {
|
|
175
|
+
policyRegistry.clear();
|
|
176
|
+
for (const policy of DEFAULT_POLICIES) {
|
|
177
|
+
policyRegistry.set(policy.name, policy);
|
|
178
|
+
}
|
|
179
|
+
registryVersion.value += 1;
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":";;AAOA,sDAIC;AAiFD,sDAEC;AAmDD,wCAcC;AAOD,8BAMC;AAKD,4CAEC;AAKD,8BAEC;AAKD,oDAEC;AAMD,sCAMC;AA7MD,yCAAyF;AACzF,uDAAuD;AAEvD;;;GAGG;AACH,SAAgB,qBAAqB,CAAC,CAAmB,EAAE,CAAmB;IAC5E,MAAM,MAAM,GAAG,iCAAsB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,iCAAsB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IACjD,OAAO,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,SAAS,mBAAmB,CAAC,KAAuB;IAClD,OAAO,CAAC,MAAc,EAAE,EAAE,CAAC,aAAa,KAAK,GAAG,CAAC;AACnD,CAAC;AAED;;;;;GAKG;AACH,MAAM,gBAAgB,GAAwB;IAC5C;QACE,IAAI,EAAE,WAAW;QACjB,MAAM,EAAE,mBAAmB,CAAC,WAAW,CAAC;QACxC,yBAAyB,EAAE,KAAK;QAChC,mBAAmB,EAAE,IAAI;QACzB,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE;YACR,iBAAiB,EAAE,KAAK;YACxB,qBAAqB,EAAE,IAAI;YAC3B,cAAc,EAAE,IAAI;YACpB,yEAAyE;YACzE,qEAAqE;YACrE,mEAAmE;YACnE,yEAAyE;YACzE,uEAAuE;YACvE,uEAAuE;YACvE,8CAA8C;YAC9C,mBAAmB,EAAE,KAAK;YAC1B,WAAW,EAAE,QAAQ;SAC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AAS,CAAC,IAAsB;IAC9C,OAAO,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,SAAgB,oBAAoB;IAClC,OAAO,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC;AAC3C,CAAC;AAED;;;GAGG;AACH,SAAgB,aAAa;IAC3B,cAAc,CAAC,KAAK,EAAE,CAAC;IACvB,KAAK,MAAM,MAAM,IAAI,gBAAgB,EAAE,CAAC;QACtC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC1C,CAAC;IACD,eAAe,CAAC,KAAK,IAAI,CAAC,CAAC;AAC7B,CAAC"}
|
package/dist/probe.d.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operations performed against objects the library did not create.
|
|
3
|
+
*
|
|
4
|
+
* A redaction walk has to ask an arbitrary object a series of questions: is
|
|
5
|
+
* this a `serialize()` result, is this a stream, what is your tag, what is your
|
|
6
|
+
* prototype, do you have a `toJSON`, what are your own properties. None of
|
|
7
|
+
* those is universally safe. Every one of them runs through a hook the object
|
|
8
|
+
* controls, and an object is free to throw from any of them:
|
|
9
|
+
*
|
|
10
|
+
* | The walk does | The object controls it through |
|
|
11
|
+
* |---|---|
|
|
12
|
+
* | `obj.name` | a `get` trap, or a getter anywhere on the prototype chain |
|
|
13
|
+
* | `Object.prototype.toString.call(obj)` | a `Symbol.toStringTag` getter, reached the same way |
|
|
14
|
+
* | `Object.getPrototypeOf(obj)` | a `getPrototypeOf` trap |
|
|
15
|
+
* | `'toJSON' in obj` | a `has` trap |
|
|
16
|
+
* | `obj.toJSON()` | the method body |
|
|
17
|
+
* | `Object.keys(obj)` / `Object.entries(obj)` | `ownKeys`, `getOwnPropertyDescriptor`, and every enumerable getter |
|
|
18
|
+
* | `String(obj)` | a `toString` or `Symbol.toPrimitive` |
|
|
19
|
+
* | `for (const entry of map)`, `array.map()`, `date.getTime()` | a Proxy in front of the built-in, whose target alone has the internal slot, or a subclass iterator |
|
|
20
|
+
* | `error.message` | a getter on the instance |
|
|
21
|
+
*
|
|
22
|
+
* The one that matters most in practice is not a Proxy at all. An ordinary
|
|
23
|
+
* object with an enumerable getter that throws is what an ORM produces for a
|
|
24
|
+
* relation that was not loaded, and `Object.entries` invokes every one of them.
|
|
25
|
+
* The Proxy cases are real too: envalid's `cleanEnv` result throws a
|
|
26
|
+
* `ReferenceError` for any variable that was not declared, which is what first
|
|
27
|
+
* exposed this class of defect.
|
|
28
|
+
*
|
|
29
|
+
* The rule these functions encode is that **failing to read is not a reason to
|
|
30
|
+
* fail the redaction**. A logger calling `safeLog` must not be the thing that
|
|
31
|
+
* takes down the request, and an object that refuses to answer must not be able
|
|
32
|
+
* to turn a redaction into an exception. Each function below answers with the
|
|
33
|
+
* safe interpretation of a refusal, and each caller documents what it does with
|
|
34
|
+
* that answer.
|
|
35
|
+
*/
|
|
36
|
+
/** What a walk substitutes for an object it could not read at all. */
|
|
37
|
+
export declare const UNREADABLE = "[Unreadable]";
|
|
38
|
+
/**
|
|
39
|
+
* Read a named property, treating a refusal as absence.
|
|
40
|
+
*
|
|
41
|
+
* Reporting absence is the safe answer for every caller of *this* function. It
|
|
42
|
+
* never decides whether a value is redacted: it answers duck-type questions
|
|
43
|
+
* that decide whether a walk replaces an object wholesale or descends into it,
|
|
44
|
+
* and descending still redacts every `SensitiveString` inside. So an object
|
|
45
|
+
* that refuses to answer one property is walked like any other rather than
|
|
46
|
+
* failing the whole redaction.
|
|
47
|
+
*
|
|
48
|
+
* `probeEntries` is the one below that does decide, and it says so.
|
|
49
|
+
*/
|
|
50
|
+
export declare function probeProperty(value: object, name: string): unknown;
|
|
51
|
+
/**
|
|
52
|
+
* The object's `Object.prototype.toString` tag.
|
|
53
|
+
*
|
|
54
|
+
* A refusal answers `'[object Object]'`, which routes the caller into the
|
|
55
|
+
* ordinary-object branch and therefore into descending and redacting. The other
|
|
56
|
+
* branch is `String(obj)`, which would ask the same uncooperative object
|
|
57
|
+
* another question and hide anything it holds behind one string.
|
|
58
|
+
*/
|
|
59
|
+
export declare function probeTag(value: object): string;
|
|
60
|
+
/**
|
|
61
|
+
* The object's prototype, or `undefined` when it refuses to say.
|
|
62
|
+
*
|
|
63
|
+
* `undefined` is distinct from `null`: a null-prototype object answers `null`
|
|
64
|
+
* and is walked as a plain object, whereas one that refuses is not claimed to
|
|
65
|
+
* be anything. The callers treat the refusal as "not `Object.prototype`", which
|
|
66
|
+
* sends it to the tag check, which sends it on to be walked.
|
|
67
|
+
*/
|
|
68
|
+
export declare function probePrototype(value: object): object | null | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Whether the object offers a callable method under this name.
|
|
71
|
+
*
|
|
72
|
+
* Covers both hooks in one place: `in` goes through a `has` trap and the read
|
|
73
|
+
* goes through a `get` trap. A refusal from either answers `false`, so the
|
|
74
|
+
* caller walks own properties instead of trusting a method it cannot reach.
|
|
75
|
+
*/
|
|
76
|
+
export declare function probeMethod(value: object, name: string): (() => unknown) | undefined;
|
|
77
|
+
/** What a guarded call returned, and whether it returned at all. */
|
|
78
|
+
export interface ProbeResult {
|
|
79
|
+
readonly ok: boolean;
|
|
80
|
+
readonly value: unknown;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Call a method the object supplied, treating a throw as no answer.
|
|
84
|
+
*
|
|
85
|
+
* `toJSON` is caller code and is entitled to throw. What it is not entitled to
|
|
86
|
+
* do is turn a log line into an exception, so a throw here means the walk falls
|
|
87
|
+
* back to the object's own properties, which is the same path an object with no
|
|
88
|
+
* `toJSON` takes.
|
|
89
|
+
*/
|
|
90
|
+
export declare function probeCall(method: () => unknown, thisArg: object): ProbeResult;
|
|
91
|
+
/**
|
|
92
|
+
* Run one operation a built-in performs on the object, treating a throw as no
|
|
93
|
+
* answer.
|
|
94
|
+
*
|
|
95
|
+
* Iterating a Map or a Set, mapping an array, reading a Date's time: each is
|
|
96
|
+
* the object's own code once a Proxy stands in front of it. `Map.prototype.entries`
|
|
97
|
+
* refuses a proxy as its receiver, an array proxy may refuse `length` or an
|
|
98
|
+
* index, and a Date proxy throws from every method, the `toJSON()` a serializer
|
|
99
|
+
* calls later included. A partial result would claim to be the whole, so the
|
|
100
|
+
* caller gets none, and substitutes {@link UNREADABLE} or its own safe answer.
|
|
101
|
+
*/
|
|
102
|
+
export declare function probeBuiltin<T>(operation: () => T): T | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* The object's own enumerable properties, or `undefined` when it cannot be
|
|
105
|
+
* enumerated at all.
|
|
106
|
+
*
|
|
107
|
+
* Two failures are separated here, because they deserve different answers.
|
|
108
|
+
*
|
|
109
|
+
* A refusal to list the keys is total: `ownKeys` or `getOwnPropertyDescriptor`
|
|
110
|
+
* threw, and nothing can be said about the object's contents. That answers
|
|
111
|
+
* `undefined`, and each caller decides what an unreadable object becomes.
|
|
112
|
+
*
|
|
113
|
+
* A refusal to read *one* property is not: the other keys are still readable,
|
|
114
|
+
* and the property that threw reads as `undefined`. That is why the values are
|
|
115
|
+
* read one at a time through `probeProperty` rather than taken from
|
|
116
|
+
* `Object.entries`, which invokes every enumerable getter and propagates the
|
|
117
|
+
* first throw. A getter that throws is the common case, not the exotic one.
|
|
118
|
+
*/
|
|
119
|
+
export declare function probeEntries(value: object): [string, unknown][] | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Whether the object will answer any question at all.
|
|
122
|
+
*
|
|
123
|
+
* `instanceof` walks the prototype chain, so an object with a `getPrototypeOf`
|
|
124
|
+
* trap that throws makes every `instanceof` in a walk throw, before any of the
|
|
125
|
+
* guards below get a chance to run. There are around thirty such checks across
|
|
126
|
+
* the two walks, and guarding each one would be thirty places to get wrong;
|
|
127
|
+
* asking once, at the top, is the same protection in one place.
|
|
128
|
+
*
|
|
129
|
+
* An object that will not name its prototype is unwalkable by construction, so
|
|
130
|
+
* each walk substitutes its own unreadable answer and stops.
|
|
131
|
+
*/
|
|
132
|
+
export declare function isUnreadable(value: object): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* The object's string form, or `undefined` when `toString` refuses.
|
|
135
|
+
*
|
|
136
|
+
* Only reached for exotic objects, where the string form is the whole of what
|
|
137
|
+
* the walk keeps. A refusal leaves the caller with nothing to keep, so it
|
|
138
|
+
* substitutes {@link UNREADABLE}.
|
|
139
|
+
*/
|
|
140
|
+
export declare function probeString(value: object): string | undefined;
|
|
141
|
+
//# sourceMappingURL=probe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"probe.d.ts","sourceRoot":"","sources":["../src/probe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,sEAAsE;AACtE,eAAO,MAAM,UAAU,iBAAiB,CAAC;AAEzC;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAMlE;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAM9C;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAMvE;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,MAAM,OAAO,CAAC,GAAG,SAAS,CAYpF;AAED,oEAAoE;AACpE,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAM7E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,SAAS,CAMjE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,SAAS,CAU3E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEnD;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAO7D"}
|
package/dist/probe.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Operations performed against objects the library did not create.
|
|
4
|
+
*
|
|
5
|
+
* A redaction walk has to ask an arbitrary object a series of questions: is
|
|
6
|
+
* this a `serialize()` result, is this a stream, what is your tag, what is your
|
|
7
|
+
* prototype, do you have a `toJSON`, what are your own properties. None of
|
|
8
|
+
* those is universally safe. Every one of them runs through a hook the object
|
|
9
|
+
* controls, and an object is free to throw from any of them:
|
|
10
|
+
*
|
|
11
|
+
* | The walk does | The object controls it through |
|
|
12
|
+
* |---|---|
|
|
13
|
+
* | `obj.name` | a `get` trap, or a getter anywhere on the prototype chain |
|
|
14
|
+
* | `Object.prototype.toString.call(obj)` | a `Symbol.toStringTag` getter, reached the same way |
|
|
15
|
+
* | `Object.getPrototypeOf(obj)` | a `getPrototypeOf` trap |
|
|
16
|
+
* | `'toJSON' in obj` | a `has` trap |
|
|
17
|
+
* | `obj.toJSON()` | the method body |
|
|
18
|
+
* | `Object.keys(obj)` / `Object.entries(obj)` | `ownKeys`, `getOwnPropertyDescriptor`, and every enumerable getter |
|
|
19
|
+
* | `String(obj)` | a `toString` or `Symbol.toPrimitive` |
|
|
20
|
+
* | `for (const entry of map)`, `array.map()`, `date.getTime()` | a Proxy in front of the built-in, whose target alone has the internal slot, or a subclass iterator |
|
|
21
|
+
* | `error.message` | a getter on the instance |
|
|
22
|
+
*
|
|
23
|
+
* The one that matters most in practice is not a Proxy at all. An ordinary
|
|
24
|
+
* object with an enumerable getter that throws is what an ORM produces for a
|
|
25
|
+
* relation that was not loaded, and `Object.entries` invokes every one of them.
|
|
26
|
+
* The Proxy cases are real too: envalid's `cleanEnv` result throws a
|
|
27
|
+
* `ReferenceError` for any variable that was not declared, which is what first
|
|
28
|
+
* exposed this class of defect.
|
|
29
|
+
*
|
|
30
|
+
* The rule these functions encode is that **failing to read is not a reason to
|
|
31
|
+
* fail the redaction**. A logger calling `safeLog` must not be the thing that
|
|
32
|
+
* takes down the request, and an object that refuses to answer must not be able
|
|
33
|
+
* to turn a redaction into an exception. Each function below answers with the
|
|
34
|
+
* safe interpretation of a refusal, and each caller documents what it does with
|
|
35
|
+
* that answer.
|
|
36
|
+
*/
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.UNREADABLE = void 0;
|
|
39
|
+
exports.probeProperty = probeProperty;
|
|
40
|
+
exports.probeTag = probeTag;
|
|
41
|
+
exports.probePrototype = probePrototype;
|
|
42
|
+
exports.probeMethod = probeMethod;
|
|
43
|
+
exports.probeCall = probeCall;
|
|
44
|
+
exports.probeBuiltin = probeBuiltin;
|
|
45
|
+
exports.probeEntries = probeEntries;
|
|
46
|
+
exports.isUnreadable = isUnreadable;
|
|
47
|
+
exports.probeString = probeString;
|
|
48
|
+
/** What a walk substitutes for an object it could not read at all. */
|
|
49
|
+
exports.UNREADABLE = '[Unreadable]';
|
|
50
|
+
/**
|
|
51
|
+
* Read a named property, treating a refusal as absence.
|
|
52
|
+
*
|
|
53
|
+
* Reporting absence is the safe answer for every caller of *this* function. It
|
|
54
|
+
* never decides whether a value is redacted: it answers duck-type questions
|
|
55
|
+
* that decide whether a walk replaces an object wholesale or descends into it,
|
|
56
|
+
* and descending still redacts every `SensitiveString` inside. So an object
|
|
57
|
+
* that refuses to answer one property is walked like any other rather than
|
|
58
|
+
* failing the whole redaction.
|
|
59
|
+
*
|
|
60
|
+
* `probeEntries` is the one below that does decide, and it says so.
|
|
61
|
+
*/
|
|
62
|
+
function probeProperty(value, name) {
|
|
63
|
+
try {
|
|
64
|
+
return value[name];
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The object's `Object.prototype.toString` tag.
|
|
72
|
+
*
|
|
73
|
+
* A refusal answers `'[object Object]'`, which routes the caller into the
|
|
74
|
+
* ordinary-object branch and therefore into descending and redacting. The other
|
|
75
|
+
* branch is `String(obj)`, which would ask the same uncooperative object
|
|
76
|
+
* another question and hide anything it holds behind one string.
|
|
77
|
+
*/
|
|
78
|
+
function probeTag(value) {
|
|
79
|
+
try {
|
|
80
|
+
return Object.prototype.toString.call(value);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return '[object Object]';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The object's prototype, or `undefined` when it refuses to say.
|
|
88
|
+
*
|
|
89
|
+
* `undefined` is distinct from `null`: a null-prototype object answers `null`
|
|
90
|
+
* and is walked as a plain object, whereas one that refuses is not claimed to
|
|
91
|
+
* be anything. The callers treat the refusal as "not `Object.prototype`", which
|
|
92
|
+
* sends it to the tag check, which sends it on to be walked.
|
|
93
|
+
*/
|
|
94
|
+
function probePrototype(value) {
|
|
95
|
+
try {
|
|
96
|
+
return Object.getPrototypeOf(value);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Whether the object offers a callable method under this name.
|
|
104
|
+
*
|
|
105
|
+
* Covers both hooks in one place: `in` goes through a `has` trap and the read
|
|
106
|
+
* goes through a `get` trap. A refusal from either answers `false`, so the
|
|
107
|
+
* caller walks own properties instead of trusting a method it cannot reach.
|
|
108
|
+
*/
|
|
109
|
+
function probeMethod(value, name) {
|
|
110
|
+
try {
|
|
111
|
+
if (!(name in value)) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
const method = probeProperty(value, name);
|
|
119
|
+
return typeof method === 'function' ? method : undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Call a method the object supplied, treating a throw as no answer.
|
|
123
|
+
*
|
|
124
|
+
* `toJSON` is caller code and is entitled to throw. What it is not entitled to
|
|
125
|
+
* do is turn a log line into an exception, so a throw here means the walk falls
|
|
126
|
+
* back to the object's own properties, which is the same path an object with no
|
|
127
|
+
* `toJSON` takes.
|
|
128
|
+
*/
|
|
129
|
+
function probeCall(method, thisArg) {
|
|
130
|
+
try {
|
|
131
|
+
return { ok: true, value: method.call(thisArg) };
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return { ok: false, value: undefined };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Run one operation a built-in performs on the object, treating a throw as no
|
|
139
|
+
* answer.
|
|
140
|
+
*
|
|
141
|
+
* Iterating a Map or a Set, mapping an array, reading a Date's time: each is
|
|
142
|
+
* the object's own code once a Proxy stands in front of it. `Map.prototype.entries`
|
|
143
|
+
* refuses a proxy as its receiver, an array proxy may refuse `length` or an
|
|
144
|
+
* index, and a Date proxy throws from every method, the `toJSON()` a serializer
|
|
145
|
+
* calls later included. A partial result would claim to be the whole, so the
|
|
146
|
+
* caller gets none, and substitutes {@link UNREADABLE} or its own safe answer.
|
|
147
|
+
*/
|
|
148
|
+
function probeBuiltin(operation) {
|
|
149
|
+
try {
|
|
150
|
+
return operation();
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The object's own enumerable properties, or `undefined` when it cannot be
|
|
158
|
+
* enumerated at all.
|
|
159
|
+
*
|
|
160
|
+
* Two failures are separated here, because they deserve different answers.
|
|
161
|
+
*
|
|
162
|
+
* A refusal to list the keys is total: `ownKeys` or `getOwnPropertyDescriptor`
|
|
163
|
+
* threw, and nothing can be said about the object's contents. That answers
|
|
164
|
+
* `undefined`, and each caller decides what an unreadable object becomes.
|
|
165
|
+
*
|
|
166
|
+
* A refusal to read *one* property is not: the other keys are still readable,
|
|
167
|
+
* and the property that threw reads as `undefined`. That is why the values are
|
|
168
|
+
* read one at a time through `probeProperty` rather than taken from
|
|
169
|
+
* `Object.entries`, which invokes every enumerable getter and propagates the
|
|
170
|
+
* first throw. A getter that throws is the common case, not the exotic one.
|
|
171
|
+
*/
|
|
172
|
+
function probeEntries(value) {
|
|
173
|
+
let keys;
|
|
174
|
+
try {
|
|
175
|
+
keys = Object.keys(value);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
return keys.map((key) => [key, probeProperty(value, key)]);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Whether the object will answer any question at all.
|
|
184
|
+
*
|
|
185
|
+
* `instanceof` walks the prototype chain, so an object with a `getPrototypeOf`
|
|
186
|
+
* trap that throws makes every `instanceof` in a walk throw, before any of the
|
|
187
|
+
* guards below get a chance to run. There are around thirty such checks across
|
|
188
|
+
* the two walks, and guarding each one would be thirty places to get wrong;
|
|
189
|
+
* asking once, at the top, is the same protection in one place.
|
|
190
|
+
*
|
|
191
|
+
* An object that will not name its prototype is unwalkable by construction, so
|
|
192
|
+
* each walk substitutes its own unreadable answer and stops.
|
|
193
|
+
*/
|
|
194
|
+
function isUnreadable(value) {
|
|
195
|
+
return probePrototype(value) === undefined;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The object's string form, or `undefined` when `toString` refuses.
|
|
199
|
+
*
|
|
200
|
+
* Only reached for exotic objects, where the string form is the whole of what
|
|
201
|
+
* the walk keeps. A refusal leaves the caller with nothing to keep, so it
|
|
202
|
+
* substitutes {@link UNREADABLE}.
|
|
203
|
+
*/
|
|
204
|
+
function probeString(value) {
|
|
205
|
+
try {
|
|
206
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string -- the caller has already established this object is exotic and carries its own toString
|
|
207
|
+
return String(value);
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=probe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"probe.js","sourceRoot":"","sources":["../src/probe.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;;;AAiBH,sCAMC;AAUD,4BAMC;AAUD,wCAMC;AASD,kCAYC;AAgBD,8BAMC;AAaD,oCAMC;AAkBD,oCAUC;AAcD,oCAEC;AASD,kCAOC;AA/KD,sEAAsE;AACzD,QAAA,UAAU,GAAG,cAAc,CAAC;AAEzC;;;;;;;;;;;GAWG;AACH,SAAgB,aAAa,CAAC,KAAa,EAAE,IAAY;IACvD,IAAI,CAAC;QACH,OAAQ,KAAiC,CAAC,IAAI,CAAC,CAAC;IAClD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,QAAQ,CAAC,KAAa;IACpC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC/C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,iBAAiB,CAAC;IAC3B,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,cAAc,CAAC,KAAa;IAC1C,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,cAAc,CAAC,KAAK,CAAk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C,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,aAAa,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;AAC7D,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,YAAY,CAAC,KAAa;IACxC,OAAO,cAAc,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC;AAC7C,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CAAC,KAAa;IACvC,IAAI,CAAC;QACH,yJAAyJ;QACzJ,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy a structure with every `SensitiveString` and every serialized sensitive
|
|
3
|
+
* value replaced by its redaction, leaving everything else usable.
|
|
4
|
+
*
|
|
5
|
+
* This is the walk for a response body or any other value that a serializer
|
|
6
|
+
* other than `JSON.stringify` is about to see. `toJSON()` already redacts on
|
|
7
|
+
* the JSON path; what it cannot cover is a serializer that copies public
|
|
8
|
+
* properties (NestJS's `ClassSerializerInterceptor`, for one, which copies
|
|
9
|
+
* `klass` and `label` and drops the marker), and a `serialize()` result placed
|
|
10
|
+
* in a body, whose raw `value` `JSON.stringify` emits faithfully. Both are
|
|
11
|
+
* closed here: an instance becomes the class's redaction, and any object
|
|
12
|
+
* carrying the `__sensitive: true` marker becomes the redaction of the class it
|
|
13
|
+
* names, or `'[REDACTED]'` when that class is not registered.
|
|
14
|
+
*
|
|
15
|
+
* `redactInStructure` is the sibling for log lines. It flattens class
|
|
16
|
+
* instances into plain objects, replaces a repeat visit with `'[Circular]'`,
|
|
17
|
+
* and stringifies exotic objects, which is right for a log record and wrong
|
|
18
|
+
* for a body that still has to serialize as the class it was. This walk instead:
|
|
19
|
+
*
|
|
20
|
+
* - never mutates its input, since an entity may be shared with an ORM identity
|
|
21
|
+
* map or a cache;
|
|
22
|
+
* - keeps each object's prototype, so an entity `toJSON()` and a serializer's
|
|
23
|
+
* `instanceof` checks still see the class;
|
|
24
|
+
* - memoizes copies, so an object referenced twice stays one shared reference
|
|
25
|
+
* and a genuine cycle produces a cyclic copy that fails at `JSON.stringify`
|
|
26
|
+
* exactly as the original would;
|
|
27
|
+
* - passes through by reference what a property copy would break or that no
|
|
28
|
+
* serializer can walk: `Date`, `WeakMap`, `WeakSet`, `RegExp`, `URL`,
|
|
29
|
+
* `Error`, `Promise`, `ArrayBuffer` and its views (`Buffer` among them),
|
|
30
|
+
* boxed primitives, and anything with a `pipe()` method;
|
|
31
|
+
* - copies a `Map` or a `Set` entry by entry, keys included, keeping its
|
|
32
|
+
* prototype and memoizing it like an object, because either can hold a
|
|
33
|
+
* `serialize()` result and `v8.serialize`, `structuredClone`, a worker's
|
|
34
|
+
* `postMessage` and `util.inspect` all walk entries. `JSON.stringify` does
|
|
35
|
+
* not, which is why they used to pass through, and was the wrong question
|
|
36
|
+
* for a walk whose purpose is every other serializer.
|
|
37
|
+
*
|
|
38
|
+
* Only own enumerable string-keyed properties are visited, which is what
|
|
39
|
+
* `JSON.stringify` visits. A value reachable only through a prototype getter,
|
|
40
|
+
* a symbol key, or a non-enumerable property is not copied; on the JSON path it
|
|
41
|
+
* is still redacted by `toJSON()`. A key named `__proto__` is copied as the
|
|
42
|
+
* own data property it was, so an entity keeps the prototype it had rather
|
|
43
|
+
* than one the payload supplied: see `assignOwn`.
|
|
44
|
+
*
|
|
45
|
+
* @param value - The value to copy
|
|
46
|
+
* @returns A copy with every sensitive value redacted, or the value itself when
|
|
47
|
+
* it is a primitive or a pass-through object
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const body = {
|
|
52
|
+
* user: userEntity, // class instance holding SensitiveString fields
|
|
53
|
+
* token: secret.serialize(),
|
|
54
|
+
* };
|
|
55
|
+
*
|
|
56
|
+
* redactCopy(body);
|
|
57
|
+
* // { user: UserEntity { ..., apiKey: '[REDACTED:credential]' }, token: '[REDACTED:credential]' }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function redactCopy(value: unknown): unknown;
|
|
61
|
+
//# sourceMappingURL=redact-copy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redact-copy.d.ts","sourceRoot":"","sources":["../src/redact-copy.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAElD"}
|