Skip to content

Commit 80d87cb

Browse files
committed
feat: disable some built-in regex
1 parent 933d824 commit 80d87cb

File tree

3 files changed

+72
-25
lines changed

3 files changed

+72
-25
lines changed

src/tools/sensitive-data-masker/sensitive-data-masker.service.test.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ describe('sensitive-data-masker', () => {
1818
}`;
1919

2020
it('should maks sensitive data', () => {
21-
expect(maskSensitiveData(
22-
data,
23-
)).toBe(`{
21+
expect(maskSensitiveData({
22+
value: data,
23+
})).toBe(`{
2424
email: 'jo****************om',
2525
creditCard: '12***************76',
2626
id: '3f********************************7b',
2727
name: 'John',
2828
surname: 'Doe',
29-
phone: '+35**********67',
29+
phone: '+3***********67',
3030
url: 'tr***********om',
3131
ip4: '83*******56',
3232
ip6: '20*************************01',
@@ -35,21 +35,40 @@ describe('sensitive-data-masker', () => {
3535
}`);
3636
});
3737
it('should maks sensitive data (with custom regex)', () => {
38-
expect(maskSensitiveData(
39-
data,
40-
'John\nDoe',
41-
)).toBe(`{
38+
expect(maskSensitiveData({
39+
value: data,
40+
customRegex: 'John\nDoe',
41+
})).toBe(`{
4242
email: 'jo****************om',
4343
creditCard: '12***************76',
4444
id: '3f********************************7b',
4545
name: '****',
4646
surname: '***',
47-
phone: '+35**********67',
47+
phone: '+3***********67',
4848
url: 'tr***********om',
4949
ip4: '83*******56',
5050
ip6: '20*************************01',
5151
mac: '3D*************4F',
5252
token: 'ey*****************************************************************************************************************************************************************b8',
53+
}`);
54+
});
55+
56+
it('should maks sensitive data (with excluded matchers)', () => {
57+
expect(maskSensitiveData({
58+
value: data,
59+
excludedMatchers: ['mac', 'ipv4'],
60+
})).toBe(`{
61+
email: 'jo****************om',
62+
creditCard: '12***************76',
63+
id: '3f********************************7b',
64+
name: 'John',
65+
surname: 'Doe',
66+
phone: '+3***********67',
67+
url: 'tr***********om',
68+
ip4: '83.24.45.56',
69+
ip6: '20*************************01',
70+
mac: '3D:F2:C9:A6:B3:4F',
71+
token: 'ey*****************************************************************************************************************************************************************b8',
5372
}`);
5473
});
5574
});
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
import { maskString } from 'data-guardian';
2+
import ipRegex from 'ip-regex';
23

3-
const jwtRegex = /\b([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-\+\/=]*)\b/g;
4-
const phoneRegex = /\b(?:(\+\d{1,4})[-.\s]?)?(?:[(](\d{1,3})[)][-.\s]?)?(\d{1,4})[-.\s]?(\d{1,4})[-.\s]?(\d{1,9})\b/g;
4+
const jwtRegex = /\b([a-zA-Z0-9_=]{5,})\.([a-zA-Z0-9_=]{5,})\.([a-zA-Z0-9_\-\+\/=]{5,})\b/g;
5+
const phoneRegex = /(?:(\+\d{1,4})[-.\s]?)(?:[(](\d{1,3})[)][-.\s]?)?(\d{1,4})[-.\s]?(\d{1,4})[-.\s]?(\d{1,9})\b/g;
56
const macRegex = /\b([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})\b/g;
6-
const ipv6Regex = /\b(?:(::|[0-9a-fA-F]{1,4}:{1,2})([0-9a-fA-F]{1,4}:{1,2}){0,6}([0-9a-fA-F]{1,4}|::)?)\b/g;
7-
const urlWithOrWithoutPrefixRegex = /\b(https?:\/\/)?(www\\.)?[-a-zA-Z0-9@:%.\_\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%\_\\+.~#?&//=]\*)\b/g;
7+
const urlWithOrWithoutPrefixRegex = /\b(https?:\/\/)?(www\.)?[a-zA-Z0-9@:%._+~#=-]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&\/=]*)\b/g;
88

9-
export function maskSensitiveData(value: string, customRegex?: string) {
9+
export type MatcherNames = 'uuid' | 'creditCard' | 'ssn' | 'url' | 'ipv4' | 'email' | 'passwordInUri' | 'mac' | 'ipv6' | 'urlWithOrWithoutPrefix' | 'jwt' | 'phone';
10+
11+
export function maskSensitiveData({
12+
value,
13+
customRegex = '',
14+
excludedMatchers = [],
15+
}: {
16+
value: string
17+
customRegex?: string
18+
excludedMatchers?: Array<MatcherNames>
19+
}) {
20+
excludedMatchers = excludedMatchers || [];
21+
const emptyRegex = /(?:)/g;
1022
return maskString(value, null as never, {
1123
customRegex: new RegExp((customRegex || '').split('\n').map(line => `(${line})`).join('|'), 'gi'),
12-
macRegex,
13-
ipv6Regex,
14-
urlWithOrWithoutPrefixRegex,
15-
jwtRegex,
16-
phoneRegex,
24+
macRegex: excludedMatchers.includes('mac') ? emptyRegex : macRegex,
25+
ipv6Regex: excludedMatchers.includes('ipv6') ? emptyRegex : ipRegex.v6({ includeBoundaries: false }),
26+
urlWithOrWithoutPrefixRegex: excludedMatchers.includes('urlWithOrWithoutPrefix') ? emptyRegex : urlWithOrWithoutPrefixRegex,
27+
jwtRegex: excludedMatchers.includes('jwt') ? emptyRegex : jwtRegex,
28+
phoneRegex: excludedMatchers.includes('phone') ? emptyRegex : phoneRegex,
1729
}, {
18-
excludeMatchers: [
30+
excludeMatchers: [...excludedMatchers, ...[
1931
'passwordMention', 'password', 'passwordSubstring',
20-
],
32+
]],
2133
});
2234
}

src/tools/sensitive-data-masker/sensitive-data-masker.vue

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { maskSensitiveData } from './sensitive-data-masker.service';
2+
import { type MatcherNames, maskSensitiveData } from './sensitive-data-masker.service';
33
import { withDefaultOnError } from '@/utils/defaults';
44
55
const defaultValue = `{
@@ -17,12 +17,18 @@ const defaultValue = `{
1717
}`;
1818
1919
const customRegex = useStorage('sensitive-data:regex', '');
20+
const excludedMatchers = useStorage('sensitive-data:exclude', [] as string[]);
21+
const allMatchers = [
22+
'uuid', 'creditCard', 'ssn', 'url', 'ipv4', 'email',
23+
'passwordInUri', 'mac', 'ipv6', 'urlWithOrWithoutPrefix',
24+
'jwt', 'phone'];
2025
2126
function transformer(value: string) {
22-
return withDefaultOnError(() => maskSensitiveData(
27+
return withDefaultOnError(() => maskSensitiveData({
2328
value,
24-
customRegex.value,
25-
), '');
29+
customRegex: customRegex.value,
30+
excludedMatchers: excludedMatchers.value as MatcherNames[],
31+
}), '');
2632
}
2733
</script>
2834

@@ -35,6 +41,16 @@ function transformer(value: string) {
3541
raw-text
3642
multiline
3743
rows="4"
44+
mb-2
45+
/>
46+
47+
<n-select
48+
v-model:value="excludedMatchers"
49+
placeholder="No Fallback"
50+
multiple
51+
:fallback-option="false"
52+
:options="allMatchers.map(v => ({ label: v, value: v }))"
53+
mb-2
3854
/>
3955

4056
<format-transformer

0 commit comments

Comments
 (0)