Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
echo "Running tests with $sanitizer sanitizer..."
export RUSTFLAGS="-Z sanitizer=$sanitizer"
export RUSTDOCFLAGS="$RUSTFLAGS"
cargo +nightly test -Z build-std --target "$TARGET"
cargo +nightly test -Z build-std --target "$TARGET" --lib --tests
done

WASM:
Expand Down
29 changes: 20 additions & 9 deletions url/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,27 @@ impl<'a> Host<Cow<'a, str>> {
};

if input.find(is_invalid_host_char).is_some() {
Err(ParseError::InvalidDomainCharacter)
} else {
Ok(Host::Domain(
match utf8_percent_encode(&input, CONTROLS).into() {
Cow::Owned(v) => Cow::Owned(v),
// if we're borrowing, then we can return the original Cow
Cow::Borrowed(_) => input,
},
))
return Err(ParseError::InvalidDomainCharacter);
}

// Call utf8_percent_encode and use the result.
// Note: This returns Cow::Borrowed for single-item results (either from input
// or from the static encoding table), and Cow::Owned for multi-item results.
// We cannot distinguish between "borrowed from input" vs "borrowed from static table"
// based on the Cow variant alone.
Ok(Host::Domain(
match utf8_percent_encode(&input, CONTROLS).into() {
Cow::Owned(v) => Cow::Owned(v),
// If we're borrowing, we need to check if it's the same as the input
Cow::Borrowed(v) => {
if v == &*input {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it should just check input.len() == v.len()? Comparing the whole string seems a bit unfortunate.

input // No encoding happened, reuse original
} else {
Cow::Owned(v.to_owned()) // Borrowed from static table, need to own it
}
}
},
))
}

pub(crate) fn into_owned(self) -> Host<String> {
Expand Down
9 changes: 9 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1383,3 +1383,12 @@ fn serde_error_message() {
r#"relative URL without a base: "§invalid#+#*Ä" at line 1 column 25"#
);
}

#[test]
fn test_parse_url_with_single_byte_control_host() {
let input = "l://\x01:";

let url1 = Url::parse(input).unwrap();
let url2 = Url::parse(url1.as_str()).unwrap();
assert_eq!(url2, url1);
}
Loading