UNPKG

@ogcio/o11y-sdk-node

Version:

Opentelemetry standard instrumentation SDK for NodeJS based project

26 lines (25 loc) 1.09 kB
import { BasicRedactor } from "./basic-redactor.js"; export class EmailRedactor extends BasicRedactor { // Linear-time by construction: every quantifier is bounded to RFC 5321/5322 // limits (local-part ≤ 64, DNS label ≤ 63, TLD 2–24). // Prevents ReDoS by capping the work done per start position to a constant, so matching // stays O(n) regardless of the input. // // An unbounded local-part (`+`) over a single large chunk with no `@` // will trigger rescanning it from every position (~O(n²)). Bounding the // quantifiers keeps matching linear. static EMAIL_REGEX = /[\p{L}\p{N}._%+-]{1,64}@([\p{L}\p{N}-]{1,63}(?:\.[\p{L}\p{N}-]{1,63})*\.\p{L}{2,24})/giu; // NOSONAR redact(value) { const domains = {}; const redactedValue = value.replace(EmailRedactor.EMAIL_REGEX, (_, domain) => { domains[domain] = (domains[domain] || 0) + 1; return "[REDACTED EMAIL]"; }); return { redactedValue, context: { domains, }, }; } }