Skip to content

Commit 72fc823

Browse files
freeinternet865freeinternet865therealaleph
authored
domain_fronter: reject malformed chunked relay responses (#49)
read_chunked() previously accepted incomplete chunked bodies when the upstream stream closed before a full chunk payload and delimiter had been read. It also drained size + 2 bytes without verifying that the delimiter after each chunk was "\r\n". Return FronterError::BadResponse("connection closed mid-chunked response") when EOF occurs before a complete chunk and delimiter are available, and return FronterError::BadResponse("chunk missing trailing CRLF") when the chunk delimiter is invalid. The test suite now covers both truncated-chunk and missing-CRLF inputs. Co-authored-by: freeinternet865 <free@internet865.com> Co-authored-by: therealaleph <therealaleph@gmail.com>
1 parent 9a229a8 commit 72fc823

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/domain_fronter.rs‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,11 +1311,17 @@ where
13111311
let n = timeout(Duration::from_secs(20), stream.read(&mut tmp)).await
13121312
.map_err(|_| FronterError::Timeout)??;
13131313
if n == 0 {
1314-
out.extend_from_slice(&buf[..buf.len().min(size)]);
1315-
return Ok(out);
1314+
return Err(FronterError::BadResponse(
1315+
"connection closed mid-chunked response".into(),
1316+
));
13161317
}
13171318
buf.extend_from_slice(&tmp[..n]);
13181319
}
1320+
if &buf[size..size + 2] != b"\r\n" {
1321+
return Err(FronterError::BadResponse(
1322+
"chunk missing trailing CRLF".into(),
1323+
));
1324+
}
13191325
out.extend_from_slice(&buf[..size]);
13201326
buf.drain(..size + 2);
13211327
}
@@ -1821,4 +1827,40 @@ mod tests {
18211827
other => panic!("unexpected error: {}", other),
18221828
}
18231829
}
1830+
1831+
#[tokio::test(flavor = "current_thread")]
1832+
async fn chunked_reader_rejects_truncated_chunk_body() {
1833+
let (mut client, mut server) = duplex(1024);
1834+
client
1835+
.write_all(b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHel")
1836+
.await
1837+
.unwrap();
1838+
drop(client);
1839+
1840+
let err = read_http_response(&mut server).await.unwrap_err();
1841+
match err {
1842+
FronterError::BadResponse(msg) => {
1843+
assert!(msg.contains("mid-chunked"), "unexpected error: {}", msg);
1844+
}
1845+
other => panic!("unexpected error: {}", other),
1846+
}
1847+
}
1848+
1849+
#[tokio::test(flavor = "current_thread")]
1850+
async fn chunked_reader_rejects_missing_chunk_crlf() {
1851+
let (mut client, mut server) = duplex(1024);
1852+
client
1853+
.write_all(b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHelloXX")
1854+
.await
1855+
.unwrap();
1856+
drop(client);
1857+
1858+
let err = read_http_response(&mut server).await.unwrap_err();
1859+
match err {
1860+
FronterError::BadResponse(msg) => {
1861+
assert!(msg.contains("trailing CRLF"), "unexpected error: {}", msg);
1862+
}
1863+
other => panic!("unexpected error: {}", other),
1864+
}
1865+
}
18241866
}

0 commit comments

Comments
 (0)