Letting one app frame another, safely
To move an operations console to a new app screen by screen, we embedded new pages in the old one with iframes. What I read along the way about framing headers, cookies, postMessage and the iframe attributes that make it work without opening a hole.
Our operations console was an older React app that the whole company used every day. New screens were being built in our newer dashboard instead. We wanted operations staff to reach those new screens without leaving the console they knew, so we embedded them: the old console shows the new page in an <iframe>, and the two talk over postMessage. I wrote about how that migration ran. This post is about everything I had to read before an iframe like that is both working and safe.
Why embed at all
The alternatives were all worse for us:
- Rebuild each new screen in the old app. Twice the work, and the old app was the one we were leaving.
- Link out to the new app. Operations staff would bounce between two apps, two navigations and two sign-in states.
- Put both apps behind one shell (micro-frontends). A bigger architectural change than a migration should need.
An iframe gives you a hard boundary for free. The embedded page runs its own code, styles and framework version, and neither side can break the other’s CSS or global state. That isolation is exactly what you want while two generations of an app live side by side.
The price is that a browser treats a framed page with suspicion, for good reason, and several separate rules have to line up. Here they are, in the order a browser applies them. Try the combinations:
1. The embedded page must agree to be framed
By default, any website can put your page in an iframe. That’s the basis of clickjacking: a hostile site frames your real page, makes it invisible, and lines it up under a button of its own, so a visitor’s click lands on your page instead. Signed-in users can be tricked into approving, deleting or paying for things.
So pages that matter should say who may frame them. There are two headers for that.
X-Frame-Options is the older one. It has two useful values:
DENY: nobody may frame this page.SAMEORIGIN: only pages from exactly the same origin (scheme, host and port) may frame it.
It can’t name a list of allowed sites. The old ALLOW-FROM value was meant to, but modern browsers ignore it, so a page relying on it isn’t protected at all.
Content-Security-Policy: frame-ancestors is the modern one, and it does take a list:
Content-Security-Policy: frame-ancestors 'self' https://console.example.com
That says: this page may be framed by its own origin and by the console, and by nobody else. frame-ancestors 'none' is the equivalent of DENY.
A few things that caught me out:
- When both headers are present,
frame-ancestorswins. Modern browsers ignoreX-Frame-Optionsif the CSP setsframe-ancestors. Many sites send both anyway, for old browsers, but the CSP is what decides. SAMEORIGINis stricter than it sounds.console.example.comanddashboard.example.comare different origins. A dashboard that sendsX-Frame-Options: SAMEORIGINcan’t be framed by its own company’s console. That’s the most common reason an embed “just doesn’t load”.- It has to be a real HTTP header.
frame-ancestorsdoesn’t work in a<meta>tag, andX-Frame-Optionshas no meta form either. If you can only edit HTML, you can’t control framing. - It goes on the page being framed, not the parent. The parent can’t make a page frameable; only the embedded page’s server can allow it.
- Allow per environment. Development, UAT and production consoles live on different hosts, so the list of allowed parents differs per environment too. Our iframe component had the same shape problem: its first version assumed the new app lived on the same origin, and the next change gave each environment its own host.
2. Cookies decide whether the frame is signed in
Even when framing is allowed, the embedded page needs its session. Its cookies are sent according to their SameSite attribute, and SameSite compares sites, not origins. A site is roughly the scheme plus the registrable domain, so console.example.com and dashboard.example.com are different origins but the same site.
- Same-site frame:
SameSite=LaxandStrictcookies are sent. The user is signed in. This is the comfortable case, and a strong argument for hosting related internal apps under one registrable domain. - Cross-site frame: only cookies marked
SameSite=None; Secureare sent, and even those are now third-party cookies. Safari and Firefox restrict third-party cookies by default, and Chrome lets users block them. Cookies with thePartitionedattribute (CHIPS) get a separate jar per top-level site, which works for some embeds but not for sharing one sign-in.
The practical lesson: if you control both apps, put them on the same site. Cross-site embedding of a signed-in app is fragile, and getting more so.
3. Talk through postMessage, and check who’s talking
The frame and its parent are separate documents. When they’re on different origins, neither can read the other’s DOM or variables. window.postMessage is the sanctioned channel. Our embedded pages sent the console a small set of typed events: the route changed (so the console’s address and breadcrumbs follow along), or the session timed out (so the whole console signs out, not just the frame).
Two rules matter more than anything else here:
// Sending: name the exact origin you mean, never '*'.
parent.postMessage({ type: 'ROUTE_CHANGE', path: '/settlements' }, 'https://console.example.com');
// Receiving: ignore anything not from the origin you expect, and anything you don't recognise.
window.addEventListener('message', (e) => {
if (e.origin !== DASHBOARD_ORIGIN) return;
if (e.data?.type === 'SESSION_TIMEOUT') signOut();
if (e.data?.type === 'ROUTE_CHANGE') syncRoute(e.data.path);
});
Any window can send you a message. Checking event.origin, and accepting only a short list of known message types, is what keeps a hostile page from driving your app.
4. The iframe element has its own controls
The <iframe> tag itself carries a few attributes worth knowing:
title: screen readers announce frames by their title. Every iframe needs one.allow: Permissions Policy for the frame. Features like the clipboard, camera or fullscreen are off inside a cross-origin frame unless the parent allows them, for exampleallow="clipboard-write". A “Copy” button that works in the app and fails in the console is usually this.sandbox: switches off scripts, forms, popups and same-origin access unless you list them back. Great for untrusted content. For your own app, be careful:allow-scriptstogether withallow-same-originon a same-origin page lets the frame remove its own sandbox, so the combination protects nothing.referrerpolicyandloading="lazy": what the frame learns about where it was opened from, and whether it loads before it’s on screen.
5. Headers that can break an embed by accident
A few other headers don’t exist to control framing, but can still stop a frame from loading:
Cross-Origin-Embedder-Policy: require-corpon the parent (needed for some advanced browser features) means every embedded page must opt in, with its own COEP header or aCross-Origin-Resource-Policythat allows it.Cross-Origin-Opener-Policyaffects popups and new windows, not iframes. It’s worth knowing so you don’t chase it when an iframe fails.- A strict CSP on the parent needs a
frame-src(orchild-src) entry for the embedded origin, or the parent refuses to load it. That’s the mirror image offrame-ancestors: the parent says what it will embed, and the child says who may embed it. Both have to agree.
6. Make the embedded page know it’s embedded
A full app inside another full app shows two sidebars and two headers. The new console checks whether it’s framed (window.self !== window.top) and, if so, hides its own navigation and lets the old console’s chrome do the job. The permission checks stay on: hiding the sidebar is about layout, never about access.
A checklist
| Where | What | Why |
|---|---|---|
| Embedded page | Content-Security-Policy: frame-ancestors 'self' <each console origin> |
Only your console may frame it |
| Embedded page | Optionally X-Frame-Options: SAMEORIGIN for old browsers |
Ignored where frame-ancestors is supported |
| Embedded page | Session cookie SameSite=Lax, apps on the same site |
The frame is signed in, without third-party cookies |
| Parent page | frame-src for the embedded origin, if the parent has a CSP |
The parent agrees to load it |
| Parent page | <iframe title=… allow=…> |
Accessible, and features like copy work |
| Both | postMessage with an exact target origin, and event.origin checks |
Only the two apps can talk to each other |
| Embedded page | Detect framing and hide duplicate navigation | One app’s chrome, not two |
What I learned
- Framing is opt-in on the embedded side. The page being framed decides who may frame it. The parent can’t override that.
- Origin and site are different words for a reason. Framing headers compare origins; cookies compare sites. Mixing them up explains most “it works in one environment but not another” bugs.
- Same-site is the easy road. Two apps under one registrable domain avoid nearly every cookie problem in embedding.
- The iframe boundary is a feature. During a migration, the hard wall between old and new code is what lets both keep shipping.
- Every message channel needs a bouncer.
postMessagewithout origin checks is an open door.