maskdata 1.3.2 → 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.
@@ -0,0 +1,31 @@
1
+ ## Mask Password
2
+ ```javascript
3
+ const MaskData = require('./maskdata');
4
+
5
+ const maskPasswordOptions = {
6
+ // Character to mask the data
7
+ // default value is '*'
8
+ maskWith: "*",
9
+
10
+ // To limit the *s in the response when the password length is more
11
+ // Default value is 16
12
+ maxMaskedCharacters: 16,
13
+
14
+ // 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.
15
+ // 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.
16
+ fixedOutputLength: undefined,
17
+
18
+ // To show(not mask) first 'n' characters in the password/secret key.
19
+ // Default value is 0.
20
+ unmaskedStartCharacters: 0,
21
+
22
+ // To show(not mask) last 'n' characters in the password/secret key.
23
+ // Default value is 0.
24
+ unmaskedEndCharacters: 0
25
+ };
26
+
27
+ const password = "Password1$";
28
+
29
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
30
+
31
+ //Output: **********
@@ -0,0 +1,43 @@
1
+ ## Mask Password with the default configuration
2
+ To mask with the default options, don't pass the configurations.
3
+ ```javascript
4
+ const MaskData = require('./maskdata');
5
+
6
+ /** Default Options
7
+ maskWith: "*"
8
+ maxMaskedCharacters: 16,
9
+ unmaskedStartCharacters: 0,
10
+ fixedOutputLength: undefined,
11
+ unmaskedEndCharacters: 0
12
+ **/
13
+
14
+ const password = "Password1$";
15
+
16
+ const maskedPassword = MaskData.maskPassword(password)
17
+
18
+ ```
19
+
20
+ #### Example: Mask password OR secretKey with some meta info at the end
21
+
22
+ ```javascript
23
+ const MaskData = require('./maskdata');
24
+
25
+ const maskPasswordOptions = {
26
+ maskWith: "X",
27
+ maxMaskedCharacters: 20, // To limit the output String length to 20.
28
+ unmaskedStartCharacters: 4,
29
+ unmaskedEndCharacters: 9, // As the last 9 characters of the secret key is meta info which can be printed for debugging or other purposes
30
+ fixedOutputLength: undefined
31
+ };
32
+
33
+ const password = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
34
+
35
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
36
+ //Output: TESTXXXXXXX:CLIENT-A
37
+
38
+ maskPasswordOptions.unmaskedStartCharacters = 0;
39
+
40
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
41
+ //Output: XXXXXXXXXXX:CLIENT-A
42
+ ```
43
+
@@ -0,0 +1,26 @@
1
+ ## Mask Phone Number
2
+ ```javascript
3
+ const MaskData = require('./maskdata');
4
+
5
+ const maskPhoneOptions = {
6
+ // Character to mask the data
7
+ // default value is '*'
8
+ maskWith: "*",
9
+
10
+ //Should be positive Integer
11
+ // If the starting 'n' digits need to be unmasked
12
+ // Default value is 4
13
+ unmaskedStartDigits: 5,
14
+
15
+ // Should be positive Integer
16
+ //If the ending 'n' digits need to be unmasked
17
+ // Default value is 1
18
+ unmaskedEndDigits: 1
19
+ };
20
+
21
+ const phoneNumber = "+911234567890";
22
+
23
+ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber, maskPhoneOptions);
24
+ //Output: +9112*******0
25
+
26
+ ```
@@ -0,0 +1,155 @@
1
+ ## Mask Phone Number with the default configuration
2
+ To mask with the default options, don't pass the configurations.
3
+ ```javascript
4
+ const MaskData = require('./maskdata');
5
+
6
+ /** Default Options
7
+ maskWith: "*"
8
+ unmaskedStartDigits: 4
9
+ unmaskedEndDigits: 1
10
+ **/
11
+ const phoneNumber = "+111234567890";
12
+
13
+ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber);
14
+
15
+ //Output: +911********0
16
+
17
+ ```
18
+ ## Generic string masking
19
+ This functionality can be used to mask any string with the below configs.
20
+ #### Example1
21
+ ```javascript
22
+ const MaskData = require('./maskdata');
23
+
24
+ const defaultStringMaskV2Options = {
25
+ // Character to mask the data
26
+ // default value is '*'
27
+ maskWith: "*",
28
+
29
+ // This is to limit the maximun characters in the output.
30
+ // Default value is 256
31
+ maxMaskedCharacters: 256,
32
+
33
+ // 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.
34
+ // 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.
35
+ fixedOutputLength: undefined,
36
+
37
+ // To show(not mask) first 'n' characters of the string.
38
+ // Default value is 0.
39
+ unmaskedStartCharacters: 0,
40
+
41
+ // To show(not mask) last 'n' characters of the string.
42
+ // Default value is 0.
43
+ unmaskedEndCharacters: 0
44
+ };
45
+
46
+ const string1 = "Password1$";
47
+
48
+ const maskedString = MaskData.maskStringV2(string1, defaultStringMaskV2Options);
49
+
50
+ //Output: **********
51
+ ```
52
+
53
+ #### Example2
54
+
55
+ ```javascript
56
+ const MaskData = require('./maskdata');
57
+
58
+ const stringMaskV2Options = {
59
+ maskWith: "X",
60
+ maxMaskedCharacters: 20, // To limit the output length to 20.
61
+ unmaskedStartCharacters: 4,
62
+ unmaskedEndCharacters: 9,
63
+ fixedOutputLength: undefined
64
+ };
65
+
66
+ const secret = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
67
+
68
+ const maskedSecret = MaskData.maskStringV2(password, stringMaskV2Options);
69
+ //Output: TESTXXXXXXX:CLIENT-A
70
+
71
+ stringMaskV2Options.unmaskedStartCharacters = 0;
72
+
73
+ maskedSecret = MaskData.maskStringV2(password, stringMaskV2Options);
74
+ //Output: XXXXXXXXXXX:CLIENT-A
75
+ ```
76
+
77
+ ## Mask the characters or words in the string
78
+ This will mask the characters or words if present in the given string.
79
+ ```javascript
80
+ const MaskData = require('./maskdata');
81
+
82
+ const maskStringOptions = {
83
+ // Character to mask the data. The default value is '*'
84
+ maskWith: "*",
85
+
86
+ /**
87
+ * It is the words/substrings to mask.
88
+ * Should be an array of strings.
89
+ * Can give multiple words/substrings.
90
+ * values[] can be used only when maskAll is false. If maskAll is true, then this is of no use.
91
+ */
92
+ values: ['is', 'test'],
93
+
94
+ /**
95
+ * If to mask only the first occurrence of each word/substring in the given string
96
+ * Should be boolean
97
+ * Default value is false
98
+ */
99
+ maskOnlyFirstOccurance: false,
100
+
101
+ /**
102
+ * If to mask all the characters in a string make maskAll: true
103
+ * If maskAll is true, the words/substrings inside values[] will not be considered.
104
+ * Default value is false
105
+ */
106
+ maskAll: false,
107
+
108
+ /**
109
+ * This is to mask/not mask the spaces in a string when masking all the characters.
110
+ * Can be used ONLY when maskAll: true
111
+ * If maskSpace is false, the spaces in the string will not be masked.
112
+ * This feature is to know the words and each word's length but to hide the content
113
+ * Default value is true
114
+ */
115
+ maskSpace: true
116
+ };
117
+
118
+ const str = "This is a test String";
119
+
120
+ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
121
+
122
+ //Output: Th** ** a **** String
123
+
124
+ ```
125
+
126
+ ### Mask all characters in the String
127
+ ```javascript
128
+ const str = "This is a test String";
129
+
130
+ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
131
+
132
+ const stringMaskOptions = {
133
+ maskWith: "*",
134
+ values: [],
135
+ maskAll: true,
136
+ maskSpace: false // Maks all characters except spaces
137
+ };
138
+
139
+ // Output: **** ** * **** *****
140
+
141
+ const str = "This is a test String";
142
+
143
+ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
144
+
145
+ const stringMaskOptions = {
146
+ maskWith: "*",
147
+ values: [],
148
+ maskAll: true,
149
+ maskSpace: true // Mask all characters including spaces
150
+ };
151
+
152
+ // Output: ********************
153
+
154
+ ```
155
+
@@ -0,0 +1,30 @@
1
+ ## Mask UUID
2
+ This will mask the alphanumeric characters in a UUID.<br/>This will not mask the hyphen present in the UUID. Masking is done, only when the input is a valid UUID with only a-f, A-F, and 0-9 and has 36 characters (32 alphanumerics + 4 hyphens, and should work with the <b>regex:</b>
3
+ > /[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/g;).
4
+
5
+ If the input is not a valid UUID, it will return the input itself without masking.
6
+ ```javascript
7
+ const MaskData = require('./maskdata');
8
+
9
+ const maskUuidOptions = {
10
+ // Character to mask the data. The default value is '*'
11
+ maskWith: "*",
12
+
13
+ // Should be a positive Integer <= 32
14
+ // If the starting 'n' alphanumeric characters need to be visible/unmasked
15
+ // Default value is 0
16
+ unmaskedStartCharacters: 0,
17
+
18
+ //Should be a positive Integer <= 32
19
+ //If the ending 'n' alphanumeric characters need to be visible/unmasked
20
+ // Default value is 0
21
+ unmaskedEndCharacters: 0
22
+ };
23
+
24
+ const uuidInput = "123e4567-e89b-12d3-a456-426614174000";
25
+
26
+ const uuidAfterMasking = MaskData.maskUuid(uuidInput, maskUuidOptions);
27
+
28
+ // Output: ********-****-****-****-************
29
+
30
+ ```
@@ -0,0 +1,220 @@
1
+ ## Replace the value of a JSON field
2
+ To replace a value by keeping the type.
3
+ ```javascript
4
+
5
+ const input = {
6
+ name: "John",
7
+ age: 33,
8
+ married: true
9
+ }
10
+
11
+ let afterReplacing = MaskData.replaceValue(input, 'age', 99);
12
+ afterReplacing = MaskData.replaceValue(input, 'married', false);
13
+
14
+ //Output:
15
+ Before replacing: {"name":"John","age":33,"married":true}
16
+ After replacing: {"name":"John","age":99,"married":false}
17
+
18
+ Type of age: number
19
+ Type of married: boolean
20
+
21
+ ```
22
+
23
+ ### Mask Multiple Fields
24
+ ```javascript
25
+ const jsonInput = {
26
+ cards: [
27
+ {
28
+ number: '1234-5678-8765-1234'
29
+ },
30
+ {
31
+ number: '1111-2222-1111-2222'
32
+ },
33
+ {
34
+ number: '0000-1111-2222-3333'
35
+ },
36
+ {
37
+ name: "No card number here"
38
+ }
39
+ ],
40
+ emails: {
41
+ primaryEmail: 'primary@Email.com',
42
+ secondaryEmail: 'secondary@Email.com',
43
+ moreEmails: ["email1@email.com", "email2@email.com", "email3@email.com", {childEmail: "child@child.com", secondChild: {nestedkid: "hello@hello.com"}}]
44
+ },
45
+ array: ["element1", "element22", "element333"],
46
+ jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJsb2wiLCJuYW1lIjoiVGVzdCIsImlhdCI6ImxvbCJ9.XNDxZcBWWEKYkCiu6XFGmAeuPF7iFnI7Sdv91gVZJMU'
47
+ };
48
+
49
+ const jsonMaskConfig = {
50
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 0, unmaskedEndDigits: 0},
51
+
52
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 0,
53
+ unmaskedEndCharactersAfterAt: 0, maskAtTheRate: false },
54
+
55
+ stringMaskOptions: { maskWith: "?", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
56
+ jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 32, maskDot: false, maskHeader: true, maskPayload: true, maskSignature: true},
57
+
58
+ cardFields: ['cards[*].number'],
59
+ emailFields: ['emails.*'],
60
+ stringFields: ['array.*'],
61
+ jwtFields: ['jwt']
62
+ }
63
+
64
+ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
65
+
66
+ // Output:
67
+ {
68
+ "cards": [
69
+ {
70
+ "number": "XXXX-XXXX-XXXX-XXXX"
71
+ },
72
+ {
73
+ "number": "XXXX-XXXX-XXXX-XXXX"
74
+ },
75
+ {
76
+ "number": "XXXX-XXXX-XXXX-XXXX"
77
+ },
78
+ {
79
+ "name": "No card number here"
80
+ }
81
+ ],
82
+ "emails": {
83
+ "primaryEmail": "*******@*********",
84
+ "secondaryEmail": "*********@*********",
85
+ "moreEmails": [
86
+ "******@*********",
87
+ "******@*********",
88
+ "******@*********",
89
+ {
90
+ "childEmail": "*****@*********",
91
+ "secondChild": {
92
+ "nestedkid": "*****@*********"
93
+ }
94
+ }
95
+ ]
96
+ },
97
+ "array": ["????????", "?????????", "??????????"],
98
+ "jwt": "************.**********.**********"
99
+ }
100
+
101
+ ```
102
+
103
+ ### Recursive Masking
104
+ If you want to mask all the fields with a name irrespective of its level in the json, use the `*{field_name}`.
105
+
106
+ Example:
107
+ ```javascript
108
+
109
+ const inputJson = {
110
+ car: "Mazda",
111
+ addressLine1: "Mask me!",
112
+ deeper: {
113
+ addressLine1: "Mask me!",
114
+ evenDeeper: { addressLine1: "Mask me!" },
115
+ },
116
+ };
117
+
118
+ const fieldsToMask = ["*addressLine1"]; // Specify the field with a '*' at the beginning and NO dot(.) anywhere else in that field.
119
+
120
+ const jsonMaskConfig = {
121
+ genericStrings: [
122
+ {
123
+ config: {
124
+ maskWith: "*",
125
+ maxMaskedCharacters: 6
126
+ },
127
+ fields: fieldsToMask
128
+ },
129
+ ],
130
+ };
131
+
132
+ const maskedOutput = maskData.maskJSON2(inputJson, jsonMaskConfig);
133
+
134
+ // Output:
135
+ {
136
+ car: 'Mazda',
137
+ addressLine1: '******',
138
+ deeper: { addressLine1: '******', evenDeeper: { addressLine1: '******' } }
139
+ }
140
+ ```
141
+
142
+ Example 2:
143
+ 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)
144
+
145
+ ```javascript
146
+
147
+ const inputJson = {
148
+ cards: [
149
+ {
150
+ number: "1234-5678-0123-0000"
151
+ },
152
+ {
153
+ number: "1111-2222-3333-4444"
154
+ },
155
+ {
156
+ number: "2222-4444-6666-8888"
157
+ },
158
+ {
159
+ number: "1111-3333-5555-7777"
160
+ },
161
+ {
162
+ number: "0000-0000-0000-0000"
163
+ }
164
+ ]
165
+ };
166
+
167
+ const cardfieldsToMask = ["*number"]; // Specify the field with a '*' at the beginning and NO dot(.) anywhere else in that field.
168
+
169
+ const jsonMaskConfig = {
170
+ cardMaskOptions: {
171
+ maskWith: "*",
172
+ unmaskedStartDigits: 4,
173
+ unmaskedEndDigits: 1
174
+ },
175
+ cardFields: cardfieldsToMask,
176
+ };
177
+
178
+ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
179
+
180
+ // Output:
181
+ {
182
+ cards: [
183
+ { number: '1234-****-****-***0' },
184
+ { number: '1111-****-****-***4' },
185
+ { number: '2222-****-****-***8' },
186
+ { number: '1111-****-****-***7' },
187
+ { number: '0000-****-****-***0' }
188
+ ]
189
+ }
190
+ ```
191
+
192
+
193
+ # Report Bugs
194
+ 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
195
+
196
+ # Give a Star
197
+ You can give a star at: https://github.com/Sumukha1496/maskdata/stargazers
198
+
199
+ # LICENSE - "MIT"
200
+ Licensed under MIT Licence
201
+
202
+ Copyright (c) 2019 Sumukha H S
203
+
204
+ Permission is hereby granted, free of charge, to any person obtaining a copy
205
+ of this software and associated documentation files (the "Software"), to deal
206
+ in the Software without restriction, including without limitation the rights
207
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
208
+ copies of the Software, and to permit persons to whom the Software is
209
+ furnished to do so, subject to the following conditions:
210
+
211
+ The above copyright notice and this permission notice shall be included in all
212
+ copies or substantial portions of the Software.
213
+
214
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
215
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
216
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
217
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
218
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
219
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
220
+ SOFTWARE.
package/index.d.ts CHANGED
@@ -51,6 +51,7 @@ declare module 'maskdata' {
51
51
  values?: string[];
52
52
  maskAll?: boolean;
53
53
  maskSpace?: boolean;
54
+ maxMaskedCharacters?: number;
54
55
  }
55
56
 
56
57
  export interface UuidMaskOptions {
@@ -91,6 +92,11 @@ declare module 'maskdata' {
91
92
  export function maskPhone(phone?: string, options?: PhoneMaskOptions): string;
92
93
  export function maskEmail2(email?: string, options?: EmailMask2Options): string;
93
94
  export function maskCard(cardNumber?: string, options?: CardMaskOptions): string;
95
+ /**
96
+ * @deprecated since version 1.3.3
97
+ * This function will be removed in future versions.
98
+ * Use `maskStringV2` instead.
99
+ */
94
100
  export function maskString(str?: string, options?: StringMaskOptions): string;
95
101
  export function maskStringV2(str?: string, options?: StringMaskV2Options): string;
96
102
  export function maskUuid(uuid?: string, options?: UuidMaskOptions): string;
package/index.js CHANGED
@@ -31,6 +31,11 @@ class MaskData {
31
31
  return MaskCard.maskCard(cardNumber, options);
32
32
  }
33
33
 
34
+ /**
35
+ * @deprecated since version 1.3.3
36
+ * This function will be removed in future versions.
37
+ * Use `maskStringV2` instead.
38
+ */
34
39
  static maskString(str, options) {
35
40
  return MaskString.maskString(str, options);
36
41
  }
@@ -53,7 +53,8 @@ const defaultStringMaskOptions = {
53
53
  maskOnlyFirstOccurance: false,
54
54
  values: [],
55
55
  maskAll: false,
56
- maskSpace: true
56
+ maskSpace: true,
57
+ maxMaskedCharacters: 2048
57
58
  };
58
59
 
59
60
  const defaultUuidMaskOptions = {
@@ -252,9 +252,20 @@ class MaskHelper {
252
252
  if (typeof options.maskSpace !== 'boolean') {
253
253
  reasons.push('maskSpace should be a boolean field');
254
254
  }
255
+ try {
256
+ options.maxMaskedCharacters = parseInt(options.maxMaskedCharacters);
257
+ if (isNaN(options.maxMaskedCharacters)) {
258
+ throw Error();
259
+ }
260
+ } catch (err) {
261
+ reasons.push('maxMaskedCharacters must be a positive integer');
262
+ }
255
263
  if (!options.maskWith || options.maskWith.toString().length <= 0) {
256
264
  options.maskWith = '*';
257
265
  }
266
+ if (options.maxMaskedCharacters < 0) {
267
+ options.maxMaskedCharacters = 2048;
268
+ }
258
269
  if (reasons.length > 0) {
259
270
  const badOptions = new BadOption('string mask configuration', reasons);
260
271
  badOptions.throwBadOptionException();
@@ -295,7 +306,7 @@ class MaskHelper {
295
306
  throw Error();
296
307
  }
297
308
  } catch (err) {
298
- reasons.push('maxMaskedCharacters must be a positive integera');
309
+ reasons.push('maxMaskedCharacters must be a positive integer');
299
310
  }
300
311
 
301
312
  if (options.maskDot === null || options.maskDot === undefined) {
@@ -33,7 +33,9 @@ class StringMask {
33
33
  }
34
34
  } else {
35
35
  for (const value of Object.values(values)) {
36
- str = str.replace(new RegExp(value, 'g'), `${options.maskWith}`.repeat(value.length));
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.3.2",
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",