Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RUSTSEC-2020-0031 #190

Merged
merged 5 commits into from Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/common.rs
Expand Up @@ -212,7 +212,7 @@ impl Display for Header {

/// Field of a header (eg. `Content-Type`, `Content-Length`, etc.)
///
/// Comparaison between two `HeaderField`s ignores case.
/// Comparison between two `HeaderField`s ignores case.
#[derive(Debug, Clone)]
pub struct HeaderField(AsciiString);

Expand All @@ -239,9 +239,13 @@ impl FromStr for HeaderField {
type Err = ();

fn from_str(s: &str) -> Result<HeaderField, ()> {
AsciiString::from_ascii(s.trim())
.map(HeaderField)
.map_err(|_| ())
if s.contains(char::is_whitespace) {
Err(())
} else {
AsciiString::from_ascii(s)
.map(HeaderField)
.map_err(|_| ())
}
}
}

Expand Down Expand Up @@ -461,4 +465,18 @@ mod test {
assert!(header.field.equiv(&"time"));
assert!(header.value.as_str() == "20: 34");
}

// This tests reslstance to RUSTSEC-2020-0031: "HTTP Request smuggling
// through malformed Transfer Encoding headers"
// (https://rustsec.org/advisories/RUSTSEC-2020-0031.html).
#[test]
fn test_strict_headers() {
assert!("Transfer-Encoding : chunked".parse::<Header>().is_err());
assert!(" Transfer-Encoding: chunked".parse::<Header>().is_err());
assert!("Transfer Encoding: chunked".parse::<Header>().is_err());
assert!(" Transfer\tEncoding : chunked".parse::<Header>().is_err());
assert!("Transfer-Encoding: chunked".parse::<Header>().is_ok());
assert!("Transfer-Encoding: chunked ".parse::<Header>().is_ok());
assert!("Transfer-Encoding: chunked ".parse::<Header>().is_ok());
}
}
12 changes: 12 additions & 0 deletions tests/input-tests.rs
Expand Up @@ -81,3 +81,15 @@ fn unsupported_expect_header() {
client.read_to_string(&mut content).unwrap();
assert!(&content[9..].starts_with("417")); // 417 status code
}

#[test]
fn invalid_header_name() {
let mut client = support::new_client_to_hello_world_server();

// note the space hidden in the Content-Length, which is invalid
(write!(client, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nContent-Type: text/plain; charset=utf8\r\nContent-Length : 5\r\n\r\nhello")).unwrap();

let mut content = String::new();
client.read_to_string(&mut content).unwrap();
assert!(&content[9..].starts_with("400 Bad Request")); // 400 status code
}