Skip to content

Commit f19ec42

Browse files
authored
Merge pull request #62 from http-rs/max-header-size
Fix unbounded header parsing
2 parents f930e7b + 61d28f4 commit f19ec42

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/client.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::str::FromStr;
1616

1717
use crate::chunked::ChunkedDecoder;
1818
use crate::date::fmt_http_date;
19-
use crate::MAX_HEADERS;
19+
use crate::{MAX_HEADERS, MAX_HEAD_LENGTH};
2020

2121
/// An HTTP encoder.
2222
#[doc(hidden)]
@@ -146,6 +146,12 @@ where
146146
// No more bytes are yielded from the stream.
147147
assert!(bytes_read != 0, "Empty response"); // TODO: ensure?
148148

149+
// Prevent CWE-400 DDOS with large HTTP Headers.
150+
ensure!(
151+
buf.len() < MAX_HEAD_LENGTH,
152+
"Head byte length should be less than 8kb"
153+
);
154+
149155
// We've hit the end delimiter of the stream.
150156
let idx = buf.len() - 1;
151157
if idx >= 3 && &buf[idx - 3..=idx] == b"\r\n\r\n" {

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@
9999
/// The maximum amount of headers parsed on the server.
100100
const MAX_HEADERS: usize = 128;
101101

102+
/// The maximum length of the head section we'll try to parse.
103+
/// See: https://nodejs.org/en/blog/vulnerability/november-2018-security-releases/#denial-of-service-with-large-http-headers-cve-2018-12121
104+
const MAX_HEAD_LENGTH: usize = 8 * 1024;
105+
102106
mod chunked;
103107
mod date;
104108
mod server;

src/server.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use http_types::{Body, Method, Request, Response};
1616

1717
use crate::chunked::ChunkedDecoder;
1818
use crate::date::fmt_http_date;
19-
use crate::MAX_HEADERS;
19+
use crate::{MAX_HEADERS, MAX_HEAD_LENGTH};
2020

2121
const CR: u8 = b'\r';
2222
const LF: u8 = b'\n';
@@ -353,6 +353,12 @@ where
353353
return Ok(None);
354354
}
355355

356+
// Prevent CWE-400 DDOS with large HTTP Headers.
357+
ensure!(
358+
buf.len() < MAX_HEAD_LENGTH,
359+
"Head byte length should be less than 8kb"
360+
);
361+
356362
// We've hit the end delimiter of the stream.
357363
let idx = buf.len() - 1;
358364
if idx >= 3 && &buf[idx - 3..=idx] == b"\r\n\r\n" {

0 commit comments

Comments
 (0)