Safety, Security & Responsible AI·Question 58 of 70Advanced

Your assistant renders HTML and JavaScript the model writes as live previews. Design the sandbox, then tell me how an attacker would chain their way out of it.

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Model-written HTML is attacker-influenced code, so the preview is improper output handling with a rendering engine attached. The design goal is that even fully malicious preview code cannot act as the user.

Full worked answer with diagram

Model-written HTML is attacker-influenced code, so the preview is improper output handling with a rendering engine attached. The design goal is that even fully malicious preview code cannot act as the user.

The design.

  • A separate origin. Serve previews from a different registrable domain that holds no cookies or sessions. Same-origin with your app means the preview is your app.
  • A sandboxed iframe, with allow-scripts but never combined with allow-same-origin for content on your origin, since together they let the frame remove its own sandbox.
  • A strict CSP inside the preview: no network by default, or a narrow allowlist if the feature needs it.
  • A typed message protocol. If the preview talks to the parent, the parent accepts only structured messages (typed JSON with known fields), checks both event.origin and event.source against the specific frame, and never passes message content to innerHTML.
  • Click to run on shared content. A preview inside a shared link should not execute on page load for a signed-in viewer.

How an attacker chains out: a real example. In October 2026 researchers turned ChatGPT's HTML Preview into an account takeover, and every link in the chain was a reasonable-looking component:

  1. The attacker publishes a conversation containing an interactive graph and an HTML Preview, then shares the link.
  2. The graph loads MathLive, a third-party math-input library, which installs a message listener on the main page that checks neither origin nor source.
  3. The sandboxed preview posts raw HTML to that listener.
  4. The HTML ends up in a srcdoc iframe that is not sandboxed. A srcdoc frame inherits its parent's origin, so it runs as chatgpt.com.
  5. The page's CSP allowed YouTube, and a YouTube oEmbed JSONP callback reflected attacker-controlled JavaScript, so script ran despite the CSP.
  6. The script called authenticated APIs as the victim, listed conversations and read a private one, then sent data out through an image reference.

OpenAI raised it from P2 to P1, fixed it on 26 October and paid a $5,000 bounty.

The lessons I would build into review.

  • Inventory every message listener on the main page, including ones that third-party libraries add. Your sandbox is only as strong as the most careless listener.
  • Never accept raw HTML across windows.
  • Keep derived documents sandboxed. Anything built from untrusted content (srcdoc, blob URLs) gets the sandbox attribute, every time.
  • Treat CSP as defence in depth. Any allowlisted host with a JSONP or reflection endpoint is a bypass, so allowlist as little as possible.
  • Test features in combination. Graphs, previews and sharing were each fine alone. The exploit needed all three.

Worth sketching. Optional, but drawing the chain makes it obvious that the fix is at several links, not one.

flowchart TD
    A["Shared link opened by signed-in user"] --> B["Sandboxed preview posts raw HTML"]
    B --> C["Library listener, no origin check"]
    C --> D["Unsandboxed srcdoc iframe<br/>runs as main origin"]
    D --> E["CSP-allowed JSONP callback runs script"]
    E --> F["Authenticated API calls as victim"]
    F --> G["Data out through an image URL"]

What a good answer covers

  • Model-written HTML is attacker-influenced code, so the preview is improper output handling with a…
  • The design goal is that even fully malicious preview code cannot act as the user
  • A separate origin
  • A sandboxed iframe, with allow-scripts but never combined with allow-same-origin for content on your…
  • A strict CSP inside the preview: no network by default, or a narrow allowlist if the feature needs it

Key terms: The design · A separate origin · A sandboxed iframe · A typed message protocol · Keep derived documents sandboxed · Test features in combination

Interviewer follow-ups to expect

  1. Product wants previews to fetch live data from an API: how do you allow that without reopening the hole?

  2. How would you find every postMessage listener in a large frontend, including ones added at runtime?

  3. What would you test before shipping a new embeddable widget next to the preview?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep