websocket: create AutoSession session only on accepted handshakes#152
Merged
Conversation
With Jaws.AutoSession enabled, any request carrying a nonempty Sec-WebSocket-Key registered a session and wrote its Set-Cookie before websocket.Accept validated the handshake, so a rejected handshake left an orphaned session behind and a session cookie on the error response. Defer session creation to the handshake commit point: an autoSessionWriter wrapper passed to websocket.Accept creates and attaches the session only when the 101 Switching Protocols status is written. On any rejection the hook never fires, leaving no state to roll back. The wrapper forwards Unwrap so Accept still finds the http.Hijacker, and WriteHeaderNow for gin's deferred header flush. Closes #139
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.
Summary
Jaws.AutoSessionenabled, any request carrying a nonemptySec-WebSocket-Keyregistered a session and wrote itsSet-Cookiebeforewebsocket.Acceptvalidated the handshake, so a malformed/rejected handshake left an orphaned session resident for the grace period and a session cookie on the error response.coder/websocket.Acceptwrites101 Switching Protocolsthrough thehttp.ResponseWriterit is given right before hijacking the connection, so a thinautoSessionWriterwrapper creates and attaches the session (and its cookie) insideWriteHeaderonly when the status is 101. On any rejection the hook never fires — no session, no cookie, nothing to roll back.UnwrapsoAcceptstill locates thehttp.Hijacker(http.ResponseControllerconvention), andWriteHeaderNowso gin's deferred header flush keeps working.AutoSessionfield doc to match the README: the session is created during a successful WebSocket upgrade.Tests
TestWS_AutoSessionFailedHandshakeDoesNotCreateSessionsends raw HTTP handshakes with defects onlywebsocket.Acceptcatches (missingConnection/Upgradeheaders per the issue repro; unsupportedSec-WebSocket-Version) and asserts the error status, noSet-Cookie,SessionCount() == 0, and a nil request session. Both variants fail against the previous behavior (cookie set, session registered) and pass with this change.TestAutoSessionWriter_ForwardsWriteHeaderNowcovers the gin-forwarding branch.gofmt -l,go vet ./...,staticcheck ./...,golangci-lint run,gosec -quiet ./...all clean;go test -race ./...passes with 100.0% statement coverage on the root package.Closes #139