AI summary
Your embedA script or element from one site that runs inside another site's page, such as a popup, a chat button or a payment form. To embed it is to add it to that page. script runs on someone else's site. When I'd use Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree., when I'd use a cross-originLoaded from a different origin than the page. The origin is the domain, protocol and port together, so a different port already counts. iframeAn HTML element that loads a separate page inside the current page., and what each one still lets through.
- Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree. keeps the page's CSS selectors out, but styles can still pass down from parent elements. Sizes based on the page’s root font size and the page’s JavaScript still affect the embedA script or element from one site that runs inside another site's page, such as a popup, a chat button or a payment form. To embed it is to add it to that page..
- A cross-originLoaded from a different origin than the page. The origin is the domain, protocol and port together, so a different port already counts. iframeAn HTML element that loads a separate page inside the current page. is the option to use when the site's own scripts must not read what's inside.
- You can combine them: the popup in Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree., with an iframeAn HTML element that loads a separate page inside the current page. only for the sensitive step.
The script works on your test page. Then someone adds it to a site with a global button rule, and your layout changes.
At Popupsmart I worked with the team on the PreactA small JavaScript library for building UI components, with the same API as React. runtime that shows popups on customer sites. By runtime I mean the embedA script or element from one site that runs inside another site's page, such as a popup, a chat button or a payment form. To embed it is to add it to that page. script a customer adds to their site. A lot of that work was making the runtime behave on sites we didn’t control, because their CSS, other scripts and loading order could all affect it. I’ll call the customer’s page the host page, or just the host.
Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree. and iframeAn HTML element that loads a separate page inside the current page.s both help with that, but they protect different things.
Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree. puts your elements in a separate tree, the shadow tree, attached to one element on the page. The root of that tree is the shadow root. It protects styles: the page’s CSS selectors stay out, and its scripts can still get in.
An iframeAn HTML element that loads a separate page inside the current page. loads a separate document inside the page. If it comes from a different origin, meaning a different domain, protocol or port, it’s a cross-originLoaded from a different origin than the page. The origin is the domain, protocol and port together, so a different port already counts. iframeAn HTML element that loads a separate page inside the current page.. Besides keeping the page’s CSS out, a cross-originLoaded from a different origin than the page. The origin is the domain, protocol and port together, so a different port already counts. iframeAn HTML element that loads a separate page inside the current page. stops the page’s scripts from reading what’s inside.
What Shadow DOM keeps out
Press “Apply host CSS” in this demo. The same host rule changes the regular button and leaves the one inside the shadow root alone.
Host rule is off.
Host selectors don’t match inside your shadow tree, and your styles don’t leak out. The page can still reach you in a few ways:
- Inherited properties such as
colorandfontcross the boundary. Putall: initialon:hostThe CSS selector for the element your shadow root is attached to. You write it inside the shadow root's own styles. to start clean, then setdisplayafter it, because that reset makes the element inline. remfollows the root element. If the host setshtml { font-size: 62.5% }, anything you sized inremshrinks. I’d usepxor your own CSS variable.- Your elements still sit in the host’s stacking orderThe order in which the browser draws elements that overlap. It decides which one ends up on top., so a high
z-indexon the page can cover them. - Your code runs in the host’s page, with its global variables and its Content-Security-PolicyA header where a site lists which scripts, styles and frames its pages may load..
- Host scripts can read an open shadow root through
shadowRoot. Closed mode,attachShadow({ mode: "closed" }), hides that property, but MDN says not to treat it as a strong security mechanism.
Gainsight’s community platform uses Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree. for custom widgets. A customer writes a widget in HTML, CSS and JavaScript, and Gainsight’s developer docs say it runs inside a custom elementAn HTML tag you define yourself in JavaScript, such as my-widget. The browser treats it like any built-in element., <gs-cc-registry-widget>, that uses Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree.. Each widget gets its own shadow root, so its styles stay out of the community page and out of the other widgets.
The same docs note that document.querySelector() can’t reach the widget’s elements. You go through the custom elementAn HTML tag you define yourself in JavaScript, such as my-widget. The browser treats it like any built-in element.’s shadowRoot instead, the same property the last point says host scripts can read.
Here’s a separate example of how to mount a PreactA small JavaScript library for building UI components, with the same API as React. widget in a shadow root with Vite.
import { render } from "preact";
import { Popup } from "./Popup";
// Vite: ?inline imports the CSS file as a string.
import css from "./popup.css?inline";
const host = document.createElement("div");
document.body.append(host);
const shadow = host.attachShadow({ mode: "open" });
const style = document.createElement("style");
style.textContent = css; // Starts with :host { all: initial; display: block; }
// Give Preact its own node so it can't touch the style element.
const mount = document.createElement("div");
shadow.append(style, mount);
render(<Popup />, mount);
export function destroy() {
render(null, mount);
host.remove();
}What an iframe adds
An iframeAn HTML element that loads a separate page inside the current page. is a separate document with its own CSS and global variables. If it’s on a different origin, the host gets very limited access to its Window and can’t read what’s inside. The two sides talk through postMessage, the browser API for sending messages between windows.
Here’s what ChromiumThe open-source browser that Chrome and Edge are built on. returns when the page runs on port 8001 and the iframeAn HTML element that loads a separate page inside the current page. comes from port 8002. A different port is already a different origin, so the page can’t read the iframeAn HTML element that loads a separate page inside the current page.:
const frame = document.querySelector("iframe");
frame.contentDocument; // null
frame.contentWindow.document; // throws SecurityErrorStripe uses cross-originLoaded from a different origin than the page. The origin is the domain, protocol and port together, so a different port already counts. iframeAn HTML element that loads a separate page inside the current page.s this way. Stripe.js, the script a shop adds to show Stripe’s payment form, loads its iframeAn HTML element that loads a separate page inside the current page.s from Stripe’s own origins, such as js.stripe.com. For Stripe’s embeddedA script or element from one site that runs inside another site's page, such as a popup, a chat button or a payment form. To embed it is to add it to that page. crypto purchase form, Stripe’s security guide says it directly: the form renders inside an iframeAn HTML element that loads a separate page inside the current page. served from crypto.link.com. The shop’s own scripts can’t read what the buyer types there.
That separation costs you a few things:
- The iframeAn HTML element that loads a separate page inside the current page. is a rectangle. Nothing inside can draw outside it, so a chat button that opens into a panel has to ask the host page to resize the iframeAn HTML element that loads a separate page inside the current page..
- The browser loads a second document.
- The customer’s Content-Security-PolicyA header where a site lists which scripts, styles and frames its pages may load. can block your iframeAn HTML element that loads a separate page inside the current page. through its
frame-srclist. That’s why Stripe’s guide lists its iframeAn HTML element that loads a separate page inside the current page. origins for sites that set this header. On your side,frame-ancestorsdecides who may embedA script or element from one site that runs inside another site's page, such as a popup, a chat button or a payment form. To embed it is to add it to that page. you, and leaving it out allows anyone. - Don’t expect its cookies or storage to follow a user between sites. Firefox keeps a separate copy for each site your iframeAn HTML element that loads a separate page inside the current page. is embeddedA script or element from one site that runs inside another site's page, such as a popup, a chat button or a payment form. To embed it is to add it to that page. in, and other browsers restrict them too.
- It needs a
title, or screen reader users have to go inside to find out what it is.
Check both sides of every message. Send with an exact target origin instead of *, and on the receiving side verify the sender and the shape of the data:
// On the host page
const WIDGET_ORIGIN = "https://widget.example.com";
const frame = document.querySelector<HTMLIFrameElement>("#widget")!;
window.addEventListener("message", (event) => {
if (event.origin !== WIDGET_ORIGIN) return;
if (event.source !== frame.contentWindow) return;
if (event.data?.type !== "widget:height") return;
if (!Number.isFinite(event.data.height)) return;
frame.style.height = `${event.data.height}px`;
});How I’d choose
For a popup, banner or chat button that sits on top of the page and doesn’t handle secrets, I’d start with Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree..
For a field the host must not read, such as a password, card number or token, I’d use an iframeAn HTML element that loads a separate page inside the current page. served from your own origin. Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree. doesn’t protect those from code running on the page.
You can combine them. Keep the popup itself in Shadow DOMA browser feature that attaches a separate tree of elements to one element on the page. The page's CSS selectors don't reach into that tree. and load an iframeAn HTML element that loads a separate page inside the current page. only for the sensitive step.
If your embedA script or element from one site that runs inside another site's page, such as a popup, a chat button or a payment form. To embed it is to add it to that page. renders HTML you don’t trust, use an iframeAn HTML element that loads a separate page inside the current page. with sandbox. That attribute turns off scripts, forms and other features inside the iframeAn HTML element that loads a separate page inside the current page., and each allow-* value turns one of them back on. On same-origin content, don’t combine allow-scripts with allow-same-origin, because the document can then remove the sandbox.
If the untrusted part is text, such as an AI model’s reply, and you only need a few formats, you may not need an iframeAn HTML element that loads a separate page inside the current page.. In the chat on my home page I don’t pass the reply to innerHTML. I build the DOM nodes myself, and I wrote up how that chat renders model output.
Whichever you pick, write down your embedA script or element from one site that runs inside another site's page, such as a popup, a chat button or a payment form. To embed it is to add it to that page.’s boundaries early: what it can change on the page, what it owns and what it has to clean up when it closes. The destroy function above is the cleanup part, and I’d want it from the first version. If I worked on the Popupsmart runtime again, writing those boundaries down is what I’d do earlier.
