nvx/apertureAll write-ups
No. 07ENGINEERING · 3 min

My cookie kept losing a race with Chrome

NVX Session gives every tab its own logins. The hardest bug was a sign-in redirect that leaves before my cookie arrives, and the fix was to stop racing Chrome at all. Plus Chrome's two-minute exception to SameSite=Lax.

By Cyber security, Monash University

Work Google in one tab, personal Google in the next, same window, both signed in. That's NVX Session, a browser extension I built that gives every tab its own cookies.

Unit tests
760
Sign-in scenarios
26 / 26
The whole download
437 KB
Two ways to give each tab its own cookies

NVX Session never lets a session's cookies into Chrome's cookie jar. It writes the Cookie header on every request itself.

In Manifest V3 an extension can't touch a request mid-flight. You hand Chrome rules ahead of time (declarativeNetRequest) and Chrome applies them. That's fast and private, but the rule has to exist before the request leaves.

Sign-in redirects

SequenceWhat a sign-in server does to you
Sign-in server
Chrome
My extension
  1. 01302 + Set-Cookie + Location: next
  2. 02Follows the redirect in ~ms, no cookie
  3. 03Here's that cookie, by the way
  4. 04Installs a rule. Too late.
  1. 01Sign-in serverChrome302 + Set-Cookie + Location: next
  2. 02ChromeSign-in serverFollows the redirect in ~ms, no cookie
  3. 03ChromeMy extensionHere's that cookie, by the way
  4. 04My extensionInstalls a rule. Too late.

The site sees a stranger. You're signed out or stuck in a loop.

The worst case was a university Moodle behind Okta, where every hop sets a new cookie. It raced forever, and I spent all of 3 October on it.

3 October, the sign-in test bed5 events
  1. 12:43

    3 of 18 sign-in scenarios working

  2. 12:49

    12 of 18

  3. 12:59

    15 of 18

  4. 13:08

    18 of 18, but only 7 clean

    The rest needed a retry. Still racing.

  5. 19:11

    26 of 26, isolation checks 20 of 20

    Stopped racing

Holding the redirect

Racing vs holding

A rule strips Location from any page response that also sets a cookie. The page stays put while I install the cookie, then I send the tab on myself with location.replace(next), so Back doesn't hit an empty page.

Racing, best case: 18 of 18 scenarios18 scenarios
  • 39%Clean
  • 61%Needed a retry
Holding: 26 of 26 scenarios26 scenarios
  • 81%Clean on the first try
  • 19%Not clean on the first try
How each test scenario runs4 stages
  1. 01

    Fake sign-in chain

    OAuth, SAML, popups, hidden iframes, meta refresh, QR polling, changing cookies

  2. 02

    Plain Chrome first

    Proves the scenario works

  3. 03

    Two sessions at once

  4. 04

    Ask each session

    Who it's signed in as

The test bed is fake sign-in chains on .test hosts.

The test bed also caught me out. My retries were counting toward my own sign-in loop detector, which then decided Okta was broken and released it for everyone.

A cookie with no SameSite attribute defaults to Lax
The specChrome
DefaultLaxLax
Sent on a cross-site POSTYes, for the cookie's first two minutes
Lax-allowing-unsafe: Chrome's two-minute window
  1. 1Window closes120 sThe extension schedules a recompile for this second
  2. Lax cookie sent on cross-site POST0 s to 120 s
It's not in the spec.

Following the spec to the letter, a SAML login POST went out with no cookies and Moodle replied invalidsesskey. So the extension copies Chrome's two-minute window too. A compiled rule is a photo of an answer, so it recompiles for the exact second that answer expires.

Two more traps in MV3

The service worker and the gap between rules4 steps
  1. 01Chrome kills the extension's service worker after about 30 s idle, and everything in memory goes with it. Service worker: idle ~30 s, killed.
  2. 02q to w: Request. The worker usually wakes up because of a request. Service worker: awake.
  3. 03It clears the old rules, then installs the new ones a few awaits later. Session rules: old rules cleared.
  4. 04That same request leaves in the gap, with Chrome's own cookies. A request: wrong cookies.
Swapping the rules

Swap old rules for new in the same updateSessionRules call. One call, no gap.

Moving a tab between accounts3 stages
  1. 01

    Block the tab

    A rule that blocks every request from that tab

  2. 02

    Swap the rules

  3. 03

    Remove the block

Requests from that tab wait a few milliseconds instead of going out with another account's cookies.

Editions

EditionWhat it has
FreeOpen source (GPL-3.0), unlimited sessions, no account, Chromium 128+ and Opera GX
Pro (A$6.99/month or A$69.99/year)Adds what free is upfront about not doing: IndexedDB isolation, fail-closed mode, cache suppression, a separate fingerprint per session, encrypted sync of settings
LicenceOne licence covers all the NVX apps

Still unfixed

A 307 that carries a POST body. You can hold a redirect, but you can't resend a body Chrome already threw away.

No comments yet

Comments are read before they appear.