Embedded iframe widget lost its login: fixing it with the Storage Access API
Our support widget, embedded on customer sites in an iframe, showed users as logged out in Safari and Firefox even after they signed in on our domain. Storage partitioning hid our first-party cookie. A requestStorageAccess() flow behind a click fixed it.
We sell a ticketing product with an embeddable widget: customers add an iframe from widget.example.com to their own sites so their users can see and reply to support tickets. Users sign in once on app.example.com, and the widget used to pick up that session through a cookie on example.com.
After a wave of tickets ("the widget keeps asking me to log in"), we checked by browser. Chrome users were mostly fine. Safari users were always logged out inside the widget. Firefox users were logged out on most customer sites. The widget's session check returned the same thing every time:
GET https://widget.example.com/api/session 401 Unauthorized
(request sent without the session cookie)
Reproducing it
Sign in at
https://app.example.comin Safari.Open a customer page at
https://shop.example.netthat embeds the widget.The widget shows the sign-in screen. DevTools for the iframe shows no cookies sent.
What we ruled out
SameSite attributes. The cookie was
SameSite=None; Secure, which is required for cross-site iframes. Correct, but not sufficient.Cookie domain.
Domain=example.comcovered both subdomains, and the cookie appeared onwidget.example.comwhen we opened it as a top-level page.CSP or frame-ancestors. The widget loaded; only its cookies were missing.
Root cause
Safari blocks third-party cookies by default, and Firefox's Total Cookie Protection partitions third-party storage by the top-level site. Inside shop.example.net, our iframe is a third party, so it only sees an empty, partitioned cookie jar, not the cookie set when the user visited our app directly. Chrome still allowed unpartitioned third-party cookies for most users, which is why the problem looked browser-specific. The web platform's answer is the Storage Access API: the iframe asks for access to its unpartitioned cookies, with a user gesture, and the browser decides whether to grant it.
What is happening under the hood
Partitioning keys storage by the pair (top-level site, embedded site). That stops a tracker embedded on many sites from recognising the same user everywhere. It also breaks legitimate embeds that rely on a login made elsewhere: comment systems, support widgets, document viewers.
The Storage Access API gives embedded content a sanctioned way to request its first-party cookies:
document.hasStorageAccess()resolves to whether the frame already has access.document.requestStorageAccess()asks for it. It must be called from a user gesture inside the iframe, such as a click.Browsers apply their own conditions. Safari requires that the user has interacted with the embedded site as a first party (visited it top-level) recently, and shows a prompt. Firefox grants automatically in some cases and prompts in others. Grants are remembered for a period per pair of sites.
A sandboxed iframe needs
allow-storage-access-by-user-activationin itssandboxattribute, and Permissions Policy can control it with thestorage-accessfeature.
For data that only needs to live inside the embed on that one site, partitioned cookies (CHIPS, the Partitioned attribute) are the right tool instead. We covered those in an earlier post on SameSite and partitioned cookies. Our widget needed the user's real session, so CHIPS did not fit.
Environment
Component | Version / value |
|---|---|
Safari | 26.1 on macOS Tahoe and iOS 26.1 |
Firefox | 145, standard Enhanced Tracking Protection |
Chrome | 142, default settings |
Widget |
|
App |
|
Embedders | Customer sites on their own domains |
Minimal reproduction
<!-- on https://shop.example.net -->
<iframe src="https://widget.example.com/embed" title="Support" width="380" height="560"></iframe>
// inside the widget
const res = await fetch('/api/session', { credentials: 'include' });
console.log(res.status); // 401 in Safari and Firefox: partitioned or blocked cookies
Who is affected
Any iframe that depends on cookies set when the user visited the embedded site directly.
Safari and all iOS browsers by default, Firefox by default, Chrome users who block third-party cookies or use incognito.
Embeds inside sandboxed iframes without the storage access token.
The fix
We built a three-step flow inside the widget:
On load, check
hasStorageAccess(). If true, use the session normally.If false, show a Continue as signed-in user button. On click, call
requestStorageAccess().If the browser refuses because the user never visited us as a first party, open a top-level sign-in popup on our domain, then ask again.
async function ensureSession(): Promise<boolean> {
if (await hasSession()) return true;
if (!('hasStorageAccess' in document)) return false;
return (await document.hasStorageAccess()) && hasSession();
}
continueButton.addEventListener('click', async () => {
try {
await document.requestStorageAccess(); // inside the click: user gesture
if (await hasSession()) return showTickets();
} catch {
// Denied or no prior first-party interaction: go top-level once.
}
const popup = window.open('https://app.example.com/embed-login', 'login', 'width=480,height=640');
window.addEventListener('message', async (e) => {
if (e.origin !== 'https://app.example.com' || e.data !== 'signed-in') return;
popup?.close();
showRetryButton(); // next click calls requestStorageAccess() again, now with interaction
}, { once: true });
});
async function hasSession() {
const res = await fetch('/api/session', { credentials: 'include' });
return res.ok;
}
The popup visit counts as first-party interaction, so the second requestStorageAccess() call succeeds in Safari after the user approves the prompt. A retry button is needed because the message event is not a user gesture.
We also told customers who sandbox the iframe to add the token:
<iframe src="https://widget.example.com/embed" title="Support"
sandbox="allow-scripts allow-same-origin allow-popups allow-forms allow-storage-access-by-user-activation"></iframe>
Alternatives and trade-offs
Token handoff instead of cookies. The embedding site's backend requests a short-lived widget token from our API and passes it to the iframe with
postMessage. No third-party cookies at all, but every customer must integrate server-side.Partitioned cookies (CHIPS). Fine for state that belongs to the embed on one site, such as a draft reply. It cannot reuse the user's main session.
Top-level redirect flows. Send the whole page to our domain and back. Works everywhere but disrupts the customer's page.
How to test the fix
Safari, first visit ever: widget shows the button, popup sign-in, retry, then tickets appear.
Safari, user already signed in on
app.example.com: one click, one prompt, tickets appear.Firefox with standard protection: one click, access granted.
Chrome with third-party cookies blocked: same flow works.
Refresh after granting:
hasStorageAccess()is true without prompting.
test('widget asks for storage access when partitioned', async ({ page }) => {
await page.goto('https://shop.example.net/test-embed');
const frame = page.frameLocator('iframe[title="Support"]');
await expect(frame.getByRole('button', { name: 'Continue as signed-in user' })).toBeVisible();
});
Confirming in production
widget_session_ok{browser="Safari"} before: 4% after: 83%
widget_session_ok{browser="Firefox"} before: 31% after: 90%
widget_storage_access{result="denied"} ~6% of requests
Denials are users who dismiss the prompt. They can still sign in inside the popup flow.
Regression guard
Our end-to-end suite embeds the widget on a separate test domain and runs in WebKit and Firefox, so partitioning applies in CI.
Widget code may not read the session cookie directly; it must go through
ensureSession().We alert when the Safari session success rate drops below 70 percent.
Security implications
Storage access re-opens a cross-site channel on purpose, so treat it carefully. Only request it after a clear user action that explains why. Set frame-ancestors in the widget's CSP to the customers that are allowed to embed it, so a malicious site cannot frame it and trick users into granting access (clickjacking). Keep session cookies HttpOnly, Secure and SameSite=None only where the embed needs it, and validate postMessage origins strictly in both directions. Never put session tokens in the iframe URL.
Prevention checklist
Assume embedded iframes have partitioned or blocked cookies by default.
Use
hasStorageAccess()andrequestStorageAccess()behind a user gesture.Provide a top-level sign-in fallback for users without prior first-party interaction.
Use CHIPS for embed-local state and a token handoff where customers can integrate server-side.
Restrict embedding with
frame-ancestors.
References
MDN, Storage Access API: https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API
MDN, Document.requestStorageAccess(): https://developer.mozilla.org/en-US/docs/Web/API/Document/requestStorageAccess
WebKit blog, Introducing Storage Access API: https://webkit.org/blog/8124/introducing-storage-access-api/
MDN, State partitioning: https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/State_Partitioning
Chrome for Developers, Storage Access API: https://developer.chrome.com/docs/privacy-security/storage-access
Add a label
Embed this post
<iframe src="https://hibug.me/iframe-widget-lost-login-storage-access-api" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I build and run the platforms other engineers ship on: CI/CD pipelines, Kubernetes clusters, GPU inference services, LLM features in production, and the cloud bills that come with all of it. I have also spent years writing and operating automated trading systems, where a rounding bug or a missed websocket message is not a log line but a position you did not intend to hold. Each post here starts from a real failure: the alert, the exact error, what we ruled out, the root cause, and how we proved the fix. Same path every time: Investigate, Explain, Fix, Verify. No hype, no price predictions, just engineering.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.