diff --git a/caps/OriginAttributes.cpp b/caps/OriginAttributes.cpp index 4d7ddf1b1480..bc4abe31a965 100644 --- a/caps/OriginAttributes.cpp +++ b/caps/OriginAttributes.cpp @@ -82,6 +82,8 @@ void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument, return; } + // Saving isInsufficientDomainLevels before rv is overwritten. + bool isInsufficientDomainLevels = (rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS); nsAutoCString scheme; rv = aURI->GetScheme(scheme); NS_ENSURE_SUCCESS_VOID(rv); @@ -97,6 +99,15 @@ void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument, mFirstPartyDomain = blobPrincipal->OriginAttributesRef().mFirstPartyDomain; return; } + + if (isInsufficientDomainLevels) { + nsAutoCString publicSuffix; + rv = tldService->GetPublicSuffix(aURI, publicSuffix); + if (NS_SUCCEEDED(rv)) { + mFirstPartyDomain = NS_ConvertUTF8toUTF16(publicSuffix); + } + return; + } } void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument, diff --git a/caps/tests/gtest/TestOriginAttributes.cpp b/caps/tests/gtest/TestOriginAttributes.cpp index 91416c7201cf..1c3428c4f398 100644 --- a/caps/tests/gtest/TestOriginAttributes.cpp +++ b/caps/tests/gtest/TestOriginAttributes.cpp @@ -3,8 +3,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "gtest/gtest.h" #include "mozilla/BasePrincipal.h" +#include "mozilla/Preferences.h" +#include "nsNetUtil.h" using mozilla::OriginAttributes; +using mozilla::Preferences; static void TestSuffix(const OriginAttributes& attrs) { nsAutoCString suffix; @@ -17,6 +20,14 @@ static void TestSuffix(const OriginAttributes& attrs) { EXPECT_EQ(attrs, attrsFromSuffix); } +static void TestFPD(const nsAString &spec, const nsAString &fpd) { + OriginAttributes attrs; + nsCOMPtr url; + ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK); + attrs.SetFirstPartyDomain(true, url); + EXPECT_TRUE(attrs.mFirstPartyDomain.Equals(fpd)); +} + TEST(OriginAttributes, Suffix_default) { OriginAttributes attrs; @@ -34,3 +45,21 @@ TEST(OriginAttributes, Suffix_maxAppId_inIsolatedMozBrowser) OriginAttributes attrs(4294967295, true); TestSuffix(attrs); } + +TEST(OriginAttributes, FirstPartyDomain_default) +{ + static const char prefKey[] = "privacy.firstparty.isolate"; + bool oldPref = Preferences::GetBool(prefKey); + Preferences::SetBool(prefKey, true); + TestFPD(NS_LITERAL_STRING("http://www.example.com"), + NS_LITERAL_STRING("example.com")); + TestFPD(NS_LITERAL_STRING("http://s3.amazonaws.com"), + NS_LITERAL_STRING("s3.amazonaws.com")); + TestFPD(NS_LITERAL_STRING("http://com"), NS_LITERAL_STRING("com")); + TestFPD(NS_LITERAL_STRING("http://.com"), NS_LITERAL_STRING("")); + TestFPD(NS_LITERAL_STRING("http://..com"), NS_LITERAL_STRING("")); + TestFPD(NS_LITERAL_STRING("http://127.0.0.1"), + NS_LITERAL_STRING("127.0.0.1")); + TestFPD(NS_LITERAL_STRING("http://[::1]"), NS_LITERAL_STRING("[::1]")); + Preferences::SetBool(prefKey, oldPref); +}