Skip to content

Let a browser sign in and hold a session - #15

Merged
sahilkalgutkar merged 1 commit into
mainfrom
feature/browser-sessions
Aug 31, 2026
Merged

sahilkalgutkar merged 1 commit into
mainfrom
feature/browser-sessions

Conversation

@sahilkalgutkar

Copy link
Copy Markdown
Owner

A browser can now visit /login, authenticate, and keep a session — rather than getting a 401 with no way forward.

GET /login    → identity provider → back with a session cookie
GET /logout   → the session is destroyed server-side

The cookie holds an opaque id, nothing else

A signed identity in the cookie would mean a session cannot be revoked before it expires, because the server has nothing to delete. Revocation is most of what makes handing out sessions safe, so the state lives server-side. Verified: replaying a captured cookie after logout returns 401.

Every attribute closes a specific hole

Attribute What it stops
HttpOnly script reading the session — an XSS bug elsewhere can't steal it
Secure the session travelling over plaintext
SameSite=Lax cross-site form posts carrying the cookie
no Domain one compromised subdomain reading the session

There's a test asserting each. The CSRF cookie is the deliberate exception to HttpOnly — double-submit depends on same-origin script reading it.

Reads need the cookie; writes need a CSRF token

SameSite=Lax already stops a cross-site page sending the cookie on a POST. The token is the second lock on the same door, and worth having because SameSite is a browser behaviour — an old browser, or a proxy that strips the attribute, defeats it. A token the attacker cannot read is not defeated by any of that.

Verified against a running server:

POST /v1/models  (cookie, no token)  → 403  auth: CSRF check failed
POST /v1/models  (cookie + token)    → 201

The comparison is constant-time. Unlike the token digests elsewhere in this package, there is no hash in front of this value to scramble a near miss.

A session is never a weaker front door

The ID token is verified through the same path an API request takes, so a cookie cannot carry an identity a bearer token could not — a user in no mapped group gets 403 and no session. Two verification routines would be two places for the rules to drift, and the weaker one becomes the way in.

A bearer token beats a cookie when both are present: a client that sent a credential meant it, and an ambient session silently overriding it is how a script ends up running as whoever last used the browser.

Session ids are minted only after authentication, so fixation is impossible, and a session never outlives the token behind it — access withdrawn at the provider is not extended by a cookie.

Open redirect

An open redirect on a login endpoint is worth more than most: the victim authenticates for real at their real provider, then gets bounced somewhere the attacker controls. That is what a convincing phish looks like.

//evil.example is rejected explicitly — it's protocol-relative, so a browser reads it as another origin while a naive "starts with /" check reads it as a local path. That one character is the whole bug. Tested with https://, //, http:// and javascript: targets.

Smaller decisions

  • The PKCE verifier, state and nonce stay server-side, keyed by a short-lived cookie. No reason to hand the browser the secret proving the exchange belongs to this login.
  • -external-url must be configured, not derived from the request — Host is caller-controlled, and building the redirect URI from it lets somebody point the provider's callback wherever they like.
  • -insecure-cookies drops Secure for local HTTP and logs a warning saying exactly what it costs.
  • With no next, login renders a "signed in" page rather than redirecting to /. The first version redirected and landed on a 404, making a successful sign-in look like a failure.

Verified live

Against the real binary: 401 before login, the full flow landing on "Signed in as sahil@example.com", cookie-only reads working, whoami naming the person, the CSRF gate refusing then allowing, open-redirect attempts staying on the server's own host, logout followed by a refused replay, and the audit line naming the person.

Coverage

89.7% across internal/.

A browser can now visit /login, authenticate, and keep a session cookie, rather
than getting a 401 with no way forward.

The cookie carries an opaque random id and nothing else. A signed identity in
the cookie would mean a session cannot be revoked before it expires, because the
server would have nothing to delete, and revocation is most of what makes
handing out sessions safe. Logout destroys the session server-side; a replayed
cookie afterwards is refused.

Every cookie attribute is there for a reason and each has a test. HttpOnly keeps
script from reading the session, so an XSS bug elsewhere cannot steal it. Secure
keeps it off plaintext. SameSite=Lax stops cross-site form posts carrying it. No
Domain is set, since one would share the session with every subdomain and a
single compromised one could read it. The CSRF cookie is the deliberate
exception to HttpOnly, because the double-submit pattern depends on same-origin
script reading it.

Reads need only the cookie; state-changing methods also need the CSRF token
echoed in a header. SameSite already closes that door, and this is the second
lock: SameSite is a browser behaviour, and an old browser or a proxy that strips
the attribute defeats it, while a token the attacker cannot read is not defeated
by any of that. The comparison is constant-time, because unlike the token
digests elsewhere in this package there is no hash in front of it to scramble a
near miss.

A session is never a weaker front door. The ID token is verified through the
same path an API request takes, so a cookie cannot carry an identity a bearer
token could not, and a user in no mapped group gets no session at all. A bearer
token beats a cookie when both are present, since a client that sent a
credential meant it.

Session ids are minted only after authentication, which makes fixation
impossible, and a session never outlives the token behind it.

The redirect target is validated rather than trusted. An open redirect on a
login endpoint is worth more than most, because the victim authenticates for
real at their real provider and is then bounced somewhere the attacker
controls. A protocol-relative //host is rejected explicitly: a browser reads it
as another origin while a naive prefix check reads it as a local path.

The PKCE verifier, state and nonce stay server-side, keyed by a short-lived
cookie, rather than being handed to the browser.

-external-url must be configured rather than derived from the request, because
Host is caller-controlled and building the redirect URI from it would let
somebody point the provider's callback wherever they liked.

Coverage across internal/ is 89.7%.
@sahilkalgutkar
sahilkalgutkar merged commit d1ba6d8 into main Aug 31, 2026
2 checks passed
@sahilkalgutkar
sahilkalgutkar deleted the feature/browser-sessions branch August 31, 2026 23:08
@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.67857% with 75 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/api/session.go 60.92% 41 Missing and 18 partials ⚠️
internal/auth/session.go 90.07% 7 Missing and 7 partials ⚠️
internal/auth/auth.go 50.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

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