Skip to content

Commit

Permalink
When parsing Uri authority, check that the square brackets are in the…
Browse files Browse the repository at this point in the history
… correct order.

This avoids panic when attempting to parse odd strings like `]o[`.

Signed-off-by: Michael Rodler <[email protected]>
Reviewed-by: Daniele Ahmed <[email protected]>
  • Loading branch information
Michael Rodler authored and seanmonstar committed Jun 12, 2023
1 parent 746fce8 commit 4756547
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Authority {
start_bracket = true;
}
b']' => {
if end_bracket {
if (!start_bracket) || end_bracket {
return Err(ErrorKind::InvalidAuthority.into());
}
end_bracket = true;
Expand Down Expand Up @@ -658,14 +658,17 @@ mod tests {
let err = Authority::try_from([0xc0u8].as_ref()).unwrap_err();
assert_eq!(err.0, ErrorKind::InvalidUriChar);

let err = Authority::from_shared(Bytes::from_static([0xc0u8].as_ref()))
.unwrap_err();
let err = Authority::from_shared(Bytes::from_static([0xc0u8].as_ref())).unwrap_err();
assert_eq!(err.0, ErrorKind::InvalidUriChar);
}

#[test]
fn rejects_invalid_use_of_brackets() {
let err = Authority::parse_non_empty(b"[]@[").unwrap_err();
assert_eq!(err.0, ErrorKind::InvalidAuthority);

// reject tie-fighter
let err = Authority::parse_non_empty(b"]o[").unwrap_err();
assert_eq!(err.0, ErrorKind::InvalidAuthority);
}
}

0 comments on commit 4756547

Please sign in to comment.