Permalink
Browse files

Merge pull request #2157 from pjknkda/master

websocket: fix missing websocket header causes an exception
  • Loading branch information...
2 parents ad08846 + 5d4a9ab commit 37081d792896a44d27c3423b73001937d972c806 @bdarnell bdarnell committed Sep 23, 2017
Showing with 15 additions and 0 deletions.
  1. +7 −0 tornado/test/websocket_test.py
  2. +8 −0 tornado/websocket.py
@@ -193,6 +193,13 @@ def test_http_request(self):
response = self.fetch('/echo')
self.assertEqual(response.code, 400)
+ def test_missing_websocket_key(self):
+ response = self.fetch('/echo',
+ headers={'Connection': 'Upgrade',
+ 'Upgrade': 'WebSocket',
+ 'Sec-WebSocket-Version': '13'})
+ self.assertEqual(response.code, 400)
+
def test_bad_websocket_version(self):
response = self.fetch('/echo',
headers={'Connection': 'Upgrade',
View
@@ -616,6 +616,14 @@ def __init__(self, handler, mask_outgoing=False,
def accept_connection(self):
try:
self._handle_websocket_headers()
+ except ValueError:
+ self.handler.set_status(400)
+ log_msg = "Missing/Invalid WebSocket headers"
+ self.handler.finish(log_msg)
+ gen_log.debug(log_msg)
+ return
+
+ try:
self._accept_connection()
except ValueError:
gen_log.debug("Malformed WebSocket request received",

0 comments on commit 37081d7

Please sign in to comment.