Skip to content

Commit

Permalink
Ensure authority and/or host fields are not empty
Browse files Browse the repository at this point in the history
We could have an authority or host that consists of an empty string
  • Loading branch information
Pi-Cla committed Mar 20, 2024
1 parent b44edeb commit 9c3c83c
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion h3/src/proto/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ impl Header {
//# Host header field.

//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//= type=TODO
//# If these fields are present, they MUST NOT be
//# empty.

Expand All @@ -87,6 +86,16 @@ impl Header {
//# :authority pseudo-header or Host header fields.
match (self.pseudo.authority, self.fields.get("host")) {
(None, None) => return Err(HeaderError::MissingAuthority),
(Some(a), _) if a.as_str().is_empty() => {
return Err(HeaderError::InvalidHeaderValue(
"authority field is empty".into(),
))
}
(_, Some(h)) if h.is_empty() => {
return Err(HeaderError::InvalidHeaderValue(
"host field is empty".into(),
))
}
(Some(a), None) => uri = uri.authority(a.as_str().as_bytes()),
(None, Some(h)) => uri = uri.authority(h.as_bytes()),
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
Expand Down Expand Up @@ -498,6 +507,42 @@ mod tests {
);
}

#[test]
fn request_has_empty_authority() {
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//= type=test
//# If these fields are present, they MUST NOT be
//# empty.

let headers = Header::try_from(vec![
(b":method", Method::GET.as_str()).into(),
(b":authority", b"").into(),
])
.unwrap();
assert_matches!(
headers.into_request_parts(),
Err(HeaderError::InvalidHeaderValue(_))
);
}

#[test]
fn request_has_empty_host() {
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//= type=test
//# If these fields are present, they MUST NOT be
//# empty.

let headers = Header::try_from(vec![
(b":method", Method::GET.as_str()).into(),
(b"host", b"").into(),
])
.unwrap();
assert_matches!(
headers.into_request_parts(),
Err(HeaderError::InvalidHeaderValue(_))
);
}

#[test]
fn request_has_authority() {
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
Expand Down

0 comments on commit 9c3c83c

Please sign in to comment.