Skip to content

Commit 0f26be1

Browse files
committed
headers: etag: reject invalid characters
rfc7232 specifies a valid set of ASCII characters that etag headers should match.
1 parent 89bcc45 commit 0f26be1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/conditional/etag.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ impl ETag {
8484
}
8585
};
8686

87+
if !s
88+
.bytes()
89+
.all(|c| c == 0x21 || (c >= 0x23 && c <= 0x7E) || c >= 0x80)
90+
{
91+
return Err(Error::from_str(
92+
StatusCode::BadRequest,
93+
"Invalid ETag header",
94+
));
95+
}
96+
8797
let etag = if weak { Self::Weak(s) } else { Self::Strong(s) };
8898
Ok(Some(etag))
8999
}
@@ -180,4 +190,11 @@ mod test {
180190
let err = ETag::from_headers(headers).unwrap_err();
181191
assert_eq!(format!("{}", err), msg);
182192
}
193+
194+
#[test]
195+
fn validate_characters() -> crate::Result<()> {
196+
assert_entry_err(r#"""hello""#, "Invalid ETag header");
197+
assert_entry_err("\"hello\x7F\"", "Invalid ETag header");
198+
Ok(())
199+
}
183200
}

0 commit comments

Comments
 (0)