diff --git a/package.json b/package.json index e8cdd8e8..3d621a7b 100644 --- a/package.json +++ b/package.json @@ -112,6 +112,7 @@ "curlconverter": "^4.10.1", "currency-codes-ts": "^3.0.0", "currency-exchanger-js": "^1.0.4", + "data-guardian": "^1.1.3", "date-fns": "^2.29.3", "dns-query": "^0.11.2", "decomposerize": "^1.4.1", @@ -147,6 +148,7 @@ "image-in-browser": "^3.2.0", "ip-bigint": "^8.2.0", "jq-wasm": "^0.0.9", + "ip-regex": "^5.0.0", "js-base64": "^3.7.6", "image-in-browser": "^3.1.0", "js-base64": "^3.7.7", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3a24c8a1..3097382e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -200,6 +200,9 @@ dependencies: currency-exchanger-js: specifier: ^1.0.4 version: 1.0.4 + data-guardian: + specifier: ^1.1.3 + version: 1.1.3 date-fns: specifier: ^2.29.3 version: 2.29.3 @@ -278,6 +281,9 @@ dependencies: ip-bigint: specifier: ^8.2.0 version: 8.2.0 + ip-regex: + specifier: ^5.0.0 + version: 5.0.0 iso-639-1: specifier: ^3.1.3 version: 3.1.3 @@ -6243,7 +6249,7 @@ packages: /@vueuse/shared@10.0.0(vue@3.3.4): resolution: {integrity: sha512-Zh3LgJqvUBWVY3SiMvXanTcfAneGbt63QPczBRDNgQ6jd/ehodO9a1lCFzaA6SWJJoI+ugVTjHFYJdoR656DVQ==} dependencies: - vue-demi: 0.14.10(vue@3.3.4) + vue-demi: 0.14.5(vue@3.3.4) transitivePeerDependencies: - '@vue/composition-api' - vue @@ -7538,6 +7544,11 @@ packages: assert-plus: 1.0.0 dev: false + /data-guardian@1.1.3: + resolution: {integrity: sha512-8M9KHTopFp3MXo7NelcmF+8L77S5jeDy3Uv4RWuYIQSaBZTawvY6W3ad/eLe8db2FcLPoC44DHJb4zWNcpoueQ==} + engines: {node: '>=12'} + dev: false + /data-uri-to-buffer@4.0.1: resolution: {integrity: sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==} engines: {node: '>= 12'} @@ -9577,6 +9588,11 @@ packages: jsbn: 1.1.0 dev: false + /ip-regex@5.0.0: + resolution: {integrity: sha512-fOCG6lhoKKakwv+C6KdsOnGvgXnmgfmp0myi3bcNwj3qfwPAxRKWEuFhvEFF7ceYIz6+1jRZ+yguLFAmUNPEfw==} + engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + dev: false + /is-alphabetical@1.0.4: resolution: {integrity: sha512-DwzsA04LQ10FHTZuL0/grVDk4rFoVH1pjAToYwBrHSxcrBIGQuXrQMtD5U1b0U2XVgKZCTLLP8u2Qxqhy3l2Vg==} dev: true @@ -10106,7 +10122,6 @@ packages: /jsesc@2.5.2: resolution: {integrity: sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==} engines: {node: '>=4'} - hasBin: true /jsesc@3.0.2: resolution: {integrity: sha512-xKqzzWXDttJuOcawBt4KnKHHIf5oQ/Cxax+0PWFG+DFDgHNAdi+TXECADI+RYiFUMmx8792xsMbbgXj4CwnP4g==} diff --git a/src/tools/index.ts b/src/tools/index.ts index 1eb708fa..045252ff 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -76,6 +76,7 @@ import { tool as markdownToHtml } from './markdown-to-html'; import { tool as nginxFormatter } from './nginx-formatter'; import { tool as potrace } from './potrace'; import { tool as rmbNumbers } from './rmb-numbers'; +import { tool as sensitiveDataMasker } from './sensitive-data-masker'; import { tool as pdfSignatureChecker } from './pdf-signature-checker'; import { tool as numeronymGenerator } from './numeronym-generator'; import { tool as macAddressGenerator } from './mac-address-generator'; @@ -375,6 +376,7 @@ export const toolsByCategory: ToolCategory[] = [ icalGenerator, icalParser, icalMerger, + sensitiveDataMasker, ], }, { diff --git a/src/tools/sensitive-data-masker/index.ts b/src/tools/sensitive-data-masker/index.ts new file mode 100644 index 00000000..ccafe88f --- /dev/null +++ b/src/tools/sensitive-data-masker/index.ts @@ -0,0 +1,12 @@ +import { ShieldLock } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Sensitive data masker', + path: '/sensitive-data-masker', + description: 'Clean sensitive data from textual content (ie logs)', + keywords: ['sensitive', 'data', 'masker', 'obfuscator', 'clean', 'log'], + component: () => import('./sensitive-data-masker.vue'), + icon: ShieldLock, + createdAt: new Date('2024-06-16'), +}); diff --git a/src/tools/sensitive-data-masker/sensitive-data-masker.service.test.ts b/src/tools/sensitive-data-masker/sensitive-data-masker.service.test.ts new file mode 100644 index 00000000..49c61137 --- /dev/null +++ b/src/tools/sensitive-data-masker/sensitive-data-masker.service.test.ts @@ -0,0 +1,75 @@ +import { describe, expect, it } from 'vitest'; +import { maskSensitiveData } from './sensitive-data-masker.service'; + +describe('sensitive-data-masker', () => { + describe('maskSensitiveData', () => { + const data = `{ + email: 'john.doe@example.com', + creditCard: '1234 5678 9000 9876', + id: '3f8a43fd-6489-4ec7-bd55-7a1ba172d77b', + name: 'John', + surname: 'Doe', + phone: '+358 40 1234567', + url: 'truc.google.com', + ip4: '83.24.45.56', + ip6: '2001:db8:0:85a3:0:0:ac1f:8001', + mac: '3D:F2:C9:A6:B3:4F', + token: 'eyJhbGciOiJIUzI1NiJ9.ew0KICAic3ViIjogIjEyMzQ1Njc4OTAiLA0KICAibmFtZSI6ICJBbGV4IEtvemxvdiIsDQogICJpYXQiOiAxNTE2MjM5MDIyDQp9.PNKysYFTCenU5bekHCmwIxCUXoYG41H_xc3uN3ZF_b8', +}`; + + it('should maks sensitive data', () => { + expect(maskSensitiveData({ + value: data, + })).toBe(`{ + email: 'jo****************om', + creditCard: '12***************76', + id: '3f********************************7b', + name: 'John', + surname: 'Doe', + phone: '+3***********67', + url: 'tr***********om', + ip4: '83*******56', + ip6: '20*************************01', + mac: '3D*************4F', + token: 'ey*****************************************************************************************************************************************************************b8', +}`); + }); + it('should maks sensitive data (with custom regex)', () => { + expect(maskSensitiveData({ + value: data, + customRegex: 'John\nDoe', + })).toBe(`{ + email: 'jo****************om', + creditCard: '12***************76', + id: '3f********************************7b', + name: '****', + surname: '***', + phone: '+3***********67', + url: 'tr***********om', + ip4: '83*******56', + ip6: '20*************************01', + mac: '3D*************4F', + token: 'ey*****************************************************************************************************************************************************************b8', +}`); + }); + + it('should maks sensitive data (with excluded matchers)', () => { + expect(maskSensitiveData({ + value: data, + excludedMatchers: ['mac', 'ipv4'], + })).toBe(`{ + email: 'jo****************om', + creditCard: '12***************76', + id: '3f********************************7b', + name: 'John', + surname: 'Doe', + phone: '+3***********67', + url: 'tr***********om', + ip4: '83.24.45.56', + ip6: '20*************************01', + mac: '3D:F2:C9:A6:B3:4F', + token: 'ey*****************************************************************************************************************************************************************b8', +}`); + }); + }); +}); diff --git a/src/tools/sensitive-data-masker/sensitive-data-masker.service.ts b/src/tools/sensitive-data-masker/sensitive-data-masker.service.ts new file mode 100644 index 00000000..28e6aa71 --- /dev/null +++ b/src/tools/sensitive-data-masker/sensitive-data-masker.service.ts @@ -0,0 +1,34 @@ +import { type SensitiveContentKey, maskString } from 'data-guardian'; +import ipRegex from 'ip-regex'; + +const jwtRegex = /\b([a-zA-Z0-9_=]{5,})\.([a-zA-Z0-9_=]{5,})\.([a-zA-Z0-9_\-\+\/=]{5,})\b/g; +const phoneRegex = /(?:(\+\d{1,4})[-.\s]?)(?:[(](\d{1,3})[)][-.\s]?)?(\d{1,4})[-.\s]?(\d{1,4})[-.\s]?(\d{1,9})\b/g; +const macRegex = /\b([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})\b/g; +const urlWithOrWithoutPrefixRegex = /\b(https?:\/\/)?(www\.)?[a-zA-Z0-9@:%._+~#=-]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&\/=]*)\b/g; + +export type MatcherNames = 'uuid' | 'creditCard' | 'ssn' | 'url' | 'ipv4' | 'email' | 'passwordInUri' | 'mac' | 'ipv6' | 'urlWithOrWithoutPrefix' | 'jwt' | 'phone'; + +export function maskSensitiveData({ + value, + customRegex = '', + excludedMatchers = [], +}: { + value: string + customRegex?: string + excludedMatchers?: Array +}) { + excludedMatchers = excludedMatchers || []; + const emptyRegex = /(?:)/g; + return maskString(value, null as never, { + customRegex: new RegExp((customRegex || '').split('\n').map(line => `(${line})`).join('|'), 'gi'), + macRegex: excludedMatchers.includes('mac') ? emptyRegex : macRegex, + ipv6Regex: excludedMatchers.includes('ipv6') ? emptyRegex : ipRegex.v6({ includeBoundaries: false }), + urlWithOrWithoutPrefixRegex: excludedMatchers.includes('urlWithOrWithoutPrefix') ? emptyRegex : urlWithOrWithoutPrefixRegex, + jwtRegex: excludedMatchers.includes('jwt') ? emptyRegex : jwtRegex, + phoneRegex: excludedMatchers.includes('phone') ? emptyRegex : phoneRegex, + }, { + excludeMatchers: [...excludedMatchers, ...[ + 'passwordMention', 'password', 'passwordSubstring', + ]] as SensitiveContentKey[], + }); +} diff --git a/src/tools/sensitive-data-masker/sensitive-data-masker.vue b/src/tools/sensitive-data-masker/sensitive-data-masker.vue new file mode 100644 index 00000000..7c9d970e --- /dev/null +++ b/src/tools/sensitive-data-masker/sensitive-data-masker.vue @@ -0,0 +1,64 @@ + + +