From 45b121572c1fa6e53dd1dacd4b25ddd98a42d0d0 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Tue, 5 Apr 2016 08:04:10 -0700 Subject: [PATCH] Hard error on invalid websocket reply messages --- daphne/http_protocol.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 0c47d66..5e596e8 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -243,6 +243,15 @@ class HTTPFactory(http.HTTPFactory): if channel.startswith("http") and isinstance(self.reply_protocols[channel], WebRequest): self.reply_protocols[channel].serverResponse(message) elif channel.startswith("websocket") and isinstance(self.reply_protocols[channel], WebSocketProtocol): + # Ensure the message is a valid WebSocket one + unknown_message_keys = set(message.keys()) - {"bytes", "text", "close"} + if unknown_message_keys: + raise ValueError( + "Got invalid WebSocket reply message on %s - contains unknown keys %s" % ( + channel, + unknown_message_keys, + ) + ) if message.get("bytes", None): self.reply_protocols[channel].serverSend(message["bytes"], True) if message.get("text", None):