maskdata 1.2.3 → 1.2.4
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 +155 -4
- package/index.d.ts +10 -0
- package/index.js +5 -0
- package/lib/helpers/Constants.js +11 -1
- package/lib/helpers/MaskHelper.js +37 -0
- package/lib/maskService/JsonMask.js +4 -1
- package/lib/maskService/JwtMask.js +92 -0
- package/package.json +6 -2
- package/tests/scribble.js +85 -0
- package/tests/testJsonFields.js +192 -383
- package/tests/testJwt.js +262 -0
package/tests/testJwt.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const maskData = require('../index');
|
|
3
|
+
const BadOption = require('../lib/ExceptionsHandler/BadOption');
|
|
4
|
+
const expect = require('chai').expect;
|
|
5
|
+
|
|
6
|
+
describe('Masking JWT tokens', function () {
|
|
7
|
+
describe('Mask JWT token with default options', function () {
|
|
8
|
+
// default options are this - let tests fail when defaults change
|
|
9
|
+
// const defaultJwtMaskOptions = {
|
|
10
|
+
// maskWith: '*',
|
|
11
|
+
// maxMaskedCharacters: 512,
|
|
12
|
+
// maskDot: true,
|
|
13
|
+
// maskHeader: true,
|
|
14
|
+
// maskPayload: true,
|
|
15
|
+
// maskSignature: true
|
|
16
|
+
// };
|
|
17
|
+
|
|
18
|
+
let testData = [
|
|
19
|
+
{
|
|
20
|
+
title: 'Random JWT token',
|
|
21
|
+
input:
|
|
22
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
23
|
+
output:
|
|
24
|
+
'*********************************************************************************************************'
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
title: 'invalid but valid syntax JWT',
|
|
28
|
+
input: 'a.b.c',
|
|
29
|
+
output: '*****'
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
title: 'longer than 512 chars',
|
|
33
|
+
input:
|
|
34
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQtbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
35
|
+
output:
|
|
36
|
+
'********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************'
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
title: 'Invalid JWT. Do not mask',
|
|
40
|
+
input: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9',
|
|
41
|
+
output: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9'
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
title: 'Invalid JWT2. Do not mask',
|
|
45
|
+
input: '..eyJpYXQiOjE1MTYyMzkwMjJ9',
|
|
46
|
+
output: '..eyJpYXQiOjE1MTYyMzkwMjJ9'
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
title: 'empty jwt. Do not mask',
|
|
50
|
+
input: '',
|
|
51
|
+
output: ''
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
title: 'length less than 5 chars jwt. Do not mask',
|
|
55
|
+
input: 'a.b.',
|
|
56
|
+
output: 'a.b.'
|
|
57
|
+
}
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
testData.forEach(({ title, input, output }) => {
|
|
61
|
+
it(`default mask - ${title}`, function () {
|
|
62
|
+
const masked = maskData.maskJwt(input);
|
|
63
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
64
|
+
expect(masked.length).to.equal(
|
|
65
|
+
input.length >= 512 ? 512 : input.length,
|
|
66
|
+
'masked output length does not match expected value'
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('Mask JWT with custom options', function () {
|
|
73
|
+
const jwtMaskOptions = {
|
|
74
|
+
maskWith: '*',
|
|
75
|
+
maxMaskedCharacters: 512,
|
|
76
|
+
maskDot: false,
|
|
77
|
+
maskHeader: false,
|
|
78
|
+
maskPayload: false,
|
|
79
|
+
maskSignature: false
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
let testData = [
|
|
83
|
+
{
|
|
84
|
+
title: 'Mask only dots',
|
|
85
|
+
input:
|
|
86
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
87
|
+
output:
|
|
88
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9*eyJpYXQiOjE1MTYyMzkwMjJ9*tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
89
|
+
configKey: 'maskDot',
|
|
90
|
+
configValue: true
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
title: 'Mask only header',
|
|
94
|
+
input:
|
|
95
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
96
|
+
output:
|
|
97
|
+
'************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
98
|
+
configKey: 'maskHeader',
|
|
99
|
+
configValue: true
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
title: 'Mask only payload',
|
|
103
|
+
input:
|
|
104
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
105
|
+
output:
|
|
106
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.************************.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
107
|
+
configKey: 'maskPayload',
|
|
108
|
+
configValue: true
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
title: 'Mask only signature',
|
|
112
|
+
input:
|
|
113
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
114
|
+
output:
|
|
115
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************',
|
|
116
|
+
configKey: 'maskSignature',
|
|
117
|
+
configValue: true
|
|
118
|
+
}
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
testData.forEach(({ title, input, output, configKey, configValue }) => {
|
|
122
|
+
const config = JSON.parse(JSON.stringify(jwtMaskOptions));
|
|
123
|
+
config[configKey] = configValue;
|
|
124
|
+
it(`default mask - ${title}`, function () {
|
|
125
|
+
const masked = maskData.maskJwt(input, config);
|
|
126
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('Unmask part of the JWT', function () {
|
|
132
|
+
const jwtMaskOptions = {
|
|
133
|
+
maskWith: '*',
|
|
134
|
+
maxMaskedCharacters: 512,
|
|
135
|
+
maskDot: false,
|
|
136
|
+
maskHeader: true,
|
|
137
|
+
maskPayload: true,
|
|
138
|
+
maskSignature: true
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
let testData = [
|
|
142
|
+
{
|
|
143
|
+
title: 'Mask header and body',
|
|
144
|
+
input:
|
|
145
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
146
|
+
output:
|
|
147
|
+
'************************************.************************.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
148
|
+
configKey: 'maskSignature',
|
|
149
|
+
configValue: false
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
title: 'Mask header and signature',
|
|
153
|
+
input:
|
|
154
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
155
|
+
output:
|
|
156
|
+
'************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************',
|
|
157
|
+
configKey: 'maskPayload',
|
|
158
|
+
configValue: false
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
title: 'Mask body and signature',
|
|
162
|
+
input:
|
|
163
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ',
|
|
164
|
+
output:
|
|
165
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.************************.*******************************************',
|
|
166
|
+
configKey: 'maskHeader',
|
|
167
|
+
configValue: false
|
|
168
|
+
}
|
|
169
|
+
];
|
|
170
|
+
|
|
171
|
+
testData.forEach(({ title, input, output, configKey, configValue }) => {
|
|
172
|
+
const config = JSON.parse(JSON.stringify(jwtMaskOptions));
|
|
173
|
+
config[configKey] = configValue;
|
|
174
|
+
it(`custom mask2 - ${title}`, function () {
|
|
175
|
+
const masked = maskData.maskJwt(input, config);
|
|
176
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('Test maxMaskedCharacters', function () {
|
|
182
|
+
const jwtMaskOptions = {
|
|
183
|
+
maskWith: '*',
|
|
184
|
+
maxMaskedCharacters: 512,
|
|
185
|
+
maskDot: true,
|
|
186
|
+
maskHeader: true,
|
|
187
|
+
maskPayload: true,
|
|
188
|
+
maskSignature: true
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
let testData = [
|
|
192
|
+
{
|
|
193
|
+
title: 'Mask with maxMaskedCharacters=1',
|
|
194
|
+
input: 'abcd.efgh.hijk',
|
|
195
|
+
output: '***',
|
|
196
|
+
maxMaskedCharacters: 1,
|
|
197
|
+
maskDot: true
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
title: 'Mask with maxMaskedCharacters=1',
|
|
201
|
+
input: 'abcd.efgh.hijk',
|
|
202
|
+
output: '*..',
|
|
203
|
+
maxMaskedCharacters: 1,
|
|
204
|
+
maskDot: false
|
|
205
|
+
}
|
|
206
|
+
];
|
|
207
|
+
|
|
208
|
+
testData.forEach(({ title, input, output, maxMaskedCharacters, maskDot }) => {
|
|
209
|
+
const config = JSON.parse(JSON.stringify(jwtMaskOptions));
|
|
210
|
+
config['maxMaskedCharacters'] = maxMaskedCharacters;
|
|
211
|
+
config['maskDot'] = maskDot;
|
|
212
|
+
it(`custom mask2 - ${title}`, function () {
|
|
213
|
+
const masked = maskData.maskJwt(input, config);
|
|
214
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('Mask with non string inputs - input will not be masked', function () {
|
|
220
|
+
const jwtMaskOptions = {
|
|
221
|
+
maskWith: '*',
|
|
222
|
+
maxMaskedCharacters: 512,
|
|
223
|
+
maskDot: false,
|
|
224
|
+
maskHeader: true,
|
|
225
|
+
maskPayload: true,
|
|
226
|
+
maskSignature: true
|
|
227
|
+
};
|
|
228
|
+
let testData = [
|
|
229
|
+
{
|
|
230
|
+
title: 'test input as number',
|
|
231
|
+
input: 12
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
title: 'test input as array',
|
|
235
|
+
input: ['12']
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
title: 'test input as object',
|
|
239
|
+
input: { a: 'b', x: 'y' }
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
title: 'test input as boolean',
|
|
243
|
+
input: false
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
title: 'undefined jwt. Do not mask',
|
|
247
|
+
input: undefined
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
title: 'null jwt. Do not mask',
|
|
251
|
+
input: null
|
|
252
|
+
}
|
|
253
|
+
];
|
|
254
|
+
|
|
255
|
+
testData.forEach(({ title, input }) => {
|
|
256
|
+
it(`non string input - ${title}`, function () {
|
|
257
|
+
const masked = maskData.maskJwt(input, jwtMaskOptions);
|
|
258
|
+
expect(masked).to.equal(input, 'Improper input');
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
});
|