Skip to content

Commit 933d824

Browse files
committed
feat: enhance cleaned PII and custom regexes
1 parent e6e51cf commit 933d824

File tree

5 files changed

+119
-39
lines changed

5 files changed

+119
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"cron-validator": "^1.3.1",
5656
"cronstrue": "^2.26.0",
5757
"crypto-js": "^4.1.1",
58+
"data-guardian": "^1.1.3",
5859
"date-fns": "^2.29.3",
5960
"dompurify": "^3.0.6",
6061
"emojilib": "^3.0.10",
@@ -70,7 +71,6 @@
7071
"libphonenumber-js": "^1.10.28",
7172
"lodash": "^4.17.21",
7273
"marked": "^10.0.0",
73-
"mask-sensitive-data": "^0.11.5",
7474
"mathjs": "^11.9.1",
7575
"mime-types": "^2.1.35",
7676
"monaco-editor": "^0.43.0",

pnpm-lock.yaml

Lines changed: 8 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { maskSensitiveData } from './sensitive-data-masker.service';
3+
4+
describe('sensitive-data-masker', () => {
5+
describe('maskSensitiveData', () => {
6+
const data = `{
7+
8+
creditCard: '1234 5678 9000 9876',
9+
id: '3f8a43fd-6489-4ec7-bd55-7a1ba172d77b',
10+
name: 'John',
11+
surname: 'Doe',
12+
phone: '+358 40 1234567',
13+
url: 'truc.google.com',
14+
ip4: '83.24.45.56',
15+
ip6: '2001:db8:0:85a3:0:0:ac1f:8001',
16+
mac: '3D:F2:C9:A6:B3:4F',
17+
token: 'eyJhbGciOiJIUzI1NiJ9.ew0KICAic3ViIjogIjEyMzQ1Njc4OTAiLA0KICAibmFtZSI6ICJBbGV4IEtvemxvdiIsDQogICJpYXQiOiAxNTE2MjM5MDIyDQp9.PNKysYFTCenU5bekHCmwIxCUXoYG41H_xc3uN3ZF_b8',
18+
}`;
19+
20+
it('should maks sensitive data', () => {
21+
expect(maskSensitiveData(
22+
data,
23+
)).toBe(`{
24+
email: 'jo****************om',
25+
creditCard: '12***************76',
26+
id: '3f********************************7b',
27+
name: 'John',
28+
surname: 'Doe',
29+
phone: '+35**********67',
30+
url: 'tr***********om',
31+
ip4: '83*******56',
32+
ip6: '20*************************01',
33+
mac: '3D*************4F',
34+
token: 'ey*****************************************************************************************************************************************************************b8',
35+
}`);
36+
});
37+
it('should maks sensitive data (with custom regex)', () => {
38+
expect(maskSensitiveData(
39+
data,
40+
'John\nDoe',
41+
)).toBe(`{
42+
email: 'jo****************om',
43+
creditCard: '12***************76',
44+
id: '3f********************************7b',
45+
name: '****',
46+
surname: '***',
47+
phone: '+35**********67',
48+
url: 'tr***********om',
49+
ip4: '83*******56',
50+
ip6: '20*************************01',
51+
mac: '3D*************4F',
52+
token: 'ey*****************************************************************************************************************************************************************b8',
53+
}`);
54+
});
55+
});
56+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { maskString } from 'data-guardian';
2+
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;
5+
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;
8+
9+
export function maskSensitiveData(value: string, customRegex?: string) {
10+
return maskString(value, null as never, {
11+
customRegex: new RegExp((customRegex || '').split('\n').map(line => `(${line})`).join('|'), 'gi'),
12+
macRegex,
13+
ipv6Regex,
14+
urlWithOrWithoutPrefixRegex,
15+
jwtRegex,
16+
phoneRegex,
17+
}, {
18+
excludeMatchers: [
19+
'passwordMention', 'password', 'passwordSubstring',
20+
],
21+
});
22+
}
Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
11
<script setup lang="ts">
2-
import maskSensitiveData from 'mask-sensitive-data';
2+
import { maskSensitiveData } from './sensitive-data-masker.service';
33
import { withDefaultOnError } from '@/utils/defaults';
44
5-
const defaultValue = `
6-
{
5+
const defaultValue = `{
76
87
creditCard: '1234 5678 9000 9876',
98
id: '3f8a43fd-6489-4ec7-bd55-7a1ba172d77b',
109
name: 'John',
1110
surname: 'Doe',
1211
phone: '+358 40 1234567',
12+
url: 'truc.google.com',
13+
ip4: '83.24.45.56',
14+
ip6: '2001:db8:0:85a3:0:0:ac1f:8001',
15+
mac: '3D:F2:C9:A6:B3:4F',
1316
token: 'eyJhbGciOiJIUzI1NiJ9.ew0KICAic3ViIjogIjEyMzQ1Njc4OTAiLA0KICAibmFtZSI6ICJBbGV4IEtvemxvdiIsDQogICJpYXQiOiAxNTE2MjM5MDIyDQp9.PNKysYFTCenU5bekHCmwIxCUXoYG41H_xc3uN3ZF_b8',
14-
}
15-
`;
17+
}`;
18+
19+
const customRegex = useStorage('sensitive-data:regex', '');
20+
1621
function transformer(value: string) {
17-
return withDefaultOnError(() => maskSensitiveData(value), '');
22+
return withDefaultOnError(() => maskSensitiveData(
23+
value,
24+
customRegex.value,
25+
), '');
1826
}
1927
</script>
2028

2129
<template>
22-
<format-transformer
23-
input-label="Your log/textual data:"
24-
:input-default="defaultValue"
25-
input-placeholder="Paste your log/textual data here..."
26-
output-label="Cleaned version:"
27-
:transformer="transformer"
28-
/>
30+
<div style="max-width: 600px;">
31+
<c-input-text
32+
v-model:value="customRegex"
33+
label="Your custom cleaning regex(es) (case insensitive):"
34+
placeholder="Your custom cleaning regex(es)"
35+
raw-text
36+
multiline
37+
rows="4"
38+
/>
39+
40+
<format-transformer
41+
input-label="Your log/textual data:"
42+
:input-default="defaultValue"
43+
input-placeholder="Paste your log/textual data here..."
44+
output-label="Cleaned version:"
45+
:transformer="transformer"
46+
/>
47+
</div>
2948
</template>

0 commit comments

Comments
 (0)