|
| 1 | +/** |
| 2 | + * All methods here are supposed to be used on the SDK |
| 3 | + * and other securelog libraries |
| 4 | + */ |
| 5 | + |
| 6 | +import { AhoCorasickCore } from "../ahocorasick"; |
| 7 | +import { ScanStringOptions } from "../types"; |
| 8 | +import { maskString } from "../util"; |
| 9 | + |
| 10 | +export const redactSensitiveData = async (options: ScanStringOptions) => { |
| 11 | + const core = new AhoCorasickCore(); |
| 12 | + const detectors = core.findMatchingDetectors(options.rawValue as string); |
| 13 | + let modifiedValue = options.rawValue; |
| 14 | + |
| 15 | + const secrets = await Promise.all( |
| 16 | + detectors.map(async (detector) => { |
| 17 | + const { scan } = detector; |
| 18 | + const scanResponse = await scan(false, options.rawValue as string); |
| 19 | + if (scanResponse) { |
| 20 | + modifiedValue = modifiedValue?.replaceAll( |
| 21 | + scanResponse.rawValue as string, |
| 22 | + maskString(scanResponse.rawValue as string, { |
| 23 | + maskValue: options.maskedValue, |
| 24 | + visibleChars: options.visibleChars, |
| 25 | + }) |
| 26 | + ); |
| 27 | + |
| 28 | + return scanResponse; |
| 29 | + } |
| 30 | + }) |
| 31 | + ); |
| 32 | + |
| 33 | + return { rawValue: modifiedValue, secrets }; |
| 34 | +}; |
| 35 | + |
| 36 | +export const scanStringAndReturnJson = async (options: ScanStringOptions) => { |
| 37 | + const core = new AhoCorasickCore(); |
| 38 | + const detectors = core.findMatchingDetectors(options.rawValue as string); |
| 39 | + const response = await Promise.all( |
| 40 | + detectors.map(async (detector) => { |
| 41 | + const { scan } = detector; |
| 42 | + const scanResponse = await scan(false, options.rawValue as string); |
| 43 | + if (scanResponse) { |
| 44 | + return scanResponse; |
| 45 | + } |
| 46 | + }) |
| 47 | + ); |
| 48 | + |
| 49 | + return response; |
| 50 | +}; |
0 commit comments