maskdata 1.3.1 → 1.3.3
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/MASKDATA_FOR_TYPESCRIPT.md +18 -14
- package/README.md +663 -582
- package/documentation/get_nested_json_property.md +27 -0
- package/documentation/main.md +117 -0
- package/documentation/mask_cards.md +0 -0
- package/documentation/mask_email_id.md +47 -0
- package/documentation/mask_email_id_with_the_default_configuration.md +355 -0
- package/documentation/mask_jwt_token.md +132 -0
- package/documentation/mask_password.md +31 -0
- package/documentation/mask_password_with_the_default_configuration.md +43 -0
- package/documentation/mask_phone_number.md +26 -0
- package/documentation/mask_phone_number_with_the_default_configuration.md +155 -0
- package/documentation/mask_uuid.md +30 -0
- package/documentation/replace_the_value_of_a_json_field.md +220 -0
- package/index.d.ts +8 -0
- package/index.js +5 -0
- package/lib/helpers/Constants.js +4 -1
- package/lib/helpers/MaskHelper.js +34 -6
- package/lib/maskService/JsonMask.js +33 -3
- package/lib/maskService/PasswordMask.js +7 -0
- package/lib/maskService/StringMask.js +9 -1
- package/package.json +6 -4
- package/tests/scribble.js +59 -67
- package/tests/testJsonFields.js +143 -0
- package/tests/testPassword.js +135 -1
- package/tests/testString.js +16 -4
- package/tests/testStringV2.js +134 -0
|
@@ -39,6 +39,14 @@ class JsonMask {
|
|
|
39
39
|
for (const key of Object.keys(typeToFunctionMap)) {
|
|
40
40
|
if (!validatedConfig[typeToFunctionMap[key][1]]) continue;
|
|
41
41
|
for (const field of validatedConfig[typeToFunctionMap[key][1]]) {
|
|
42
|
+
if (field.startsWith('*') && !field.includes('.')) {
|
|
43
|
+
this.maskRecursive(
|
|
44
|
+
maskedObj,
|
|
45
|
+
field.substring(1),
|
|
46
|
+
key,
|
|
47
|
+
validatedConfig[typeToFunctionMap[key][2]]
|
|
48
|
+
);
|
|
49
|
+
}
|
|
42
50
|
this.mask(maskedObj, field, key, validatedConfig[typeToFunctionMap[key][2]]);
|
|
43
51
|
}
|
|
44
52
|
}
|
|
@@ -47,6 +55,9 @@ class JsonMask {
|
|
|
47
55
|
if (genericString['fields'] && genericString['fields'].length > 0) {
|
|
48
56
|
let validatedMaskConfig = MaskHelper.validateStringMask2Options(genericString['config']);
|
|
49
57
|
for (let field of genericString['fields']) {
|
|
58
|
+
if (field.startsWith('*') && !field.includes('.')) {
|
|
59
|
+
this.maskRecursive(maskedObj, field.substring(1), 'generic', validatedMaskConfig);
|
|
60
|
+
}
|
|
50
61
|
this.mask(maskedObj, field, 'generic', validatedMaskConfig);
|
|
51
62
|
}
|
|
52
63
|
}
|
|
@@ -55,12 +66,31 @@ class JsonMask {
|
|
|
55
66
|
return maskedObj;
|
|
56
67
|
}
|
|
57
68
|
|
|
69
|
+
static maskRecursive(json, field, type, maskConfig) {
|
|
70
|
+
for (let key in json) {
|
|
71
|
+
if (json[key] === null || json[key] === undefined) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (key === field && typeof json[key] !== 'object') {
|
|
75
|
+
const value = JsonGetSet.get(json, field);
|
|
76
|
+
if (value !== null && value !== undefined) {
|
|
77
|
+
const masked = typeToFunctionMap[type][0](value, maskConfig);
|
|
78
|
+
JsonGetSet.set(json, field, masked);
|
|
79
|
+
}
|
|
80
|
+
} else if (typeof json[key] === 'object') {
|
|
81
|
+
this.maskRecursive(json[key], field, type, maskConfig);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return json;
|
|
85
|
+
}
|
|
86
|
+
|
|
58
87
|
static mask(json, field, type, maskConfig) {
|
|
59
88
|
if (field.includes('.*') && !field.endsWith('.*')) {
|
|
60
|
-
|
|
61
|
-
|
|
89
|
+
return json;
|
|
90
|
+
}
|
|
91
|
+
if (field.startsWith('*') && field.includes('.')) {
|
|
92
|
+
return json;
|
|
62
93
|
}
|
|
63
|
-
|
|
64
94
|
if (field.includes('[*].')) {
|
|
65
95
|
const index = field.indexOf('[*]');
|
|
66
96
|
const left = field.substring(0, index);
|
|
@@ -13,6 +13,13 @@ class PasswordMask {
|
|
|
13
13
|
options = Constants.defaultPasswordMaskOptions;
|
|
14
14
|
}
|
|
15
15
|
let maskPasswordLength = password.length;
|
|
16
|
+
if (options.fixedOutputLength !== undefined && options.fixedOutputLength >= 0) {
|
|
17
|
+
maskPasswordLength = options.fixedOutputLength;
|
|
18
|
+
if (password.length < options.fixedOutputLength) {
|
|
19
|
+
password =
|
|
20
|
+
password + `${options.maskWith}`.repeat(options.fixedOutputLength - password.length);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
16
23
|
if (password.length > options.maxMaskedCharacters) {
|
|
17
24
|
maskPasswordLength = parseInt(options.maxMaskedCharacters);
|
|
18
25
|
}
|
|
@@ -33,7 +33,9 @@ class StringMask {
|
|
|
33
33
|
}
|
|
34
34
|
} else {
|
|
35
35
|
for (const value of Object.values(values)) {
|
|
36
|
-
|
|
36
|
+
const escapedValue = value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
37
|
+
const regex = new RegExp(escapedValue, 'g');
|
|
38
|
+
str = str.replace(regex, `${options.maskWith}`.repeat(value.length));
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
41
|
return str;
|
|
@@ -49,6 +51,12 @@ class StringMask {
|
|
|
49
51
|
options = Constants.defaultStringMaskV2Options;
|
|
50
52
|
}
|
|
51
53
|
let maskStringLength = string.length;
|
|
54
|
+
if (options.fixedOutputLength !== undefined && options.fixedOutputLength >= 0) {
|
|
55
|
+
maskStringLength = options.fixedOutputLength;
|
|
56
|
+
if (string.length < options.fixedOutputLength) {
|
|
57
|
+
string = string + `${options.maskWith}`.repeat(options.fixedOutputLength - string.length);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
52
60
|
if (string.length > options.maxMaskedCharacters) {
|
|
53
61
|
maskStringLength = parseInt(options.maxMaskedCharacters);
|
|
54
62
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maskdata",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "Node module to mask various kinds of data in the required formats",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -26,6 +26,8 @@
|
|
|
26
26
|
"mask json",
|
|
27
27
|
"mask string",
|
|
28
28
|
"mask object",
|
|
29
|
+
"mask json",
|
|
30
|
+
"recursive mask json",
|
|
29
31
|
"Get json fields",
|
|
30
32
|
"Set json fields",
|
|
31
33
|
"Replcae json fields",
|
|
@@ -49,8 +51,8 @@
|
|
|
49
51
|
"chai": "^4.3.10",
|
|
50
52
|
"mocha": "^10.2.0",
|
|
51
53
|
"mocha-junit-reporter": "^2.2.1",
|
|
52
|
-
"nyc": "^
|
|
53
|
-
"prettier": "^3.
|
|
54
|
-
"typescript": "^5.
|
|
54
|
+
"nyc": "^17.0.0",
|
|
55
|
+
"prettier": "^3.3.3",
|
|
56
|
+
"typescript": "^5.5.4"
|
|
55
57
|
}
|
|
56
58
|
}
|
package/tests/scribble.js
CHANGED
|
@@ -53,73 +53,6 @@ console.log(`Unmasked shortEmail: ${shortEmail}`);
|
|
|
53
53
|
console.log(`shortEmail after masking: ${MaskData.maskEmail2(shortEmail, emailMask2Options)}`);
|
|
54
54
|
console.log('========================================');
|
|
55
55
|
|
|
56
|
-
const jsonMaskOptions = {
|
|
57
|
-
fields: ['password', 'firstName'],
|
|
58
|
-
maxMaskedCharactersStr: 3
|
|
59
|
-
};
|
|
60
|
-
const json = {
|
|
61
|
-
password: 'PasswordValue',
|
|
62
|
-
firstName: 'FIRST_NAME',
|
|
63
|
-
lastName: 'LAST_NAME'
|
|
64
|
-
};
|
|
65
|
-
console.log(`Unmasked object: `);
|
|
66
|
-
console.log(JSON.stringify(json));
|
|
67
|
-
console.log(`Object after masking:`);
|
|
68
|
-
console.log(JSON.stringify(MaskData.maskJSONFields(json, jsonMaskOptions)));
|
|
69
|
-
console.log('========================================');
|
|
70
|
-
console.log(JSON.stringify(json));
|
|
71
|
-
console.log('========================================');
|
|
72
|
-
const jsonMaskOptions2 = {
|
|
73
|
-
fields: [
|
|
74
|
-
'level1.level2.level3.field3',
|
|
75
|
-
'level1.level2.field2',
|
|
76
|
-
'level1.field1',
|
|
77
|
-
'value1',
|
|
78
|
-
'level1.level2.level3.field4[0].Hello',
|
|
79
|
-
'level1.level2.level3.field4[2]'
|
|
80
|
-
]
|
|
81
|
-
};
|
|
82
|
-
const maskAllFieldsOptions = {
|
|
83
|
-
fields: ['level1.level2.field3[*].Hello', 'level1.level2.level3.*']
|
|
84
|
-
};
|
|
85
|
-
const nestedAllObject = {
|
|
86
|
-
level1: {
|
|
87
|
-
field1: 'field1Value',
|
|
88
|
-
level2: {
|
|
89
|
-
field2: 'field2Value',
|
|
90
|
-
field3: [{ Hello: 'HelloValue', Hi: 'one' }, { Hello: 'Hello again' }],
|
|
91
|
-
level3: {
|
|
92
|
-
field4: 'field4Value',
|
|
93
|
-
field5: 'field5Value'
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
value1: 'value'
|
|
98
|
-
};
|
|
99
|
-
console.log(`Unmasked nested object: `);
|
|
100
|
-
console.log(JSON.stringify(nestedAllObject));
|
|
101
|
-
console.log(`Nested Object after masking all:`);
|
|
102
|
-
console.log(JSON.stringify(MaskData.maskJSONFields(nestedAllObject, maskAllFieldsOptions)));
|
|
103
|
-
console.log('========================================');
|
|
104
|
-
const nestedObject = {
|
|
105
|
-
level1: {
|
|
106
|
-
field1: 'field1',
|
|
107
|
-
level2: {
|
|
108
|
-
field2: 'field2',
|
|
109
|
-
level3: {
|
|
110
|
-
field3: 'field3',
|
|
111
|
-
field4: [{ Hello: 'world' }, { Hello: 'Newworld' }, 'Just a String']
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
value1: 'value'
|
|
116
|
-
};
|
|
117
|
-
console.log(`Unmasked nested object: `);
|
|
118
|
-
console.log(JSON.stringify(nestedObject));
|
|
119
|
-
console.log(`Nested Object after masking:`);
|
|
120
|
-
console.log(JSON.stringify(MaskData.maskJSONFields(nestedObject, jsonMaskOptions2)));
|
|
121
|
-
console.log('========================================');
|
|
122
|
-
|
|
123
56
|
const stringMaskOptions = {
|
|
124
57
|
maskWith: '*',
|
|
125
58
|
values: ['is', 'API']
|
|
@@ -543,3 +476,62 @@ const maskedOutput = MaskData.maskJSON2(jsonInput3, jsonMaskConfig3);
|
|
|
543
476
|
|
|
544
477
|
console.log(maskedOutput);
|
|
545
478
|
console.log(maskedOutput['emails'].moreEmails[3].secondChild.nestedkid);
|
|
479
|
+
|
|
480
|
+
const JsonMask2Configs = {
|
|
481
|
+
genericStrings: [
|
|
482
|
+
{
|
|
483
|
+
config: {
|
|
484
|
+
maskWith: '*',
|
|
485
|
+
maxMaskedCharacters: 6
|
|
486
|
+
},
|
|
487
|
+
fields: ['*addressLine1']
|
|
488
|
+
}
|
|
489
|
+
]
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
console.log(
|
|
493
|
+
MaskData.maskJSON2(
|
|
494
|
+
{
|
|
495
|
+
car: 'Mazda',
|
|
496
|
+
addressLine1: 'Mask me!',
|
|
497
|
+
deeper: {
|
|
498
|
+
addressLine1: 'Mask me!',
|
|
499
|
+
evenDeeper: { addressLine1: 'Mask me!' }
|
|
500
|
+
}
|
|
501
|
+
},
|
|
502
|
+
JsonMask2Configs
|
|
503
|
+
)
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
const inputJson = {
|
|
507
|
+
cards: [
|
|
508
|
+
{
|
|
509
|
+
number: '1234-5678-0123-0000'
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
number: '1111-2222-3333-4444'
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
number: '2222-4444-6666-8888'
|
|
516
|
+
},
|
|
517
|
+
{
|
|
518
|
+
number: '1111-3333-5555-7777'
|
|
519
|
+
},
|
|
520
|
+
{
|
|
521
|
+
number: '0000-0000-0000-0000'
|
|
522
|
+
}
|
|
523
|
+
]
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
const cardfieldsToMask = ['*number']; // Specify the field with a '*' at the beginning and NO dot(.) anywhere else in that field.
|
|
527
|
+
|
|
528
|
+
const jsonMaskConfigx = {
|
|
529
|
+
cardMaskOptions: {
|
|
530
|
+
maskWith: '*',
|
|
531
|
+
unmaskedStartDigits: 4,
|
|
532
|
+
unmaskedEndDigits: 1
|
|
533
|
+
},
|
|
534
|
+
cardFields: cardfieldsToMask
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
console.log(MaskData.maskJSON2(inputJson, jsonMaskConfigx));
|
package/tests/testJsonFields.js
CHANGED
|
@@ -375,6 +375,149 @@ describe('JSON mask2', function () {
|
|
|
375
375
|
});
|
|
376
376
|
});
|
|
377
377
|
|
|
378
|
+
describe('Test recursive masking', function () {
|
|
379
|
+
let input = JSON.parse(JSON.stringify(jsonInput));
|
|
380
|
+
input['cards'] = [
|
|
381
|
+
{ number: '1234-5678-1234-5678' },
|
|
382
|
+
{ number: '2222-3333-4444-5555' },
|
|
383
|
+
{ number: '5555-6666-7777-8888' },
|
|
384
|
+
{ number: '1111-3333-5555-7777' },
|
|
385
|
+
{ number1: '0000-0000-0000-0000' }
|
|
386
|
+
];
|
|
387
|
+
input['randomStrings'] = {};
|
|
388
|
+
input['randomStrings']['key1'] = 'This is row 1 random string';
|
|
389
|
+
input['randomStrings']['row2'] = ['Entry1', 'Entry2', 'Entry3'];
|
|
390
|
+
input['randomStrings']['row3'] = {
|
|
391
|
+
key1: 'Row3 Object 1',
|
|
392
|
+
key2: 'Row3 Object 2',
|
|
393
|
+
key3: ['Entry1', 'Entry2', 'Entry3', { key1: 'mask me also' }]
|
|
394
|
+
};
|
|
395
|
+
input['randomStrings']['row4'] = [
|
|
396
|
+
{
|
|
397
|
+
key1: 'mask me also1'
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
key1: 'mask me also2'
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
key1: 'mask me also3'
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
key1: 'mask me also4'
|
|
407
|
+
}
|
|
408
|
+
];
|
|
409
|
+
let testData = [
|
|
410
|
+
{
|
|
411
|
+
title: 'Mask generic string fields with *',
|
|
412
|
+
input: input,
|
|
413
|
+
outputCredit: 'XXXX-XXXX-XXXX-1234',
|
|
414
|
+
outputDebit: 'XXXX-XXXX-XXXX-3333'
|
|
415
|
+
}
|
|
416
|
+
];
|
|
417
|
+
|
|
418
|
+
testData.forEach(({ title, input, outputCredit, outputDebit }) => {
|
|
419
|
+
const jsonMaskConfig = JSON.parse(JSON.stringify(Constants.defaultjsonMask2Configs));
|
|
420
|
+
const cardMaskOptions = JSON.parse(JSON.stringify(Constants.defaultCardMaskOptions));
|
|
421
|
+
cardMaskOptions['maskWith'] = 'X';
|
|
422
|
+
cardMaskOptions['unmaskedStartDigits'] = 0;
|
|
423
|
+
cardMaskOptions['unmaskedEndDigits'] = 4;
|
|
424
|
+
jsonMaskConfig['cardFields'] = ['credit', 'debit', '*number'];
|
|
425
|
+
jsonMaskConfig['cardMaskOptions'] = cardMaskOptions;
|
|
426
|
+
jsonMaskConfig['genericStrings'] = [
|
|
427
|
+
{
|
|
428
|
+
fields: ['randomString'],
|
|
429
|
+
config: {
|
|
430
|
+
maskWith: '?',
|
|
431
|
+
unmaskedStartCharacters: 2,
|
|
432
|
+
unmaskedEndCharacters: 3,
|
|
433
|
+
maxMaskedCharacters: 8
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
fields: ['*key1'],
|
|
438
|
+
config: {
|
|
439
|
+
maskWith: '*',
|
|
440
|
+
unmaskedStartCharacters: 2,
|
|
441
|
+
unmaskedEndCharacters: 3,
|
|
442
|
+
maxMaskedCharacters: 8
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
fields: ['*key10'],
|
|
447
|
+
config: {
|
|
448
|
+
maskWith: '*',
|
|
449
|
+
unmaskedStartCharacters: 2,
|
|
450
|
+
unmaskedEndCharacters: 3,
|
|
451
|
+
maxMaskedCharacters: 8
|
|
452
|
+
}
|
|
453
|
+
},
|
|
454
|
+
{ fields: ['randomStrings.row2.*'], config: { maskWith: 'X', unmaskedEndCharacters: 1 } }
|
|
455
|
+
];
|
|
456
|
+
it(`${title}`, function () {
|
|
457
|
+
const masked = maskData.maskJSON2(input, jsonMaskConfig);
|
|
458
|
+
expect(masked['credit']).to.equal(outputCredit);
|
|
459
|
+
expect(masked['debit']).to.equal(outputDebit);
|
|
460
|
+
expect(masked['cards'][0]['number']).to.equal('XXXX-XXXX-XXXX-5678');
|
|
461
|
+
expect(masked['cards'][1]['number']).to.equal('XXXX-XXXX-XXXX-5555');
|
|
462
|
+
expect(masked['cards'][2]['number']).to.equal('XXXX-XXXX-XXXX-8888');
|
|
463
|
+
expect(masked['cards'][3]['number']).to.equal('XXXX-XXXX-XXXX-7777');
|
|
464
|
+
expect(masked['cards'][4]['number1']).to.equal('0000-0000-0000-0000');
|
|
465
|
+
expect(masked['primaryEmail']).to.equal(input['primaryEmail']);
|
|
466
|
+
expect(masked['secondaryEmail']).to.equal(input['secondaryEmail']);
|
|
467
|
+
expect(masked['password']).to.equal(input['password']);
|
|
468
|
+
expect(masked['homePhone']).to.equal(input['homePhone']);
|
|
469
|
+
expect(masked['workPhone']).to.equal(input['workPhone']);
|
|
470
|
+
expect(masked['addressLine1']).to.equal(input['addressLine1']);
|
|
471
|
+
expect(masked['addressLine2']).to.equal(input['addressLine2']);
|
|
472
|
+
expect(masked['randomString']).to.equal('Th???ing');
|
|
473
|
+
expect(masked['randomStrings']['key1']).to.equal('Th***ing');
|
|
474
|
+
expect(masked['randomStrings']['row2'][0]).to.equal('XXXXX1');
|
|
475
|
+
expect(masked['randomStrings']['row2'][1]).to.equal('XXXXX2');
|
|
476
|
+
expect(masked['randomStrings']['row2'][2]).to.equal('XXXXX3');
|
|
477
|
+
expect(masked['randomStrings']['row3']['key3'][0]).to.equal('Entry1');
|
|
478
|
+
expect(masked['randomStrings']['row3']['key3'][1]).to.equal('Entry2');
|
|
479
|
+
expect(masked['randomStrings']['row3']['key3'][2]).to.equal('Entry3');
|
|
480
|
+
expect(masked['randomStrings']['row3']['key3'][3]['key1']).to.equal('ma***lso');
|
|
481
|
+
expect(masked['randomStrings']['row4'][0]['key1']).to.equal('ma***so1');
|
|
482
|
+
expect(masked['randomStrings']['row4'][1]['key1']).to.equal('ma***so2');
|
|
483
|
+
expect(masked['randomStrings']['row4'][2]['key1']).to.equal('ma***so3');
|
|
484
|
+
expect(masked['randomStrings']['row4'][3]['key1']).to.equal('ma***so4');
|
|
485
|
+
expect(masked['unknownField']).to.equal(undefined);
|
|
486
|
+
});
|
|
487
|
+
it(`Recursive masking negative test cases; Do not mask`, function () {
|
|
488
|
+
const inputJson = {
|
|
489
|
+
cards: {
|
|
490
|
+
number: '1234-5678-0123-0000',
|
|
491
|
+
deeper: {
|
|
492
|
+
number: '0000-0000-0000-0000'
|
|
493
|
+
}
|
|
494
|
+
},
|
|
495
|
+
cards2: [
|
|
496
|
+
{
|
|
497
|
+
number: '1111-2222-3333-4444'
|
|
498
|
+
},
|
|
499
|
+
{
|
|
500
|
+
number: '2222-4444-6666-8888'
|
|
501
|
+
}
|
|
502
|
+
]
|
|
503
|
+
};
|
|
504
|
+
const jsonMaskConfig = {
|
|
505
|
+
cardMaskOptions: {
|
|
506
|
+
maskWith: '*',
|
|
507
|
+
unmaskedStartDigits: 4,
|
|
508
|
+
unmaskedEndDigits: 1
|
|
509
|
+
},
|
|
510
|
+
cardFields: ['*cards.number']
|
|
511
|
+
};
|
|
512
|
+
const masked = maskData.maskJSON2(inputJson, jsonMaskConfig);
|
|
513
|
+
expect(masked['cards']['number']).to.equal('1234-5678-0123-0000');
|
|
514
|
+
expect(masked['cards']['deeper']['number']).to.equal('0000-0000-0000-0000');
|
|
515
|
+
expect(masked['cards2'][0]['number']).to.equal('1111-2222-3333-4444');
|
|
516
|
+
expect(masked['cards2'][1]['number']).to.equal('2222-4444-6666-8888');
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
|
|
378
521
|
describe('Mask email fields with custom options', function () {
|
|
379
522
|
let testData = [
|
|
380
523
|
{
|
package/tests/testPassword.js
CHANGED
|
@@ -9,7 +9,8 @@ describe('Masking password', function () {
|
|
|
9
9
|
// maskWith: "*",
|
|
10
10
|
// maxMaskedCharacters: 16,
|
|
11
11
|
// unmaskedStartCharacters: 0,
|
|
12
|
-
// unmaskedEndCharacters: 0
|
|
12
|
+
// unmaskedEndCharacters: 0,
|
|
13
|
+
// fixedOutputLength: undefined
|
|
13
14
|
// };
|
|
14
15
|
|
|
15
16
|
let testData = [
|
|
@@ -117,6 +118,139 @@ describe('Masking password', function () {
|
|
|
117
118
|
});
|
|
118
119
|
});
|
|
119
120
|
|
|
121
|
+
describe('Mask with fixedOutputLength config', function () {
|
|
122
|
+
const config = {
|
|
123
|
+
maskWith: 'X',
|
|
124
|
+
fixedOutputLength: 6
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
let testData = [
|
|
128
|
+
{
|
|
129
|
+
title: 'Random string',
|
|
130
|
+
input: '1234-5678-1234-5678',
|
|
131
|
+
output: 'XXXXXX'
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
title: 'input string length equal to fixedOutputLength',
|
|
135
|
+
input: '12-345',
|
|
136
|
+
output: 'XXXXXX'
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
title: 'input string length less than fixedOutputLength',
|
|
140
|
+
input: '12',
|
|
141
|
+
output: 'XXXXXX'
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
title: 'input string length greater than fixedOutputLength',
|
|
145
|
+
input: '1234567',
|
|
146
|
+
output: 'XXXXXX'
|
|
147
|
+
}
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
testData.forEach(({ title, input, output }) => {
|
|
151
|
+
it(`custom mask - ${title}`, function () {
|
|
152
|
+
const masked = maskData.maskPassword(input, config);
|
|
153
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('custom mask - test with fixedOutputLength and maxMaskedCharacters; No effect from maxMaskedCharacters', function () {
|
|
158
|
+
const config = {
|
|
159
|
+
maskWith: 'x',
|
|
160
|
+
maxMaskedCharacters: 4,
|
|
161
|
+
fixedOutputLength: 6
|
|
162
|
+
};
|
|
163
|
+
const input = '1234-5678-1234-5678';
|
|
164
|
+
const output = 'xxxxxx';
|
|
165
|
+
const masked = maskData.maskPassword(input, config);
|
|
166
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('unmaskedStartCharacters + unmaskedEndCharacters < fixedOutputLength; No masking done', function () {
|
|
170
|
+
const config = {
|
|
171
|
+
maskWith: 'x',
|
|
172
|
+
fixedOutputLength: 6,
|
|
173
|
+
unmaskedStartCharacters: 4, // 4+4=8 > 6
|
|
174
|
+
unmaskedEndCharacters: 4
|
|
175
|
+
};
|
|
176
|
+
const input = '1234-5678-1234-5678';
|
|
177
|
+
const output = '123478';
|
|
178
|
+
const masked = maskData.maskPassword(input, config);
|
|
179
|
+
expect(masked).to.equal(
|
|
180
|
+
output,
|
|
181
|
+
'No masking done because fixedOutputLength shorter than start + end'
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('unmaskedStartCharacters + unmaskedEndCharacters = fixedOutputLength; No masking done', function () {
|
|
186
|
+
const config = {
|
|
187
|
+
maskWith: 'x',
|
|
188
|
+
fixedOutputLength: 8,
|
|
189
|
+
unmaskedStartCharacters: 4, // 4+4=8
|
|
190
|
+
unmaskedEndCharacters: 4
|
|
191
|
+
};
|
|
192
|
+
const input = '1234-abcd-efgh-5678';
|
|
193
|
+
const output = '12345678';
|
|
194
|
+
const masked = maskData.maskPassword(input, config);
|
|
195
|
+
expect(masked).to.equal(
|
|
196
|
+
output,
|
|
197
|
+
'No masking done because fixedOutputLength equal to start + end'
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
it('fixedOutputLength = null; Treat it as undefined and mask with other default configs', function () {
|
|
201
|
+
const config = {
|
|
202
|
+
maskWith: 'x',
|
|
203
|
+
fixedOutputLength: null
|
|
204
|
+
};
|
|
205
|
+
const input = '1234-abcd-efgh-5678';
|
|
206
|
+
const output = 'xxxxxxxxxxxxxxxx';
|
|
207
|
+
const masked = maskData.maskPassword(input, config);
|
|
208
|
+
expect(masked).to.equal(
|
|
209
|
+
output,
|
|
210
|
+
'Default config masking should have been done as fixedOutputLength is null and is treated as undefined'
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
it('fixedOutputLength = undefined; Treat it as undefined and mask with other default configs', function () {
|
|
214
|
+
const config = {
|
|
215
|
+
maskWith: 'x',
|
|
216
|
+
fixedOutputLength: undefined
|
|
217
|
+
};
|
|
218
|
+
const input = '1234-abcd-efgh-5678';
|
|
219
|
+
const output = 'xxxxxxxxxxxxxxxx';
|
|
220
|
+
const masked = maskData.maskPassword(input, config);
|
|
221
|
+
expect(masked).to.equal(
|
|
222
|
+
output,
|
|
223
|
+
'Default config masking should have been done as fixedOutputLength is undefined'
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
it('fixedOutputLength < 0; Treat it as undefined and mask with other default configs', function () {
|
|
227
|
+
const config = {
|
|
228
|
+
maskWith: 'x',
|
|
229
|
+
fixedOutputLength: -1
|
|
230
|
+
};
|
|
231
|
+
const input = '1234-abcd-efgh-5678';
|
|
232
|
+
const output = 'xxxxxxxxxxxxxxxx';
|
|
233
|
+
const masked = maskData.maskPassword(input, config);
|
|
234
|
+
expect(masked).to.equal(
|
|
235
|
+
output,
|
|
236
|
+
'Default config masking should have been done as fixedOutputLength is negative as is treated as undefined'
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
it('fixedOutputLength = 0; Output length = 0 characters', function () {
|
|
240
|
+
const config = {
|
|
241
|
+
maskWith: 'x',
|
|
242
|
+
fixedOutputLength: 0
|
|
243
|
+
};
|
|
244
|
+
const input = '1234-abcd-efgh-5678';
|
|
245
|
+
const output = '';
|
|
246
|
+
const masked = maskData.maskPassword(input, config);
|
|
247
|
+
expect(masked).to.equal(
|
|
248
|
+
output,
|
|
249
|
+
'Output length should be 0 as fixedOutputLength is explicitly mentioned as 0'
|
|
250
|
+
);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
120
254
|
describe('Mask with special input strings', function () {
|
|
121
255
|
const maskOptions = {
|
|
122
256
|
maskWith: 'x',
|
package/tests/testString.js
CHANGED
|
@@ -19,6 +19,11 @@ describe('Masking strings', function () {
|
|
|
19
19
|
title: 'test string with newline',
|
|
20
20
|
input: 'this is a\nstring with newline',
|
|
21
21
|
output: ''
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
title: 'test string with regex characters',
|
|
25
|
+
input: 'this is a $tring with ^,&,/,[,.,*,+,?,{,},(,),|,]',
|
|
26
|
+
output: ''
|
|
22
27
|
}
|
|
23
28
|
];
|
|
24
29
|
|
|
@@ -37,6 +42,7 @@ describe('Masking strings', function () {
|
|
|
37
42
|
localTestData[0].output = localTestData[0].input;
|
|
38
43
|
localTestData[1].output = localTestData[1].input;
|
|
39
44
|
localTestData[2].output = localTestData[2].input;
|
|
45
|
+
localTestData[3].output = localTestData[3].input;
|
|
40
46
|
|
|
41
47
|
localTestData.forEach(({ title, input, output }) => {
|
|
42
48
|
it(`default mask - ${title}`, function () {
|
|
@@ -61,6 +67,7 @@ describe('Masking strings', function () {
|
|
|
61
67
|
localTestData[0].output = localTestData[0].input;
|
|
62
68
|
localTestData[1].output = localTestData[1].input;
|
|
63
69
|
localTestData[2].output = localTestData[2].input;
|
|
70
|
+
localTestData[3].output = localTestData[3].input;
|
|
64
71
|
|
|
65
72
|
localTestData.forEach(({ title, input, output }) => {
|
|
66
73
|
it(`custom mask - ${title}`, function () {
|
|
@@ -84,6 +91,7 @@ describe('Masking strings', function () {
|
|
|
84
91
|
localTestData[0].output = '???? ?? ? ?????????? ??????';
|
|
85
92
|
localTestData[1].output = '?'.repeat(localTestData[1].input.length); // no space here
|
|
86
93
|
localTestData[2].output = '???? ?? ???????? ???? ???????';
|
|
94
|
+
localTestData[3].output = '???? ?? ? ?????? ???? ???????????????????????????';
|
|
87
95
|
|
|
88
96
|
localTestData.forEach(({ title, input, output }) => {
|
|
89
97
|
it(`custom mask - ${title}`, function () {
|
|
@@ -108,6 +116,7 @@ describe('Masking strings', function () {
|
|
|
108
116
|
localTestData[0].output = maskChar.repeat(localTestData[0].input.length);
|
|
109
117
|
localTestData[1].output = maskChar.repeat(localTestData[1].input.length);
|
|
110
118
|
localTestData[2].output = maskChar.repeat(localTestData[2].input.length);
|
|
119
|
+
localTestData[3].output = maskChar.repeat(localTestData[3].input.length);
|
|
111
120
|
|
|
112
121
|
localTestData.forEach(({ title, input, output }) => {
|
|
113
122
|
it(`custom mask - ${title}`, function () {
|
|
@@ -121,7 +130,7 @@ describe('Masking strings', function () {
|
|
|
121
130
|
const maskOptions = {
|
|
122
131
|
maskWith: 'x',
|
|
123
132
|
maskOnlyFirstOccurance: false,
|
|
124
|
-
values: ['is', 'ing'],
|
|
133
|
+
values: ['is', 'ing', '$string'],
|
|
125
134
|
maskAll: true,
|
|
126
135
|
maskSpace: false
|
|
127
136
|
};
|
|
@@ -131,6 +140,7 @@ describe('Masking strings', function () {
|
|
|
131
140
|
localTestData[0].output = 'xxxx xx x xxxxxxxxxx xxxxxx';
|
|
132
141
|
localTestData[1].output = 'x'.repeat(localTestData[1].input.length); // no spaces here
|
|
133
142
|
localTestData[2].output = 'xxxx xx xxxxxxxx xxxx xxxxxxx';
|
|
143
|
+
localTestData[3].output = 'xxxx xx x xxxxxx xxxx xxxxxxxxxxxxxxxxxxxxxxxxxxx';
|
|
134
144
|
|
|
135
145
|
localTestData.forEach(({ title, input, output }) => {
|
|
136
146
|
it(`custom mask - ${title}`, function () {
|
|
@@ -144,7 +154,7 @@ describe('Masking strings', function () {
|
|
|
144
154
|
const maskOptions = {
|
|
145
155
|
maskWith: '*',
|
|
146
156
|
maskOnlyFirstOccurance: false,
|
|
147
|
-
values: ['is', 'ing'],
|
|
157
|
+
values: ['is', 'ing', '&,/'],
|
|
148
158
|
maskAll: false,
|
|
149
159
|
maskSpace: true
|
|
150
160
|
};
|
|
@@ -153,6 +163,7 @@ describe('Masking strings', function () {
|
|
|
153
163
|
localTestData[0].output = 'Th** ** a test***API Str***';
|
|
154
164
|
localTestData[1].output = 'th****astr***';
|
|
155
165
|
localTestData[2].output = 'th** ** a\nstr*** with newline';
|
|
166
|
+
localTestData[3].output = 'th** ** a $tr*** with ^,***,[,.,*,+,?,{,},(,),|,]';
|
|
156
167
|
|
|
157
168
|
localTestData.forEach(({ title, input, output }) => {
|
|
158
169
|
it(`custom mask - ${title}`, function () {
|
|
@@ -166,7 +177,7 @@ describe('Masking strings', function () {
|
|
|
166
177
|
const maskOptions = {
|
|
167
178
|
maskWith: '*',
|
|
168
179
|
maskOnlyFirstOccurance: true,
|
|
169
|
-
values: ['is', 'ing'],
|
|
180
|
+
values: ['is', 'ing', '^,&,/,[,.,*,+,?,{,},(,),|,]'],
|
|
170
181
|
maskAll: false,
|
|
171
182
|
maskSpace: true
|
|
172
183
|
};
|
|
@@ -175,6 +186,7 @@ describe('Masking strings', function () {
|
|
|
175
186
|
localTestData[0].output = 'Th** is a test***API String';
|
|
176
187
|
localTestData[1].output = 'th**isastr***';
|
|
177
188
|
localTestData[2].output = 'th** is a\nstr*** with newline';
|
|
189
|
+
localTestData[3].output = 'th** is a $tr*** with ***************************';
|
|
178
190
|
|
|
179
191
|
localTestData.forEach(({ title, input, output }) => {
|
|
180
192
|
it(`custom mask - ${title}`, function () {
|
|
@@ -233,7 +245,7 @@ describe('Masking strings', function () {
|
|
|
233
245
|
}
|
|
234
246
|
];
|
|
235
247
|
|
|
236
|
-
testData.forEach(({ title, input
|
|
248
|
+
testData.forEach(({ title, input }) => {
|
|
237
249
|
it(`special input - ${title}`, function () {
|
|
238
250
|
const masked = maskData.maskString(input, maskOptions);
|
|
239
251
|
expect(masked).to.equal(input, "input won't be masked");
|