import { isArray, isObject } from "es-toolkit/compat"; /** * Removes sensitive fields from data attached to logs and errors, and limits * the depth of nested data. * * @param input the data to sanitize. * @param level the current nesting level. * @returns the sanitized data. */ export function sanitize(input: T, level = 0): T { // Errors have non-enumerable message/stack which are dropped by spreads // and JSON serialization, so convert them to a plain object up-front. if (input instanceof Error) { return { name: input.name, message: filterUrls(input.message), stack: input.stack && filterUrls(input.stack), } as unknown as T; } // Sanitize the serialized form of values such as dates, URLs and models, // as this is what appears in the log output. const value: unknown = hasToJSON(input) ? input.toJSON() : input; return sanitizeValue(value, level) as T; } export const SensitiveHeaderPattern = /auth|cookie|token|secret|key|session|signature/i; // OAuth callback codes and state, and signed attachment URL signatures. const SensitiveQueryParamPattern = /^(code|state|sig)$/i; /** * Replaces the values of sensitive parameters in a query string. * * @param query the query string, without a leading "?". * @returns the filtered query string. */ export function filterQueryString(query: string): string { const params = new URLSearchParams(query); for (const name of Array.from(params.keys())) { if ( SensitiveQueryParamPattern.test(name) || SensitiveHeaderPattern.test(name) ) { params.set(name, "[Filtered]"); } } return params.toString(); } /** * Sanitizes a value without calling its toJSON method, as JSON serialization * only calls it once. * * @param input the value to sanitize. * @param level the current nesting level. * @returns the sanitized value. */ function sanitizeValue(input: unknown, level: number): unknown { if (typeof input === "string") { return filterUrls(input); } if (typeof input !== "object" || input === null) { return input; } if (level > 3) { return "[…]"; } if (isArray(input)) { return input.map((item) => sanitize(item, level + 1)); } const output: Record = { ...input }; for (const key of Object.keys(output)) { output[key] = isSensitiveField(key) ? "[Filtered]" : sanitize(output[key], level + 1); } return output; } const sensitiveFields = new Set([ "accesstoken", "refreshtoken", "token", "password", "content", "verificationcode", "deleteconfirmationcode", "apikey", "secret", "authorization", ]); /** * Checks if a field name is sensitive, ignoring case, underscores and hyphens. * * @param key the field name to check. * @returns true if the field should be filtered. */ function isSensitiveField(key: string): boolean { return sensitiveFields.has(key.toLowerCase().replace(/[_-]/g, "")); } /** * Checks if a value has a toJSON method. * * @param value the value to check. * @returns true if the value has a toJSON method. */ function hasToJSON(value: unknown): value is { toJSON: () => unknown } { return ( isObject(value) && "toJSON" in value && typeof value.toJSON === "function" ); } // The query string of an absolute URL inside text, such as an error message. const UrlQueryPattern = /(\bhttps?:\/\/[^\s?#]*\?)([^\s#"'<>]+)/gi; /** * Replaces the values of sensitive query parameters in URLs within text. * * @param text the text to filter. * @returns the filtered text. */ function filterUrls(text: string): string { return text.replace( UrlQueryPattern, (_match, prefix: string, query: string) => prefix + filterQueryString(query) ); }