From 6cdfe9a18a5a08f64d348ebc7bbe56ab23c22232 Mon Sep 17 00:00:00 2001 From: Valentin Gosu Date: Tue, 30 Jan 2024 10:51:15 +0000 Subject: [PATCH] Bug 1843717 - Don't replace backslashes in the hash of the URL r=necko-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 --- netwerk/base/nsNetUtil.cpp | 10 +--------- netwerk/base/nsURLHelper.cpp | 5 +++++ netwerk/test/unit/test_URIs.js | 12 ++++++++++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/netwerk/base/nsNetUtil.cpp b/netwerk/base/nsNetUtil.cpp index 769afb2f68a9..4c03cf63c312 100644 --- a/netwerk/base/nsNetUtil.cpp +++ b/netwerk/base/nsNetUtil.cpp @@ -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); } diff --git a/netwerk/base/nsURLHelper.cpp b/netwerk/base/nsURLHelper.cpp index 41bf0aa97762..3850c6865a74 100644 --- a/netwerk/base/nsURLHelper.cpp +++ b/netwerk/base/nsURLHelper.cpp @@ -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); diff --git a/netwerk/test/unit/test_URIs.js b/netwerk/test/unit/test_URIs.js index 0f53b1fc4a1b..b9ec2b3f2d48 100644 --- a/netwerk/test/unit/test_URIs.js +++ b/netwerk/test/unit/test_URIs.js @@ -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#"); +}); -- 2.11.4.GIT