Skip to content
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

fix: validate path bytes are at least utf8 #756

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/byte_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ impl ByteStr {
// Invariant: assumed by the safety requirements of this function.
ByteStr { bytes }
}

pub(crate) fn from_utf8(bytes: Bytes) -> Result<ByteStr, std::str::Utf8Error> {
str::from_utf8(&bytes)?;
// Invariant: just checked is utf8
Ok(ByteStr { bytes })
}
}

impl ops::Deref for ByteStr {
Expand Down
36 changes: 30 additions & 6 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ impl PathAndQuery {
let mut query = NONE;
let mut fragment = None;

let mut is_maybe_not_utf8 = false;

// block for iterator borrow
{
let mut iter = src.as_ref().iter().enumerate();
Expand Down Expand Up @@ -50,7 +52,12 @@ impl PathAndQuery {
0x40..=0x5F |
0x61..=0x7A |
0x7C |
0x7E..=0xFF => {}
0x7E => {}

// potentially utf8, might not, should check
0x7F..=0xFF => {
is_maybe_not_utf8 = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea to avoid checking utf8 in most case, maybe we could do something similar in http-parse, will look at it (since utf8 is not common in path)

}

// These are code points that are supposed to be
// percent-encoded in the path but there are clients
Expand Down Expand Up @@ -82,7 +89,11 @@ impl PathAndQuery {
0x21 |
0x24..=0x3B |
0x3D |
0x3F..=0xFF => {}
0x3F..=0x7E => {}

0x7F..=0xFF => {
is_maybe_not_utf8 = true;
}

b'#' => {
fragment = Some(i);
Expand All @@ -99,10 +110,13 @@ impl PathAndQuery {
src.truncate(i);
}

Ok(PathAndQuery {
data: unsafe { ByteStr::from_utf8_unchecked(src) },
query,
})
let data = if is_maybe_not_utf8 {
ByteStr::from_utf8(src).map_err(|_| ErrorKind::InvalidUriChar)?
} else {
unsafe { ByteStr::from_utf8_unchecked(src) }
};

Ok(PathAndQuery { data, query })
}

/// Convert a `PathAndQuery` from a static string.
Expand Down Expand Up @@ -566,6 +580,16 @@ mod tests {
assert_eq!(Some("pizza=🍕"), pq("/test?pizza=🍕").query());
}

#[test]
fn rejects_invalid_utf8_in_path() {
PathAndQuery::try_from(&[b'/', 0xFF][..]).expect_err("reject invalid utf8");
}

#[test]
fn rejects_invalid_utf8_in_query() {
PathAndQuery::try_from(&[b'/', b'a', b'?', 0xFF][..]).expect_err("reject invalid utf8");
}

#[test]
fn json_is_fine() {
assert_eq!(
Expand Down