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 for fetching blob URLs in Rust WebAssembly using reqwest #1797

Merged
merged 1 commit into from
Apr 10, 2023
Merged
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
30 changes: 30 additions & 0 deletions src/into_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ pub trait IntoUrlSealed {

impl IntoUrlSealed for Url {
fn into_url(self) -> crate::Result<Url> {
// With blob url the `self.has_host()` check is always false, so we
// remove the `blob:` scheme and check again if the url is valid.
#[cfg(target_arch = "wasm32")]
Copy link
Owner

Choose a reason for hiding this comment

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

Just so I understand this, the issue was that the next check, has_host(), was false, and so the scheme was considered bad? If that's so, it'd be good to include a code comment here explaining that, so we remember why this check is here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I just added an explanation.

if self.scheme() == "blob"
&& self.path().starts_with("http") // Check if the path starts with http or https to avoid validating a `blob:blob:...` url.
&& self.as_str()[5..].into_url().is_ok()
{
return Ok(self);
}

if self.has_host() {
Ok(self)
} else {
Expand Down Expand Up @@ -87,4 +97,24 @@ mod tests {
"builder error for url (file:///etc/hosts): URL scheme is not allowed"
);
}

#[test]
fn into_url_blob_scheme() {
let err = "blob:https://example.com".into_url().unwrap_err();
assert_eq!(
err.to_string(),
"builder error for url (blob:https://example.com): URL scheme is not allowed"
);
}

if_wasm! {
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn into_url_blob_scheme_wasm() {
let url = "blob:http://example.com".into_url().unwrap();

assert_eq!(url.as_str(), "blob:http://example.com");
}
}
}