HTTP Archive (HAR 1.2) traces exported from Chrome or Firefox DevTools routinely include live Authorization bearer tokens, session cookies, CSRF headers, signed URLs, query parameters, and internal server IPs. Regex blocklists break as soon as an app adds custom auth headers like x-csrf-custom or vendor fields.
While building the client-side Sunasty HAR Sanitizer and HAR Analyzer, we replaced regex blocklisting with strict whitelist reconstruction and released the core engine as the zero-dependency MIT package har-clean on npm and JSR (@sunasty/har-sanitizer).
1. Bounded Structural JSON Validation Before Parsing
Untrusted HAR traces can hide deeply nested objects, duplicate keys, or oversized numeric exponents. parseHarDocument validates exact JSON structural limits (30 MiB bytes, max 20,000 entries, depth 60, 1,000,000 nodes) before inspecting entries:
import { parseExactJson, JSON_LIMITS } from 'har-clean/json-exact.js';
parseExactJson(rawHarText, {
...JSON_LIMITS,
bytes: 30 * 1024 * 1024,
depth: 60,
nodes: 1000000,
});
2. Strict Whitelist Reconstruction vs. Object Mutation
Instead of mutating input objects or cloning via object spread, sanitizeHar(text, policy) creates fresh HAR 1.2 entries containing only explicitly approved fields:
import { inspectHar, buildSafeDefaultPolicy, sanitizeHar } from 'har-clean';
const inspection = inspectHar(harJsonString);
const policy = buildSafeDefaultPolicy(harJsonString, {
domains: inspection.domains.map((d) => d.domain),
retainPaths: true,
retainTimes: false,
});
const result = sanitizeHar(harJsonString, policy);
console.log(result.report.entriesAfter, result.report.removals.length);
- Headers and cookies are rebuilt as empty
[]arrays by default so non-standard auth headers never survive. - Query strings are stripped at
URLparsing time, and cross-domainredirectURLfields outside the domain allowlist are dropped. - Relative waterfall timings (
computeWaterfallSummary) and reproduciblecURLcommands (generateCurlCommand) still work even whenretainTimesshifts timestamps to remove wall-clock activity.
Links & Interactive Browser Tool
- Browser tool (100% client-side, zero uploads): https://sunasty.com/har-sanitizer/
- Developer & privacy utilities: https://sunasty.com/json-diff/, https://sunasty.com/pii-redactor/, https://sunasty.com/dev/
- CLI usage:
npx har-clean trace.har -o clean.har - Source & packages: GitHub · npm · JSR
Top comments (0)