-
Notifications
You must be signed in to change notification settings - Fork 344
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 mutating path of URL without authority (idempotency, empty path segments) #996
Open
theskim
wants to merge
3
commits into
servo:main
Choose a base branch
from
theskim:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+228
−11
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -239,7 +239,7 @@ | |||||
I::Item: AsRef<str>, | ||||||
{ | ||||||
let scheme_type = SchemeType::from(self.url.scheme()); | ||||||
let path_start = self.url.path_start as usize; | ||||||
let mut path_start = self.url.path_start as usize; | ||||||
self.url.mutate(|parser| { | ||||||
parser.context = parser::Context::PathSegmentSetter; | ||||||
for segment in segments { | ||||||
|
@@ -253,7 +253,44 @@ | |||||
{ | ||||||
parser.serialization.push('/'); | ||||||
} | ||||||
let mut has_host = true; // FIXME account for this? | ||||||
|
||||||
let mut path_empty = false; | ||||||
|
||||||
// Check ':' and then see if the next character is '/' | ||||||
let mut has_host = if let Some(index) = parser.serialization.find(":") { | ||||||
if parser.serialization.len() > index + 1 | ||||||
&& parser.serialization.as_bytes().get(index + 1) == Some(&b'/') | ||||||
{ | ||||||
let rest = &parser.serialization[(index + ":/".len())..]; | ||||||
let host_part = rest.split('/').next().unwrap_or(""); | ||||||
path_empty = rest.is_empty(); | ||||||
!host_part.is_empty() && !host_part.contains('@') | ||||||
} else { | ||||||
false | ||||||
} | ||||||
} else { | ||||||
false | ||||||
}; | ||||||
|
||||||
// For cases where normalization is applied across both the serialization and the path. | ||||||
// Append "/." immediately after the scheme (up to ":") | ||||||
// This is done if three conditions are met. | ||||||
// https://url.spec.whatwg.org/#url-serializing | ||||||
// 1. The host is null | ||||||
// 2. The url's path length is greater than 1 | ||||||
// 3. the first segment of the URL's path is an empty string | ||||||
if !has_host && segment.len() > 1 && path_empty { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think the entire If can be simplified to this? |
||||||
if let Some(index) = parser.serialization.find(":") { | ||||||
if parser.serialization.len() == index + 2 | ||||||
&& parser.serialization.as_bytes().get(index + 1) == Some(&b'/') | ||||||
{ | ||||||
// Append an extra '/' to ensure that "/./path" becomes "/.//path" | ||||||
parser.serialization.insert_str(index + ":".len(), "/./"); | ||||||
path_start += "/.".len(); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
parser.parse_path( | ||||||
scheme_type, | ||||||
&mut has_host, | ||||||
|
@@ -262,6 +299,7 @@ | |||||
); | ||||||
} | ||||||
}); | ||||||
self.url.path_start = path_start as u32; | ||||||
self | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1387,3 +1387,127 @@ fn serde_error_message() { | |
r#"relative URL without a base: "§invalid#+#*Ä" at line 1 column 25"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_fuzzing_uri_failures() { | ||
use url::quirks; | ||
let mut url = Url::parse("data:/.dummy.path").unwrap(); | ||
assert!(!url.cannot_be_a_base()); | ||
|
||
url.set_path(".dummy.path"); | ||
assert_eq!(url.as_str(), "data:/.dummy.path"); | ||
assert_eq!(url.path(), "/.dummy.path"); | ||
url.check_invariants().unwrap(); | ||
|
||
url.path_segments_mut() | ||
.expect("should have path segments") | ||
.push(".another.dummy.path"); | ||
assert_eq!(url.as_str(), "data:/.dummy.path/.another.dummy.path"); | ||
assert_eq!(url.path(), "/.dummy.path/.another.dummy.path"); | ||
url.check_invariants().unwrap(); | ||
|
||
url = Url::parse("web+demo:/").unwrap(); | ||
assert!(!url.cannot_be_a_base()); | ||
|
||
url.set_path("//.dummy.path"); | ||
assert_eq!(url.path(), "//.dummy.path"); | ||
|
||
let segments: Vec<_> = url | ||
.path_segments() | ||
.expect("should have path segments") | ||
.collect(); | ||
assert_eq!(segments, vec!["", ".dummy.path"]); | ||
assert_eq!(url.as_str(), "web+demo:/.//.dummy.path"); | ||
|
||
quirks::set_hostname(&mut url, ".dummy.host").unwrap(); | ||
assert_eq!(url.as_str(), "web+demo://.dummy.host//.dummy.path"); | ||
url.check_invariants().unwrap(); | ||
|
||
quirks::set_hostname(&mut url, "").unwrap(); | ||
assert_eq!(url.as_str(), "web+demo:////.dummy.path"); | ||
url.check_invariants().unwrap(); | ||
} | ||
|
||
#[test] | ||
fn test_can_be_a_base_with_set_path() { | ||
use url::quirks; | ||
let mut url = Url::parse("web+demo:/").unwrap(); | ||
assert!(!url.cannot_be_a_base()); | ||
|
||
url.set_path("//not-a-host"); | ||
assert_eq!(url.path(), "//not-a-host"); | ||
|
||
let segments: Vec<_> = url | ||
.path_segments() | ||
.expect("should have path segments") | ||
.collect(); | ||
|
||
assert_eq!(segments, vec!["", "not-a-host"]); | ||
|
||
url.set_query(Some("query")); | ||
url.set_fragment(Some("frag")); | ||
|
||
assert_eq!(url.as_str(), "web+demo:/.//not-a-host?query#frag"); | ||
quirks::set_hostname(&mut url, "test").unwrap(); | ||
assert_eq!(url.as_str(), "web+demo://test//not-a-host?query#frag"); | ||
url.check_invariants().unwrap(); | ||
quirks::set_hostname(&mut url, "").unwrap(); | ||
assert_eq!(url.as_str(), "web+demo:////not-a-host?query#frag"); | ||
url.check_invariants().unwrap(); | ||
} | ||
|
||
#[test] | ||
fn test_can_be_a_base_with_path_segments_mut() { | ||
let mut url = Url::parse("web+demo:/").unwrap(); | ||
assert!(!url.cannot_be_a_base()); | ||
|
||
url.path_segments_mut() | ||
.expect("should have path segments") | ||
.push("") | ||
.push("not-a-host"); | ||
|
||
url.set_query(Some("query")); | ||
url.set_fragment(Some("frag")); | ||
|
||
assert_eq!(url.as_str(), "web+demo:/.//not-a-host?query#frag"); | ||
assert_eq!(url.path(), "//not-a-host"); | ||
url.check_invariants().unwrap(); | ||
|
||
let segments: Vec<_> = url | ||
.path_segments() | ||
.expect("should have path segments") | ||
.collect(); | ||
assert_eq!(segments, vec!["", "not-a-host"]); | ||
} | ||
|
||
#[test] | ||
fn test_valid_indices_after_set_path() { | ||
// Testing everything | ||
let mut url = Url::parse("moz:/").unwrap(); | ||
assert!(!url.cannot_be_a_base()); | ||
|
||
url.set_path("/.//p"); | ||
url.set_host(Some("host")).unwrap(); | ||
url.set_query(Some("query")); | ||
url.set_fragment(Some("frag")); | ||
assert_eq!(url.as_str(), "moz://host//p?query#frag"); | ||
assert_eq!(url.host(), Some(Host::Domain("host"))); | ||
assert_eq!(url.path(), "//p"); | ||
assert_eq!(url.query(), Some("query")); | ||
assert_eq!(url.fragment(), Some("frag")); | ||
url.check_invariants().unwrap(); | ||
|
||
url = Url::parse("moz:/.//").unwrap(); | ||
assert!(!url.cannot_be_a_base()); | ||
|
||
url.set_path("p"); | ||
url.set_host(Some("host")).unwrap(); | ||
url.set_query(Some("query")); | ||
url.set_fragment(Some("frag")); | ||
assert_eq!(url.as_str(), "moz://host/p?query#frag"); | ||
assert_eq!(url.host(), Some(Host::Domain("host"))); | ||
assert_eq!(url.path(), "/p"); | ||
assert_eq!(url.query(), Some("query")); | ||
assert_eq!(url.fragment(), Some("frag")); | ||
url.check_invariants().unwrap(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test I suggested in my prev commit still fails: #[test]
fn test_set_path_again() {
// Testing everything
let mut url = Url::parse("moz:/").unwrap();
assert_eq!(url.as_str(), "moz:/");
assert!(!url.cannot_be_a_base());
url.set_path("//p");
assert_eq!(url.as_str(), "moz:/.//p");
url.set_path("//d");
assert_eq!(url.as_str(), "moz:/.//d");
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if this works for a URL we've already parsed.
For example, this test case fails:
I think the problem is that has_host is computed before we remove it again in line 1783