Skip to content

Commit

Permalink
fix websocket header validation, ensure it meets token requirements, add
Browse files Browse the repository at this point in the history
testing
  • Loading branch information
jmr0 committed Jan 24, 2016
1 parent 5b2d2c0 commit 3846cf5
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 37 deletions.
31 changes: 2 additions & 29 deletions components/script/dom/bindings/str.rs
Expand Up @@ -11,6 +11,7 @@ use std::ops;
use std::str;
use std::str::FromStr;
use util::mem::HeapSizeOf;
use util::str::is_token;

/// Encapsulates the IDL `ByteString` type.
#[derive(JSTraceable, Clone, Eq, PartialEq, HeapSizeOf)]
Expand Down Expand Up @@ -49,35 +50,7 @@ impl ByteString {
/// [RFC 2616](http://tools.ietf.org/html/rfc2616#page-17).
pub fn is_token(&self) -> bool {
let ByteString(ref vec) = *self;
if vec.is_empty() {
return false; // A token must be at least a single character
}
vec.iter().all(|&x| {
// http://tools.ietf.org/html/rfc2616#section-2.2
match x {
0...31 | 127 => false, // CTLs
40 |
41 |
60 |
62 |
64 |
44 |
59 |
58 |
92 |
34 |
47 |
91 |
93 |
63 |
61 |
123 |
125 |
32 => false, // separators
x if x > 127 => false, // non-CHARs
_ => true,
}
})
is_token(vec)
}

/// Returns whether `self` is a `field-value`, as defined by
Expand Down
11 changes: 4 additions & 7 deletions components/script/dom/websocket.rs
Expand Up @@ -34,11 +34,12 @@ use net_traits::unwrap_websocket_protocol;
use net_traits::{WebSocketCommunicate, WebSocketConnectData, WebSocketDomAction, WebSocketNetworkEvent};
use script_thread::ScriptThreadEventCategory::WebSocketEvent;
use script_thread::{CommonScriptMsg, Runnable, ScriptChan};
use std::ascii::AsciiExt;
use std::borrow::ToOwned;
use std::cell::Cell;
use std::ptr;
use std::thread;
use util::str::DOMString;
use util::str::{DOMString, is_token};
use websocket::client::request::Url;
use websocket::header::{Headers, WebSocketProtocol};
use websocket::ws::util::url::parse_url;
Expand Down Expand Up @@ -218,17 +219,13 @@ impl WebSocket {
for (i, protocol) in protocols.iter().enumerate() {
// https://tools.ietf.org/html/rfc6455#section-4.1
// Handshake requirements, step 10
if protocol.is_empty() {
return Err(Error::Syntax);
}

if protocols[i + 1..].iter().any(|p| p == protocol) {
if protocols[i + 1..].iter().any(|p| p.eq_ignore_ascii_case(protocol)) {
return Err(Error::Syntax);
}

// TODO: also check that no separator characters are used
// https://tools.ietf.org/html/rfc6455#section-4.1
if protocol.chars().any(|c| c < '\u{0021}' || c > '\u{007E}') {
if !is_token(protocol.as_bytes()) {
return Err(Error::Syntax);
}
}
Expand Down
34 changes: 34 additions & 0 deletions components/util/str.rs
Expand Up @@ -565,3 +565,37 @@ pub fn search_index(index: usize, indices: CharIndices) -> isize {
}
character_count
}

/// Returns whether `s` is a `token`, as defined by
/// [RFC 2616](http://tools.ietf.org/html/rfc2616#page-17).
pub fn is_token(s: &[u8]) -> bool {
if s.is_empty() {
return false; // A token must be at least a single character
}
s.iter().all(|&x| {
// http://tools.ietf.org/html/rfc2616#section-2.2
match x {
0...31 | 127 => false, // CTLs
40 |
41 |
60 |
62 |
64 |
44 |
59 |
58 |
92 |
34 |
47 |
91 |
93 |
63 |
61 |
123 |
125 |
32 => false, // separators
x if x > 127 => false, // non-CHARs
_ => true,
}
})
}
17 changes: 16 additions & 1 deletion tests/wpt/metadata/MANIFEST.json
Expand Up @@ -33212,7 +33212,22 @@
},
"local_changes": {
"deleted": [],
"items": {},
"items": {
"testharness": {
"websockets/Create-asciiSep-protocol-string.htm": [
{
"path": "websockets/Create-asciiSep-protocol-string.htm",
"url": "/websockets/Create-asciiSep-protocol-string.htm"
}
],
"websockets/Create-protocols-repeated-case-insensitive.htm": [
{
"path": "websockets/Create-protocols-repeated-case-insensitive.htm",
"url": "/websockets/Create-protocols-repeated-case-insensitive.htm"
}
]
}
},
"reftest_nodes": {}
},
"reftest_nodes": {
Expand Down
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>W3C WebSocket API - Create WebSocket - ascii protocol string with separator</title>
<script type="text/javascript" src="/resources/testharness.js"></script>
<script type="text/javascript" src="/resources/testharnessreport.js"></script>
<script type="text/javascript" src="websocket.js?pipe=sub"></script>
</head>
<body>
<div id="log"></div>
<script type="text/javascript">
if(window.WebSocket) {
test(function () {
var asciiWithSep = "/echo";
var wsocket;
assert_throws("SYNTAX_ERR", function () { wsocket = CreateWebSocketWithAsciiSep(asciiWithSep) });
}, "W3C WebSocket API - Create WebSocket - Pass a valid URL and a protocol string with an ascii separator character - SYNTAX_ERR is thrown")
}
</script>
</body>
</html>
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>W3C WebSocket API - Create WebSocket - repeated protocols with different case</title>
<script type="text/javascript" src="/resources/testharness.js"></script>
<script type="text/javascript" src="/resources/testharnessreport.js"></script>
<script type="text/javascript" src="websocket.js?pipe=sub"></script>
</head>
<body>
<div id="log"></div>
<script type="text/javascript">
test(function () {
var wsocket;
assert_throws("SYNTAX_ERR", function () { wsocket = CreateWebSocketWithRepeatedProtocolsCaseInsensitive() });
}, "W3C WebSocket API - Create WebSocket - Pass a valid URL and an array of protocol strings with repeated values but different case - SYNTAX_ERR is thrown")
</script>
</body>
</html>
13 changes: 13 additions & 0 deletions tests/wpt/web-platform-tests/websockets/websocket.js
Expand Up @@ -8,6 +8,7 @@ var __CONTROLPATH = "control";
var __PROTOCOL = "echo";
var __PROTOCOLS = ["echo", "chat"];
var __REPEATED__PROTOCOLS = ["echo", "echo"];
var __REPEATED__PROTOCOLS_CASE_INSENSITIVE = ["echo", "eCho"];
var __URL;
var __IS__WEBSOCKET;
var __PASS = "Pass";
Expand Down Expand Up @@ -47,6 +48,12 @@ function CreateWebSocketNonAsciiProtocol(nonAsciiProtocol) {
wsocket = new WebSocket(__URL, nonAsciiProtocol);
}

function CreateWebSocketWithAsciiSep(asciiWithSep) {
IsWebSocket();
__URL = "ws://" + __SERVER__NAME + ":" + __PORT + "/" + __PATH;
wsocket = new WebSocket(__URL, asciiWithSep);
}

function CreateWebSocketWithBlockedPort(blockedPort) {
IsWebSocket();
__URL = "wss://" + __SERVER__NAME + ":" + blockedPort + "/" + __PATH;
Expand All @@ -71,6 +78,12 @@ function CreateWebSocketWithRepeatedProtocols() {
wsocket = new WebSocket(__URL, __REPEATED__PROTOCOLS);
}

function CreateWebSocketWithRepeatedProtocolsCaseInsensitive() {
IsWebSocket();
__URL = "ws://" + __SERVER__NAME + ":" + __PORT + "/" + __PATH;
wsocket = new WebSocket(__URL, __REPEATED__PROTOCOLS_CASE_INSENSITIVE);
}

function CreateWebSocket(isSecure, isProtocol, isProtocols) {
IsWebSocket();
if (isSecure) {
Expand Down

0 comments on commit 3846cf5

Please sign in to comment.