Skip to content

Commit

Permalink
fix(headers): add length checks to ETag parsing
Browse files Browse the repository at this point in the history
Bug found using `cargo fuzz`.
  • Loading branch information
pyfisch committed Mar 5, 2017
1 parent d80aae5 commit 643fac1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/header/common/etag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ header! {
test_header!(test14,
vec![b"matched-\"dquotes\""],
None::<ETag>);
test_header!(test15,
vec![b"\""],
None::<ETag>);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/header/shared/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,17 @@ impl FromStr for EntityTag {
let length: usize = s.len();
let slice = &s[..];
// Early exits if it doesn't terminate in a DQUOTE.
if !slice.ends_with('"') {
if !slice.ends_with('"') || slice.len() < 2 {
return Err(::Error::Header);
}
// The etag is weak if its first char is not a DQUOTE.
if slice.starts_with('"') && check_slice_validity(&slice[1..length-1]) {
if slice.len() >= 2 && slice.starts_with('"')
&& check_slice_validity(&slice[1..length-1]) {
// No need to check if the last char is a DQUOTE,
// we already did that above.
return Ok(EntityTag { weak: false, tag: slice[1..length-1].to_owned() });
} else if slice.starts_with("W/\"") && check_slice_validity(&slice[3..length-1]) {
} else if slice.len() >= 4 && slice.starts_with("W/\"")
&& check_slice_validity(&slice[3..length-1]) {
return Ok(EntityTag { weak: true, tag: slice[3..length-1].to_owned() });
}
Err(::Error::Header)
Expand Down

0 comments on commit 643fac1

Please sign in to comment.