Developer and Text Tools

Iframe Sandbox and Trusted Types: Contain Untrusted Content

How the iframe sandbox and allow attributes limit embedded content, which token combinations are dangerous, and how to migrate a site to Trusted Types.

Saizul Amin
Saizul Amin3 Nov 2026 · 3 min read
Iframe Sandbox and Trusted Types: Contain Untrusted Content

Two defences against code you do not control

Modern pages are assembled from many sources: widgets, embedded players, user-written content, analytics and a long list of npm packages. Any of these can turn hostile through a bug or a compromise. Two browser features help limit the damage. The sandbox attribute contains what runs inside an iframe. Trusted Types stop your own page from turning attacker-controlled strings into HTML or script.

Least privilege for iframes

An iframe with an empty sandbox attribute is locked down. Scripts do not run, forms do not submit, pop-ups and downloads are blocked, and the content gets a unique opaque origin, so it cannot read cookies or storage. You then add tokens for exactly what the content needs. A comment preview or an e-mail body rendered from untrusted HTML often needs nothing at all. A booking widget may need allow-scripts, allow-forms and allow-popups.

Some tokens deserve extra care. allow-same-origin restores the real origin of the content. Combined with allow-scripts on content served from your own origin, it lets the frame reach into your page and remove the sandbox, so the protection disappears. Use that pair only for content on a different origin that you trust. allow-top-navigation lets the frame redirect your whole page at any time; the by-user-activation variant requires a click. allow-popups-to-escape-sandbox opens windows without restrictions and suits trusted outbound links only.

The allow attribute is the other half. Powerful features such as the camera, fullscreen, payment and geolocation are blocked in cross-origin frames unless you delegate them, and the page's own Permissions-Policy sets the upper limit. A video embed typically needs fullscreen, autoplay, encrypted-media and picture-in-picture; very little else.

Trusted Types in plain words

Most DOM-based cross-site scripting comes from a small set of sinks: innerHTML and similar properties, document.write, eval and new Function, string timers, and script or worker URLs. Trusted Types make these sinks refuse plain strings once you send require-trusted-types-for 'script' in your Content Security Policy. Instead, code must pass values through a named policy, a function you write that sanitises HTML, checks script URLs against an allowlist or refuses input entirely. Dangerous code paths become few, named and reviewable.

A realistic migration

Turning enforcement on in one step breaks every sink at once, so teams migrate in stages. Use the Iframe Sandbox and Trusted Types Policy Builder to scan your front-end code for sinks and see how many of each type you have. Many can simply switch to textContent or DOM methods that need no HTML. For the rest, define one or two policies: one that sanitises HTML, for example with DOMPurify, and one that allows scripts only from your own CDN.

Next, ship the header in report-only mode together with a default policy that logs every unconverted sink. Watch the reports for a week or two, fix each caller, then switch the header to enforce. Third-party libraries that create policies with the same name may need the allow-duplicates keyword; libraries that rely on eval are best isolated in a sandboxed frame.

Test what you build

For iframes, look at the probe in the builder: it shows whether scripts run inside the frame, which origin it has and whether it can reach the parent, which turns the abstract token list into something you can see. For Trusted Types, use your browser's console during report-only mode; every violation names the sink and the line. Both features complement server-side sanitising and a strict Content Security Policy; none of them replaces careful code review.

Where each defence stops

Neither feature is a complete answer on its own. A sandbox limits a frame but cannot stop a widget from showing misleading content inside its own box, and Trusted Types only guard the sinks they know about, not server-side templates. Keep them as layers alongside output encoding on the server and regular dependency updates.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages