diff --git a/package.json b/package.json index 2a3889ee..f4c676db 100644 --- a/package.json +++ b/package.json @@ -277,6 +277,7 @@ "@types/figlet": "^1.7.0", "@types/js-beautify": "^1.14.3", "@types/hex-array": "^1.0.2", + "@types/har-format": "^1.2.16", "@types/jsdom": "^21.1.7", "@types/jsonpath": "^0.2.4", "@types/libmime": "^5.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index adc5123c..1064bc22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -715,6 +715,9 @@ devDependencies: '@types/figlet': specifier: ^1.7.0 version: 1.7.0 + '@types/har-format': + specifier: ^1.2.16 + version: 1.2.16 '@types/hex-array': specifier: ^1.0.2 version: 1.0.2 @@ -5733,6 +5736,10 @@ packages: /@types/gensync@1.0.4: resolution: {integrity: sha512-C3YYeRQWp2fmq9OryX+FoDy8nXS6scQ7dPptD8LnFDAUNcKWJjXQKDNJD3HVm+kOUsXhTOkpi69vI4EuAr95bA==} + /@types/har-format@1.2.16: + resolution: {integrity: sha512-fluxdy7ryD3MV6h8pTfTYpy/xQzCFC7m89nOH9y94cNqJ1mDIDPut7MnRHI3F6qRmh/cT2fUjG1MLdCNb4hE9A==} + dev: true + /@types/hast@3.0.4: resolution: {integrity: sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==} dependencies: diff --git a/src/tools/har-sanitizer/har-sanitizer.vue b/src/tools/har-sanitizer/har-sanitizer.vue new file mode 100644 index 00000000..ba4a7b4f --- /dev/null +++ b/src/tools/har-sanitizer/har-sanitizer.vue @@ -0,0 +1,150 @@ + + + diff --git a/src/tools/har-sanitizer/index.ts b/src/tools/har-sanitizer/index.ts new file mode 100644 index 00000000..337cbc0a --- /dev/null +++ b/src/tools/har-sanitizer/index.ts @@ -0,0 +1,12 @@ +import { ClearFormatting } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'HAR Sanitizer', + path: '/har-sanitizer', + description: 'HAR Files Sanitizer', + keywords: ['har', 'sanitizer'], + component: () => import('./har-sanitizer.vue'), + icon: ClearFormatting, + createdAt: new Date('2024-06-17'), +}); diff --git a/src/tools/har-sanitizer/lib/downloadFile.ts b/src/tools/har-sanitizer/lib/downloadFile.ts new file mode 100644 index 00000000..8a54741d --- /dev/null +++ b/src/tools/har-sanitizer/lib/downloadFile.ts @@ -0,0 +1,19 @@ +export function downloadFile(harOutput: string, name: string) { + const blob = new Blob([harOutput], { type: 'application/json' }); + + // Create a URL for the Blob + const url = URL.createObjectURL(blob); + + // Create an anchor element to trigger the download + const a = document.createElement('a'); + a.href = url; + // Set file name + a.download = name; + a.style.display = 'none'; + document.body.appendChild(a); + a.click(); + + // Clean up by removing the anchor and revoking the URL + document.body.removeChild(a); + URL.revokeObjectURL(url); +} diff --git a/src/tools/har-sanitizer/lib/har_sanitize.tsx b/src/tools/har-sanitizer/lib/har_sanitize.tsx new file mode 100644 index 00000000..0b804ecd --- /dev/null +++ b/src/tools/har-sanitizer/lib/har_sanitize.tsx @@ -0,0 +1,237 @@ +/* eslint-disable prefer-regex-literals */ +import type { Cookie, Har, Header, Param, QueryString } from 'har-format'; + +export interface PossibleScrubItems { + headers: string[] + cookies: string[] + queryArgs: string[] + postParams: string[] + mimeTypes: string[] +} + +const defaultMimeTypesList = ['application/javascript', 'text/javascript']; + +const defaultWordList = [ + 'Authorization', + 'SAMLRequest', + 'SAMLResponse', + 'access_token', + 'appID', + 'assertion', + 'auth', + 'authenticity_token', + 'challenge', + 'client_id', + 'client_secret', + 'code', + 'code_challenge', + 'code_verifier', + 'email', + 'facetID', + 'fcParams', + 'id_token', + 'password', + 'refresh_token', + 'serverData', + 'shdf', + 'state', + 'token', + 'usg', + 'vses2', + 'x-client-data', +]; + +export const defaultScrubItems = [...defaultMimeTypesList, ...defaultWordList]; + +// The default list of regexes that aren't word dependent +// Uses double list so it matches format of word regex +const defaultRegex = [ + [ + // Redact signature on JWTs + { + regex: new RegExp( + '\\b(ey[A-Za-z0-9-_=]+)\\.(ey[A-Za-z0-9-_=]+)\\.[A-Za-z0-9-_.+/=]+\\b', + 'g', + ), + replacement: '$1.$2.redacted', + }, + ], +]; + +function buildRegex(word: string) { + return [ + { + // [full word]=[capture] + regex: new RegExp( + `([\\s";,&?]+${word}=)([\\w+-_/=#|.%&:!*()\`~'"]+?)(&|\\\\",|",|"\\s|"}}|;){1}`, + 'g', + ), + replacement: `$1[${word} redacted]$3`, + }, + // Set up this way in case "value" isn't directly after "name" + // { + // "name": "[word]", + // "something": "not wanted", + // "value": "[capture]" + // } + { + regex: new RegExp( + `("name": "${word}",[\\s\\w+:"-\\%!*()\`~'.,#]*?"value": ")((?:\\\\"|[^"])*?)(")`, + 'g', + ), + replacement: `$1[${word} redacted]$3`, + }, + // "name" comes after "value" + // { + // "value": "[capture]", + // "something": "not wanted", + // "name": "[word]" + // } + { + regex: new RegExp( + `("value": ")([\\w+-_:&+=#$~/()\\\\.\\,*!|%"\\s;]+)("[,\\s}}]+)([\\s\\w+:"-\\\\%!*\`()~'#.]*"name": "${word}")`, + 'g', + ), + replacement: `$1[${word} redacted]$3$4`, + }, + ]; +} + +function removeContentForMimeTypes(input: string, scrubList: string[]) { + const harJSON = JSON.parse(input); + + const entries = harJSON.log.entries; + if (!entries) { + throw new Error('failed to find entries in HAR file'); + } + + for (const entry of entries) { + const response = entry.response; + if (response && scrubList.includes(response.content.mimeType)) { + response.content.text = `[${response.content.mimeType} redacted]`; + } + } + + return JSON.stringify(harJSON, null, 2); +} + +export function getHarInfo(input: string): PossibleScrubItems { + const output = { + headers: new Set(), + queryArgs: new Set(), + cookies: new Set(), + postParams: new Set(), + mimeTypes: new Set(), + }; + const harJSON: Har = JSON.parse(input); + + const entries = harJSON.log.entries; + if (!entries) { + throw new Error('failed to find entries in HAR file'); + } + + for (const entry of entries) { + const response = entry.response; + response.headers.map((header: Header) => output.headers.add(header.name)); + response.cookies.map((cookie: Cookie) => output.cookies.add(cookie.name)); + output.mimeTypes.add(response.content.mimeType); + + const request = entry.request; + request.headers.map((header: Header) => output.headers.add(header.name)); + request.queryString.map((arg: QueryString) => + output.queryArgs.add(arg.name), + ); + request.cookies.map((cookie: Cookie) => output.cookies.add(cookie.name)); + if (request.postData) { + request.postData.params?.map((param: Param) => + output.postParams.add(param.name), + ); + } + } + + return { + headers: [...output.headers].sort((a, b) => a.localeCompare(b)), + queryArgs: [...output.queryArgs].sort((a, b) => a.localeCompare(b)), + cookies: [...output.cookies].sort((a, b) => a.localeCompare(b)), + postParams: [...output.postParams].sort((a, b) => a.localeCompare(b)), + mimeTypes: [...output.mimeTypes].sort((a, b) => a.localeCompare(b)), + }; +} + +function getScrubMimeTypes( + options?: SanitizeOptions, + possibleScrubItems?: PossibleScrubItems, +) { + if (options?.allMimeTypes && !!possibleScrubItems) { + return possibleScrubItems.mimeTypes; + } + return options?.scrubMimetypes || defaultMimeTypesList; +} + +function getScrubWords( + options?: SanitizeOptions, + possibleScrubItems?: PossibleScrubItems, +) { + let scrubWords = options?.scrubWords || []; + if (options?.allCookies && !!possibleScrubItems) { + scrubWords = scrubWords.concat(possibleScrubItems.cookies); + } + if (options?.allHeaders && !!possibleScrubItems) { + scrubWords = scrubWords.concat(possibleScrubItems.headers); + } + if (options?.allQueryArgs && !!possibleScrubItems) { + scrubWords = scrubWords.concat(possibleScrubItems.queryArgs); + } + if (options?.allPostParams && !!possibleScrubItems) { + scrubWords = scrubWords.concat(possibleScrubItems.postParams); + } + + return scrubWords || defaultScrubItems; +} + +interface SanitizeOptions { + scrubWords?: string[] + scrubMimetypes?: string[] + allCookies?: boolean + allHeaders?: boolean + allQueryArgs?: boolean + allMimeTypes?: boolean + allPostParams?: boolean +} + +export function sanitize(input: string, options?: SanitizeOptions) { + let possibleScrubItems: PossibleScrubItems | undefined; + if ( + options?.allCookies + || options?.allHeaders + || options?.allMimeTypes + || options?.allQueryArgs + || options?.allPostParams + ) { + // we have to parse the HAR to get the full list of things we could scrub + possibleScrubItems = getHarInfo(input); + } + + // Remove specific mime responses first + input = removeContentForMimeTypes( + input, + getScrubMimeTypes(options, possibleScrubItems), + ); + + // trim the list of words we are looking for down to the ones actually in the HAR file + const wordList = getScrubWords(options, possibleScrubItems).filter(val => + input.includes(val), + ); + + // build list of regexes needed to actually scrub the file + const wordSpecificScrubList = wordList.map(word => buildRegex(word)); + const allScrubList = defaultRegex.concat(wordSpecificScrubList); + + for (const scrubList of allScrubList) { + for (const scrub of scrubList) { + input = input.replace(scrub.regex, scrub.replacement); + } + } + + return input; +} diff --git a/src/tools/har-sanitizer/lib/hash.ts b/src/tools/har-sanitizer/lib/hash.ts new file mode 100644 index 00000000..43e71c76 --- /dev/null +++ b/src/tools/har-sanitizer/lib/hash.ts @@ -0,0 +1,18 @@ +export type HashMap = Record; + +export function toHashString(obj: HashMap): string { + const params = new URLSearchParams(); + Object.entries(obj).forEach(([key, stringArr]) => { + stringArr.forEach((value) => { + params.append(key, value); + }); + }); + return `#${params.toString()}`; +} + +export function getHashMap(hash: string): HashMap { + const existingParams = new URLSearchParams(hash.replace('#', '')); + return Object.fromEntries( + [...existingParams.keys()].map(key => [key, existingParams.getAll(key)]), + ); +} diff --git a/src/tools/index.ts b/src/tools/index.ts index eee9b728..c02127f3 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -161,6 +161,7 @@ import { tool as cssPrettifier } from './css-prettifier'; import { tool as htmlPrettifier } from './html-prettifier'; import { tool as x509CertificateGenerator } from './x509-certificate-generator'; import { tool as ipIncludeExclude } from './ip-include-exclude'; +import { tool as harSanitizer } from './har-sanitizer'; import { tool as pdfSignatureChecker } from './pdf-signature-checker'; import { tool as uuidConverter } from './uuid-converter'; import { tool as numeronymGenerator } from './numeronym-generator'; @@ -505,6 +506,7 @@ export const toolsByCategory: ToolCategory[] = [ sipAuth, xVrSpamcauseDecoder, myIp, + harSanitizer, ], }, {