Skip to content

Commit

Permalink
Check wsproto response status code
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Dec 3, 2023
1 parent 435e6dc commit 80797c9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/protocols/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,8 @@ async def test_server_reject_connection_with_invalid_status(
http_protocol_cls,
unused_tcp_port: int,
):
# this test checks that even if there is an error in the response, the server
# can successfully send a 500 error back to the client
async def app(scope, receive, send):
assert scope["type"] == "websocket"
assert "websocket.http.response" in scope["extensions"]
Expand Down
4 changes: 4 additions & 0 deletions uvicorn/protocols/websockets/wsproto_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ async def send(self, message: "ASGISendEvent") -> None:

elif message_type == "websocket.http.response.start":
message = typing.cast("WebSocketResponseStartEvent", message)
# ensure status code is in the valid range
if not (100 <= message["status"] < 600):
msg = "Invalid HTTP status code '%d' in response."
raise RuntimeError(msg % message["status"])
self.logger.info(
'%s - "WebSocket %s" %d',
self.scope["client"],
Expand Down

0 comments on commit 80797c9

Please sign in to comment.