[WebSocket]The Sec-WebSocket-Accept MUST NOT appear more than once in an HTTP response
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 21 Mar 2012 16:46:41 +0000 (16:46 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 21 Mar 2012 16:46:41 +0000 (16:46 +0000)
https://bugs.webkit.org/show_bug.cgi?id=81655

Patch by Li Yin <li.yin@intel.com> on 2012-03-21
Reviewed by Adam Barth.

Source/WebCore:

Test: http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header.html

* Modules/websockets/WebSocketHandshake.cpp:
(WebCore::WebSocketHandshake::readHTTPHeaders):

LayoutTests:

* http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header-expected.txt: Added.
* http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header.html: Added.
* http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header_wsh.py: Added.
(web_socket_do_extra_handshake):
(web_socket_transfer_data):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@111554 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header.html [new file with mode: 0644]
LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header_wsh.py [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/Modules/websockets/WebSocketHandshake.cpp

index e801602f281dd342b22979f616aed6c265ca563b..eae734b76cf9d6fee16cd0768cb9a49bb7a9d89f 100644 (file)
@@ -1,3 +1,16 @@
+2012-03-21  Li Yin  <li.yin@intel.com>
+
+        [WebSocket]The Sec-WebSocket-Accept MUST NOT appear more than once in an HTTP response
+        https://bugs.webkit.org/show_bug.cgi?id=81655
+
+        Reviewed by Adam Barth.
+
+        * http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header-expected.txt: Added.
+        * http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header.html: Added.
+        * http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header_wsh.py: Added.
+        (web_socket_do_extra_handshake):
+        (web_socket_transfer_data):
+
 2012-03-21  Yury Semikhatsky  <yurys@chromium.org>
 
         Web Inspector: event listener section doesn't show all event listeners of the element ancestors
diff --git a/LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header-expected.txt b/LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header-expected.txt
new file mode 100644 (file)
index 0000000..f9b4bb4
--- /dev/null
@@ -0,0 +1,9 @@
+CONSOLE MESSAGE: The Sec-WebSocket-Accept header MUST NOT appear more than once in an HTTP response
+Test that WebSocket handshake fails if there are more one Sec-WebSocket-Accept header field in the response.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header.html b/LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header.html
new file mode 100644 (file)
index 0000000..08721ca
--- /dev/null
@@ -0,0 +1,51 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+<script src="../../../../js-test-resources/js-test-pre.js"></script>
+</head>
+<body>
+<div id="description"></div>
+<div id="console"></div>
+<script>
+description("Test that WebSocket handshake fails if there are more one Sec-WebSocket-Accept header field in the response.");
+
+window.jsTestIsAsync = true;
+if (window.layoutTestController)
+    layoutTestController.overridePreference("WebKitHixie76WebSocketProtocolEnabled", 0);
+
+function endTest()
+{
+    clearTimeout(timeoutID);
+    finishJSTest();
+}
+
+var url = "ws://localhost:8880/websocket/tests/hybi/handshake-fail-by-more-accept-header";
+var ws = new WebSocket(url);
+
+ws.onopen = function()
+{
+    testFailed("Unexpectedly Connected.");
+};
+
+ws.onmessage = function(messageEvent)
+{
+    testFailed("Unexpectedly Received: '" + messageEvent.data + "'");
+};
+
+ws.onclose = function()
+{
+    endTest();
+};
+
+function timeOutCallback()
+{
+    debug("Timed out in state: " + ws.readyState);
+    endTest();
+}
+
+var timeoutID = setTimeout(timeOutCallback, 3000);
+
+</script>
+<script src="../../../../js-test-resources/js-test-post.js"></script>
+</body>
+</html>
diff --git a/LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header_wsh.py b/LayoutTests/http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header_wsh.py
new file mode 100644 (file)
index 0000000..684459b
--- /dev/null
@@ -0,0 +1,18 @@
+from mod_pywebsocket import handshake
+from mod_pywebsocket.handshake.hybi import compute_accept
+
+
+def web_socket_do_extra_handshake(request):
+    msg = 'HTTP/1.1 101 Switching Protocols\r\n'
+    msg += 'Upgrade: websocket\r\n'
+    msg += 'Connection: Upgrade\r\n'
+    msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0]
+    msg += 'Sec-WebSocket-Accept: XXXXthisiswrongXXXX\r\n' 
+    msg += '\r\n'
+    request.connection.write(msg)
+    print msg
+    raise handshake.AbortedByUserException('Abort the connection') # Prevents pywebsocket from sending its own handshake message.
+
+
+def web_socket_transfer_data(request):
+    pass
index edb1f7b8abcdaec3c16952e74aa3897262a0df2f..ccba737be97c020c3a25cca6134c4f5ea566c577 100644 (file)
@@ -1,3 +1,15 @@
+2012-03-21  Li Yin  <li.yin@intel.com>
+
+        [WebSocket]The Sec-WebSocket-Accept MUST NOT appear more than once in an HTTP response
+        https://bugs.webkit.org/show_bug.cgi?id=81655
+
+        Reviewed by Adam Barth.
+
+        Test: http/tests/websocket/tests/hybi/handshake-fail-by-more-accept-header.html
+
+        * Modules/websockets/WebSocketHandshake.cpp:
+        (WebCore::WebSocketHandshake::readHTTPHeaders):
+
 2012-03-21  Alexei Filippov  <alexeif@chromium.org>
 
         Web Inspector: Speedup heap snapshot loading.
index 8cc509e59699490b5b0e459d1dd18f78f37d6b09..e221a9dc821e4680473d8ef7a9ec2780156bb06e 100644 (file)
@@ -575,6 +575,7 @@ const char* WebSocketHandshake::readHTTPHeaders(const char* start, const char* e
 
     Vector<char> name;
     Vector<char> value;
+    bool sawSecWebSocketAcceptHeaderField = false;
     for (const char* p = start; p < end; p++) {
         name.clear();
         value.clear();
@@ -644,6 +645,13 @@ const char* WebSocketHandshake::readHTTPHeaders(const char* start, const char* e
                 m_failureReason = m_extensionDispatcher.failureReason();
                 return 0;
             }
+        } else if (equalIgnoringCase("Sec-WebSocket-Accept", nameStr)) {
+            if (sawSecWebSocketAcceptHeaderField) {
+                m_failureReason = "The Sec-WebSocket-Accept header MUST NOT appear more than once in an HTTP response";
+                return 0;
+            }
+            m_response.addHeaderField(nameStr, valueStr);
+            sawSecWebSocketAcceptHeaderField = true;
         } else
             m_response.addHeaderField(nameStr, valueStr);
     }