Bug 1942030 - nsStandarURL.setHost("") throws for URLTYPE_STANDARD r=necko-reviewers,kershaw

Differential Revision: https://phabricator.services.mozilla.com/D234487
This commit is contained in:
Valentin Gosu
2025-01-17 14:07:19 +00:00
parent 23fac99b8a
commit bafb4341c1
3 changed files with 40 additions and 8 deletions

View File

@@ -1903,9 +1903,9 @@ nsresult nsStandardURL::SetHost(const nsACString& input) {
NS_WARNING("cannot set host on no-auth url");
return NS_ERROR_UNEXPECTED;
}
if (flat.IsEmpty()) {
// Setting an empty hostname is not allowed for
// URLTYPE_STANDARD and URLTYPE_AUTHORITY.
if (mURLType == URLTYPE_AUTHORITY && flat.IsEmpty()) {
// Setting an empty hostname is not allowed for URLTYPE_AUTHORITY.
return NS_ERROR_UNEXPECTED;
}
@@ -1946,7 +1946,8 @@ nsresult nsStandardURL::SetHost(const nsACString& input) {
// NormalizeIDN always copies if the call was successful
len = hostBuf.Length();
if (!len) {
if (!len && (mURLType == URLTYPE_AUTHORITY || mPort != -1 ||
Userpass(true).Length() > 0)) {
return NS_ERROR_MALFORMED_URI;
}

View File

@@ -1149,5 +1149,16 @@ add_task(async function test_bug1939493() {
// Clearing the host should fail, as there are still user, port, pass in play.
Assert.throws(() => {
uri = uri.mutate().setHost("").finalize();
}, /NS_ERROR_UNEXPECTED/);
}, /NS_ERROR_MALFORMED_URI/);
uri = uri
.mutate()
.setUserPass("")
.setUsername("")
.setPassword("")
.setPort(-1)
.setHost("")
.finalize();
Assert.equal(uri.spec, "resource:///components/");
});

View File

@@ -1110,13 +1110,33 @@ add_task(async function test_emptyHostWithURLType() {
"Empty host is not allowed for URLTYPE_AUTHORITY"
);
url = makeURL("http://foo.com/bar/", Ci.nsIStandardURL.URLTYPE_STANDARD);
url = makeURL("http://user@foo.com/bar/", Ci.nsIStandardURL.URLTYPE_STANDARD);
Assert.throws(
() => url.mutate().setHost("").finalize().spec,
/NS_ERROR_UNEXPECTED/,
"Empty host is not allowed for URLTYPE_STANDARD"
/NS_ERROR_MALFORMED_URI/,
"Setting an empty host should throw if there is a username present"
);
url = makeURL(
"http://:password@foo.com/bar/",
Ci.nsIStandardURL.URLTYPE_STANDARD
);
Assert.throws(
() => url.mutate().setHost("").finalize().spec,
/NS_ERROR_MALFORMED_URI/,
"Setting an empty host should throw if there is a password present"
);
url = makeURL("http://foo.com:123/bar/", Ci.nsIStandardURL.URLTYPE_STANDARD);
Assert.throws(
() => url.mutate().setHost("").finalize().spec,
/NS_ERROR_MALFORMED_URI/,
"Setting an empty host should throw if there is a port present"
);
url = makeURL("http://foo.com/bar/", Ci.nsIStandardURL.URLTYPE_STANDARD);
Assert.equal(url.mutate().setHost("").finalize().spec, "http:///bar/");
url = makeURL("http://foo.com/bar/", Ci.nsIStandardURL.URLTYPE_NO_AUTHORITY);
equal(
url.spec,