From 962ab431753e0e41d45f70e76d54f5ae9df98ee0 Mon Sep 17 00:00:00 2001 From: Joe Innes Date: Tue, 28 Apr 2026 11:07:41 +0200 Subject: [PATCH] fix extension sign-in inside Firefox container tabs `browser.cookies.get` without a `storeId` reads from the background context's default store, so a `margin_session` cookie set inside a container tab was invisible to the extension and the popup stayed on the Sign In screen. Try the active tab's cookie store first, then the default, then iterate `getAllCookieStores()` for first-match. --- extension/src/utils/api.ts | 43 +++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/extension/src/utils/api.ts b/extension/src/utils/api.ts index da27c40..f555ccd 100644 --- a/extension/src/utils/api.ts +++ b/extension/src/utils/api.ts @@ -8,11 +8,44 @@ async function getApiUrl(): Promise { async function getSessionCookie(): Promise { try { const apiUrl = await getApiUrl(); - const cookie = await browser.cookies.get({ - url: apiUrl, - name: 'margin_session', - }); - return cookie?.value || null; + + const readFrom = async (storeId?: string) => { + const cookie = await browser.cookies.get({ + url: apiUrl, + name: 'margin_session', + ...(storeId ? { storeId } : {}), + }); + return cookie?.value || null; + }; + + let activeStoreId: string | undefined; + try { + const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true }); + activeStoreId = (activeTab as { cookieStoreId?: string } | undefined)?.cookieStoreId; + } catch { + // ignore: tabs.query can fail in some contexts + } + + if (activeStoreId) { + const value = await readFrom(activeStoreId); + if (value) return value; + } + + const defaultValue = await readFrom(); + if (defaultValue) return defaultValue; + + try { + const stores = await browser.cookies.getAllCookieStores(); + for (const store of stores) { + if (store.id === activeStoreId) continue; + const value = await readFrom(store.id); + if (value) return value; + } + } catch { + // ignore: getAllCookieStores may not be supported everywhere + } + + return null; } catch (error) { console.error('Get cookie error:', error); return null; -- 2.51.2