DEV Community

Cover image for 🧹 HAR Files Leak Sessions: Scrub Them Without Breaking the Proof
Krishna Kumar
Krishna Kumar

Posted on

🧹 HAR Files Leak Sessions: Scrub Them Without Breaking the Proof

You've proven the bug. You have the request that shows it, the response that confirms it, and the browser's network log that ties it together. The easiest way to hand all of that to a triager, a developer or a vendor's support desk is a HAR file: one click in DevTools, one attachment.

That file is also a copy of your session. Every cookie, every Authorization header, every token the app returned in a response body is in there, in plain JSON. Whoever reads the attachment, or whoever later gets into the system it was uploaded to, can try to replay it.

This isn't a hypothetical. In October 2023, Okta reported unauthorized access to its support system, where customers upload HAR files for troubleshooting. Okta noted that HAR files can contain cookies and session tokens, revoked the tokens embedded in them, and recommended sanitizing credentials before sharing. Cloudflare wrote that an attack on its own systems started from a token stolen from that support system, and released an open-source HAR sanitizer in response.

This post is about the reporter's side of that problem: how to strip a HAR so it still proves the bug, and how to prove to yourself that the scrub actually worked.

Only capture and share traffic from systems you own or are authorized to test, using your own test accounts.

What's in a HAR, exactly

HAR (HTTP Archive) is a JSON format for recording a browser's HTTP transactions. Its W3C spec, now marked as a historical draft, is short and worth a skim. A few details matter for scrubbing:

  • Each entry has a request and a response. Headers live in headers arrays of {name, value} pairs.
  • Cookies appear twice: once inside the Cookie / Set-Cookie headers, and again as parsed cookies arrays on the request and the response. Clean one and forget the other, and the value is still there.
  • The URL is stored whole, and its query string again as parsed queryString pairs. Tokens in query strings show up in both.
  • postData holds what you sent (text, or params for form posts). That's where a login request keeps your password.
  • response.content.text holds the response body, possibly base64-encoded (encoding: "base64"). Login and token endpoints return credentials here.
  • redirectURL copies the Location header, which can carry an OAuth code or similar.
  • Tools may add custom fields, which must start with an underscore. A parser that didn't write them is supposed to ignore them, but they're still in the file, and you don't know what's inside.

The spec's privacy section is one sentence: HAR files may contain sensitive data, and the user agent should tell the user so before the file goes anywhere.

Where a credential can sit in one HAR entry, and which places Chrome's sanitized export is documented to cover

Chrome's "sanitized" export helps, but read what it covers

Chrome DevTools now exports a sanitized HAR by default. Per the DevTools release notes and the Network panel reference, the sanitized export leaves out the Cookie, Set-Cookie and Authorization headers. To get them back you have to turn on a setting and pick "Export HAR (with sensitive data)".

That's a good default, and you should use it. But look at what's on the list: three header names. The documentation doesn't say anything about:

  • the parsed cookies arrays that repeat the cookie values
  • custom auth headers such as X-Api-Key
  • tokens in URLs and query strings (RFC 6750 already warns that bearer tokens in page URLs end up in history and logs)
  • passwords in postData
  • tokens and personal data in response bodies

Response bodies are the easy one to miss. In OWASP Juice Shop, for example, the login route returns the session JWT and the user's email inside the JSON body (authentication.token and authentication.umail in routes/login.ts). A header-only sanitizer leaves both in place.

So treat the sanitized export as step one, then check the file yourself.

Scrub it so the proof survives

The lazy fix is to delete every header and every body. The result is safe and useless: the triager can no longer see the thing you were proving.

Take a typical access-control proof. Alice's session fetches an invoice that belongs to Bob, and Bob fetching his own invoice is the control. If you blank both Authorization headers to REDACTED, the two requests look identical. The proof depends on showing that they came from different sessions.

The fix is to replace each distinct secret with a stable placeholder: the same token always becomes <SECRET_3>, and a different token becomes <SECRET_6>. The OWASP Logging Cheat Sheet suggests the same idea for logs: keep session IDs, access tokens and passwords out, and use a hashed value instead when you still need to track which events belong to one session.

Here's a scrubber that does that with only the Python standard library:

