Whitelist HAR Sanitization in the Browser: Zero-Copy Redaction Without Secret Leaks
HTTP Archive (HAR 1.2) files exported from browser DevTools routinely contain live Authorization bearer tokens, session cookies, CSRF tokens, signed S3 URLs, query parameters, internal server IP addresses, and JSON response payloads. Naive blocklist scrubbers fail whenever an API introduces non-standard auth headers (such as x-csrf-custom) or custom vendor keys attached to entries.
For the client-side Sunasty HAR Sanitizer and HAR Analyzer, we replaced regex-based blocklisting with full whitelist reconstruction packaged as the open-source MIT library har-clean (also available on JSR as @sunasty/har-sanitizer).
1. Bounded Structural JSON Validation Before Parsing
Untrusted HAR archives can contain deeply nested JSON payloads, duplicate property shadowing (where two different parsers keep different values for the same key), or malformed numeric exponents. Before parseHarDocument inspects a trace, parseExactJson checks structural invariants within bounded limits (30 MiB HAR size, up to 20,000 entries, max JSON depth 60):
import { parseExactJson, JSON_LIMITS } from 'har-clean/json-exact.js';
// Rejects duplicate keys, depth > 60, or > 1,000,000 nodes
parseExactJson(rawHarText, {
...JSON_LIMITS,
bytes: 30 * 1024 * 1024,
depth: 60,
nodes: 1000000,
});
2. Forensic Inventory Across Unknown and Vendor Fields
Before stripping anything, inspectHar(harText) walks every entry to map request domains, detect retainable plain-text request or response bodies (capped at 1 MiB decoded), and catalog up to 1,000 sensitive field paths across URLs, headers, cookies, post data, IP addresses, and unexpected keys under /[unknown].
3. Strict Whitelist Reconstruction
Instead of mutating or cloning the original objects with Object.assign or object spread, sanitizeHar(text, policy) constructs fresh HAR 1.2 entry objects containing only explicitly allowed 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);
// result.har is a freshly built HAR 1.2 document
// result.report.removals documents every dropped path and reason
console.log(result.report.entriesAfter, result.report.removals.length);
- Headers & Cookies: Rebuilt as empty arrays
[]by default, preventing custom auth headers from leaking. - Query Strings & Redirect URLs: Stripped at URL origin/path level via
URLparsing; cross-domainredirectURLvalues outside the selected domain policy are dropped. - Timestamps & Waterfall Summary: When
retainTimesis false, timestamps are shifted relative to the earliest request baseline so debuggers preserve waterfall latency (computeWaterfallSummary) and cURL exports (generateCurlCommand) without revealing wall-clock user activity.
4. Related Privacy & Developer Utilities
Pair HAR redaction with the local JSON Diff tool, Email Header Analyzer, EML Viewer, and PII Redactor inside the Sunasty Developer Tools suite.
5. Open-Source Package & CLI Links
The MIT library and CLI (npx har-clean trace.har -o clean.har) are published on npm (har-clean), JSR (@sunasty/har-sanitizer), and GitHub.