Skip to content

Commit 1d7ec47

Browse files
committed
Merge pull request #191 from reem/max-header-lengths
(fix) Harden header parsing against memory exhaustion attacks.
2 parents 7f93184 + b1ab03f commit 1d7ec47

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/http.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ pub fn read_http_version<R: Reader>(stream: &mut R) -> HttpResult<HttpVersion> {
486486
}
487487
}
488488

489+
const MAX_HEADER_NAME_LENGTH: uint = 100;
490+
const MAX_HEADER_FIELD_LENGTH: uint = 1000;
491+
489492
/// The raw bytes when parsing a header line.
490493
///
491494
/// A String and Vec<u8>, divided by COLON (`:`). The String is guaranteed
@@ -525,7 +528,10 @@ pub fn read_header<R: Reader>(stream: &mut R) -> HttpResult<Option<RawHeaderLine
525528
}
526529
},
527530
b':' => break,
528-
b if is_token(b) => name.push(b as char),
531+
b if is_token(b) => {
532+
if name.len() > MAX_HEADER_NAME_LENGTH { return Err(HttpHeaderError); }
533+
name.push(b as char)
534+
},
529535
_nontoken => return Err(HttpHeaderError)
530536
};
531537
}
@@ -542,6 +548,7 @@ pub fn read_header<R: Reader>(stream: &mut R) -> HttpResult<Option<RawHeaderLine
542548
b' ' if ows => {},
543549
b => {
544550
ows = false;
551+
if value.len() > MAX_HEADER_FIELD_LENGTH { return Err(HttpHeaderError); }
545552
value.push(b)
546553
}
547554
};

0 commit comments

Comments
 (0)