#!/usr/bin/env python3
"""Scrub secrets from a HAR file but keep the proof readable.

Each distinct secret value becomes a stable placeholder (<SECRET_1>, ...),
so a reader can still tell which requests shared a session.
Usage: python3 har_scrub.py in.har out.har
"""
import json, re, sys
from urllib.parse import urlsplit, urlunsplit, parse_qsl, urlencode

SENSITIVE_NAME = re.compile(
    r"(auth|cookie|token|secret|session|passw|api[-_]?key|csrf|signature|code)",
    re.I)
JWT = re.compile(r"eyJ[\w-]+\.eyJ[\w-]+\.[\w-]*")
BEARER = re.compile(r"(?i)\b(bearer|basic)\s+[\w.~+/=-]+")

placeholders = {}  # original value -> placeholder


def mask(value):
    if not value:
        return value
    if value not in placeholders:
        placeholders[value] = f"<SECRET_{len(placeholders) + 1}>"
    return placeholders[value]


def scrub_text(text):
    """Catch tokens that sit in free text (bodies, URLs, odd headers)."""
    text = JWT.sub(lambda m: mask(m.group(0)), text)
    return BEARER.sub(lambda m: m.group(1) + " " + mask(m.group(0).split(None, 1)[1]), text)


def mask_header(name, value):
    """Mask the secret part only, so the same token gets the same placeholder
    whether it appears in a header, a cookie list or a response body."""
    name = name.lower()
    if name == "cookie":
        pairs = [c.strip().split("=", 1) for c in value.split(";") if "=" in c]
        return "; ".join(f"{k}={mask(v)}" for k, v in pairs)
    if name == "set-cookie":
        first, _, attrs = value.partition(";")
        k, _, v = first.partition("=")
        return f"{k}={mask(v)}" + (";" + attrs if attrs else "")
    scheme, _, cred = value.partition(" ")
    if scheme.lower() in ("bearer", "basic") and cred:
        return f"{scheme} {mask(cred)}"
    return mask(value)


def scrub_pairs(pairs):
    for p in pairs:
        value = p.get("value", "")
        if SENSITIVE_NAME.search(p.get("name", "")):
            p["value"] = mask_header(p["name"], value)
        elif value.startswith(("http://", "https://", "/")):   # Location, Referer...
            p["value"] = scrub_url(value)
        else:
            p["value"] = scrub_text(value)


def scrub_url(url):
    parts = urlsplit(url)
    q = [(k, mask(v) if SENSITIVE_NAME.search(k) else v)
         for k, v in parse_qsl(parts.query, keep_blank_values=True)]
    return scrub_text(urlunsplit(parts._replace(query=urlencode(q, safe="<>_"))))


def scrub_json(node):
    if isinstance(node, dict):
        return {k: (mask(v) if isinstance(v, str) and SENSITIVE_NAME.search(k)
                    else scrub_json(v)) for k, v in node.items()}
    if isinstance(node, list):
        return [scrub_json(v) for v in node]
    return scrub_text(node) if isinstance(node, str) else node


def scrub_body(text, mime):
    if not text:
        return text
    if "json" in (mime or ""):
        try:
            return json.dumps(scrub_json(json.loads(text)))
        except ValueError:
            pass
    if "x-www-form-urlencoded" in (mime or ""):
        pairs = [{"name": k, "value": v} for k, v in parse_qsl(text, keep_blank_values=True)]
        scrub_pairs(pairs)
        return urlencode([(p["name"], p["value"]) for p in pairs], safe="<>_")
    return scrub_text(text)


def drop_custom_fields(obj):
    """HAR custom fields start with '_' and can hold anything: drop them."""
    if isinstance(obj, dict):
        return {k: drop_custom_fields(v) for k, v in obj.items() if not k.startswith("_")}
    if isinstance(obj, list):
        return [drop_custom_fields(v) for v in obj]
    return obj


def scrub_entry(e):
    req, res = e["request"], e["response"]
    req["url"] = scrub_url(req["url"])
    for part in (req, res):
        scrub_pairs(part.get("headers", []))
        for c in part.get("cookies", []):
            c["value"] = mask(c.get("value", ""))
    scrub_pairs(req.get("queryString", []))
    post = req.get("postData")
    if post:
        scrub_pairs(post.get("params", []))
        post["text"] = scrub_body(post.get("text", ""), post.get("mimeType"))
    content = res.get("content", {})
    if content.get("encoding") == "base64":
        content["text"] = "<BINARY BODY REMOVED>"
    else:
        content["text"] = scrub_body(content.get("text", ""), content.get("mimeType"))
    if res.get("redirectURL"):
        res["redirectURL"] = scrub_url(res["redirectURL"])


