-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from desci-labs/m0ar/proxy-wash-response-secrets
reverse-proxy: add support for washing secrets in response bodies
- Loading branch information
Showing
3 changed files
with
64 additions
and
19 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
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,38 @@ | ||
export type Mapping = Record<string, string>; | ||
|
||
/** | ||
* Build a route mapping from envvars prefixed `PROXY_MAPPING_` | ||
*/ | ||
export const buildMappingFromEnv = (): Mapping => Object.fromEntries( | ||
Object.entries(process.env as { [s:string]: string } ) | ||
.filter(([k, _]) => k.startsWith("PROXY_MAPPING_")) | ||
.map(([k, v]) => [k.replace("PROXY_MAPPING_", ""), v]) | ||
.map(([k, v]) => ["/" + k.toLowerCase(), v]) | ||
); | ||
|
||
/** | ||
* Find sensitive strings in mapped URL's | ||
*/ | ||
export const getSensitiveStrings = (mapping: Mapping) => { | ||
const urls = Object.values(mapping); | ||
|
||
const alchemyTokens = urls | ||
.filter(url => url.includes("alchemy.com")) | ||
/* 32-char alphanum token with dashes and underscores */ | ||
.map(url => url.match(/[a-zA-Z0-9_-]{32}/)) | ||
/* Remove potential null match for tokenless url */ | ||
.flatMap(maybeMatch => maybeMatch ? [maybeMatch] : []) | ||
/* Get the match from RexExpMatchArray */ | ||
.map(matchArr => matchArr[0]); | ||
|
||
return alchemyTokens; | ||
}; | ||
|
||
export const redactSensitive = ( | ||
sensitiveStrings: string[], | ||
dirty: string | ||
): string => | ||
sensitiveStrings.reduce( | ||
(acc, nextSecret) => acc.replaceAll(nextSecret, "[redacted]"), | ||
dirty, // initial accumulator | ||
); |