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 bug parsing url query with escaped & or = #4172

Merged
merged 2 commits into from
Mar 15, 2024
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 crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ pub struct WebInfo {
#[cfg(target_arch = "wasm32")]
#[derive(Clone, Debug)]
pub struct Location {
/// The full URL (`location.href`) without the hash.
/// The full URL (`location.href`) without the hash, percent-decoded.
///
/// Example: `"http://www.example.com:80/index.html?foo=bar"`.
pub url: String,
Expand Down
27 changes: 16 additions & 11 deletions crates/eframe/src/web/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,44 +99,42 @@ pub fn web_location() -> epi::Location {
.search()
.unwrap_or_default()
.strip_prefix('?')
.map(percent_decode)
.unwrap_or_default();

let query_map = parse_query_map(&query)
.iter()
.map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
.collect();
.unwrap_or_default()
.to_owned();

epi::Location {
// TODO(emilk): should we really percent-decode the url? 🤷‍♂️
url: percent_decode(&location.href().unwrap_or_default()),
protocol: percent_decode(&location.protocol().unwrap_or_default()),
host: percent_decode(&location.host().unwrap_or_default()),
hostname: percent_decode(&location.hostname().unwrap_or_default()),
port: percent_decode(&location.port().unwrap_or_default()),
hash,
query_map: parse_query_map(&query),
query,
query_map,
origin: percent_decode(&location.origin().unwrap_or_default()),
}
}

fn parse_query_map(query: &str) -> BTreeMap<&str, &str> {
/// query is percent-encoded
fn parse_query_map(query: &str) -> BTreeMap<String, String> {
query
.split('&')
.filter_map(|pair| {
if pair.is_empty() {
None
} else {
Some(if let Some((key, value)) = pair.split_once('=') {
(key, value)
(percent_decode(key), percent_decode(value))
} else {
(pair, "")
(percent_decode(pair), String::new())
})
}
})
.collect()
}

// TODO(emilk): this test is never acgtually run, because this whole module is wasm32 only 🤦‍♂️
#[test]
fn test_parse_query() {
assert_eq!(parse_query_map(""), BTreeMap::default());
Expand All @@ -157,4 +155,11 @@ fn test_parse_query() {
parse_query_map("foo&baz&&"),
BTreeMap::from_iter([("foo", ""), ("baz", "")])
);
assert_eq!(
parse_query_map("badger=data.rrd%3Fparam1%3Dfoo%26param2%3Dbar&mushroom=snake"),
BTreeMap::from_iter([
("badger", "data.rrd?param1=foo&param2=bar"),
("mushroom", "snake")
])
);
}
Loading