Skip to content

Scope the session cookie to the instance - #5

Merged
Gabriel-Lewis merged 2 commits into
devfrom
fix/instance-scoped-session-cookie
Aug 30, 2026
Merged

Gabriel-Lewis merged 2 commits into
devfrom
fix/instance-scoped-session-cookie

Conversation

@Gabriel-Lewis

Copy link
Copy Markdown
Owner

Fixes logins breaking when several Explo instances run on one host — the usual unraid setup, one container per port.

Root cause

Cookies are scoped by host, not by port (RFC 6265 §8.5). Every instance wrote its session ID to a cookie named literally session for that host, so the browser kept a single entry and each instance overwrote the last.

Sessions live in each process's own in-memory store, so an ID minted by instance A means nothing to instance B. SessionManager.start failed to read it, minted a fresh unauthenticated session, and the user was bounced to the login screen. The same collision fails CSRF checks, since csrf_token belongs to whichever session was written last.

Loading the second instance was enough to break the first — no login required.

Fix

The cookie name is derived from WEB_ADDR (explo_session_7288), which is exactly what distinguishes instances on one host. UI_COOKIE_NAME overrides it for deployments that can't be told apart that way — several instances behind one reverse proxy on the same host and port.

An address with no readable port falls back to the shared explo_session rather than emitting a malformed cookie, and the suffix is sanitized because WEB_ADDR may name a service (:http) rather than a number.

Upgrade note

Existing sessions don't survive the rename: everyone logs in once more after upgrading. The stale session cookie is left in the jar until the browser expires it; nothing reads it any more.

No chart change needed — env/extraEnv already pass arbitrary variables through.

Testing

The regression test drives two instances through a single cookiejar, which is what a browser does, and asserts that visiting the second leaves the first logged in. Verified it fails against the old fixed name (first instance = 401, want 200 -- the second instance clobbered its session cookie) and passes with the fix.

Also covers every listen-address form (:7288, 0.0.0.0:7288, [::]:7288, :http, no port, empty, unsafe characters) and that the session actually round-trips under the derived name.

go build ./..., go vet ./src/... and go test ./src/... all pass.

🤖 Generated with Claude Code

Gabriel-Lewis and others added 2 commits August 29, 2026 16:38
Running several Explo instances on one host -- the usual unraid setup,
one container per port -- logged the user out of whichever instance they
had visited first. Loading the second instance was enough to break the
first; no login was needed.

Cookies are scoped by host, not by port (RFC 6265 section 8.5). Every
instance wrote its session ID to a cookie literally named "session" for
that host, so the browser kept one entry and each instance overwrote the
last. Sessions live in each process's own in-memory store, so an ID
minted by one instance means nothing to the next: start() failed to read
it, minted a fresh unauthenticated session, and the user was bounced to
the login screen. The same collision fails CSRF checks, since the token
belongs to whichever session was written last.

The cookie name is now derived from WEB_ADDR -- explo_session_7288 --
which is exactly what distinguishes instances on one host. UI_COOKIE_NAME
overrides it for deployments that cannot be told apart this way, such as
several instances behind one reverse proxy on the same host and port. An
address with no readable port falls back to the shared name rather than
emitting a malformed cookie, and the derived suffix is sanitized because
WEB_ADDR may name a service ("http") rather than a number.

Existing sessions do not survive the rename: everyone logs in once more
after upgrading, and the stale "session" cookie is left in the jar until
the browser expires it. Nothing reads it any more.

The regression test drives two instances through one cookie jar, which
is what a browser does, and asserts that visiting the second leaves the
first logged in.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
golangci-lint's errcheck rejected the bare deferred Close. The helper
reports the failure instead of discarding it, matching how the rest of
the suite treats errors it does not expect.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Gabriel-Lewis
Gabriel-Lewis merged commit c5c935e into dev Aug 30, 2026
4 checks passed
@Gabriel-Lewis
Gabriel-Lewis deleted the fix/instance-scoped-session-cookie branch August 30, 2026 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant