Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return an error rather than panicking when HeaderName is too long #433

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,4 @@ pub use self::name::{
/// Generally, 64kb for a header name is WAY too much than would ever be needed
/// in practice. Restricting it to this size enables using `u16` values to
/// represent offsets when dealing with header names.
const MAX_HEADER_NAME_LEN: usize = 1 << 16;
const MAX_HEADER_NAME_LEN: usize = (1 << 16) - 1;
39 changes: 26 additions & 13 deletions src/header/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,10 +1116,6 @@ fn parse_hdr<'a>(
($d:ident, $src:ident, 35) => { to_lower!($d, $src, 34); $d[34] = table[$src[34] as usize]; };
}

assert!(len < super::MAX_HEADER_NAME_LEN,
"header name too long -- max length is {}",
super::MAX_HEADER_NAME_LEN);

match len {
0 => Err(InvalidHeaderName::new()),
2 => {
Expand Down Expand Up @@ -1509,17 +1505,16 @@ fn parse_hdr<'a>(
validate(b, len)
}
}
_ => {
if len < 64 {
for i in 0..len {
b[i] = table[data[i] as usize];
}

validate(b, len)
} else {
Ok(HdrName::custom(data, false))
len if len < 64 => {
for i in 0..len {
b[i] = table[data[i] as usize];
}
validate(b, len)
}
len if len <= super::MAX_HEADER_NAME_LEN => {
Ok(HdrName::custom(data, false))
}
_ => Err(InvalidHeaderName::new()),
}
}

Expand Down Expand Up @@ -2156,6 +2151,24 @@ mod tests {
}
}

#[test]
fn test_invalid_name_lengths() {
assert!(
HeaderName::from_bytes(&[]).is_err(),
"zero-length header name is an error",
);
let mut long = vec![b'a'; super::super::MAX_HEADER_NAME_LEN];
assert!(
HeaderName::from_bytes(long.as_slice()).is_ok(),
"max header name length is ok",
);
long.push(b'a');
assert!(
HeaderName::from_bytes(long.as_slice()).is_err(),
"longer than max header name length is an error",
);
}

#[test]
fn test_from_hdr_name() {
use self::StandardHeader::Vary;
Expand Down