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

Backport #190 for 0.6.x #194

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 20 additions & 1 deletion src/common.rs
Expand Up @@ -248,7 +248,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 @@ -469,4 +475,17 @@ mod test {
assert!(header.field.equiv(&"time"));
assert!(header.value.as_str() == "20: 34");
}

// This tests resistance 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
}