Safeguard
Security Guides•

Embed a third-party widget as an iframe or a script tag?

A sandboxed cross-origin iframe limits what a vendor widget can reach, while a script tag gives it your whole page. Use this guide to pick per widget.

Safeguard Security Team
3 min read

Choose a sandboxed cross-origin iframe when the vendor allows it, because the widget cannot reach your DOM or cookies. Use a script tag only when the widget must interact deeply with your page, and then add CSP, version pinning and monitoring. OWASP recommends the iframe approach for vendor code.

The difference in one table

Script tagCross-origin sandboxed iframe
Runs inYour page contextIts own origin and window
Can read your form fieldsYesNo
Can change your pageYesNo, only its own frame
CommunicationDirect function callspostMessage, with origin checks
Layout integrationFull controlFixed size or resize messages
Vendor supportAlwaysNot always offered
Page speed and stylingShares your stylesOwn styles, extra load

OWASP's cheat sheet advises loading vendor code in a cross-origin iframe with a sandbox attribute so it cannot reach the host DOM or cookies, and exchanging messages through postMessage while checking the sender's origin.

Questions to decide

  1. Does the widget need to read or write your page? A chat bubble that only draws itself can live in a frame. A personalization script that rewrites your page cannot.
  2. Does it sit on a sensitive page? Login, checkout and account pages should carry as little third party code as possible.
  3. Does the vendor publish a frame based embed? Many payment and identity providers do, and it is often the supported route.
  4. Can you live with the layout limits? Frames need sizing and sometimes resize messages.
  5. What data would a compromised version reach? If the answer is credentials or payment details, push hard for the iframe.

How to set up the iframe safely

  1. Serve the widget from a different origin than your site.
  2. Add the sandbox attribute with the smallest token set that works. Start with none and add tokens as needed, see iframe sandbox tokens.
  3. Use postMessage with a specific target origin, never a wildcard, for anything sensitive.
  4. On receiving messages, check the origin and validate the data shape before acting.
  5. Add a frame-src directive in your CSP so only expected frame hosts load.

If you must use a script tag

Remember OWASP's warning that a data layer is not a sandbox. Anything running in your page's trust boundary stays inside it.

Safeguard scans the dependencies you bundle and builds an SBOM, which covers code you take into your own build rather than embedding. A free scan is at gold.safeguard.sh/scan.

Quick check

Is an iframe automatically safe?

No. It needs to be cross-origin, sandboxed with minimal tokens, and your message handling must check origins. A permissive sandbox gives little protection.

Why not iframe everything?

Some scripts must change your page to work, and frames add layout and performance work. Use frames where isolation is possible.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.