← Back to HTML & CSS, page 4

html-css-0069 · Hard senior

How does the iframe sandbox attribute work, and how do you embed third-party content safely?

The sandbox attribute puts an iframe under strict restrictions and an opaque origin, and tokens such as allow-scripts and allow-forms give back specific powers. Combining allow-scripts with allow-same-origin on same-origin content defeats the sandbox. Embedded sites can also block framing with headers.

Full answer

An iframe embeds another page inside yours. The embedded page can run scripts, submit forms, open popups and, if it comes from your own origin, read and change your page. When the content is untrusted, such as user-submitted HTML, a payment widget or an ad, restrict it.

The sandbox attribute switches on a strict set of restrictions. With sandbox and no value, the frame cannot run scripts, submit forms, open popups, use plugins, or change the address of the top page, and its content is treated as coming from a unique opaque origin. You then give back only the powers needed using tokens:

  • allow-scripts lets scripts run.
  • allow-forms lets forms submit.
  • allow-popups lets it open new windows.
  • allow-same-origin lets it keep its real origin instead of the opaque one.

Be careful with the last two together. If the embedded content comes from the same origin as your page and you give both allow-scripts and allow-same-origin, the frame's script can remove its own sandbox attribute, so the sandbox protects nothing. Use that pair only for content from a different origin.

Other attributes: title names the frame for screen readers, loading="lazy" delays loading, referrerpolicy limits what address is sent, and allow grants permissions such as the camera or fullscreen to specific frames.

The embedded site can also refuse framing. A site sends the X-Frame-Options header or a Content-Security-Policy: frame-ancestors rule to say which pages may embed it. If a page will not show up in your frame, check those headers first.

Both frames contain a script that tries to read the parent page. The first frame, without sandbox, shares the origin of the page, so the read works and it reports allowed. The second frame has sandbox="allow-scripts", so it runs scripts but has an opaque origin, and the browser blocks the read. It reports blocked.

Treat the sandbox as one layer. Pair it with a Content Security Policy and with server-side checks on anything the frame sends back.

html
<pre id="out">waiting</pre>
<iframe id="open" title="Not sandboxed" width="200" height="40"
  srcdoc="<script>try { parent.document.title; parent.postMessage('a:allowed', '*'); } catch (e) { parent.postMessage('a:blocked', '*'); }</script>"></iframe>
<iframe id="boxed" title="Sandboxed" width="200" height="40" sandbox="allow-scripts"
  srcdoc="<script>try { parent.document.title; parent.postMessage('b:allowed', '*'); } catch (e) { parent.postMessage('b:blocked', '*'); }</script>"></iframe>
<script>
  window.results = {};
  window.addEventListener("message", e => {
    const [who, what] = String(e.data).split(":");
    results[who] = what;
    document.getElementById("out").textContent = JSON.stringify(results);
  });
</script>

The framed pages are inline srcdoc pages, not real third-party sites. The check confirmed that the plain frame could read the parent page and the sandboxed frame could not. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • sandbox with no value blocks scripts, forms, popups and gives an opaque origin.
  • Tokens such as allow-scripts and allow-forms restore specific abilities.
  • allow-scripts plus allow-same-origin on same-origin content removes the protection.
  • title, loading and referrerpolicy are good defaults for every iframe.
  • X-Frame-Options and CSP frame-ancestors decide whether a site can be framed.

Common mistakes

  • Using allow-scripts together with allow-same-origin on same-origin content. The frame can then remove its own sandbox.
  • Leaving out the title attribute, so screen reader users hear only that there is a frame.
  • Assuming any site can be embedded. Many send X-Frame-Options or frame-ancestors headers that block it.

Follow-ups

  • What does an empty sandbox attribute block?
  • Why does the same-origin iframe in the example read the parent while the sandboxed one cannot?

Tips

  • Start from the principle: begin with a full sandbox and add only the tokens you need.
  • Mention the framing headers on the embedded side. It explains the most common 'blank iframe' bug.