-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(security): handling of unsafe characters in outbound header names…
… and values
- Loading branch information
Showing
7 changed files
with
173 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use test_harness::test; | ||
use trillium_client::{Client, KnownHeaderName}; | ||
use trillium_testing::{connector, harness}; | ||
|
||
#[test(harness)] | ||
async fn bad_characters_in_header_value() { | ||
assert!(Client::new(connector(())) | ||
.get("http://example.com") | ||
.with_header( | ||
KnownHeaderName::Referer, | ||
"x\r\nConnection: keep-alive\r\n\r\nGET / HTTP/1.1\r\nHost: example.com\r\n\r\n" | ||
) | ||
.await | ||
.is_err()); | ||
} | ||
|
||
#[test(harness)] | ||
async fn bad_characters_in_header_name() { | ||
assert!(Client::new(connector(())) | ||
.get("http://example.com") | ||
.with_header("dnt: 1\r\nConnection", "keep-alive") | ||
.await | ||
.is_err()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use indoc::{formatdoc, indoc}; | ||
use pretty_assertions::assert_eq; | ||
use stopper::Stopper; | ||
use test_harness::test; | ||
use trillium_http::{Conn, KnownHeaderName, SERVER}; | ||
use trillium_testing::{harness, TestResult, TestTransport}; | ||
|
||
const TEST_DATE: &str = "Tue, 21 Nov 2023 21:27:21 GMT"; | ||
|
||
async fn handler(mut conn: Conn<TestTransport>) -> Conn<TestTransport> { | ||
conn.set_status(200); | ||
conn.set_response_body("response: 0123456789"); | ||
conn.response_headers_mut() | ||
.insert(KnownHeaderName::Date, TEST_DATE); | ||
conn.response_headers_mut().insert( | ||
KnownHeaderName::Connection, | ||
"close\r\nGET / HTTP/1.1\r\nHost: example.com\r\n\r\n", | ||
); | ||
conn.response_headers_mut().insert("Bad\r\nHeader", "true"); | ||
conn | ||
} | ||
|
||
#[test(harness)] | ||
async fn bad_headers() -> TestResult { | ||
let (client, server) = TestTransport::new(); | ||
|
||
trillium_testing::spawn(async move { | ||
Conn::map(server, Stopper::new(), handler).await.unwrap(); | ||
}); | ||
|
||
client.write_all(indoc! {" | ||
GET / HTTP/1.1\r | ||
Host: example.com\r | ||
\r | ||
"}); | ||
|
||
let expected_response = formatdoc! {" | ||
HTTP/1.1 200 OK\r | ||
Server: {SERVER}\r | ||
Date: {TEST_DATE}\r | ||
Content-Length: 20\r | ||
\r | ||
response: 0123456789\ | ||
"}; | ||
|
||
assert_eq!(client.read_available_string().await, expected_response); | ||
|
||
Ok(()) | ||
} |