All guides

The sandbox attribute

The sandbox attribute removes powers from the page inside an iframe. An empty sandbox blocks scripts, forms, popups, and top-level navigation, and gives the page a unique origin. Each flag you add gives one power back.

5 min read · Updated

How the sandbox works

Add sandbox to an iframe to lock it down. With no value, the framed page gets the strictest rules:

<iframe src="https://partner.example/ad" sandbox title="Ad"></iframe>

The framed page can still show HTML and CSS. It cannot run scripts, submit forms, open popups, or change the URL of your page. The browser also gives it a unique origin that matches no other origin. So it cannot read its own cookies or localStorage.

To give back a power, add its flag. Separate flags with spaces:

<iframe
  src="https://partner.example/widget"
  sandbox="allow-scripts allow-forms allow-popups"
  title="Partner widget"
></iframe>

The sandbox only removes powers. It cannot give a frame a power that headers or the browser already block.

Every flag

FlagWhat it allows
allow-scriptsRun JavaScript. Without it, most modern apps show a blank page.
allow-same-originKeep the real origin. Without it, cookies, localStorage, and same-origin requests fail.
allow-formsSubmit forms. Without it, a form shows but sending it does nothing.
allow-popupsOpen new windows or tabs with window.open() or target="_blank". Without it, these fail with no error.
allow-popups-to-escape-sandboxOpen popups without the sandbox. Without it, the new tab gets the same limits as the frame.
allow-modalsUse alert(), confirm(), prompt(), and print(). A <dialog> works without it.
allow-downloadsStart file downloads.
allow-top-navigationChange the URL of the top page at any time.
allow-top-navigation-by-user-activationChange the URL of the top page, but only after a click or tap.
allow-top-navigation-to-custom-protocolsOpen links with non-HTTP schemes, such as mailto: or app links.
allow-storage-access-by-user-activationAsk for access to its own cookies with the Storage Access API.
allow-pointer-lockUse the Pointer Lock API, for example in games.
allow-orientation-lockLock the screen orientation.
allow-presentationStart a presentation session on a second screen.

Use allow-top-navigation-by-user-activation in place of allow-top-navigation. A frame with the full flag can send your visitor to another site with no click.

The dangerous pair: allow-scripts and allow-same-origin

Do not use allow-scripts and allow-same-origin together when the framed page has the same origin as your page.

With both flags, the framed page can run a script and reach into your page. It can then remove the sandbox attribute from its own iframe and reload. After that, it runs with no sandbox at all.

<!-- Unsafe: same origin as the parent, both flags set -->
<iframe src="/user-content/123" sandbox="allow-scripts allow-same-origin"></iframe>

For content from users, serve it from a different origin, for example https://usercontent.your-site.com. Then the pair is much safer, because the origin check keeps the frame out of your page.

Sandbox flags for common embeds

EmbedFlags to start with
Static HTML previewnone (empty sandbox)
Code playground on its own originallow-scripts allow-modals
Third-party widget or formallow-scripts allow-same-origin allow-forms allow-popups
Login or payment flowallow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox

Add one flag at a time and test. Open the browser console. Most sandbox blocks print a message that names the missing flag.

When the sandbox breaks a page

A server can also sandbox its own page with the sandbox directive of Content-Security-Policy. The same flags apply.

Check the page first

Paste the framed URL into the iframe tester. If the server blocks framing, no sandbox flag can fix it.

To try flags on a live page, use the sandbox playground. Turn each flag on or off and see what the framed page can still do. The embed code generator then writes the iframe code with the flags you pick.

Sources

Check your page now

Know before you embed.

Free, no sign-up, nothing stored. Paste a URL and get a verdict, a live preview, and the fix.