Skip to content

Commit 9a229a8

Browse files
freeinternet865freeinternet865
andauthored
domain_fronter: reject truncated Content-Length relay responses (#48)
read_http_response() handled Content-Length bodies by breaking out of the read loop on EOF and returning whatever bytes had been collected so far. That allowed a truncated upstream relay response to be treated as a complete HTTP response. Return FronterError::BadResponse("connection closed before full response body") when the stream closes before the declared Content-Length has been read. The test suite now covers a response that declares Content-Length: 5 but only sends 3 body bytes. Co-authored-by: freeinternet865 <free@internet865.com>
1 parent 481bdbc commit 9a229a8

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎src/domain_fronter.rs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,9 @@ where
12511251
let n = timeout(Duration::from_secs(20), stream.read(&mut tmp[..want])).await
12521252
.map_err(|_| FronterError::Timeout)??;
12531253
if n == 0 {
1254-
break;
1254+
return Err(FronterError::BadResponse(
1255+
"connection closed before full response body".into(),
1256+
));
12551257
}
12561258
body.extend_from_slice(&tmp[..n]);
12571259
}
@@ -1801,4 +1803,22 @@ mod tests {
18011803
assert_eq!(status2, 200);
18021804
assert_eq!(body2, b"OK");
18031805
}
1806+
1807+
#[tokio::test(flavor = "current_thread")]
1808+
async fn content_length_reader_rejects_truncated_body() {
1809+
let (mut client, mut server) = duplex(1024);
1810+
client
1811+
.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHel")
1812+
.await
1813+
.unwrap();
1814+
drop(client);
1815+
1816+
let err = read_http_response(&mut server).await.unwrap_err();
1817+
match err {
1818+
FronterError::BadResponse(msg) => {
1819+
assert!(msg.contains("full response body"), "unexpected error: {}", msg);
1820+
}
1821+
other => panic!("unexpected error: {}", other),
1822+
}
1823+
}
18041824
}

0 commit comments

Comments
 (0)