DEV Community

Cover image for Autopsy #11: The Chat Bubble That Left With the Session Tokens
frontend.rocks
frontend.rocks

Posted on Edited on Originally published at frontend.rocks

Autopsy #11: The Chat Bubble That Left With the Session Tokens

One line before lunch

Thomas asked us to drop a one-line third-party support chat snippet into the authenticated dashboard layout so enterprise customers could reach support without leaving the app. Julie pasted the <script> tag into the root shell, verified the little blue bubble showed up in the bottom-right corner with zero console errors, and shipped it before lunch.

Six minutes after an upstream incident on the chat vendor's CDN, Marc got a WAF alert. Authenticated browsers from 3.8% of active sessions were firing POST requests to an unfamiliar external domain, carrying session tokens and CSRF nonces straight out of localStorage. We rotated signing keys, invalidated active sessions, and pulled the vendor snippet within eleven minutes, while security began auditing what the script had actually done inside the browser.

There was no server breach. The vendor script was running inside our first-party origin, listening to postMessage, and writing incoming message HTML directly into widgetRoot.innerHTML without sanitizing anything. Every browser running our dashboard was executing attacker-controlled markup inside our authenticated session, with full read access to localStorage, DOM inputs, and authenticated fetch() cookies.

Regex versus HTML

In a hurry, someone shipped an emergency regex filter before innerHTML (html.replace(/<script/gi, '')) while leaving script-src 'self' 'unsafe-inline' in our Content Security Policy so legacy inline snippets would keep working.

In staging, <script>alert(1)</script> was stripped cleanly and the team breathed out for about ninety seconds.

In production, token exfiltration did not slow down by a single millisecond. The attacker's payload was <img src=x onerror="fetch(...)">, which does not contain the string <script anywhere in it and which 'unsafe-inline' happily executes the moment the DOM parses the <img> tag and fails to load x. Even if we had blocked <img>, HTML offers dozens of event-handler attributes (<svg onload=...>, <details ontoggle=...>) that execute JavaScript without ever writing <script>.

Isolating the widget from our origin

When a third-party <script> runs in your origin, it shares your DOM, your cookies, and your localStorage. We moved the support chat out of the root shell into a cross-origin <iframe sandbox="allow-scripts"> (omitting allow-same-origin so it gets an opaque origin) served from a separate domain. Because our dashboard also enables cross-origin isolation via Cross-Origin-Opener-Policy: same-origin (for top-level window.opener isolation) and Cross-Origin-Embedder-Policy: require-corp, the vendor iframe response must send Cross-Origin-Resource-Policy: cross-origin, or be embedded with credentialless, so the browser loads it without leaking our origin's storage or DOM.

For the tiny bridge that passes the user's support ticket ID into the iframe, we added runtime boundary validation on window.addEventListener('message', ...), checking event.origin against an exact allowlist and parsing payloads with a strict Zod schema. Any message whose origin, shape, or ticket format fails validation is dropped before touching application state. We also guarded against prototype chain pollution (Object.hasOwn) so an untrusted payload can never forge an inherited bypass flag on Object.prototype.

Nonces and Trusted Types

Interactive Failure Simulator — Tap to step through the live model

Breaking our own Markdown preview

First try: we flipped require-trusted-types-for 'script' straight into Content-Security-Policy enforce mode and deployed to production.

Within two minutes, an internal Markdown preview component started throwing TypeError: This document requires 'TrustedHTML' assignment. It was assigning sanitized HTML strings directly to element.innerHTML without going through a TrustedTypePolicy, which broke release-note previews for every logged-in customer.

Content-Security-Policy-Report-Only: require-trusted-types-for 'script'; report-uri /csp-reports
Enter fullscreen mode Exit fullscreen mode

We switched the header to Content-Security-Policy-Report-Only for 24 hours, caught the two forgotten internal sinks from the violation reports, wrapped them in a DOMPurify Trusted Types policy, and then flipped enforcement back on cleanly. We also added a Playwright check that asserts zero CSP violation reports across all authenticated routes.

What stayed in the codebase

A <script src="https://..."> tag is not a UI component; it is a blank-check lease on your origin.

The little blue chat bubble now lives inside its own sandboxed iframe, where there is nothing left for it to steal. Marc still checks the CSP report endpoint every Monday morning, and it has stayed empty for three months across every authenticated route we run in production and staging. Third-party widgets are now treated like untrusted network peers by default.

Top comments (0)