def main(src, dst):
    with open(src, encoding="utf-8-sig") as f:   # spec: UTF-8, BOM allowed
        har = drop_custom_fields(json.load(f))
    for entry in har["log"]["entries"]:
        scrub_entry(entry)
    out = json.dumps(har, indent=2)
    leaked = [v for v in placeholders if len(v) >= 6 and v in out]
    if leaked:
        sys.exit(f"refusing to write: {len(leaked)} secret value(s) still present")
    with open(dst, "w", encoding="utf-8") as f:
        f.write(out)
    print(f"{len(har['log']['entries'])} entries, {len(placeholders)} distinct secrets masked")


if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2])
Enter fullscreen mode Exit fullscreen mode

A few design choices worth calling out:

  • It errs toward masking. Any header, cookie, query parameter or JSON key whose name contains auth, token, session, code and so on gets masked. That will also hit harmless names like author or countryCode. That's fine for evidence.
  • Cookie and bearer values are masked without their wrapper. Bearer eyJ… in a header and the bare eyJ… in a login response map to the same placeholder, so the reader can see that the token the login returned is the one the next request used.
  • It refuses to write a file that still contains a value it masked. Placeholders are assigned as values are found, so the last step searches the output for every original value. An earlier version of this script didn't handle the Location header: it masked the OAuth code in redirectURL but left the same code in the header, and this check caught it and refused to write.
  • Base64 bodies and custom _ fields are dropped, not inspected. You rarely need them to prove a web bug, and you can't easily check what's inside them.

Here it is on a stand-in HAR for app.example.com, with four entries: a login, the cross-account read, the control, and an export link with a token in the query string. A small summary script prints one line per entry:

import json, sys
from urllib.parse import urlsplit

har = json.load(open(sys.argv[1], encoding="utf-8-sig"))
for e in har["log"]["entries"]:
    req, res = e["request"], e["response"]
    u = urlsplit(req["url"])
    path = u.path + ("?" + u.query if u.query else "")
    auth = next((h["value"] for h in req["headers"]
                 if h["name"].lower() == "authorization"), "-")
    print(f'{req["method"]:5} {path:44} {res["status"]}  {auth:18} {res["content"]["text"][:42]}')
Enter fullscreen mode Exit fullscreen mode
$ python3 har_scrub.py standin.har clean.har
4 entries, 8 distinct secrets masked

