@hackylabs/deep-redact 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -27
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/utils/redactorUtils.js +23 -2
- package/dist/esm/index.mjs +18 -0
- package/dist/esm/utils/{redactorUtils.js → redactorUtils.mjs} +23 -2
- package/dist/types/types.d.ts +4 -1
- package/package.json +25 -14
package/README.md
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
# Deep Redact
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://badge.fury.io/js/@hackylabs%2Fdeep-redact)
|
|
4
|
+
[](https://github.com/hackylabs/deep-redact/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
Faster than Fast Redact <sup>1</sup> as well as being safer and more configurable than many other redaction solutions,
|
|
4
7
|
Deep Redact is a zero-dependency tool that redacts sensitive information from strings and objects. It is designed to be
|
|
5
8
|
used in a production environment where sensitive information needs to be redacted from logs, error messages, files,
|
|
6
9
|
and other outputs.
|
|
7
10
|
|
|
8
11
|
Circular references and other unsupported values are handled gracefully, and the library is designed to be as fast as
|
|
9
|
-
possible while still being
|
|
12
|
+
possible while still being easy to use and configure.
|
|
10
13
|
|
|
11
14
|
Supporting both CommonJS and ESM, with named and default exports, Deep Redact is designed to be versatile and easy to
|
|
12
15
|
use in any modern JavaScript or TypeScript project in Node or the browser.
|
|
13
16
|
|
|
17
|
+
[](https://ko-fi.com/hackylabs)
|
|
18
|
+
|
|
14
19
|
## Installation
|
|
15
20
|
|
|
16
21
|
```bash
|
|
@@ -26,9 +31,8 @@ library outside of your global logging/error-reporting libraries.</h4>
|
|
|
26
31
|
// ./src/example.ts
|
|
27
32
|
import {DeepRedact} from '@hackylabs/deep-redact'; // If you're using CommonJS, import with require('@hackylabs/deep-redact') instead. Both CommonJS and ESM support named and default imports.
|
|
28
33
|
|
|
29
|
-
const
|
|
34
|
+
const objRedaction = new DeepRedact({
|
|
30
35
|
blacklistedKeys: ['sensitive', 'password', /name/i],
|
|
31
|
-
serialise: false,
|
|
32
36
|
})
|
|
33
37
|
|
|
34
38
|
const obj = {
|
|
@@ -41,7 +45,8 @@ const obj = {
|
|
|
41
45
|
}
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
|
|
48
|
+
// Recursively redact sensitive information from an object
|
|
49
|
+
objRedaction.redact(obj)
|
|
45
50
|
// {
|
|
46
51
|
// keepThis: 'This is fine',
|
|
47
52
|
// sensitive: '[REDACTED]',
|
|
@@ -51,6 +56,19 @@ redaction.redact(obj)
|
|
|
51
56
|
// firstName: '[REDACTED]'
|
|
52
57
|
// }
|
|
53
58
|
// }
|
|
59
|
+
|
|
60
|
+
const strRedaction = new DeepRedact({
|
|
61
|
+
stringTests: [
|
|
62
|
+
{
|
|
63
|
+
pattern: /<(email|password)>([^<]+)<\/\1>/gi,
|
|
64
|
+
replacer: (value: string, pattern: RegExp) => value.replace(pattern, '<$1>[REDACTED]</$1>'),
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
// Partially redact sensitive information from a string
|
|
70
|
+
strRedaction.redact('<email>someone@somewhere.com</email><keepThis>This is fine</keepThis><password>secret</password>')
|
|
71
|
+
// '<email>[REDACTED]</email><keepThis>This is fine</keepThis><password>[REDACTED]</password>'
|
|
54
72
|
```
|
|
55
73
|
|
|
56
74
|
## Configuration
|
|
@@ -60,7 +78,7 @@ redaction.redact(obj)
|
|
|
60
78
|
| key | description | type | options | default | required |
|
|
61
79
|
| --- | --- | --- | --- | --- | --- |
|
|
62
80
|
| blacklistedKeys | Deeply compare names of these keys against the keys in your object. | array | Array<string│RegExp│BlacklistKeyConfig> | [] | N |
|
|
63
|
-
| stringTests | Array of regular expressions to perform against string values, whether that value is a flat string or nested within an object. | array | RegExp
|
|
81
|
+
| stringTests | Array of regular expressions to perform against string values, whether that value is a flat string or nested within an object. | array | Array<RegExp│StringTestConfig> | [] | N |
|
|
64
82
|
| fuzzyKeyMatch | Loosely compare key names by checking if the key name of your unredacted object is included anywhere within the name of your blacklisted key. For example, is "pass" (your key) included in "password" (from config). | boolean | | false | N |
|
|
65
83
|
| caseSensitiveKeyMatch | Loosely compare key names by normalising the strings. This involves removing non-word characters and transforms the string to lowercase. This means you never have to worry having to list duplicate keys in different formats such as snake_case, camelCase, PascalCase or any other case. | boolean | | true | N |
|
|
66
84
|
| remove | Determines whether or not to remove the key from the object when it is redacted. | boolean | | false | N |
|
|
@@ -81,32 +99,54 @@ redaction.redact(obj)
|
|
|
81
99
|
| remove | boolean | Main options `remove` | N |
|
|
82
100
|
| retainStructure | boolean | Main options `retainStructure` | N |
|
|
83
101
|
|
|
102
|
+
### StringTestConfig
|
|
103
|
+
|
|
104
|
+
| key | description | type | required |
|
|
105
|
+
| --- | --- | --- | --- |
|
|
106
|
+
| pattern | A regular expression to perform against a string value, whether that value is a flat string or nested within an object. | RegExp | Y |
|
|
107
|
+
| replacer | A function that will be called with the value of the string that matched the pattern and the pattern itself. This function should return the new (redacted) value to replace the original value. | function | Y |
|
|
108
|
+
|
|
84
109
|
### Benchmark
|
|
85
|
-
Comparisons are made against JSON.stringify
|
|
86
|
-
|
|
87
|
-
Deep Redact
|
|
110
|
+
Comparisons are made against JSON.stringify, Regex.replace, Fast Redact &
|
|
111
|
+
(one of my other creations, [@hackylabs/obglob](https://npmjs.com/package/@hackylabs/obglob)) as well as different
|
|
112
|
+
configurations of Deep Redact, using [this test object](./test/setup/dummyUser.ts). Fast Redact was configured to redact
|
|
113
|
+
the same keys on the same object as Deep Redact without using wildcards.
|
|
114
|
+
|
|
115
|
+
The benchmark is run on a 2021 iMac with an M1 chip with 16GB memory running macOS Sequoia 15.0.0.
|
|
116
|
+
|
|
117
|
+
JSON.stringify is included as a benchmark because it is the fastest way to deeply iterate over an object, although it
|
|
118
|
+
doesn't redact any sensitive information.
|
|
119
|
+
|
|
120
|
+
Regex.replace is included as a benchmark because it is the fastest way to redact sensitive information from a string.
|
|
121
|
+
However, a regex pattern for all keys to be redacted is much harder to configure than a dedicated redaction library,
|
|
122
|
+
especially when dealing with multiple types of values. It also doesn't handle circular references or other unsupported
|
|
123
|
+
values as gracefully as deep-redact unless a third-party library is used to stringify the object beforehand.
|
|
88
124
|
|
|
89
|
-
|
|
125
|
+
Fast-redact is included as a benchmark because it's the next fastest library available specifically for redaction.
|
|
90
126
|
|
|
91
|
-
JSON.stringify
|
|
92
|
-
|
|
93
|
-
library available. Neither JSON.stringify nor Fast Redact offer the same level of configurability as deep-redact.
|
|
127
|
+
Neither JSON.stringify, Regex.replace nor Fast Redact offer the same level of configurability as deep-redact. Both Fast
|
|
128
|
+
Redact and Obglob are slower and rely on dependencies.
|
|
94
129
|
|
|
95
130
|

|
|
96
131
|
|
|
97
132
|
| scenario | ops / sec | op duration (ms) | margin of error | sample count |
|
|
98
133
|
| --- | --- | --- | --- | --- |
|
|
99
|
-
|
|
|
100
|
-
|
|
|
101
|
-
| DeepRedact,
|
|
102
|
-
|
|
|
103
|
-
| DeepRedact,
|
|
104
|
-
| DeepRedact,
|
|
105
|
-
| DeepRedact,
|
|
106
|
-
| DeepRedact,
|
|
107
|
-
|
|
|
108
|
-
| DeepRedact,
|
|
109
|
-
| DeepRedact, default config, 1000 large objects |
|
|
110
|
-
|
|
|
111
|
-
|
|
|
112
|
-
|
|
|
134
|
+
| DeepRedact, XML | 171985.01 | 0.0058144601 | 0.00004 | 85993 |
|
|
135
|
+
| JSON.stringify, large object | 162406.59 | 0.0061573855 | 0.00002 | 81204 |
|
|
136
|
+
| DeepRedact, remove item, single object | 25293.55 | 0.039535775 | 0.00023 | 12647 |
|
|
137
|
+
| Regex replace, large object | 22529.52 | 0.0443862153 | 0.00024 | 11265 |
|
|
138
|
+
| DeepRedact, custom replacer function, single object | 22324.84 | 0.0447931554 | 0.00037 | 11163 |
|
|
139
|
+
| DeepRedact, default config, large object | 21437.04 | 0.046648239 | 0.00028 | 10719 |
|
|
140
|
+
| DeepRedact, replace string by length, single object | 21018.32 | 0.0475775519 | 0.00084 | 10510 |
|
|
141
|
+
| DeepRedact, fuzzy matching, single object | 18000.38 | 0.0555543858 | 0.0003 | 9001 |
|
|
142
|
+
| DeepRedact, retain structure, single object | 17581.5 | 0.056877956 | 0.00037 | 8791 |
|
|
143
|
+
| DeepRedact, config per key, single object | 16914.73 | 0.0591200552 | 0.0004 | 8458 |
|
|
144
|
+
| DeepRedact, default config, 1000 large objects | 7989.05 | 0.1251712531 | 0.0018 | 3995 |
|
|
145
|
+
| fast redact, large object | 5929.58 | 0.1686459096 | 0.00126 | 2965 |
|
|
146
|
+
| ObGlob, large object | 4939.98 | 0.2024300664 | 0.01105 | 2470 |
|
|
147
|
+
| DeepRedact, case insensitive matching, single object | 4721.59 | 0.2117932541 | 0.00378 | 2361 |
|
|
148
|
+
| DeepRedact, fuzzy and case insensitive matching, single object | 4686.29 | 0.2133882948 | 0.0018 | 2344 |
|
|
149
|
+
| JSON.stringify, 1000 large objects | 222.05 | 4.5035912679 | 0.04553 | 112 |
|
|
150
|
+
| ObGlob, 1000 large objects | 164.55 | 6.0771323614 | 0.11829 | 83 |
|
|
151
|
+
| fast redact, 1000 large objects | 120.7 | 8.2848202295 | 0.05702 | 61 |
|
|
152
|
+
| Regex replace, 1000 large objects | 93.5 | 10.6954525319 | 0.39014 | 47 |
|
package/dist/cjs/index.js
CHANGED
|
@@ -77,6 +77,24 @@ class DeepRedact {
|
|
|
77
77
|
},
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
|
+
if (value instanceof Set) {
|
|
81
|
+
return {
|
|
82
|
+
__unsupported: {
|
|
83
|
+
type: 'set',
|
|
84
|
+
values: Array.from(value),
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (value instanceof Map) {
|
|
89
|
+
return {
|
|
90
|
+
__unsupported: {
|
|
91
|
+
type: 'map',
|
|
92
|
+
entries: Object.fromEntries(value.entries()),
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (value instanceof URL)
|
|
97
|
+
return value.toString();
|
|
80
98
|
if (value instanceof Date)
|
|
81
99
|
return value.toISOString();
|
|
82
100
|
return value;
|
|
@@ -80,11 +80,32 @@ class RedactorUtils {
|
|
|
80
80
|
* @param shouldRedact
|
|
81
81
|
*/
|
|
82
82
|
this.redactString = (value, replacement, remove, shouldRedact) => {
|
|
83
|
-
if (!value)
|
|
83
|
+
if (!value || typeof value !== 'string')
|
|
84
84
|
return value;
|
|
85
85
|
const { stringTests } = this.config;
|
|
86
|
-
if (!shouldRedact
|
|
86
|
+
if (!shouldRedact) {
|
|
87
|
+
const result = stringTests === null || stringTests === void 0 ? void 0 : stringTests.map((test) => {
|
|
88
|
+
if (test instanceof RegExp) {
|
|
89
|
+
if (!test.test(value))
|
|
90
|
+
return value;
|
|
91
|
+
if (remove)
|
|
92
|
+
return undefined;
|
|
93
|
+
if (typeof replacement === 'function')
|
|
94
|
+
return replacement(value);
|
|
95
|
+
if (this.config.replaceStringByLength)
|
|
96
|
+
return replacement.repeat(value.length);
|
|
97
|
+
return replacement;
|
|
98
|
+
}
|
|
99
|
+
if (remove && test.pattern.test(value))
|
|
100
|
+
return undefined;
|
|
101
|
+
return test.replacer(value, test.pattern);
|
|
102
|
+
}).filter(Boolean)[0];
|
|
103
|
+
if (result)
|
|
104
|
+
return result;
|
|
105
|
+
if (remove)
|
|
106
|
+
return undefined;
|
|
87
107
|
return value;
|
|
108
|
+
}
|
|
88
109
|
if (remove)
|
|
89
110
|
return undefined;
|
|
90
111
|
if (typeof replacement === 'function')
|
package/dist/esm/index.mjs
CHANGED
|
@@ -72,6 +72,24 @@ class DeepRedact {
|
|
|
72
72
|
},
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
|
+
if (value instanceof Set) {
|
|
76
|
+
return {
|
|
77
|
+
__unsupported: {
|
|
78
|
+
type: 'set',
|
|
79
|
+
values: Array.from(value),
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (value instanceof Map) {
|
|
84
|
+
return {
|
|
85
|
+
__unsupported: {
|
|
86
|
+
type: 'map',
|
|
87
|
+
entries: Object.fromEntries(value.entries()),
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (value instanceof URL)
|
|
92
|
+
return value.toString();
|
|
75
93
|
if (value instanceof Date)
|
|
76
94
|
return value.toISOString();
|
|
77
95
|
return value;
|
|
@@ -131,11 +131,32 @@ class RedactorUtils {
|
|
|
131
131
|
* @param shouldRedact
|
|
132
132
|
*/
|
|
133
133
|
redactString = (value, replacement, remove, shouldRedact) => {
|
|
134
|
-
if (!value)
|
|
134
|
+
if (!value || typeof value !== 'string')
|
|
135
135
|
return value;
|
|
136
136
|
const { stringTests } = this.config;
|
|
137
|
-
if (!shouldRedact
|
|
137
|
+
if (!shouldRedact) {
|
|
138
|
+
const result = stringTests?.map((test) => {
|
|
139
|
+
if (test instanceof RegExp) {
|
|
140
|
+
if (!test.test(value))
|
|
141
|
+
return value;
|
|
142
|
+
if (remove)
|
|
143
|
+
return undefined;
|
|
144
|
+
if (typeof replacement === 'function')
|
|
145
|
+
return replacement(value);
|
|
146
|
+
if (this.config.replaceStringByLength)
|
|
147
|
+
return replacement.repeat(value.length);
|
|
148
|
+
return replacement;
|
|
149
|
+
}
|
|
150
|
+
if (remove && test.pattern.test(value))
|
|
151
|
+
return undefined;
|
|
152
|
+
return test.replacer(value, test.pattern);
|
|
153
|
+
}).filter(Boolean)[0];
|
|
154
|
+
if (result)
|
|
155
|
+
return result;
|
|
156
|
+
if (remove)
|
|
157
|
+
return undefined;
|
|
138
158
|
return value;
|
|
159
|
+
}
|
|
139
160
|
if (remove)
|
|
140
161
|
return undefined;
|
|
141
162
|
if (typeof replacement === 'function')
|
package/dist/types/types.d.ts
CHANGED
|
@@ -58,7 +58,10 @@ export interface BaseDeepRedactConfig {
|
|
|
58
58
|
* /^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/, // redact any string that looks like an IP address.
|
|
59
59
|
* ]
|
|
60
60
|
*/
|
|
61
|
-
stringTests?: RegExp
|
|
61
|
+
stringTests?: Array<RegExp | {
|
|
62
|
+
pattern: RegExp;
|
|
63
|
+
replacer: (value: string, pattern: RegExp) => string;
|
|
64
|
+
}>;
|
|
62
65
|
/**
|
|
63
66
|
* Perform a fuzzy match on the key. This will match any key that contains the string, rather than a case-sensitive match.
|
|
64
67
|
* @default false
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackylabs/deep-redact",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A fast, safe and configurable zero-dependency library for redacting strings or deeply redacting arrays and objects.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,7 +9,26 @@
|
|
|
9
9
|
"types": "./dist/types/index.d.ts",
|
|
10
10
|
"main": "./dist/cjs/index.js",
|
|
11
11
|
"module": "./dist/esm/index.mjs",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/esm/index.mjs",
|
|
15
|
+
"require": "./dist/cjs/index.js",
|
|
16
|
+
"types": "./dist/types/index.d.ts"
|
|
17
|
+
},
|
|
18
|
+
"./utils/redactorUtils": {
|
|
19
|
+
"import": "./dist/esm/utils/redactorUtils.mjs",
|
|
20
|
+
"require": "./dist/cjs/utils/redactorUtils.js",
|
|
21
|
+
"types": "./dist/types/utils/redactorUtils.d.ts"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
12
27
|
"keywords": [
|
|
28
|
+
"json",
|
|
29
|
+
"xml",
|
|
30
|
+
"document",
|
|
31
|
+
"fast",
|
|
13
32
|
"redact",
|
|
14
33
|
"redaction",
|
|
15
34
|
"redactor",
|
|
@@ -26,19 +45,9 @@
|
|
|
26
45
|
"GDPR",
|
|
27
46
|
"PII"
|
|
28
47
|
],
|
|
29
|
-
"exports": {
|
|
30
|
-
".": {
|
|
31
|
-
"import": "./dist/esm/index.js",
|
|
32
|
-
"require": "./dist/cjs/index.js",
|
|
33
|
-
"types": "./dist/types/index.d.ts"
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
"files": [
|
|
37
|
-
"dist"
|
|
38
|
-
],
|
|
39
48
|
"repository": {
|
|
40
49
|
"type": "git",
|
|
41
|
-
"url": "https://github.com/hackylabs/deep-redact"
|
|
50
|
+
"url": "git+https://github.com/hackylabs/deep-redact"
|
|
42
51
|
},
|
|
43
52
|
"scripts": {
|
|
44
53
|
"lint": "eslint",
|
|
@@ -53,10 +62,11 @@
|
|
|
53
62
|
"update-license": "npx ts-node ./scripts/update-license.ts"
|
|
54
63
|
},
|
|
55
64
|
"//": [
|
|
56
|
-
"fast-redact installed only as
|
|
65
|
+
"fast-redact and obglob are installed only as benchmark comparisons and are not used in the library",
|
|
57
66
|
"all dependencies are for development purposes only"
|
|
58
67
|
],
|
|
59
68
|
"devDependencies": {
|
|
69
|
+
"@hackylabs/obglob": "1.1.2",
|
|
60
70
|
"@memlab/core": "1.1.34",
|
|
61
71
|
"@types/fast-redact": "3.0.4",
|
|
62
72
|
"@types/node": "20.14.12",
|
|
@@ -71,7 +81,8 @@
|
|
|
71
81
|
"eslint-plugin-promise": "7.0.0",
|
|
72
82
|
"fast-redact": "3.5.0",
|
|
73
83
|
"image-charts": "6.1.19",
|
|
74
|
-
"
|
|
84
|
+
"superjson": "2.2.1",
|
|
85
|
+
"typescript": "5.6.2",
|
|
75
86
|
"vitest": "2.0.4"
|
|
76
87
|
}
|
|
77
88
|
}
|