maskdata 1.3.1 → 1.3.2

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 CHANGED
@@ -4,7 +4,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
4
4
  # Table of Contents
5
5
  - [Features](#features)
6
6
  - [Install maskdata](#install-maskdata)
7
- - [Version 1.3.1 Features](#release-features)
7
+ - [Version 1.3.2 Features](#release-features)
8
8
  - [How to Use](#how-to-use)
9
9
  - [Maskdata for Typescript](#maskdata-for-typescript)
10
10
  - [Mask Card number](#mask-card-number)
@@ -13,6 +13,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
13
13
  - [Mask JSON fields](#mask-json)
14
14
  - [JSON mask examples with nested fields](#json-mask-examples)
15
15
  - [Mask multiple fields at once using .* and [*].](#mask-multiple-fields)
16
+ - [Recursive masking / Mask all the fields with a name in the json](#recursive-masking)
16
17
  - [Mask Password](#mask-password)
17
18
  - [Mask Password with the default configuration](#mask-password-with-the-default-configuration)
18
19
  - [Mask Phone Number](#mask-phone-number)
@@ -44,6 +45,12 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
44
45
  > npm i maskdata
45
46
 
46
47
  # Release Features
48
+ ### Version 1.3.2
49
+ - Recursive masking feature: [Details](#recursive-masking)
50
+ - https://github.com/Sumukha1496/maskdata/issues/45
51
+ - https://github.com/Sumukha1496/maskdata/issues/47
52
+ - Fixed output length masking: [Mask Password](#mask-password) and [Mask String](#generic-string-masking)
53
+ - https://github.com/Sumukha1496/maskdata/issues/46
47
54
  ### Version 1.3.1
48
55
  - Removal of maskJSONFields function: https://www.npmjs.com/package/maskdata/v/1.1.10#mask-fields-in-a-json
49
56
  - String mask version2 for generic string masking in response to the below issues: [Details](#generic-string-masking)
@@ -51,14 +58,6 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
51
58
  - https://github.com/Sumukha1496/maskdata/issues/40
52
59
  - https://github.com/Sumukha1496/maskdata/issues/42
53
60
  - Supporting generic string masking(string mask V2) as part of maskJson2: [generic String masking in a JSON](#json-mask-examples)
54
- ### Version 1.2.6
55
- - JWT token masking: Mask JWT tokens with configs to mask as per your need. More details: [Mask JWT Token](#mask-jwt-token)
56
- - Mask Json now supports JWT token masking also. More details: [Mask JWT in a JSON](#mask-json)
57
- - Bug fix in maskJson2 where it was not checking for the empty/null/undefined fields and was resulting in error `"TypeError: validatedConfig[typeToFunctionMap[key][1]] is not iterable"`
58
- - Better test coverage to the module with the addition of new test cases
59
- ### Version 1.2.3
60
- - Bug fix for masking a list of elements in the nested json. More details: [Mask multiple fields](#mask-multiple-fields)
61
- - Deprecated *maskJsonFields*(For documentation on the maskJsonFields, check previous version README.md files) function and will be removed in the subsequent versions. Use *maksJson2* instead: https://www.npmjs.com/package/maskdata#mask-json
62
61
 
63
62
  # How to Use
64
63
  ```javascript
@@ -518,6 +517,10 @@ const maskPasswordOptions = {
518
517
  // Default value is 16
519
518
  maxMaskedCharacters: 16,
520
519
 
520
+ // To fix the length of output irrespective of the length of the input. This comes in handy when the input length < maxMaskedCharacters but we want a fixed output length.
521
+ // Default value is undefined. If this value is set, then maxMaskedCharacters will not be considered and the output length will always be equal to fixedOutputLength characters.
522
+ fixedOutputLength: undefined,
523
+
521
524
  // To show(not mask) first 'n' characters in the password/secret key.
522
525
  // Default value is 0.
523
526
  unmaskedStartCharacters: 0,
@@ -542,6 +545,7 @@ const MaskData = require('./maskdata');
542
545
  maskWith: "*"
543
546
  maxMaskedCharacters: 16,
544
547
  unmaskedStartCharacters: 0,
548
+ fixedOutputLength: undefined,
545
549
  unmaskedEndCharacters: 0
546
550
  **/
547
551
 
@@ -560,7 +564,8 @@ const maskPasswordOptions = {
560
564
  maskWith: "X",
561
565
  maxMaskedCharacters: 20, // To limit the output String length to 20.
562
566
  unmaskedStartCharacters: 4,
563
- unmaskedEndCharacters: 9 // As the last 9 characters of the secret key is meta info which can be printed for debugging or other purposes
567
+ unmaskedEndCharacters: 9, // As the last 9 characters of the secret key is meta info which can be printed for debugging or other purposes
568
+ fixedOutputLength: undefined
564
569
  };
565
570
 
566
571
  const password = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
@@ -634,6 +639,10 @@ const defaultStringMaskV2Options = {
634
639
  // Default value is 256
635
640
  maxMaskedCharacters: 256,
636
641
 
642
+ // To fix the length of output irrespective of the length of the input. This comes in handy when the input length < maxMaskedCharacters but we want a fixed output length.
643
+ // Default value is undefined. If this value is set, then maxMaskedCharacters will not be considered and the output length will always be equal to fixedOutputLength characters.
644
+ fixedOutputLength: undefined,
645
+
637
646
  // To show(not mask) first 'n' characters of the string.
638
647
  // Default value is 0.
639
648
  unmaskedStartCharacters: 0,
@@ -659,7 +668,8 @@ const stringMaskV2Options = {
659
668
  maskWith: "X",
660
669
  maxMaskedCharacters: 20, // To limit the output length to 20.
661
670
  unmaskedStartCharacters: 4,
662
- unmaskedEndCharacters: 9
671
+ unmaskedEndCharacters: 9,
672
+ fixedOutputLength: undefined
663
673
  };
664
674
 
665
675
  const secret = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
@@ -1047,6 +1057,95 @@ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
1047
1057
 
1048
1058
  ```
1049
1059
 
1060
+ ### Recursive Masking
1061
+ If you want to mask all the fields with a name irrespective of its level in the json, use the `*{field_name}`.
1062
+
1063
+ Example:
1064
+ ```javascript
1065
+
1066
+ const inputJson = {
1067
+ car: "Mazda",
1068
+ addressLine1: "Mask me!",
1069
+ deeper: {
1070
+ addressLine1: "Mask me!",
1071
+ evenDeeper: { addressLine1: "Mask me!" },
1072
+ },
1073
+ };
1074
+
1075
+ const fieldsToMask = ["*addressLine1"]; // Specify the field with a '*' at the beginning and NO dot(.) anywhere else in that field.
1076
+
1077
+ const jsonMaskConfig = {
1078
+ genericStrings: [
1079
+ {
1080
+ config: {
1081
+ maskWith: "*",
1082
+ maxMaskedCharacters: 6
1083
+ },
1084
+ fields: fieldsToMask
1085
+ },
1086
+ ],
1087
+ };
1088
+
1089
+ const maskedOutput = maskData.maskJSON2(inputJson, jsonMaskConfig);
1090
+
1091
+ // Output:
1092
+ {
1093
+ car: 'Mazda',
1094
+ addressLine1: '******',
1095
+ deeper: { addressLine1: '******', evenDeeper: { addressLine1: '******' } }
1096
+ }
1097
+ ```
1098
+
1099
+ Example 2:
1100
+ Using Recursive masking to mask card numbers in a json using mask card config. Recursive masking can be used to mask all types of data mentioned [here](#mask-json)
1101
+
1102
+ ```javascript
1103
+
1104
+ const inputJson = {
1105
+ cards: [
1106
+ {
1107
+ number: "1234-5678-0123-0000"
1108
+ },
1109
+ {
1110
+ number: "1111-2222-3333-4444"
1111
+ },
1112
+ {
1113
+ number: "2222-4444-6666-8888"
1114
+ },
1115
+ {
1116
+ number: "1111-3333-5555-7777"
1117
+ },
1118
+ {
1119
+ number: "0000-0000-0000-0000"
1120
+ }
1121
+ ]
1122
+ };
1123
+
1124
+ const cardfieldsToMask = ["*number"]; // Specify the field with a '*' at the beginning and NO dot(.) anywhere else in that field.
1125
+
1126
+ const jsonMaskConfig = {
1127
+ cardMaskOptions: {
1128
+ maskWith: "*",
1129
+ unmaskedStartDigits: 4,
1130
+ unmaskedEndDigits: 1
1131
+ },
1132
+ cardFields: cardfieldsToMask,
1133
+ };
1134
+
1135
+ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
1136
+
1137
+ // Output:
1138
+ {
1139
+ cards: [
1140
+ { number: '1234-****-****-***0' },
1141
+ { number: '1111-****-****-***4' },
1142
+ { number: '2222-****-****-***8' },
1143
+ { number: '1111-****-****-***7' },
1144
+ { number: '0000-****-****-***0' }
1145
+ ]
1146
+ }
1147
+ ```
1148
+
1050
1149
 
1051
1150
  # Report Bugs
1052
1151
  If there is any help needed with the library functionalities or if there is any bug/issue, please raise an issue in GitHub: https://github.com/Sumukha1496/maskdata/issues
package/index.d.ts CHANGED
@@ -21,6 +21,7 @@ declare module 'maskdata' {
21
21
  export interface PasswordMaskOptions {
22
22
  maskWith?: string;
23
23
  maxMaskedCharacters?: number;
24
+ fixedOutputLength?: undefined | number;
24
25
  unmaskedStartCharacters?: number;
25
26
  unmaskedEndCharacters?: number;
26
27
  }
@@ -28,6 +29,7 @@ declare module 'maskdata' {
28
29
  export interface StringMaskV2Options {
29
30
  maskWith?: string;
30
31
  maxMaskedCharacters?: number;
32
+ fixedOutputLength?: undefined | number;
31
33
  unmaskedStartCharacters?: number;
32
34
  unmaskedEndCharacters?: number;
33
35
  }
@@ -29,6 +29,7 @@ const defaultJsonMaskOptions = {
29
29
  const defaultPasswordMaskOptions = {
30
30
  maskWith: '*',
31
31
  maxMaskedCharacters: 16,
32
+ fixedOutputLength: undefined,
32
33
  unmaskedStartCharacters: 0,
33
34
  unmaskedEndCharacters: 0
34
35
  };
@@ -36,6 +37,7 @@ const defaultPasswordMaskOptions = {
36
37
  const defaultStringMaskV2Options = {
37
38
  maskWith: '*',
38
39
  maxMaskedCharacters: 256,
40
+ fixedOutputLength: undefined,
39
41
  unmaskedStartCharacters: 0,
40
42
  unmaskedEndCharacters: 0
41
43
  };
@@ -1,4 +1,3 @@
1
- const { isBoolean } = require('lodash');
2
1
  const BadOption = require('../ExceptionsHandler/BadOption');
3
2
  const Constants = require('./Constants');
4
3
 
@@ -18,16 +17,19 @@ class MaskHelper {
18
17
  options.maxMaskedCharacters = parseInt(options.maxMaskedCharacters);
19
18
  options.unmaskedStartCharacters = parseInt(options.unmaskedStartCharacters);
20
19
  options.unmaskedEndCharacters = parseInt(options.unmaskedEndCharacters);
20
+ options.fixedOutputLength =
21
+ options.fixedOutputLength !== undefined ? parseInt(options.fixedOutputLength) : undefined;
21
22
  if (
22
23
  isNaN(options.maxMaskedCharacters) ||
23
24
  isNaN(options.unmaskedStartCharacters) ||
24
- isNaN(options.unmaskedEndCharacters)
25
+ isNaN(options.unmaskedEndCharacters) ||
26
+ (options.fixedOutputLength !== undefined && isNaN(options.fixedOutputLength))
25
27
  ) {
26
28
  throw Error();
27
29
  }
28
30
  } catch (err) {
29
31
  reasons.push(
30
- 'maxMaskedCharacters, unmaskedStartCharacters and unmaskedEndCharacters must be positive integer'
32
+ 'maxMaskedCharacters, unmaskedStartCharacters & unmaskedEndCharacters must be integers and fixedOutputLength must be undefined or an integer'
31
33
  );
32
34
  }
33
35
  if (options.maxMaskedCharacters < 0) {
@@ -39,6 +41,12 @@ class MaskHelper {
39
41
  if (options.unmaskedEndCharacters < 0) {
40
42
  options.unmaskedEndCharacters = 0;
41
43
  }
44
+ if (options.fixedOutputLength !== undefined && options.fixedOutputLength < 0) {
45
+ options.fixedOutputLength = undefined;
46
+ }
47
+ if (options.fixedOutputLength !== undefined && options.fixedOutputLength >= 0) {
48
+ options.maxMaskedCharacters = options.fixedOutputLength;
49
+ }
42
50
  if (!options.maskWith || options.maskWith.toString().length <= 0) {
43
51
  options.maskWith = '*';
44
52
  }
@@ -55,16 +63,19 @@ class MaskHelper {
55
63
  options.maxMaskedCharacters = parseInt(options.maxMaskedCharacters);
56
64
  options.unmaskedStartCharacters = parseInt(options.unmaskedStartCharacters);
57
65
  options.unmaskedEndCharacters = parseInt(options.unmaskedEndCharacters);
66
+ options.fixedOutputLength =
67
+ options.fixedOutputLength !== undefined ? parseInt(options.fixedOutputLength) : undefined;
58
68
  if (
59
69
  isNaN(options.maxMaskedCharacters) ||
60
70
  isNaN(options.unmaskedStartCharacters) ||
61
- isNaN(options.unmaskedEndCharacters)
71
+ isNaN(options.unmaskedEndCharacters) ||
72
+ (options.fixedOutputLength !== undefined && isNaN(options.fixedOutputLength))
62
73
  ) {
63
74
  throw Error();
64
75
  }
65
76
  } catch (err) {
66
77
  reasons.push(
67
- 'maxMaskedCharacters, unmaskedStartCharacters and unmaskedEndCharacters must be positive integer'
78
+ 'maxMaskedCharacters, unmaskedStartCharacters & unmaskedEndCharacters must be integers and fixedOutputLength must be undefined or an integer'
68
79
  );
69
80
  }
70
81
  if (options.maxMaskedCharacters < 0) {
@@ -76,6 +87,12 @@ class MaskHelper {
76
87
  if (options.unmaskedEndCharacters < 0) {
77
88
  options.unmaskedEndCharacters = 0;
78
89
  }
90
+ if (options.fixedOutputLength !== undefined && options.fixedOutputLength < 0) {
91
+ options.fixedOutputLength = undefined;
92
+ }
93
+ if (options.fixedOutputLength !== undefined && options.fixedOutputLength >= 0) {
94
+ options.maxMaskedCharacters = options.fixedOutputLength;
95
+ }
79
96
  if (!options.maskWith || options.maskWith.toString().length <= 0) {
80
97
  options.maskWith = '*';
81
98
  }
@@ -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
- console.warn('Ignoring the invalid field while masking: ', field);
61
- return;
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
  }
@@ -49,6 +49,12 @@ class StringMask {
49
49
  options = Constants.defaultStringMaskV2Options;
50
50
  }
51
51
  let maskStringLength = string.length;
52
+ if (options.fixedOutputLength !== undefined && options.fixedOutputLength >= 0) {
53
+ maskStringLength = options.fixedOutputLength;
54
+ if (string.length < options.fixedOutputLength) {
55
+ string = string + `${options.maskWith}`.repeat(options.fixedOutputLength - string.length);
56
+ }
57
+ }
52
58
  if (string.length > options.maxMaskedCharacters) {
53
59
  maskStringLength = parseInt(options.maxMaskedCharacters);
54
60
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
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": "^15.1.0",
53
- "prettier": "^3.0.3",
54
- "typescript": "^5.2.2"
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));
@@ -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
  {
@@ -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',
@@ -10,6 +10,7 @@ describe('Masking String V2', function () {
10
10
  // maxMaskedCharacters: 256,
11
11
  // unmaskedStartCharacters: 0,
12
12
  // unmaskedEndCharacters: 0
13
+ // fixedOutputLength: undefined
13
14
  // };
14
15
 
15
16
  let testData = [
@@ -121,6 +122,139 @@ describe('Masking String V2', function () {
121
122
  });
122
123
  });
123
124
 
125
+ describe('Mask with fixedOutputLength', function () {
126
+ const maskOptions = {
127
+ maskWith: 'X',
128
+ fixedOutputLength: 6
129
+ };
130
+
131
+ let testData = [
132
+ {
133
+ title: 'Random string',
134
+ input: '1234-5678-1234-5678',
135
+ output: 'XXXXXX'
136
+ },
137
+ {
138
+ title: 'input string length equal to fixedOutputLength',
139
+ input: '12-345',
140
+ output: 'XXXXXX'
141
+ },
142
+ {
143
+ title: 'input string length less than fixedOutputLength',
144
+ input: '12',
145
+ output: 'XXXXXX'
146
+ },
147
+ {
148
+ title: 'input string length greater than fixedOutputLength',
149
+ input: '1234567',
150
+ output: 'XXXXXX'
151
+ }
152
+ ];
153
+
154
+ testData.forEach(({ title, input, output }) => {
155
+ it(`custom mask - ${title}`, function () {
156
+ const masked = maskData.maskStringV2(input, maskOptions);
157
+ expect(masked).to.equal(output, 'masked output does not match expected value');
158
+ });
159
+ });
160
+
161
+ it('custom mask - test with fixedOutputLength and maxMaskedCharacters; No effect from maxMaskedCharacters', function () {
162
+ const option = {
163
+ maskWith: 'x',
164
+ maxMaskedCharacters: 4,
165
+ fixedOutputLength: 6
166
+ };
167
+ const input = '1234-5678-1234-5678';
168
+ const output = 'xxxxxx';
169
+ const masked = maskData.maskStringV2(input, option);
170
+ expect(masked).to.equal(output, 'masked output does not match expected value');
171
+ });
172
+
173
+ it('unmaskedStartCharacters + unmaskedEndCharacters < fixedOutputLength; No masking done', function () {
174
+ const shortMaxCharsOption = {
175
+ maskWith: 'x',
176
+ fixedOutputLength: 6,
177
+ unmaskedStartCharacters: 4, // 4+4=8 > 6
178
+ unmaskedEndCharacters: 4
179
+ };
180
+ const input = '1234-5678-1234-5678';
181
+ const output = '123478';
182
+ const masked = maskData.maskStringV2(input, shortMaxCharsOption);
183
+ expect(masked).to.equal(
184
+ output,
185
+ 'No masking done because fixedOutputLength shorter than start + end'
186
+ );
187
+ });
188
+
189
+ it('unmaskedStartCharacters + unmaskedEndCharacters = fixedOutputLength; No masking done', function () {
190
+ const shortMaxCharsOption = {
191
+ maskWith: 'x',
192
+ fixedOutputLength: 8,
193
+ unmaskedStartCharacters: 4, // 4+4=8
194
+ unmaskedEndCharacters: 4
195
+ };
196
+ const input = '1234-abcd-efgh-5678';
197
+ const output = '12345678';
198
+ const masked = maskData.maskStringV2(input, shortMaxCharsOption);
199
+ expect(masked).to.equal(
200
+ output,
201
+ 'No masking done because fixedOutputLength equal to start + end'
202
+ );
203
+ });
204
+ it('fixedOutputLength = null; Treat it as undefined and mask with other default configs', function () {
205
+ const config = {
206
+ maskWith: 'x',
207
+ fixedOutputLength: null
208
+ };
209
+ const input = '1234-abcd-efgh-5678';
210
+ const output = 'xxxxxxxxxxxxxxxxxxx';
211
+ const masked = maskData.maskStringV2(input, config);
212
+ expect(masked).to.equal(
213
+ output,
214
+ 'Default config masking should have been done as fixedOutputLength is null and is treated as undefined'
215
+ );
216
+ });
217
+ it('fixedOutputLength = undefined; Treat it as undefined and mask with other default configs', function () {
218
+ const config = {
219
+ maskWith: 'x',
220
+ fixedOutputLength: undefined
221
+ };
222
+ const input = '1234-abcd-efgh-5678';
223
+ const output = 'xxxxxxxxxxxxxxxxxxx';
224
+ const masked = maskData.maskStringV2(input, config);
225
+ expect(masked).to.equal(
226
+ output,
227
+ 'Default config masking should have been done as fixedOutputLength is undefined'
228
+ );
229
+ });
230
+ it('fixedOutputLength < 0; Treat it as undefined and mask with other default configs', function () {
231
+ const config = {
232
+ maskWith: 'x',
233
+ fixedOutputLength: -1
234
+ };
235
+ const input = '1234-abcd-efgh-5678';
236
+ const output = 'xxxxxxxxxxxxxxxxxxx';
237
+ const masked = maskData.maskStringV2(input, config);
238
+ expect(masked).to.equal(
239
+ output,
240
+ 'Default config masking should have been done as fixedOutputLength is negative as is treated as undefined'
241
+ );
242
+ });
243
+ it('fixedOutputLength = 0; Output length = 0 characters', function () {
244
+ const config = {
245
+ maskWith: 'x',
246
+ fixedOutputLength: 0
247
+ };
248
+ const input = '1234-abcd-efgh-5678';
249
+ const output = '';
250
+ const masked = maskData.maskStringV2(input, config);
251
+ expect(masked).to.equal(
252
+ output,
253
+ 'Output length should be 0 as fixedOutputLength is explicitly mentioned as 0'
254
+ );
255
+ });
256
+ });
257
+
124
258
  describe('Mask with special input strings', function () {
125
259
  const maskOptions = {
126
260
  maskWith: 'x',