Skip to content

Commit

Permalink
Bug 1843717 - Don't replace backslashes in the hash of the URL r=neck…
Browse files Browse the repository at this point in the history
…o-reviewers,kershaw

This bug seems to have existed since bug 249282
The probem was that on windows we wanted to normalize the path of the
file URLs. When doing that bug 249282 also started normalizing the
inputs of relative file URLs, such that it triggers this bug.

When normalizing the input, the normalization should have stopped at the
'#' character. This patch fixes net_NormalizeFileURL to address that.

Since file URLs are only ever parsed with nsStandardURL and this
implementation already normalizes file paths for all special schemes
(file, http, https, etc) calling net_NormalizeFileURL before creating
a file URL is no longer needed, so we can remove the call in
NS_NewURI entirely.

Differential Revision: https://phabricator.services.mozilla.com/D199721
  • Loading branch information
valenting committed Jan 30, 2024
1 parent 5266c6f commit 6cdfe9a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
10 changes: 1 addition & 9 deletions netwerk/base/nsNetUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1862,18 +1862,10 @@ nsresult NS_NewURI(nsIURI** aURI, const nsACString& aSpec,
}

if (scheme.EqualsLiteral("file")) {
nsAutoCString buf(aSpec);
#if defined(XP_WIN)
buf.Truncate();
if (!net_NormalizeFileURL(aSpec, buf)) {
buf = aSpec;
}
#endif

return NS_MutateURI(new nsStandardURL::Mutator())
.Apply(&nsIFileURLMutator::MarkFileURL)
.Apply(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_NO_AUTHORITY, -1, buf, aCharset,
nsIStandardURL::URLTYPE_NO_AUTHORITY, -1, aSpec, aCharset,
aBaseURI, nullptr)
.Finalize(aURI);
}
Expand Down
5 changes: 5 additions & 0 deletions netwerk/base/nsURLHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ bool net_NormalizeFileURL(const nsACString& aURL, nsCString& aResultBuf) {
aResultBuf += '/';
begin = s + 1;
}
if (*s == '#') {
// Don't normalize any backslashes following the hash.
s = endIter.get();
break;
}
}
if (writing && s > begin) aResultBuf.Append(begin, s - begin);

Expand Down
12 changes: 12 additions & 0 deletions netwerk/test/unit/test_URIs.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,15 @@ add_task(async function round_trip_invalid_ace_label() {
uri = Services.io.newURI("http://a.b.c.XN--pokxncvks");
}, /NS_ERROR_MALFORMED_URI/);
});

add_task(async function test_bug1843717() {
// Make sure file path normalization on windows
// doesn't affect the hash of the URL.
let base = Services.io.newURI("file:///abc\\def/");
let uri = Services.io.newURI("foo\\bar#x\\y", null, base);
Assert.equal(uri.spec, "file:///abc/def/foo/bar#x\\y");
uri = Services.io.newURI("foo\\bar#xy", null, base);
Assert.equal(uri.spec, "file:///abc/def/foo/bar#xy");
uri = Services.io.newURI("foo\\bar#", null, base);
Assert.equal(uri.spec, "file:///abc/def/foo/bar#");
});

0 comments on commit 6cdfe9a

Please sign in to comment.