From 5f77b1812de2705c35519fb382646660387ec422 Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Tue, 1 Oct 2019 04:23:16 +0000 Subject: [PATCH] Bug 1342258 - Refactor SetDomain to IsRegistrableDomainSuffixOfOrEqualTo r=smaug This commit refactors the SetDomain method in a Document to call a new function IsRegistrableDomainSuffixOfOrEqualTo(), defined in HTML [1]. This commit tries not to rename anything except input variables, so as to remain as clear as possible. It likely should have various variables renamed, but given the author's unfamiliarity with this module, review seems a good time to do that. It's also duplicating comments a little bit; let me know which one(s) you'd like to keep! Note: Commentary on the HTML change is available in the PR [2], and the rationale for this behavior in Web Auentication, where this algorithm will be used, is also recorded [3]. Updates: Refactored two new protected methods to avoid code duplication. [1] https://html.spec.whatwg.org/multipage/browsers.html#is-a-registrable-domain-suffix-of-or-is-equal-to [2] https://github.com/whatwg/html/pull/2365 [3] https://github.com/w3ctag/spec-reviews/issues/97#issuecomment-175766580 MozReview-Commit-ID: 4Dr8yOMdhez UltraBlame original commit: ffa2f50d49ce0356bbf02ea0940060283dcc007c --- dom/html/nsHTMLDocument.cpp | 392 ++++++++++++++++++++++++------------ dom/html/nsHTMLDocument.h | 40 ++++ 2 files changed, 307 insertions(+), 125 deletions(-) diff --git a/dom/html/nsHTMLDocument.cpp b/dom/html/nsHTMLDocument.cpp index e01ebe76e4b12..7cb4482ac23b7 100644 --- a/dom/html/nsHTMLDocument.cpp +++ b/dom/html/nsHTMLDocument.cpp @@ -4037,84 +4037,32 @@ return NS_OK ; } -NS_IMETHODIMP -nsHTMLDocument -: -: -SetDomain -( -const -nsAString -& -aDomain -) -{ -ErrorResult -rv -; -SetDomain -( -aDomain -rv -) -; -return -rv -. -StealNSResult -( -) -; -} -void +already_AddRefed +< +nsIURI +> nsHTMLDocument : : -SetDomain +CreateInheritingURIForHost ( const -nsAString -& -aDomain -ErrorResult -& -rv -) -{ -if -( -mSandboxFlags +nsACString & -SANDBOXED_DOMAIN +aHostString ) { -rv -. -Throw -( -NS_ERROR_DOM_SECURITY_ERR -) -; -return -; -} if ( -aDomain +aHostString . IsEmpty ( ) ) { -rv -. -Throw -( -NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN -) -; return +nullptr ; } nsCOMPtr @@ -4133,14 +4081,8 @@ if uri ) { -rv -. -Throw -( -NS_ERROR_FAILURE -) -; return +nullptr ; } nsCOMPtr @@ -4150,7 +4092,7 @@ nsIURI newURI ; nsresult -rv2 +rv = uri - @@ -4167,21 +4109,15 @@ if ( NS_FAILED ( -rv2 +rv ) ) { -rv -. -Throw -( -rv2 -) -; return +nullptr ; } -rv2 +rv = newURI - @@ -4197,93 +4133,94 @@ if ( NS_FAILED ( -rv2 +rv ) ) { -rv -. -Throw -( -rv2 -) -; return +nullptr ; } -rv2 +rv = newURI - > SetHostAndPort ( -NS_ConvertUTF16toUTF8 -( -aDomain -) +aHostString ) ; if ( NS_FAILED ( -rv2 +rv ) ) { -rv +return +nullptr +; +} +return +newURI . -Throw +forget ( -rv2 ) ; -return -; } -nsAutoCString -current -domain -; -if -( -NS_FAILED -( -uri -- +already_AddRefed +< +nsIURI > -GetAsciiHost +nsHTMLDocument +: +: +RegistrableDomainSuffixOfInternal ( -current -) -) +const +nsAString +& +aNewDomain +nsIURI +* +aOrigHost ) -current -. -Truncate +{ +nsAutoCString +domain += +NS_ConvertUTF16toUTF8 ( +aNewDomain ) ; +nsAutoCString +current +; if ( NS_FAILED ( -newURI +aOrigHost - > GetAsciiHost ( -domain +current ) ) ) -domain +{ +current . Truncate ( ) ; +} bool ok = @@ -4358,14 +4295,8 @@ if tldService ) { -rv -. -Throw -( -NS_ERROR_NOT_AVAILABLE -) -; return +nullptr ; } nsAutoCString @@ -4380,7 +4311,7 @@ tldService > GetBaseDomain ( -uri +aOrigHost 0 currentBaseDomain ) @@ -4450,6 +4381,217 @@ if ok ) { +return +nullptr +; +} +return +CreateInheritingURIForHost +( +domain +) +; +} +bool +nsHTMLDocument +: +: +IsRegistrableDomainSuffixOfOrEqualTo +( +const +nsAString +& +aHostSuffixString +const +nsACString +& +aOrigHost +) +{ +if +( +aHostSuffixString +. +IsEmpty +( +) +) +{ +return +false +; +} +nsCOMPtr +< +nsIURI +> +origURI += +CreateInheritingURIForHost +( +aOrigHost +) +; +if +( +! +origURI +) +{ +return +false +; +} +nsCOMPtr +< +nsIURI +> +newURI += +RegistrableDomainSuffixOfInternal +( +aHostSuffixString +origURI +) +; +if +( +! +newURI +) +{ +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 +) +{ +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 +; +} +nsCOMPtr +< +nsIURI +> +newURI += +RegistrableDomainSuffixOfInternal +( +aDomain +uri +) +; +if +( +! +newURI +) +{ rv . Throw diff --git a/dom/html/nsHTMLDocument.h b/dom/html/nsHTMLDocument.h index 3c6db7a1393f0..fec0fa74295e9 100644 --- a/dom/html/nsHTMLDocument.h +++ b/dom/html/nsHTMLDocument.h @@ -680,6 +680,19 @@ ErrorResult rv ) ; +bool +IsRegistrableDomainSuffixOfOrEqualTo +( +const +nsAString +& +aHostSuffixString +const +nsACString +& +aOrigHost +) +; void GetCookie ( @@ -1362,6 +1375,33 @@ GetDomainURI ( ) ; +already_AddRefed +< +nsIURI +> +CreateInheritingURIForHost +( +const +nsACString +& +aHostString +) +; +already_AddRefed +< +nsIURI +> +RegistrableDomainSuffixOfInternal +( +const +nsAString +& +aHostSuffixString +nsIURI +* +aOrigHost +) +; nsresult WriteCommon (