Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit d8fc808

Browse files
committed
Bug 711886 - Fail Websocket if server replies with non-matching subprotocol, r=mcmanus
1 parent 625d452 commit d8fc808

6 files changed

Lines changed: 42 additions & 6 deletions

File tree

dom/base/test/file_websocket_wsh.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def web_socket_do_extra_handshake(request):
2828
time.sleep(13)
2929
elif request.ws_protocol == "test-41b":
3030
request.sts = "max-age=100"
31+
elif request.ws_protocol == "test-49":
32+
# subprotocols are compared case-sensitively, so this should fail
33+
request.ws_protocol = "teST-49"
3134
else:
3235
pass
3336

dom/base/test/test_websocket5.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
test47, // Make sure onerror/onclose aren't called during close()
2222
test48, // see bug 1227136 - client calls close() from onopen() and waits
2323
// until WebSocketChannel::mSocketIn is nulled out on socket thread
24+
test49, // Test that we fail if subprotocol returned from server doesn't match
2425
];
2526

2627
function testWebSocket() {

dom/base/test/websocket_tests.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,3 +1242,29 @@ function test48() {
12421242
SpecialPowers.clearUserPref(pref_close);
12431243
});
12441244
}
1245+
1246+
function test49()
1247+
{
1248+
return new Promise(function(resolve, reject) {
1249+
var ws = CreateTestWS("ws://mochi.test:8888/tests/dom/base/test/file_websocket", "test-49");
1250+
var gotError = 0;
1251+
ok(ws.readyState == 0, "create bad readyState in test-49!");
1252+
1253+
ws.onopen = function()
1254+
{
1255+
ok(false, "Connection must fail in test-49")
1256+
}
1257+
1258+
ws.onerror = function(e)
1259+
{
1260+
gotError = 1
1261+
}
1262+
1263+
ws.onclose = function(e)
1264+
{
1265+
ok(gotError, "Should get error in test-49!");
1266+
resolve();
1267+
}
1268+
});
1269+
}
1270+

dom/workers/test/websocket_worker5.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var tests = [
88
test44, // Test sending/receving binary ArrayBuffer
99
test46, // Test that we don't dispatch incoming msgs once in CLOSING state
1010
test47, // Make sure onerror/onclose aren't called during close()
11+
test49, // Test that we fail if subprotocol returned from server doesn't match
1112
];
1213

1314
doTest();

netwerk/protocol/websocket/WebSocketChannel.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3853,9 +3853,10 @@ WebSocketChannel::OnStartRequest(nsIRequest *aRequest,
38533853
return NS_ERROR_ILLEGAL_VALUE;
38543854
}
38553855

3856-
// If we sent a sub protocol header, verify the response matches
3857-
// If it does not, set mProtocol to "" so the protocol attribute
3858-
// of the WebSocket JS object reflects that
3856+
// If we sent a sub protocol header, verify the response matches.
3857+
// If response contains protocol that was not in request, fail.
3858+
// If response contained no protocol header, set to "" so the protocol
3859+
// attribute of the WebSocket JS object reflects that
38593860
if (!mProtocol.IsEmpty()) {
38603861
nsAutoCString respProtocol;
38613862
rv = mHttpChannel->GetResponseHeader(
@@ -3865,7 +3866,7 @@ WebSocketChannel::OnStartRequest(nsIRequest *aRequest,
38653866
rv = NS_ERROR_ILLEGAL_VALUE;
38663867
val = mProtocol.BeginWriting();
38673868
while ((token = nsCRT::strtok(val, ", \t", &val))) {
3868-
if (PL_strcasecmp(token, respProtocol.get()) == 0) {
3869+
if (PL_strcmp(token, respProtocol.get()) == 0) {
38693870
rv = NS_OK;
38703871
break;
38713872
}
@@ -3877,9 +3878,11 @@ WebSocketChannel::OnStartRequest(nsIRequest *aRequest,
38773878
mProtocol = respProtocol;
38783879
} else {
38793880
LOG(("WebsocketChannel::OnStartRequest: "
3880-
"subprotocol [%s] not found - %s returned",
3881-
mProtocol.get(), respProtocol.get()));
3881+
"Server replied with non-matching subprotocol [%s]: aborting",
3882+
respProtocol.get()));
38823883
mProtocol.Truncate();
3884+
AbortSession(NS_ERROR_ILLEGAL_VALUE);
3885+
return NS_ERROR_ILLEGAL_VALUE;
38833886
}
38843887
} else {
38853888
LOG(("WebsocketChannel::OnStartRequest "

testing/web-platform/meta/websockets/constructor/011.html.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66

77
[011.html]
88
type: testharness
9+
[WebSockets: protocol mismatch]
10+
expected: FAIL

0 commit comments

Comments
 (0)