generated from 47ng/typescript-library-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
321 lines (298 loc) · 9.94 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import * as redactEnv from './index'
describe('redact', () => {
it('passes through untouched with no env', () => {
const env = {}
const secrets = redactEnv.build([], env)
const provided = 'foo'
const expected = 'foo'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('passes through untouched when no secrets are present', () => {
const env = {
secret: 'stuff'
}
const secrets = redactEnv.build(['secret'], env)
const provided = 'foo does not contain secrets'
const expected = 'foo does not contain secrets'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('ignores unlisted variables', () => {
const env = {
secret: 'stuff',
notSecret: 'public'
}
const secrets = redactEnv.build(['secret'], env)
const provided = 'foo contains public stuff'
const expected = 'foo contains public *'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('ignores missing variables', () => {
const env = {
foo: 'bar'
}
const secrets = redactEnv.build(['key'], env)
const provided = 'key is missing, bar is not marked as secure'
const expected = 'key is missing, bar is not marked as secure'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('does not redact booleans values as string', () => {
const env = {
a: 'true',
b: 'false'
}
const secrets = redactEnv.build(['a', 'b'], env)
const provided = '(false !== null) === true'
const expected = '(false !== null) === true'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('does not redact null values as string', () => {
const env = {
a: 'null'
}
const secrets = redactEnv.build(['a'], env)
const provided = '(false !== null) === true'
const expected = '(false !== null) === true'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('does not redact undefined env values', () => {
const env = {
a: undefined
}
const secrets = redactEnv.build(['a'], env)
const provided = 'hello, world'
const expected = 'hello, world'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts secrets recursively', () => {
const env = {
foo: 'bar',
egg: 'spam'
}
const secrets = redactEnv.build(['foo', 'egg'], env)
const provided = 'the barman ordered spam, bacon and spam at the bar'
const expected = 'the *man ordered *, bacon and * at the *'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts over multiple lines', () => {
const env = {
foo: 'bar',
egg: 'spam'
}
const secrets = redactEnv.build(['foo', 'egg'], env)
const provided = `the barman
ordered spam,
bacon and spam
at the bar
`
const expected = `the *man
ordered *,
bacon and *
at the *
`
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts escaped strings', () => {
const env = {
a: 'foo',
b: 'bar',
c: 'eg\\Wg',
d: 'sp\nam',
e: 'c:\\foo'
}
const secrets = redactEnv.build(['a', 'b', 'c', 'd', 'e'], env)
const provided = 'foo\nbar"eg\\Wg"\tsp\nam;c:\\foo'
const expected = '*\n*"*"\t*;*'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts quoted strings', () => {
const env = {
a: '"foo"',
b: '"bar"'
}
const secrets = redactEnv.build(['a', 'b'], env)
const provided = 'foo-bar-"foo"-"bar"'
const expected = 'foo-bar-*-*'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
})
describe('JSON', () => {
it('redacts entire JSON', () => {
const foo = {
key: 'secret',
foo: null,
bar: 42,
egg: ['spam']
}
const env = {
foo: JSON.stringify(foo)
}
const secrets = redactEnv.build(['foo'], env)
const provided = JSON.stringify(foo)
const expected = '*'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts string fields in JSON safely', () => {
const foo = {
key: 'secret',
foo: null,
bar: 123456,
egg: ['spam', 'secret'],
nested: {
foo: 'secret'
},
spam: false
}
const env = {
a: 'secret'
}
const secrets = redactEnv.build(['a'], env)
const provided = JSON.stringify(foo)
const expected =
'{"key":"*","foo":null,"bar":123456,"egg":["spam","*"],"nested":{"foo":"*"},"spam":false}'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts JSON string keys if matching', () => {
const foo = {
secret: 'public'
}
const env = {
a: 'secret'
}
const secrets = redactEnv.build(['a'], env)
const provided = JSON.stringify(foo)
const expected = '{"*":"public"}'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('will not redact backslashed paths in JSON', () => {
// Because they are escaped again by JSON.stringify
const env = {
a: 'c:\\foo' // windows-like path
}
const secrets = redactEnv.build(['a'], env)
const provided = JSON.stringify(env)
const expected = '{"a":"c:\\\\foo"}'
expect(provided).toEqual(expected)
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
})
describe('RegExp', () => {
it('redacts regexp special characters', () => {
const env = {
a: '(?<=.) {2,}(?=[A-Z])'
}
const secrets = redactEnv.build(['a'], env)
const provided = 'foo bar (?<=.) {2,}(?=[A-Z]) spam'
const expected = 'foo bar * spam'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
})
describe('Real-life usage', () => {
it('redacts base64 secrets', () => {
const env = {
a: 'p4A5WEvyGSwAPtKt8Nk9aLnBxPg12yjkFMhWiyz9tlc=',
b: 'xV1luhZ0Gg0Egnspt6yfoANvIV9fS+yQpiswk8vSFuU=',
c: 'nQZBwkEJsBIa4MBsKFWBVxo57PNBjNarwC4I1/eLmZc=',
d: 'hAnrr6nQ/p8XpC09SBeOe2zVOF2nZpMbZY8H+Kaj7cg='
}
const secrets = redactEnv.build(['a', 'b', 'c', 'd'], env)
const provided = JSON.stringify(env)
const expected = '{"a":"*","b":"*","c":"*","d":"*"}'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts hex secrets', () => {
const env = {
a: '096a4351d7a2f374879f12e4302105b3308079c51aaef11b28f903768ce1cba5',
b: 'a5f8013bf70683ebda27962ccb06d103d317965b5c43dfb56b25df102ae94173',
c: '8aeea251d7c8767216dcb6c755c4900d0c82827c36c1e42dc203692cd76d47a9',
d: 'e29c60fa3804d9c47556d921d2f1734cc5822df032c0b5f19008ea3347128f1e'
}
const secrets = redactEnv.build(['a', 'b', 'c', 'd'], env)
const provided = JSON.stringify(env)
const expected = '{"a":"*","b":"*","c":"*","d":"*"}'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts UUIDs', () => {
// testing for dash-separated hex
const env = {
a: '03e6a20d-e1a4-440b-8581-54b4f39b427e'
}
const secrets = redactEnv.build(['a'], env)
const provided = JSON.stringify(env)
const expected = '{"a":"*"}'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts JWTs', () => {
// testing for dot-separated base64
const env = {
a: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRGFAYSIsImlhdCI6MTUxNjIzOTAyMn0.0umQbI-3Le8b5mtQKAongS9vUsp6biOtyEGeXhHjZu8',
b: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkphaG4gRGFAYSIsImlhdCI6MTUxNjIzOTAyMn0.8RIUw178Ye1qtSIXezU_ruB43mSYBrIdNsT2NWCMZXU'
}
const secrets = redactEnv.build(['a', 'b'], env)
const provided = JSON.stringify(env)
const expected = '{"a":"*","b":"*"}'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts string representation of numbers', () => {
const env = {
a: '1234567890',
b: '0987654321'
}
const secrets = redactEnv.build(['a', 'b'], env)
const provided = JSON.stringify(env)
const expected = '{"a":"*","b":"*"}'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
it('redacts URIs', () => {
const env = {
a: 'https://github.com/47ng/redact-env?query=string&foo=bar#hash',
b: 'twitter://@fortysevenfx',
c: 'postgres://username:password@host:port/database'
}
const secrets = redactEnv.build(['a', 'b', 'c'], env)
const provided = JSON.stringify(env)
const expected = '{"a":"*","b":"*","c":"*"}'
const received = redactEnv.redact(provided, secrets, '*')
expect(received).toEqual(expected)
})
})
describe('Defaults', () => {
it('redacts with [secure] by default', () => {
const env = {
password: 'supersecret'
}
const secrets = redactEnv.build(['password'], env)
const provided = 'my password is supersecret'
const expected = 'my password is [secure]'
const received = redactEnv.redact(provided, secrets)
expect(received).toEqual(expected)
})
it('uses Node.js process.env by default', () => {
process.env.FOO = 'foo'
const secrets = redactEnv.build(['FOO'])
const provided = 'my password is foo'
const expected = 'my password is [secure]'
const received = redactEnv.redact(provided, secrets)
expect(received).toEqual(expected)
})
})