Repository navigation
Let a browser sign in and hold a session - #15
Merged
Merged
Conversation
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%.
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A browser can now visit
/login, authenticate, and keep a session — rather than getting a 401 with no way forward.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
HttpOnlySecureSameSite=LaxDomainThere'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=Laxalready 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:
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.exampleis 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 withhttps://,//,http://andjavascript:targets.Smaller decisions
-external-urlmust be configured, not derived from the request —Hostis caller-controlled, and building the redirect URI from it lets somebody point the provider's callback wherever they like.-insecure-cookiesdropsSecurefor local HTTP and logs a warning saying exactly what it costs.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,
whoaminaming 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/.