Skip to content

Commit

Permalink
Backed out changeset ffa2f50d49ce (bug 1342258) for failing mochitest…
Browse files Browse the repository at this point in the history
… dom/tests/mochitest/dom-level0/test_setting_document.domain_idn.html. r=backout
  • Loading branch information
Archaeopteryx committed Mar 20, 2017
1 parent 399d6c0 commit 56361d2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 99 deletions.
134 changes: 41 additions & 93 deletions dom/html/nsHTMLDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,51 +906,64 @@ nsHTMLDocument::GetDomain(nsAString& aDomain)
return NS_OK;
}

already_AddRefed<nsIURI>
nsHTMLDocument::CreateInheritingURIForHost(const nsACString& aHostString)
NS_IMETHODIMP
nsHTMLDocument::SetDomain(const nsAString& aDomain)
{
if (aHostString.IsEmpty()) {
return nullptr;
ErrorResult rv;
SetDomain(aDomain, rv);
return rv.StealNSResult();
}

void
nsHTMLDocument::SetDomain(const nsAString& aDomain, ErrorResult& rv)
{
if (mSandboxFlags & SANDBOXED_DOMAIN) {
// We're sandboxed; disallow setting domain
rv.Throw(NS_ERROR_DOM_SECURITY_ERR);
return;
}

if (aDomain.IsEmpty()) {
rv.Throw(NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN);
return;
}

// Create new URI
nsCOMPtr<nsIURI> uri = GetDomainURI();

if (!uri) {
return nullptr;
rv.Throw(NS_ERROR_FAILURE);
return;
}

nsCOMPtr<nsIURI> newURI;
nsresult rv = uri->Clone(getter_AddRefs(newURI));
if (NS_FAILED(rv)) {
return nullptr;
nsresult rv2 = uri->Clone(getter_AddRefs(newURI));
if (NS_FAILED(rv2)) {
rv.Throw(rv2);
return;
}

rv = newURI->SetUserPass(EmptyCString());
if (NS_FAILED(rv)) {
return nullptr;
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.
rv = newURI->SetHostAndPort(aHostString);
if (NS_FAILED(rv)) {
return nullptr;
rv2 = newURI->SetHostAndPort(NS_ConvertUTF16toUTF8(aDomain));
if (NS_FAILED(rv2)) {
rv.Throw(rv2);
return;
}

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))) {
nsAutoCString current, domain;
if (NS_FAILED(uri->GetAsciiHost(current)))
current.Truncate();
}
if (NS_FAILED(newURI->GetAsciiHost(domain)))
domain.Truncate();

bool ok = current.Equals(domain);
if (current.Length() > domain.Length() &&
Expand All @@ -961,83 +974,18 @@ nsHTMLDocument::RegistrableDomainSuffixOfInternal(const nsAString& aNewDomain,
nsCOMPtr<nsIEffectiveTLDService> tldService =
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
if (!tldService) {
return nullptr;
rv.Throw(NS_ERROR_NOT_AVAILABLE);
return;
}

nsAutoCString currentBaseDomain;
ok = NS_SUCCEEDED(tldService->GetBaseDomain(aOrigHost, 0, 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
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)
{
ErrorResult rv;
SetDomain(aDomain, rv);
return rv.StealNSResult();
}

void
nsHTMLDocument::SetDomain(const nsAString& aDomain, ErrorResult& rv)
{
if (mSandboxFlags & SANDBOXED_DOMAIN) {
// We're sandboxed; disallow setting domain
rv.Throw(NS_ERROR_DOM_SECURITY_ERR);
return;
}

if (aDomain.IsEmpty()) {
rv.Throw(NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN);
return;
}

nsCOMPtr<nsIURI> uri = GetDomainURI();
if (!uri) {
rv.Throw(NS_ERROR_FAILURE);
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.

nsCOMPtr<nsIURI> newURI = RegistrableDomainSuffixOfInternal(aDomain, uri);
if (!newURI) {
// Error: illegal domain
rv.Throw(NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN);
return;
Expand Down
6 changes: 0 additions & 6 deletions dom/html/nsHTMLDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ class nsHTMLDocument : public nsDocument,
virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
override;
void SetDomain(const nsAString& aDomain, mozilla::ErrorResult& rv);
bool IsRegistrableDomainSuffixOfOrEqualTo(const nsAString& aHostSuffixString,
const nsACString& aOrigHost);
void GetCookie(nsAString& aCookie, mozilla::ErrorResult& rv);
void SetCookie(const nsAString& aCookie, mozilla::ErrorResult& rv);
void NamedGetter(JSContext* cx, const nsAString& aName, bool& aFound,
Expand Down Expand Up @@ -276,10 +274,6 @@ class nsHTMLDocument : public nsDocument,
static void DocumentWriteTerminationFunc(nsISupports *aRef);

already_AddRefed<nsIURI> GetDomainURI();
already_AddRefed<nsIURI> CreateInheritingURIForHost(const nsACString& aHostString);
already_AddRefed<nsIURI> RegistrableDomainSuffixOfInternal(const nsAString& aHostSuffixString,
nsIURI* aOrigHost);


nsresult WriteCommon(JSContext *cx, const nsAString& aText,
bool aNewlineTerminate);
Expand Down

0 comments on commit 56361d2

Please sign in to comment.