Iframe cookies: how do cookies work in a cross-site iframe?
IntermediateSecurity interview questionBy UIRound, updated 24 November 2026
Short answer
A cookie in an iframe from another site is a third-party cookie. The browser sends it only if it was set with SameSite=None; Secure, and only if the browser allows third-party cookies at all. As of late 2026, Safari blocks them, Firefox gives each top-level site its own copy, and Chrome still allows them by default. Use a Partitioned cookie, the Storage Access API or a token passed with postMessage instead.
First-party and third-party context
What matters is not where the cookie came from but which site is in the address bar. Suppose shop.example embeds <iframe src="https://pay.example/checkout">.
- On
pay.exampleitself, as a top-level page, its cookies are first-party. EverySameSitevalue works. - Inside the iframe on
shop.example, the same cookies are third-party, because the top-level site (shop.example) differs from the cookie's site (pay.example). This applies to the iframe's own navigation request and to everyfetchthe iframe makes to its own server, even though those requests are same-origin from the iframe's point of view.
The browser compares sites, not origins, and it checks the whole chain of frames up to the top. If app.example.com embeds widgets.example.com, the iframe is same-site and its cookies are first-party, because both share the registrable domain example.com. SameSite cookies, Strict vs Lax vs None explains how a site is defined; this page goes deeper on the iframe case.
Why a cross-site iframe needs SameSite=None; Secure
Loading an iframe is not a top-level navigation, so SameSite=Lax cookies are not sent with it, and Strict ones never are. A cookie without the attribute is treated as Lax in Chromium browsers. So an iframe that signs users in with a normal session cookie looks logged out the moment someone embeds it.
The cookie has to opt in to cross-site use:
Set-Cookie: __Host-sid=8f2c; Path=/; Secure; HttpOnly; SameSite=NoneBrowsers reject SameSite=None without Secure, so the embed must be served over HTTPS. For the other attributes (HttpOnly, Path, the __Host- prefix), see what the HttpOnly, Secure, Domain and Path attributes do.
SameSite=None is a permission, not a guarantee. It says the cookie may travel cross-site. Whether the browser actually stores and sends it in a third-party iframe is a separate policy, and that is where browsers differ.
What each browser does as of late 2026
| Browser | Unpartitioned third-party cookies by default | What the iframe sees |
|---|---|---|
| Safari | Blocked by Intelligent Tracking Prevention since Safari 13.1 (2020) | No cookies, unless the cookie is Partitioned (Safari 26.2 and later) or the frame gets storage access |
| Firefox | Partitioned by Total Cookie Protection, on by default since 2022 | A separate cookie jar per top-level site; cookies of known trackers are blocked |
| Chrome | Allowed, unless the user blocks them in settings or uses Incognito | The same cookies on every embedding site |
Safari. Cross-site cookies have been blocked across the board since Safari 13.1 on macOS and iOS 13.4. The only routes to unpartitioned cookies are the Storage Access API or the user turning off cross-site tracking prevention. Safari 26.2 (December 2025) brought back CHIPS as an opt-in, so a cookie with Partitioned now works in an embed. Older Safari versions drop it.
Firefox. Total Cookie Protection, part of the default Enhanced Tracking Protection, stores third-party cookies in a jar keyed by the top-level site. Your iframe can set and read cookies, but the jar on shop.example is not the jar on other-shop.example, and neither is the jar pay.example uses as a top-level page. So a user who signed in on pay.example directly still looks signed out inside the iframe.
Chrome. Google planned to remove third-party cookies, then changed course:
- 22 July 2024: Google said that "instead of deprecating third-party cookies" it would introduce a new experience in Chrome that lets users make a choice.
- 22 April 2025: Google said it would not roll out a standalone third-party cookie prompt and would keep the current approach: users choose in Chrome's Privacy and security settings. Incognito already blocks third-party cookies by default.
- 17 October 2025: Google retired most Privacy Sandbox technologies, including Topics, Protected Audience, Attribution Reporting, IP Protection and Related Website Sets (with
requestStorageAccessFor). It kept CHIPS and FedCM, and its status page lists the Storage Access API as still supported.
So in Chrome a SameSite=None; Secure cookie still works in an iframe for most users. It fails for users who block third-party cookies and in Incognito, and it already fails in Safari and behaves differently in Firefox. Design for the strictest browser, not for Chrome's default.
Partitioned cookies (CHIPS)
CHIPS (Cookies Having Independent Partitioned State) lets a cookie opt in to a separate jar per top-level site:
Set-Cookie: __Host-cart=c91a; Path=/; Secure; HttpOnly; SameSite=None; PartitionedThe browser stores the cookie under two keys: the host that set it and a partition key, which is the site (scheme plus registrable domain) of the top-level page at the time. Set by pay.example inside https://shop.example, the cookie is readable only when pay.example is embedded on shop.example, including on its subdomains such as checkout.shop.example. Embedded on other-shop.example, the iframe gets a different, empty jar.
Because a partitioned cookie cannot follow a user across sites, it is not a tracking tool, and that is why browsers allow it while blocking unpartitioned cookies. The rules:
PartitionedrequiresSecure. AddSameSite=Noneso it is still sent cross-site in browsers that ignore the attribute.__Host-is recommended, so the cookie stays bound to one host.- It can be set from script in the iframe too:
document.cookie = "__Host-cart=c91a; Secure; Path=/; SameSite=None; Partitioned". - Support: Chrome 114, Firefox 141 and Safari 26.2. MDN marks it Baseline 2025.
CHIPS fits state that belongs to one embedding: a cart, a chat session, a CSRF token for the widget on that page. It cannot show "the account you signed into on pay.example last week", because that cookie lives in the unpartitioned jar.
The Storage Access API
When the iframe really needs its unpartitioned, first-party cookies, it can ask for them:
async function ensureCookieAccess() {
if (!document.hasStorageAccess) return true; // API missing: hope cookies work
if (await document.hasStorageAccess()) return true;
try {
const status = await navigator.permissions.query({ name: "storage-access" });
if (status.state === "granted") {
await document.requestStorageAccess(); // resolves without a gesture
return true;
}
} catch {
// some browsers do not support querying this permission
}
return false;
}
signInButton.addEventListener("click", async () => {
try {
await document.requestStorageAccess(); // must run inside the user gesture
await loadAccount(); // fetch with credentials: "include" now carries cookies
} catch {
// Denied, or the user never visited pay.example. A rejected request uses up the click, so a popup
// opened here would be blocked: ask for a second click instead.
showSignInPopupButton();
}
});document.hasStorageAccess()resolves to a boolean: does this document already have its unpartitioned cookies?document.requestStorageAccess()resolves if access is granted and rejects if not. It needs transient user activation (a click or tap in the iframe) unless permission was already granted for the same pair of top-level site and embedded site. A rejection consumes the gesture, so the iframe cannot call it in a loop.- It rejects in an insecure context, in a document with an opaque origin, and in a sandboxed iframe without
allow-storage-access-by-user-activation(it also needsallow-scripts allow-same-origin). The iframe sandbox attribute lists these tokens. - Browsers add their own checks. Safari and Chrome require that the user has visited and interacted with the embedded site as a top-level page before, which stops dozens of embeds from all prompting. Both may show a prompt; grants expire after about 30 days without interaction.
- In Chrome the cookies must still be
SameSite=None; Secure, and the API matters only for users who block third-party cookies, Incognito included. requestStorageAccess({ localStorage: true })can also unlock unpartitioned storage other than cookies, returning a handle such ashandle.localStorage. Check support before relying on it.
storage-access is also a Permissions Policy feature. Its default allowlist is *, so an embed does not need allow="storage-access" on the iframe. An embedder can switch it off with Permissions-Policy: storage-access=(). Chrome 133 also added Storage Access headers (Sec-Fetch-Storage-Access and Activate-Storage-Access) so a server can activate an existing grant without first loading a page and running script.
Patterns for embedded widgets
Pick by what the widget needs to remember:
- State for one embedding only. A
Partitionedsession cookie. No prompt, works in every current engine. - The user's own account on your site. Storage Access from a click, with a fallback when it is denied.
- Sign-in when access is denied or the user never visited your site. Open your site as a top-level page in a popup or redirect. The user signs in first-party, which also satisfies the "visited before" rule, and then either the iframe calls
requestStorageAccess()again, or the popup hands a short-lived token back withpostMessage. For identity providers, FedCM is the browser-native route; how SSO works covers why hidden-iframe silent login broke. - The embedder already has a server relationship with you. The host page asks its own server for a short-lived, scoped token and passes it in with
postMessage. The iframe keeps it in memory and sends it in anAuthorizationheader. No cookies are involved, so no browser policy can block it. Validate origins on both sides as in using postMessage safely. - Recognising one user across every embedding site. Not supported. That is the tracking these rules exist to stop.
Set cookies for an embedded checkout iframe practises choosing between exactly these fallbacks.
Follow-up questions interviewers ask
Why does my iframe cookie work in Chrome but not in Safari?
Chrome still sends unpartitioned third-party cookies by default; Safari blocks them. Mark the cookie Partitioned if per-site state is enough, or use the Storage Access API or a token.
Is a cookie set by the iframe on a same-site parent third-party?
No. If the top-level page and the iframe share a site, such as app.example.com and widgets.example.com, the cookies are first-party and Lax works. Third-party means cross-site, not cross-origin.
Does Firefox block my iframe's cookies?
Usually not, it partitions them: they work, but separately per top-level site, and they are not the cookies your site has as a top-level page. Cookies from domains on its tracker list are blocked.
Can a partitioned cookie be read by the top-level site?
No. It belongs to the iframe's host, under the embedding site's partition. The parent page reads only its own cookies, and the two sides talk through postMessage.
Does localStorage in an iframe have the same problem?
Yes. Browsers partition localStorage, IndexedDB and similar storage in third-party iframes by top-level site, so it behaves like a partitioned cookie. The Storage Access API with a types argument is the way to request unpartitioned storage.
Mistakes to avoid
Setting
SameSite=NonewithoutSecure, so the cookie is rejectedTesting only in Chrome with default settings and shipping an embed that is logged out in Safari
Calling
requestStorageAccess()on page load instead of inside a click handlerExpecting a partitioned cookie to carry a login made on your own site
Sending tokens with
postMessage(data, "*")or skipping theevent.origincheck
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Set cookies for an embedded checkout iframe: SameSite, Partitioned and fallbacks when third-party cookies are blockedMedium35 min
- Design an embeddable customer-support chat widget like Intercom: loader snippet, isolation, real-time messages, unread state and themingHard60 min
- Design a checkout SDK's internals like Razorpay Checkout: iframe isolation, postMessage protocol, UPI intent, 3-D Secure and callbacksHard55 min
- Design an embeddable poll widget used on thousands of sitesHard50 min
- Parse and serialize cookies safelyEasy25 min