Skip to content

Deny leading/trailing whitespace in HeaderValue ( fixes #245 ). #256

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
84 changes: 79 additions & 5 deletions src/header/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ pub struct HeaderValue {
is_sensitive: bool,
}

#[derive(Debug)]
enum InvalidHeaderValueKind {
InvalidByte,
LeadingWhitespace,
TrailingWhitespace
}

/// A possible error when converting a `HeaderValue` from a string or byte
/// slice.
#[derive(Debug)]
pub struct InvalidHeaderValue {
_priv: (),
kind: InvalidHeaderValueKind
}

/// A possible error when converting a `HeaderValue` from a string or byte
Expand Down Expand Up @@ -71,7 +78,16 @@ impl HeaderValue {
panic!("invalid header value");
}
}

if let Some(&b) = bytes.first() {
if is_whitespace(b) {
panic!("invalid header value");
}
}
if let Some(&b) = bytes.last() {
if is_whitespace(b) {
panic!("invalid header value");
}
}
HeaderValue {
inner: Bytes::from_static(bytes),
is_sensitive: false,
Expand Down Expand Up @@ -183,7 +199,21 @@ impl HeaderValue {
for &b in src.as_ref() {
if !is_valid(b) {
return Err(InvalidHeaderValue {
_priv: (),
kind: InvalidHeaderValueKind::InvalidByte
});
}
}
if let Some(&b) = src.as_ref().first() {
if is_whitespace(b) {
return Err(InvalidHeaderValue {
kind: InvalidHeaderValueKind::LeadingWhitespace
});
}
}
if let Some(&b) = src.as_ref().last() {
if is_whitespace(b) {
return Err(InvalidHeaderValue {
kind: InvalidHeaderValueKind::TrailingWhitespace
});
}
}
Expand Down Expand Up @@ -565,6 +595,10 @@ fn is_visible_ascii(b: u8) -> bool {
b >= 32 && b < 127 || b == b'\t'
}

fn is_whitespace(b: u8) -> bool {
b == b' ' || b == b'\t'
}

#[inline]
fn is_valid(b: u8) -> bool {
b >= 32 && b != 127 || b == b'\t'
Expand All @@ -578,7 +612,11 @@ impl fmt::Display for InvalidHeaderValue {

impl Error for InvalidHeaderValue {
fn description(&self) -> &str {
"failed to parse header value"
match self.kind {
InvalidHeaderValueKind::InvalidByte => "failed to parse header value (invalid character)",
InvalidHeaderValueKind::LeadingWhitespace => "failed to parse header value (leading whitespace)",
InvalidHeaderValueKind::TrailingWhitespace => "failed to parse header value (trailing whitespace)"
}
}
}

Expand Down Expand Up @@ -763,7 +801,7 @@ impl<'a> PartialOrd<HeaderValue> for &'a str {

#[test]
fn test_try_from() {
HeaderValue::try_from(vec![127]).unwrap_err();
assert_eq!(HeaderValue::try_from(vec![127]).unwrap_err().description(), "failed to parse header value (invalid character)");
}

#[test]
Expand All @@ -784,3 +822,39 @@ fn test_debug() {
sensitive.set_sensitive(true);
assert_eq!("Sensitive", format!("{:?}", sensitive));
}

#[test]
fn test_leading_whitespace() {
assert_eq!(HeaderValue::from_str(" A").unwrap_err().description(), "failed to parse header value (leading whitespace)");
assert_eq!(HeaderValue::from_str("\tA").unwrap_err().description(), "failed to parse header value (leading whitespace)");
}

#[test]
#[should_panic(expected = "invalid header value")]
fn test_leading_whitespace_static() {
HeaderValue::from_static(" A");
}

#[test]
#[should_panic(expected = "invalid header value")]
fn test_leading_tab_static() {
HeaderValue::from_static("\tA");
}

#[test]
fn test_trailing_whitespace() {
assert_eq!(HeaderValue::from_str("A ").unwrap_err().description(), "failed to parse header value (trailing whitespace)");
assert_eq!(HeaderValue::from_str("A\t").unwrap_err().description(), "failed to parse header value (trailing whitespace)");
}

#[test]
#[should_panic(expected = "invalid header value")]
fn test_trailing_whitespace_static() {
HeaderValue::from_static("A ");
}

#[test]
#[should_panic(expected = "invalid header value")]
fn test_trailing_tab_static() {
HeaderValue::from_static("A\t");
}