Files
servo/components/url/tests/main.rs
Simon Wülker 37a1f93b91 url: Let origins of file:// URLs be potentially trustworthy (#43989)
The origin of a `file` URL is unspecified. Engines act like they're
opaque except in a few special cases - one of which is the "is
potentially trustworthy" algorithm. This change allows consumers of
`servo-url` to distinguish between regular opaque origins and file
origins. Then we use that to mark file origins as "potentially
trustworthy" which is what the spec wants.

For now we can get away without changes to the `url` crate (the one used
in the wider ecosystem, not just servo), but I'm unsure if that will be
the case in the future.

Testing: This change adds a test
Fixes: https://github.com/servo/servo/issues/42540

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
2026-04-07 18:29:30 +00:00

92 lines
2.7 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::path::Path;
use std::str::FromStr;
use servo_url::ServoUrl;
use url::Url;
#[test]
fn test_matches_about_blank_matches_simple_about_blank() {
let mut url = ServoUrl::from_str("about:blank").unwrap();
assert!(url.matches_about_blank());
// Cannot set password.
assert!(url.set_password(Some("Test")).is_err());
assert!(url.matches_about_blank());
// Cannot set username.
assert!(url.set_username("user1").is_err());
assert!(url.matches_about_blank());
// Note: cannot set host, `set_host` not available on `ServoUrl`.
assert!(url.host().is_none());
}
#[test]
fn test_matches_about_blank_doest_matches_fake_about_blank() {
let url = ServoUrl::from_str("about:blank2").unwrap();
assert!(!url.matches_about_blank());
}
#[test]
fn test_matches_about_blank_does_not_match() {
let mut url = ServoUrl::from_str("ftp://user1:secret1@example.com").unwrap();
assert!(!url.matches_about_blank());
url.set_password(Some("Test")).unwrap();
assert!(!url.matches_about_blank());
url.set_username("user1").unwrap();
assert!(!url.matches_about_blank());
}
#[test]
fn test_matches_about_blank_does_not_match_from_other_url() {
let mut url = Url::parse("https://example.com").unwrap();
// Cannot construct something passing off like about:blank from url.
assert!(url.set_scheme("about").is_err());
assert!(url.set_host(None).is_err());
assert!(url.set_password(Some("Test")).is_ok());
url.set_path("blank");
let servo_url = ServoUrl::from_url(url);
assert!(!servo_url.matches_about_blank());
}
#[test]
fn test_matches_about_blank_does_not_match_invariants_maintained_from_url() {
// Invariants of about:blank maintained at the url level as well.
let mut url = Url::parse("about:blank").unwrap();
// Cannot set password.
assert!(url.set_password(Some("Test")).is_err());
// Cannot set username.
assert!(url.set_username("user1").is_err());
// Cannot set host.
assert!(url.set_host(Some("rust-lang.org")).is_err());
let servo_url = ServoUrl::from_url(url.clone());
assert!(servo_url.matches_about_blank());
// Can set path, but match will fail.
url.set_path("test");
let servo_url = ServoUrl::from_url(url);
assert!(!servo_url.matches_about_blank());
}
#[test]
fn test_file_urls_are_potentially_trustworthy() {
let path = Path::new(&env!("CARGO_MANIFEST_PATH"))
.canonicalize()
.unwrap();
let url: ServoUrl = ServoUrl::from_file_path(path.clone()).unwrap();
assert!(url.origin().is_potentially_trustworthy())
}