What the iframe sandbox attribute does
The sandbox attribute on an <iframe> runs the embedded document under a set
of additional restrictions on top of the same-origin policy. The moment you add
the attribute, almost everything is switched off: scripts, forms, popups,
top-level navigation, the document’s own origin, plugins, and pointer lock. You
then opt back in to specific capabilities by listing allow-* tokens. This
“deny by default, allow explicitly” model is why sandbox is a core tool for
safely embedding untrusted third-party content.
How it works
Tokens are space-separated inside the attribute value:
<iframe src="widget.html" sandbox="allow-scripts allow-forms"></iframe>
Each token re-enables exactly one feature. With no tokens the frame is maximally
locked down and is also forced into a unique opaque origin, so even
same-origin content is treated as cross-origin. The single most important rule:
combining allow-scripts with allow-same-origin lets the framed page script
its own sandbox away, so reserve that pairing for fully trusted content. The
toggler below builds the attribute and flags that dangerous combination.
Tips and notes
Prefer allow-top-navigation-by-user-activation over the unconditional
allow-top-navigation so framed content can only redirect the page after a real
click. allow-popups-to-escape-sandbox lets popups open without inheriting the
restrictions — useful for legitimate auth flows, risky for ads. Sandbox pairs
naturally with Content-Security-Policy and referrerpolicy; treat them as
layers, not substitutes.