Backed out changeset ffa2f50d49ce (bug 1342258) for failing mochitest dom/tests/mochitest/dom-level0/test_setting_document.domain_idn.html. r=backout
This commit is contained in:
@@ -906,104 +906,6 @@ nsHTMLDocument::GetDomain(nsAString& aDomain)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
already_AddRefed<nsIURI>
|
||||
nsHTMLDocument::CreateInheritingURIForHost(const nsACString& aHostString)
|
||||
{
|
||||
if (aHostString.IsEmpty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Create new URI
|
||||
nsCOMPtr<nsIURI> uri = GetDomainURI();
|
||||
if (!uri) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIURI> newURI;
|
||||
nsresult rv = uri->Clone(getter_AddRefs(newURI));
|
||||
if (NS_FAILED(rv)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
rv = newURI->SetUserPass(EmptyCString());
|
||||
if (NS_FAILED(rv)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// We use SetHostAndPort because we want to reset the port number if needed.
|
||||
rv = newURI->SetHostAndPort(aHostString);
|
||||
if (NS_FAILED(rv)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return newURI.forget();
|
||||
}
|
||||
|
||||
already_AddRefed<nsIURI>
|
||||
nsHTMLDocument::RegistrableDomainSuffixOfInternal(const nsAString& aNewDomain,
|
||||
nsIURI* aOrigHost)
|
||||
{
|
||||
// Check new domain - must be a superdomain of the current host
|
||||
// For example, a page from foo.bar.com may set domain to bar.com,
|
||||
// but not to ar.com, baz.com, or fi.foo.bar.com.
|
||||
nsAutoCString domain = NS_ConvertUTF16toUTF8(aNewDomain);
|
||||
nsAutoCString current;
|
||||
if (NS_FAILED(aOrigHost->GetAsciiHost(current))) {
|
||||
current.Truncate();
|
||||
}
|
||||
|
||||
bool ok = current.Equals(domain);
|
||||
if (current.Length() > domain.Length() &&
|
||||
StringEndsWith(current, domain) &&
|
||||
current.CharAt(current.Length() - domain.Length() - 1) == '.') {
|
||||
// We're golden if the new domain is the current page's base domain or a
|
||||
// subdomain of it.
|
||||
nsCOMPtr<nsIEffectiveTLDService> tldService =
|
||||
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
||||
if (!tldService) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
nsAutoCString currentBaseDomain;
|
||||
ok = NS_SUCCEEDED(tldService->GetBaseDomain(aOrigHost, 0, currentBaseDomain));
|
||||
NS_ASSERTION(StringEndsWith(domain, currentBaseDomain) ==
|
||||
(domain.Length() >= currentBaseDomain.Length()),
|
||||
"uh-oh! slight optimization wasn't valid somehow!");
|
||||
ok = ok && domain.Length() >= currentBaseDomain.Length();
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
// Error: illegal domain
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return CreateInheritingURIForHost(domain);
|
||||
}
|
||||
|
||||
bool
|
||||
nsHTMLDocument::IsRegistrableDomainSuffixOfOrEqualTo(const nsAString& aHostSuffixString,
|
||||
const nsACString& aOrigHost)
|
||||
{
|
||||
// https://html.spec.whatwg.org/multipage/browsers.html#is-a-registrable-domain-suffix-of-or-is-equal-to
|
||||
if (aHostSuffixString.IsEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIURI> origURI = CreateInheritingURIForHost(aOrigHost);
|
||||
if (!origURI) {
|
||||
// Error: failed to parse input domain
|
||||
return false;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIURI> newURI = RegistrableDomainSuffixOfInternal(aHostSuffixString, origURI);
|
||||
if (!newURI) {
|
||||
// Error: illegal domain
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsHTMLDocument::SetDomain(const nsAString& aDomain)
|
||||
{
|
||||
@@ -1026,18 +928,64 @@ nsHTMLDocument::SetDomain(const nsAString& aDomain, ErrorResult& rv)
|
||||
return;
|
||||
}
|
||||
|
||||
// Create new URI
|
||||
nsCOMPtr<nsIURI> uri = GetDomainURI();
|
||||
|
||||
if (!uri) {
|
||||
rv.Throw(NS_ERROR_FAILURE);
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIURI> newURI;
|
||||
nsresult rv2 = uri->Clone(getter_AddRefs(newURI));
|
||||
if (NS_FAILED(rv2)) {
|
||||
rv.Throw(rv2);
|
||||
return;
|
||||
}
|
||||
|
||||
rv2 = newURI->SetUserPass(EmptyCString());
|
||||
if (NS_FAILED(rv2)) {
|
||||
rv.Throw(rv2);
|
||||
return;
|
||||
}
|
||||
|
||||
// We use SetHostAndPort because we want to reset the port number if needed.
|
||||
rv2 = newURI->SetHostAndPort(NS_ConvertUTF16toUTF8(aDomain));
|
||||
if (NS_FAILED(rv2)) {
|
||||
rv.Throw(rv2);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check new domain - must be a superdomain of the current host
|
||||
// For example, a page from foo.bar.com may set domain to bar.com,
|
||||
// but not to ar.com, baz.com, or fi.foo.bar.com.
|
||||
nsAutoCString current, domain;
|
||||
if (NS_FAILED(uri->GetAsciiHost(current)))
|
||||
current.Truncate();
|
||||
if (NS_FAILED(newURI->GetAsciiHost(domain)))
|
||||
domain.Truncate();
|
||||
|
||||
nsCOMPtr<nsIURI> newURI = RegistrableDomainSuffixOfInternal(aDomain, uri);
|
||||
if (!newURI) {
|
||||
bool ok = current.Equals(domain);
|
||||
if (current.Length() > domain.Length() &&
|
||||
StringEndsWith(current, domain) &&
|
||||
current.CharAt(current.Length() - domain.Length() - 1) == '.') {
|
||||
// We're golden if the new domain is the current page's base domain or a
|
||||
// subdomain of it.
|
||||
nsCOMPtr<nsIEffectiveTLDService> tldService =
|
||||
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
||||
if (!tldService) {
|
||||
rv.Throw(NS_ERROR_NOT_AVAILABLE);
|
||||
return;
|
||||
}
|
||||
|
||||
nsAutoCString currentBaseDomain;
|
||||
ok = NS_SUCCEEDED(tldService->GetBaseDomain(uri, 0, currentBaseDomain));
|
||||
NS_ASSERTION(StringEndsWith(domain, currentBaseDomain) ==
|
||||
(domain.Length() >= currentBaseDomain.Length()),
|
||||
"uh-oh! slight optimization wasn't valid somehow!");
|
||||
ok = ok && domain.Length() >= currentBaseDomain.Length();
|
||||
}
|
||||
if (!ok) {
|
||||
// Error: illegal domain
|
||||
rv.Throw(NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user