Skip to content

Commit 643fac1

Browse files
committed
fix(headers): add length checks to ETag parsing
Bug found using `cargo fuzz`.
1 parent d80aae5 commit 643fac1

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/header/common/etag.rs

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ header! {
8383
test_header!(test14,
8484
vec![b"matched-\"dquotes\""],
8585
None::<ETag>);
86+
test_header!(test15,
87+
vec![b"\""],
88+
None::<ETag>);
8689
}
8790
}
8891

src/header/shared/entity.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,17 @@ impl FromStr for EntityTag {
123123
let length: usize = s.len();
124124
let slice = &s[..];
125125
// Early exits if it doesn't terminate in a DQUOTE.
126-
if !slice.ends_with('"') {
126+
if !slice.ends_with('"') || slice.len() < 2 {
127127
return Err(::Error::Header);
128128
}
129129
// The etag is weak if its first char is not a DQUOTE.
130-
if slice.starts_with('"') && check_slice_validity(&slice[1..length-1]) {
130+
if slice.len() >= 2 && slice.starts_with('"')
131+
&& check_slice_validity(&slice[1..length-1]) {
131132
// No need to check if the last char is a DQUOTE,
132133
// we already did that above.
133134
return Ok(EntityTag { weak: false, tag: slice[1..length-1].to_owned() });
134-
} else if slice.starts_with("W/\"") && check_slice_validity(&slice[3..length-1]) {
135+
} else if slice.len() >= 4 && slice.starts_with("W/\"")
136+
&& check_slice_validity(&slice[3..length-1]) {
135137
return Ok(EntityTag { weak: true, tag: slice[3..length-1].to_owned() });
136138
}
137139
Err(::Error::Header)

0 commit comments

Comments
 (0)