$ python3 har_summary.py clean.har
POST  /api/login                                   200  -                  {"authentication": {"token": "<SECRET_3>",
GET   /api/invoices/1002                           200  Bearer <SECRET_3>  {"id": 1002, "owner": "bob-test", "total":
GET   /api/invoices/1002                           200  Bearer <SECRET_6>  {"id": 1002, "owner": "bob-test", "total":
GET   /export?format=csv&access_token=<SECRET_7>   302  -
Enter fullscreen mode Exit fullscreen mode

The proof is still readable: the token Alice got at login (<SECRET_3>) fetched invoice 1002, which belongs to bob-test, and Bob's own session (<SECRET_6>) is a different token. None of the eight values can be replayed.

Prove the scrub worked

A scrubber that says "done" is a detection, not a proof. Check the output with something that doesn't share the scrubber's logic. This checker walks every string in the file and flags anything that still looks like a credential:

#!/usr/bin/env python3
"""Second opinion: flag anything in a HAR that still looks like a credential."""
import json, re, sys

JWT = re.compile(r"eyJ[\w-]+\.eyJ[\w-]+\.[\w-]*")
CRED = re.compile(r"(?i)\b(bearer|basic)\s+(?!<SECRET_)\S+")
NAME = re.compile(r"(auth|cookie|token|secret|session|passw|api[-_]?key)", re.I)
PLACEHOLDER = re.compile(r"^(<SECRET_\d+>|\w+ <SECRET_\d+>)$")


def walk(node, path="$"):
    if isinstance(node, dict):
        if "name" in node and "value" in node and NAME.search(str(node["name"])):
            v = str(node["value"]).split(";")[0].split("=", 1)[-1].strip()
            if v and not PLACEHOLDER.match(v):
                yield path, f"{node['name']} has a raw value"
        for k, v in node.items():
            yield from walk(v, f"{path}.{k}")
    elif isinstance(node, list):
        for i, v in enumerate(node):
            yield from walk(v, f"{path}[{i}]")
    elif isinstance(node, str):
        if JWT.search(node):
            yield path, "JWT-shaped string"
        if CRED.search(node):
            yield path, "Bearer/Basic credential"


with open(sys.argv[1], encoding="utf-8-sig") as f:
    findings = list(walk(json.load(f)))
for path, why in findings:
    print(f"{why:28} {path}")
print("clean" if not findings else f"{len(findings)} finding(s)")
sys.exit(1 if findings else 0)
Enter fullscreen mode Exit fullscreen mode

On the raw stand-in file it finds 14 problems, including a JWT the stand-in file tucks into a custom _initiator field (output trimmed):

$ python3 har_check.py standin.har
session has a raw value      $.log.entries[0].response.cookies[0]
Set-Cookie has a raw value   $.log.entries[0].response.headers[0]
JWT-shaped string            $.log.entries[0].response.content.text
...
JWT-shaped string            $.log.entries[1]._initiator.note
access_token has a raw value $.log.entries[3].request.queryString[1]
14 finding(s)

$ python3 har_check.py clean.har
clean
Enter fullscreen mode Exit fullscreen mode

Then do the check no generic tool can do for you: search the scrubbed file for the values you know. Your test account's password, its email, the API key you were issued. In the stand-in run, every secret value was gone, but alice@example.com was still in the login body and the request body. Neither script treats an email as a secret. That's the reason to capture with test accounts that use stand-in addresses, not your personal ones.

Make the token dead, not just hidden

Scrubbing protects the file. It doesn't protect against the copy you didn't scrub: the one in your downloads folder, a chat paste, an earlier upload. So finish by ending the session you captured:

  1. Log out of the test account, or revoke the token or API key if the app lets you.
  2. Replay one of the captured requests with the old token, on your own account and within your scope. You want a 401 or a redirect to login, not a 200.

The OWASP Session Management Cheat Sheet says logout must invalidate the session on the server side. Not every app does this, especially with stateless tokens that stay valid until they expire. If the old token still works after logout, you've learned something about the app worth noting, and the file needs to stay private until the token expires.

If you're the one receiving HARs

Support and security teams are on the other end of this, and the same ideas apply:

  • Ask for the sanitized export, and say why in your request template.
  • Treat every HAR upload as containing live credentials. Scan it with a checker like the one above and keep it out of systems that many people can read.
  • Revoke sessions found in uploaded files, as Okta did. Delete the files when the ticket closes.
  • Tell people about the browser tools and Cloudflare's HAR sanitizer, which strips session cookies and JWTs client-side (Cloudflare says it never sees the full session token).

Try it yourself

Only on systems you own or are authorized to test:

  1. Run OWASP Juice Shop locally: docker run --rm -p 127.0.0.1:3000:3000 bkimminich/juice-shop.
  2. Register a test account with a stand-in email, log in, and browse your basket with DevTools' Network panel open.
  3. Export a sanitized HAR. Open it and search for token. If the export kept response bodies, the JWT from the login response is still there.
  4. Run har_scrub.py, then har_check.py, then search for your test password and email.
  5. Log out, replay a captured request with the old token, and see what you get.

Compare the result with what Cloudflare's sanitizer produces from the same file. Two tools that disagree are telling you where to look.

Conclusion

A HAR file is the best evidence a web bug report can carry, and the worst thing to leave lying around. Good evidence proves the bug to the person reading it and is useless to anyone who steals it. Export sanitized, scrub with stable placeholders so the proof still reads, check the result with a second tool and your own known values, and end the session you captured. Detection isn't proof, and that goes for your own scrubber too.

Further reading

I'm Krishna, a pentester and the founder of Xalgorix, an open-source AI pentester that only reports vulnerabilities it has actually exploited.


🔐 If this was useful, follow me for more hands-on web & API security write-ups.

🔗 Connect with me on LinkedIn: https://www.linkedin.com/in/xalgord/

Top comments (0)