The sandbox attribute on <iframe> applies security restrictions to the embedded page. An emptysandbox turns on the full set of restrictions. A space-separated list of allow-* tokens lifts only those specific limits.
Use it when embedding untrusted or third-party content. Pair with a clear title, and often src or srcdoc.
Baseline · Widely available.
Not deprecated. Presence alone enables restrictions; tokens are opt-outs from the lockdown.
Never combine allow-scripts + allow-same-origin on same-origin embeds.
That pair can let the framed page remove sandbox, undoing the protection (MDN).
allow-scripts lets the inline script update the text.
allow-forms would permit submit (not used in this tiny doc).
Other limits (popups, top navigation, …) stay on until you allow them.
Example 3 — Dangerous token pair (do not copy for same-origin)
Study the anti-pattern. Prefer not combining these when the embed is same-origin.
HTML
<!-- BAD when iframe is same-origin as the parent -->
<iframe
title="Unsafe combination"
src="/some-same-origin-page"
sandbox="allow-scripts allow-same-origin">
</iframe>
<!-- Safer mindset: keep scripts OR same-origin access limited;
serve untrusted content from a separate origin -->
Warning: Do not use allow-scripts + allow-same-origin together for same-origin embeds — the framed page can remove sandbox.
How It Works
allow-same-origin restores a normal origin for the frame.
allow-scripts lets that frame run script.
Together + same-origin parent → script can strip sandbox.
Fix: avoid the pair, or host untrusted content on another origin.
Edge Cases & Pitfalls
Most sandbox bugs are over-allowing tokens or assuming the attribute alone is enough.
tokens
Empty = strict; tokens = exceptions
Add only the capabilities the embed truly needs.
danger
allow-scripts + allow-same-origin
Strongly discouraged for same-origin embeds — can defeat sandboxing.
popups
New tabs inherit sandbox
Unless you use allow-popups-to-escape-sandbox. Forms in those tabs may silently fail without allow-forms.
pdf
Built-in PDF viewer may be blocked
Sandbox is a poor fit for native PDF preview restrictions.
a11y
Always set title on iframes
Screen readers need a short name for the frame.
Compatibility
Browser Support
sandbox is an HTML attribute on <iframe>. Individual tokens may differ slightly by engine — check MDN for edge tokens.
✓ Baseline · Widely available
sandbox
Supported in modern browsers. Prefer the least privilege set of allow-* tokens.
96%Global support (approx.)
Google ChromeYes
Full support
Mozilla FirefoxYes
Full support
Apple SafariYes
Full support
Microsoft EdgeYes
Full support
OperaYes
Full support
Samsung InternetYes
Full support
Internet Explorer10+
Partial / older tokens
sandboxWidely supported
Bottom line: Safe to use for hardening iframes. Review newer tokens before relying on them in older browsers.
Remember
Key Takeaways
Role: restrict an iframe; empty = strictest.
Tokens lift only the capabilities you list.
Avoidallow-scripts + allow-same-origin together.
Complement with a separate origin + CSP when content is untrusted.
One line:sandbox locks down an iframe; allow-* tokens carefully open holes.
Frequently Asked Questions
On an iframe, sandbox applies a set of security restrictions to the embedded browsing context. An empty sandbox enables all restrictions. Space-separated allow-* tokens lift specific ones.
Maximum lockdown for that frame: no scripts, no forms, no popups, treated as a unique origin, and more — until you add allow-* tokens.
If the framed page is same-origin as the parent, that pair lets the embed remove the sandbox attribute, undoing the protection. MDN strongly discourages the combination.
No. Sandbox restricts the iframe. CSP is a separate, complementary defense for the document. Use both when appropriate.
By default, new tabs/windows inherit the sandbox. allow-popups-to-escape-sandbox lets those new contexts run without the same flags.
No. It is a Baseline widely available security attribute on iframe.