-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
269c686
commit 60318bd
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {IncomingHttpHeaders} from 'http'; | ||
import {Dict} from '../../types'; | ||
import {prepareSensitiveKeysRedacter} from './redact-sensitive-keys'; | ||
|
||
export type SensitiveHeadersRedacter = (inputHeaders: Dict | IncomingHttpHeaders) => Dict; | ||
|
||
export default function prepareSensitiveHeadersRedacter( | ||
sensitiveHeaders: Array<string> = [], | ||
headersWithSensitiveUrls: Array<string> = [], | ||
redactSensitiveQueryParams: (input: string) => string = (input) => input, | ||
isDevMode = false, | ||
) { | ||
const redactSensitiveHeaders: SensitiveHeadersRedacter = (inputHeaders) => { | ||
if (isDevMode) { | ||
return inputHeaders; | ||
} | ||
|
||
const redactSensitiveKeys = prepareSensitiveKeysRedacter(sensitiveHeaders); | ||
|
||
const result = redactSensitiveKeys(inputHeaders); | ||
|
||
Object.keys(result).forEach((headerName) => { | ||
if (headersWithSensitiveUrls.includes(headerName.toLowerCase())) { | ||
result[headerName] = redactSensitiveQueryParams(result[headerName] as string); | ||
} | ||
}); | ||
|
||
return result; | ||
}; | ||
return redactSensitiveHeaders; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {URL} from 'url'; | ||
import {Dict} from '../../types'; | ||
import {prepareSensitiveKeysRedacter} from './redact-sensitive-keys'; | ||
|
||
export type SensitiveQueryParamsRedacter = (input?: string) => string; | ||
|
||
export default function prepareSensitiveQueryParamsRedacter( | ||
sensitiveQueryParams: Array<string> = [], | ||
isDevMode = false, | ||
) { | ||
const redactSensitiveQueryParams: SensitiveQueryParamsRedacter = (input = ''): string => { | ||
if (isDevMode || !input) { | ||
return input; | ||
} | ||
|
||
if (sensitiveQueryParams.length === 0) { | ||
return input; | ||
} | ||
|
||
const defaultBase = 'http://127.0.0.1'; | ||
const parsedUrl = new URL(input, defaultBase); | ||
|
||
const matchedSensitiveQueryParams = sensitiveQueryParams.reduce<Dict>((acc, key) => { | ||
if (parsedUrl.searchParams.has(key)) { | ||
acc[key] = parsedUrl.searchParams.get(key); | ||
} | ||
return acc; | ||
}, {}); | ||
|
||
if (Object.keys(matchedSensitiveQueryParams).length === 0) { | ||
return input; | ||
} | ||
|
||
const redactSensitiveKeys = prepareSensitiveKeysRedacter(sensitiveQueryParams); | ||
const redactedSensitiveQueryParams = redactSensitiveKeys(matchedSensitiveQueryParams); | ||
|
||
Object.keys(redactedSensitiveQueryParams).forEach((key) => { | ||
parsedUrl.searchParams.set(key, redactedSensitiveQueryParams[key] as string); | ||
}); | ||
|
||
const resultUrl = parsedUrl.toString(); | ||
|
||
return resultUrl.startsWith(defaultBase) ? resultUrl.replace(defaultBase, '') : resultUrl; | ||
}; | ||
|
||
return redactSensitiveQueryParams; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters