Architecture · Auth
Multiple Sessions in One Browser: Cookie Slot Architecture
An instructor has to log into student accounts in the same browser without closing their own session. Cookies are stored per origin, not per tab, which made that impossible. Here is the fix, the most dangerous class of bug it introduces, and an unexpected HTTP 500.
01. The problem
On our education platform a teacher logs in as a student to see the student's screen. But a browser stores cookies per origin, not per tab. As long as there is a single access_token cookie, a login in the second tab overwrites the first tab's cookie: the moment the teacher opens the student page, their own tab turns into the student too.
Opening a new tab doesn't change that; it was working as designed. So this wasn't a bug, it was the cookie model itself.
02. The fix: splitting the cookie into slots
Instead of one cookie, slots: access_token, access_token_2 … access_token_10. Each tab remembers which slot it uses in sessionStorage and adds an X-Session-Slot: N header to every request. The backend rewrites the request's cookie according to that header; everything else (JWT validation, CSRF) runs unchanged.
Tab (slot 2) Backend
------------ -------
sessionStorage.session_slot = 2
|
|-- every request: X-Session-Slot: 2 --> SessionSlotResolver
| | no header -> leave untouched
| | header -> REPLACE the access_token
| | cookie with access_token_2
| v
| JWT middleware (unchanged)
| v
| CSRF middleware
03. Why isn't the Authorization header enough?
The first idea was to put the slot's token into the Authorization header. It doesn't work: the JWT middleware's lookup order is cookie:access_token, header:Authorization. While a cookie exists, the header is never read. The only correct way is to rewrite the request's cookie before the middleware runs.
// Simplified example
func SessionSlotResolver() fiber.Handler {
return func(c *fiber.Ctx) error {
raw := c.Get("X-Session-Slot")
if raw == "" {
return c.Next() // feature off: nothing changes
}
slot, ok := parseSlot(raw) // 2..10
if !ok {
return c.Next()
}
token := c.Cookies(cookieNameForSlot(slot))
if token == "" {
// No slot cookie: do NOT fall back to slot 1. Delete it, otherwise privileges escalate
c.Request().Header.DelCookie("access_token")
return c.Next()
}
c.Request().Header.SetCookie("access_token", token)
return c.Next()
}
}
04. The most dangerous class of bug
If the slot cookie is missing (the tab hasn't logged in yet, or its session expired), access_token must be deleted from the request. Otherwise the JWT middleware falls back to slot 1's cookie, the teacher's, and the student tab is silently authorized as the teacher.
This privilege-escalation bug is the kind nobody notices: no error message, just a tab signed in as the wrong person. So I locked the one-line behavior with a dedicated test: "does not fall back to slot 1 when the slot cookie is missing". There are separate isolation tests at handler level too.
05. Backward compatibility and a kill switch
- •No header, nothing changes. If the resolver doesn't see the header it simply calls
Next(). Slot 1 never sends a header; existing users' flow goes through exactly the same lines. - •Can be disabled by configuration. A setting (
multiSession.disabled) switches the feature off without waiting for a deploy. - •The Ctrl+T scenario. If a teacher skips the menu button, opens a blank tab and logs in as a student directly, the login would overwrite slot 1's cookie and the teacher would lose their session. Before the login request we check whether someone else has a live session in slot 1; if so, the tab is silently moved to a free slot.
06. A surprise: HTTP 500 after the 6th slot
In testing, after the sixth session, login and token-refresh requests started returning HTTP 500. The cause wasn't in the code but in the size of the Cookie header: each slot carries its own JWT, so the header grows and exceeds the HTTP server's default read buffer (4 KB in Fiber). I raised the read buffer to 16 KB and also kept the header buffer at the reverse proxy wide (with 10 slots the Cookie header is about 5.2 KB).
Lesson: when you turn a feature into "N of them", the limit may show up not in your logic but in the infrastructure's default limits. I set the upper bound (10 slots) deliberately and measured the buffers against it.