DEV Community

Bastien Sulyan
Bastien Sulyan

Posted on Edited on Originally published at bsulyan.github.io

Whitelist HAR Sanitization in JavaScript: Zero-Copy Redaction Without Secret Leaks

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,
});
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
  • Headers and cookies are rebuilt as empty [] arrays by default so non-standard auth headers never survive.
  • Query strings are stripped at URL parsing time, and cross-domain redirectURL fields outside the domain allowlist are dropped.
  • Relative waterfall timings (computeWaterfallSummary) and reproducible cURL commands (generateCurlCommand) still work even when retainTimes shifts timestamps to remove wall-clock activity.

Links & Interactive Browser Tool

Top comments (0)