Handle malformed HTTP request with multiple Connection headers #2002
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.
This PR fixes a problem with Websockets when running Uvicorn on GitHub Codespaces
Detailed discussion:
https://github.com/orgs/community/discussions/57596
Summary
Please do not merge quickly, I am not sure if this is a Uvicorn bug or a problem with the GitHub codespaces reverse proxy sending a malformed HTTP requests.
According to RFC7230 there should not be duplicated Connection headers.
I am running Uvicorn in GitHub Codespaces. I am forwarding a port with visibility public and port protocol HTTP.
My first Websocket connection works, but after a few seconds the Websocket endpoint starts returning HTTP 404.
I captured traffic at the Uvicorn server with tcpdump and I figured out that when I receive a HTTP 404 the HTTP request has a duplicated
Connection
header.This is a problem and looking at the Uvicorn implementation it will make the Websockets connection fail to upgrade randomly depending on which one of the duplicated
Connection
header is read first.uvicorn/uvicorn/protocols/http/h11_impl.py
Lines 173 to 183 in 1cb58c7
I confirm that with the patch proposed in this PR fixes the problem completely because
connection
will be a list with the values['upgrade', 'keep-alive']
. However it could be that the request is just malformed and Uvicorn should not implement anything to cope with malformed HTTP requests.Please review.
Checklist