bug 1125503 - when canonicalizing hostnames, check string length before calling Last() r=mmc

--HG--
extra : amend_source : 9d07347f76b4d6b2fd1ab77f7025043575c3b4f9
This commit is contained in:
David Keeler 2015-01-26 12:47:50 -08:00
parent a4a005ae1b
commit 3752aec566
3 changed files with 18 additions and 4 deletions

View File

@ -375,7 +375,8 @@ PublicKeyPinningService::CanonicalizeHostname(const char* hostname)
{
nsAutoCString canonicalizedHostname(hostname);
ToLowerCase(canonicalizedHostname);
while (canonicalizedHostname.Last() == '.') {
while (canonicalizedHostname.Length() > 0 &&
canonicalizedHostname.Last() == '.') {
canonicalizedHostname.Truncate(canonicalizedHostname.Length() - 1);
}
return canonicalizedHostname;

View File

@ -279,12 +279,14 @@ nsSiteSecurityService::GetHost(nsIURI* aURI, nsACString& aResult)
nsAutoCString host;
nsresult rv = innerURI->GetAsciiHost(host);
if (NS_FAILED(rv) || host.IsEmpty()) {
return NS_ERROR_UNEXPECTED;
if (NS_FAILED(rv)) {
return rv;
}
aResult.Assign(PublicKeyPinningService::CanonicalizeHostname(host.get()));
if (aResult.IsEmpty()) {
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}

View File

@ -42,4 +42,15 @@ function run_test() {
"example.com.", 0));
do_check_false(SSService.isSecureHost(Ci.nsISiteSecurityService.HEADER_HSTS,
"example.com..", 0));
// Somehow creating this malformed URI succeeds - we need to handle it
// gracefully.
uri = Services.io.newURI("https://../foo", null, null);
do_check_eq(uri.host, "..");
try {
SSService.isSecureURI(Ci.nsISiteSecurityService.HEADER_HSTS, uri, 0);
do_check_false(true); // this shouldn't run
} catch (e) {
do_check_eq(e.result, Cr.NS_ERROR_UNEXPECTED);
}
}