Your AI widget works on staging and goes blank in client production
The usual culprit is two CSP allowlists on opposite sides of an iframe, and knowing which one refuses saves a week of email with the security team.
In brief
- When an iframe is blocked, check two places: frame-src (or default-src) on the client's page and frame-ancestors on the widget's page. frame-ancestors only works when sent as an HTTP header.
- An iframe isolates the widget from the client's page. A Web Component with Shadow DOM isolates only styles and IDs; its code still runs under the client's CSP.
- Use a fresh nonce per request instead of unsafe-inline, send postMessage to a specific targetOrigin and always check the origin of incoming messages.
The demo on staging ran without a hitch, the client nodded and the go-live date was set. On the morning of the deploy to the real customer portal, the space reserved for the AI assistant is an empty white box, and the console shows a single red line: “Refused to frame”.
Not one line of your code has changed. The page around it has: the client’s production site enforces a strict Content Security Policy, and staging does not.
An engineer who can read the boundary between the two pages quickly fixes this in a morning. One who cannot spends a week trading emails with the client’s security team.
This guide explains how to read that boundary: whether to choose an iframe or a Web Component, what the client must open for each, and how to let the two pages talk without creating a new hole.
An iframe has two gatekeepers
Chrome for Developers’ documentation on CSP starts from a plain fact: by default, the browser runs any code a page asks for. So a page has to declare an allowlist of sources it trusts. With an iframe, there are two allowlists, one on each side.
On the client’s page, frame-src sets where that page may load iframes from. On your widget’s page, frame-ancestors sets which parent pages may embed it. MDN is explicit that the two directives point in opposite directions. Faced with a blank box, the first question is which side is refusing.
Two traps commonly lead to the wrong diagnosis. The first: if the client’s CSP has no frame-src, the browser falls back to child-src, then to default-src. An innocent-looking default-src 'self' is enough to block your iframe, even while the client insists they “don’t block any frames”.
The second trap is on your side: frame-ancestors has no effect when declared in a <meta> tag; it must be sent as an HTTP header. If the widget sits on a static host and the policy lives in the HTML, you have in effect declared nothing. Configure it at the CDN or the server.
Iframe or Web Component: choose the boundary first, the technology second
Web Components are a trio: custom elements, Shadow DOM and HTML templates. According to MDN, their purpose is encapsulation, so there are fewer name and style collisions with the host page. Shadow DOM keeps the widget’s styles and IDs separate from the rest of the page, so the client’s CSS does not break your buttons, and vice versa.
The point that is easy to misread is that this isolation is visual, not a security boundary. A custom element runs directly in the client’s document.
The consequence follows directly. Because it lives in the client’s page, the widget’s scripts and API calls must pass the page’s CSP, like every other script there. An iframe on a separate origin loads the widget as a separate document, and the two sides exchange data through postMessage, a channel whose both ends you control.
That gives a workable rule for choosing. If the widget needs to blend into the interface and read page state, and the client accepts running your code in their page, a Web Component makes sense. If the widget holds tokens, conversation history or end-user data that you do not want any other script on the page to touch, choose an iframe.
Example: a voice assistant in a customer portal
Say you are adding an AI assistant with voice input to https://app.client.example. The widget lives at https://widget.vendor.example and the API at https://api.vendor.example. You choose an iframe because the widget holds the conversation session. The first step is to declare your side in a header:
Content-Security-Policy: frame-ancestors https://app.client.example
On the client’s side, suppose the current header contains only default-src 'self'. Under the fallback rule, the iframe will be blocked. Ask the client to add exactly one directive rather than loosen default-src:
Content-Security-Policy: default-src 'self';
script-src 'self' 'nonce-{fresh-value-per-request}' 'strict-dynamic';
frame-src https://widget.vendor.example
The snippet that loads the widget is an inline script, so it needs a nonce. Infobip, in its documentation for the Live Chat widget, recommends a nonce and discourages unsafe-inline unless the client’s directive already contains it. Chrome for Developers adds that the nonce must be generated fresh for every request and must not be guessable. A nonce hard-coded in a template protects nothing.
Why strict-dynamic? Atlassian explains in its widget documentation that the script that loads the widget is updated frequently, so a hash-based CSP is a poor fit, and the company recommends strict-dynamic.
Atlassian also requires the client to open frame-src to the widget’s host, as in the example above.
Now the voice part. The iframe tag needs an allow attribute:
<iframe src="https://widget.vendor.example/chat?tenant=client"
allow="microphone"
sandbox="allow-scripts allow-same-origin allow-forms"></iframe>
MDN notes that allow can only tighten the policy already set by the page’s Permissions-Policy header; it does not replace that header. If the client sends Permissions-Policy: microphone=(), your microphone button will stay silent whatever the iframe tag says. Ask about this during discovery, not on go-live day.
The sandbox attribute needs a careful read. MDN warns strongly against combining allow-scripts and allow-same-origin when the embedded content shares an origin with the parent page, because the embedded page can then remove its own sandbox.
The configuration above is acceptable only because the widget sits on its own origin. If the client one day proposes proxying the widget to app.client.example/assistant to “tidy things up”, that is the moment to drop one of the two flags.
postMessage is a back door if you forget the origin check
The client’s page often needs to pass context into the widget, such as which order the user is viewing. The widget needs to report out, for example that the conversation has been handed to a human agent. The standard channel for this is postMessage, and MDN sets two rules for it.
When sending, always specify a concrete targetOrigin and never use "*", because the target window may have navigated elsewhere and the data could land with a stranger. When receiving, check origin, and ideally source as well. MDN states plainly that skipping this step opens the way to cross-site scripting attacks.
const WIDGET = 'https://widget.vendor.example';
const frame = document.querySelector('iframe');
frame.contentWindow.postMessage({ type: 'context', orderId: 'A123' }, WIDGET);
window.addEventListener('message', (e) => {
if (e.origin !== WIDGET) return;
if (e.source !== frame.contentWindow) return;
if (e.data?.type === 'handoff') openAgentPanel(e.data.ticketId);
});
The widget side does the same in reverse, and the list of valid origins should come from each tenant’s configuration rather than being hard-coded.
The mistakes that push go-live back a week
The most common is fixing the wrong side: asking the client to loosen their CSP when the block is actually your own frame-ancestors, or the reverse. The second is asking for too much. The client will refuse default-src * or unsafe-inline, and with good reason.
The third is assuming that choosing an iframe means requesting only frame-src. Infobip states clearly that for its Live Chat widget, which loads through an iframe, the client must still open script-src, connect-src, frame-src and style-src together.
Even when the widget lives in an iframe, the code that loads it on the client’s page still has to pass the client’s CSP. So hand over the full list from the start, instead of making the client open one directive after each failure.
What to bring to the meeting with the security team
That list should become a prepared document, “CSP requirements for embedding the widget”, setting out each directive, each domain and the reason it is needed. With it, the meeting goes line by line instead of circling through questions.
The document is also the best evidence for a CV. Instead of “frontend integration”, write that you designed the embedding configuration and the CSP requirements that took the widget to production on a system with a strict security policy.
Security teams trust people who understand their policy more than people who simply ask them to open it up.
Was this article useful?
Thanks for the feedback!
8 sources
- Content-Security-Policy: frame-ancestors directive - HTTP | MDN · 2026-08-27
- Content-Security-Policy: frame-src directive - HTTP | MDN
- <iframe> HTML inline frame element - HTML | MDN
- Web Components - Web APIs | MDN
- Window: postMessage() method - Web APIs | MDN
- Content Security Policy (CSP)
- About the widget security policy | Customer Service Management | Atlassian Support
- Content Security Policy | Privacy & Security | Chrome for Developers