moebius#131: URL parser - stop preserving empty passwords

https://github.com/MoonchildProductions/moebius/issues/131
This commit is contained in:
janekptacijarabaci 2018-04-15 08:09:17 +02:00 • committed by Roy Tam
commit de0621cc7e
4 changed files with 31 additions and 5 deletions

View file

@ -74,7 +74,7 @@ var data = [
}, },
{ {
wrong: 'user:@example.com:8080/this/is/a/test.html', wrong: 'user:@example.com:8080/this/is/a/test.html',
fixed: 'http://user:@example.com:8080/this/is/a/test.html', fixed: 'http://user@example.com:8080/this/is/a/test.html',
}, },
{ {
wrong: '//user:pass@example.com:8080/this/is/a/test.html', wrong: '//user:pass@example.com:8080/this/is/a/test.html',

View file

@ -781,11 +781,13 @@ nsStandardURL::BuildNormalizedSpec(const char *spec)
i = AppendSegmentToBuf(buf, i, spec, username, mUsername, i = AppendSegmentToBuf(buf, i, spec, username, mUsername,
&encUsername, useEncUsername, &diff); &encUsername, useEncUsername, &diff);
ShiftFromPassword(diff); ShiftFromPassword(diff);
if (password.mLen >= 0) { if (password.mLen > 0) {
buf[i++] = ':'; buf[i++] = ':';
i = AppendSegmentToBuf(buf, i, spec, password, mPassword, i = AppendSegmentToBuf(buf, i, spec, password, mPassword,
&encPassword, useEncPassword, &diff); &encPassword, useEncPassword, &diff);
ShiftFromHost(diff); ShiftFromHost(diff);
} else {
mPassword.mLen = -1;
} }
buf[i++] = '@'; buf[i++] = '@';
} }
@ -1621,7 +1623,7 @@ nsStandardURL::SetUserPass(const nsACString &input)
usernameLen), usernameLen),
esc_Username | esc_AlwaysCopy, esc_Username | esc_AlwaysCopy,
buf, ignoredOut); buf, ignoredOut);
if (passwordLen >= 0) { if (passwordLen > 0) {
buf.Append(':'); buf.Append(':');
passwordLen = encoder.EncodeSegmentCount(userpass.get(), passwordLen = encoder.EncodeSegmentCount(userpass.get(),
URLSegment(passwordPos, URLSegment(passwordPos,
@ -1629,6 +1631,8 @@ nsStandardURL::SetUserPass(const nsACString &input)
esc_Password | esc_Password |
esc_AlwaysCopy, buf, esc_AlwaysCopy, buf,
ignoredOut); ignoredOut);
} else {
passwordLen = -1;
} }
if (mUsername.mLen < 0) if (mUsername.mLen < 0)
buf.Append('@'); buf.Append('@');
@ -1659,8 +1663,10 @@ nsStandardURL::SetUserPass(const nsACString &input)
// update positions and lengths // update positions and lengths
mUsername.mLen = usernameLen; mUsername.mLen = usernameLen;
mPassword.mLen = passwordLen; mPassword.mLen = passwordLen;
if (passwordLen) if (passwordLen > 0) {
mPassword.mPos = mUsername.mPos + mUsername.mLen + 1; mPassword.mPos = mUsername.mPos + mUsername.mLen + 1;
}
return NS_OK; return NS_OK;
} }

View file

@ -109,7 +109,7 @@ var gTests = [
nsIURL: true, nsINestedURI: false }, nsIURL: true, nsINestedURI: false },
{ spec: "ftp://foo:@ftp.mozilla.org:100/pub/mozilla.org/README", { spec: "ftp://foo:@ftp.mozilla.org:100/pub/mozilla.org/README",
scheme: "ftp", scheme: "ftp",
prePath: "ftp://foo:@ftp.mozilla.org:100", prePath: "ftp://foo@ftp.mozilla.org:100",
port: 100, port: 100,
username: "foo", username: "foo",
password: "", password: "",

View file

@ -472,3 +472,23 @@ add_test(function test_invalidHostChars() {
// hostname separators, so there is no way to set them and fail. // hostname separators, so there is no way to set them and fail.
run_next_test(); run_next_test();
}); });
add_test(function test_emptyPassword() {
var url = stringToURL("http://a:@example.com");
do_check_eq(url.spec, "http://a@example.com/");
url.password = "pp";
do_check_eq(url.spec, "http://a:pp@example.com/");
url.password = "";
do_check_eq(url.spec, "http://a@example.com/");
url.userPass = "xxx:";
do_check_eq(url.spec, "http://xxx@example.com/");
url.password = "zzzz";
do_check_eq(url.spec, "http://xxx:zzzz@example.com/");
url.userPass = "xxxxx:yyyyyy";
do_check_eq(url.spec, "http://xxxxx:yyyyyy@example.com/");
url.userPass = "z:";
do_check_eq(url.spec, "http://z@example.com/");
url.password = "ppppppppppp";
do_check_eq(url.spec, "http://z:ppppppppppp@example.com/");
run_next_test();
});