iframe sandbox: What Each Token Allows
An empty sandbox attribute locks an iframe down hard: no scripts, no form submission, no popups, no alerts and an opaque origin that fails every same-origin check. Each allow- token you add gives one power back. Toggle the tokens below to see exactly what breaks, then copy the smallest set your embed needs.
| Embedding | Suggested sandbox value |
|---|---|
| Static HTML preview you generated | sandbox (empty) |
| Interactive demo or code playground output | sandbox="allow-scripts" |
| Third-party form that posts its answers | sandbox="allow-scripts allow-forms allow-same-origin" (only for a different origin) |
| Widget with links that open in a new tab | add allow-popups allow-popups-to-escape-sandbox |
| Game that captures the mouse | add allow-pointer-lock |
Updated
The sandbox attribute is the cheapest security win you can add to an iframe that shows content you did not write: user HTML, an ad slot, a preview of a generated page. The hard part is knowing which tokens a given embed needs, because a missing one usually fails silently. The live frame below runs real tests, so you can see each failure instead of guessing. For the basics of embedding, start with our HTML iframe and video guide.
Toggle tokens and test the frame
The frame below is a srcdoc document. allow-same-origin is not offered here, because a srcdoc frame would then share this page's origin, the exact pairing the tips warn about.
Tokens
- allow-scripts
- allow-forms
- allow-popups
- allow-modals
Current attribute
sandbox=""Scripts are blocked, so nothing in this frame can run.
All sandbox tokens
Tokens are space separated and case insensitive. Anything not listed stays blocked.
| Token | Allows |
|---|---|
| allow-scripts | Run scripts, but not open popup windows |
| allow-same-origin | Keep the document's real origin instead of an opaque one |
| allow-forms | Submit forms (otherwise they render but send nothing) |
| allow-modals | Use alert(), confirm(), print() and prompt() |
| allow-popups | Open windows with window.open() or target="_blank" |
| allow-popups-to-escape-sandbox | Let those new windows open without the sandbox |
| allow-downloads | Download files from links or navigation |
| allow-top-navigation | Navigate the top-level page |
| allow-top-navigation-by-user-activation | Navigate the top-level page, only after a click |
| allow-top-navigation-to-custom-protocols | Open non-http protocols such as mailto: |
| allow-pointer-lock | Use the Pointer Lock API |
| allow-orientation-lock | Lock the screen orientation |
| allow-presentation | Start a presentation session |
| allow-storage-access-by-user-activation | Request unpartitioned cookies through the Storage Access API |
Copyable patterns
Preview user HTML with scripts off
The button inside the frame has an onclick handler, but with an empty sandbox it never runs.
<iframe sandbox title="Preview of the submitted page" style="width:100%;height:90px;border:1px solid #cbd5e1"
srcdoc="<p>Hello from the preview.</p><button onclick="this.textContent='Ran'">Press me</button>"></iframe>Third-party widget on another origin
The widget runs its own script and posts its form, and links can open real tabs. It cannot redirect your page.
<iframe
src="https://widgets.example.net/signup"
title="Newsletter sign-up form"
sandbox="allow-scripts allow-forms allow-same-origin allow-popups allow-popups-to-escape-sandbox"
loading="lazy"
width="400" height="320"></iframe>The same restrictions sent as a response header
A server can sandbox its own page with CSP, which applies even when the page is opened directly rather than framed.
Content-Security-Policy: sandbox allow-scripts allow-forms
How the opaque origin changes things
Without allow-same-origin, the framed document gets a special origin that always fails the same-origin policy. Its self.origin reads as the string "null".
That one rule switches off a lot, because the framed page no longer counts as part of its own site. Widgets that depend on their own cookies or saved state often break here, which is why so many ask for allow-same-origin.
Silent failures are the norm
A blocked popup does not throw; window.open simply returns null. A blocked alert returns straight away without showing anything. A blocked form just sits there. When an embed misbehaves, check the browser console for the frame before blaming the embed's own code.
That is why the demo on this page reports results inside the frame: you see the same quiet behaviour a real embed would hit.
sandbox, allow and CSP do different jobs
sandbox limits what the framed document may do. The allow attribute sets a permissions policy for features such as camera, fullscreen or geolocation. Content-Security-Policy, sent by the framed page's server, limits which scripts and resources it loads.
A careful embed of untrusted content uses all three, and the frame-ancestors part of CSP decides the reverse question: who may frame a page at all.
How it works
- Tick tokens in the list. The frame on the right reloads with exactly that sandbox value, shown above it as copyable code.
- With allow-scripts off, the frame shows only its fallback line, because nothing inside it can run.
- Turn allow-scripts on and the frame reports its own origin, then offers buttons to submit a form, open a popup and show an alert.
- Each button reports whether the browser let it happen. Add the matching token and press it again to compare.
Tips
- Start from an empty sandbox and add tokens one at a time until the embed works. Removing them from a long list is harder to reason about.
- Never combine allow-scripts and allow-same-origin for content served from your own origin. The framed page can then remove its own sandbox.
- Prefer srcdoc or a separate subdomain for user-written HTML, so even a mistake in tokens cannot reach your main site's cookies.
- Sandboxing does not limit camera, microphone or similar features on its own. Use the allow attribute for permissions policy alongside it.
- If links inside the frame must open normal tabs, add allow-popups-to-escape-sandbox, otherwise the new tab inherits the same restrictions.
Frequently asked questions
What does an empty sandbox attribute do?
It applies every restriction at once. The framed document cannot run scripts, submit forms, open popups or modals, navigate the top window, lock the pointer or orientation, start downloads, and it is treated as coming from a unique origin that never matches anyone.
Why is allow-scripts with allow-same-origin dangerous?
When the framed document is on the same origin as your page, those two tokens together let its script reach up and delete the sandbox attribute, so it ends up no safer than an ordinary iframe. The pairing is fine for content from a genuinely different origin, which cannot touch your page anyway.
My sandboxed form does nothing when I press submit. Why?
Without allow-forms the form still renders, but submitting it triggers no validation and sends no data to a server. Add allow-forms. If the form also relies on cookies or storage from its own site, it will need allow-same-origin too.
Can a sandboxed iframe redirect my whole page?
Not unless you add allow-top-navigation, or allow-top-navigation-by-user-activation, which only permits it after the person clicks inside the frame. Leaving both out stops an embed from replacing your page with another site.
Is sandbox enough to show untrusted HTML safely?
It is the main layer, not the only one. Serve untrusted content from a separate origin or srcdoc, keep allow-same-origin off when scripts are on, and add a Content-Security-Policy to the framed document if you control how it is served.
Related: HTML iframe and video guide, Fix iframe refused to connect, iframe generator, CORS explained.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- IndexedDB vs localStorage: Which Browser Storage to Use
- How to Install Node.js and npm on a Mac
- Invoice Template in Google Docs: Make One in Minutes
- Accessible JavaScript Accordion and Tabs
- JavaScript Array Methods Cheat Sheet (Live)
- JavaScript Promises, Async/Await and Fetch
- JavaScript Date Format: Builder and Examples
- JavaScript Event Listeners: Click, Submit, Scroll and Delegation
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App