Skip to content

Commit 0714322

Browse files
Fix fragment boundaries in UrlString::base_str (#21920)
`UrlString::base_str` searches for `?` before `#`, so a `?` inside fragment data can leave the fragment attached to a distribution filename or archive-extension check. Split at the first query or fragment delimiter while preserving the original URL and the query parameters used for requests. Follow-up to #10253. Co-authored-by: Zanie Blue <[email protected]>
1 parent 8460ef5 commit 0714322

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎crates/uv-distribution-types/src/file.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ impl UrlString {
180180
/// Return the [`UrlString`] with any query parameters and fragments removed.
181181
pub fn base_str(&self) -> &str {
182182
self.as_ref()
183-
.split_once('?')
184-
.or_else(|| self.as_ref().split_once('#'))
183+
.split_once(['?', '#'])
185184
.map(|(path, _)| path)
186185
.unwrap_or(self.as_ref())
187186
}
@@ -288,6 +287,12 @@ mod tests {
288287
let url = UrlString("https://example.com/path#fragment".into());
289288
assert_eq!(url.base_str(), "https://example.com/path");
290289

290+
let url = UrlString("https://example.com/path#fragment?query".into());
291+
assert_eq!(url.base_str(), "https://example.com/path");
292+
293+
let url = UrlString("https://example.com/path#fragment/part?query".into());
294+
assert_eq!(url.base_str(), "https://example.com/path");
295+
291296
let url = UrlString("https://example.com/path".into());
292297
assert_eq!(url.base_str(), "https://example.com/path");
293298
}

‎crates/uv-distribution-types/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,9 @@ mod test {
18081808
"https://example.com/foo-0.1.0.tar.gz#fragment",
18091809
"https://example.com/foo-0.1.0.tar.gz?query",
18101810
"https://example.com/foo-0.1.0.tar.gz?query#fragment",
1811+
"https://example.com/foo-0.1.0.tar.gz#fragment?query",
1812+
"https://example.com/foo-0.1.0.tar.gz#fragment/3?query",
1813+
"https://example.com/foo%2D0.1.0.tar.gz#fragment/3?query",
18111814
"https://example.com/foo-0.1.0.tar.gz?query=1/2#fragment",
18121815
"https://example.com/foo-0.1.0.tar.gz?query=1/2#fragment/3",
18131816
"https://example.com/foo%2D0.1.0.tar.gz?query=1/2#fragment/3",

0 commit comments

Comments
 (0)