Fix Recoverer upgrade detection for case-insensitive tokenized Connection header#1077
Open
andrewstellman wants to merge 2 commits intogo-chi:masterfrom
Open
Fix Recoverer upgrade detection for case-insensitive tokenized Connection header#1077andrewstellman wants to merge 2 commits intogo-chi:masterfrom
andrewstellman wants to merge 2 commits intogo-chi:masterfrom
Conversation
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
The
Recoverermiddleware checks whether a request is a WebSocket upgrade before writing a 500 status after a panic. It currently uses an exact string comparison:This misses two cases that are valid per HTTP specs:
Connection: upgrade(lowercase) — HTTP headers are case-insensitive (RFC 7230 §3.2), butHeader.Getreturns the value as-is and the comparison is case-sensitive.Connection: keep-alive, Upgrade— theConnectionheader can contain multiple comma-separated tokens (RFC 7230 §6.1), butHeader.Getreturns the full value and the comparison requires an exact match.In both cases, the Recoverer incorrectly writes a 500 status on a connection that is being upgraded, which can corrupt the WebSocket handshake.
Fix
Replace the exact string comparison with a helper that splits the header into comma-separated tokens and does a case-insensitive match against each one.
Tests
Four test cases in
TestRecovererUpgradeConnectionDetection:UpgradeUpgradeupgradekeep-alive, UpgradeFull test suite passes (
go test ./...).