Bug 1542309 - Set firstPartyDomain to public suffix if getBaseDomain fails. r=baku
authorAlex Catarineu <acat@torproject.org>
Fri, 12 Apr 2019 15:44:40 +0000
changeset 469472 f0ae73dd90f7f3f22bae40a15cd4b47479de50ec
parent 469471 8d11a480d4ae56576b438cfcf5baf1876db25565
child 469473 a7bb3810f26f2eb5266777e53e6afe17d89cc7e3
push id35872
push userncsoregi@mozilla.com
push dateMon, 15 Apr 2019 15:24:06 +0000
treeherdermozilla-central@16d953cca414 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1542309
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1542309 - Set firstPartyDomain to public suffix if getBaseDomain fails. r=baku Right now the firstPartyDomain is not set when host is in the public suffix list. The patch fixes it by setting firstPartyDomain to eTLD.getPublicSuffix in these cases. Differential Revision: https://phabricator.services.mozilla.com/D26767
caps/OriginAttributes.cpp
caps/tests/gtest/TestOriginAttributes.cpp
--- a/caps/OriginAttributes.cpp
+++ b/caps/OriginAttributes.cpp
@@ -77,31 +77,42 @@ void OriginAttributes::SetFirstPartyDoma
       mFirstPartyDomain.AppendLiteral("]");
     } else {
       mFirstPartyDomain = NS_ConvertUTF8toUTF16(ipAddr);
     }
 
     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);
   if (scheme.EqualsLiteral("about")) {
     mFirstPartyDomain.AssignLiteral(ABOUT_URI_FIRST_PARTY_DOMAIN);
     return;
   }
 
   nsCOMPtr<nsIPrincipal> blobPrincipal;
   if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(
           aURI, getter_AddRefs(blobPrincipal))) {
     MOZ_ASSERT(blobPrincipal);
     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,
                                            const nsACString& aDomain) {
   bool isFirstPartyEnabled = IsFirstPartyEnabled();
 
   // If the pref is off or this is not a top level load, bail out.
   if (!isFirstPartyEnabled || !aIsTopLevelDocument) {
--- a/caps/tests/gtest/TestOriginAttributes.cpp
+++ b/caps/tests/gtest/TestOriginAttributes.cpp
@@ -1,27 +1,38 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * 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;
   attrs.CreateSuffix(suffix);
 
   OriginAttributes attrsFromSuffix;
   bool success = attrsFromSuffix.PopulateFromSuffix(suffix);
   EXPECT_TRUE(success);
 
   EXPECT_EQ(attrs, attrsFromSuffix);
 }
 
+static void TestFPD(const nsAString &spec, const nsAString &fpd) {
+  OriginAttributes attrs;
+  nsCOMPtr<nsIURI> 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;
   TestSuffix(attrs);
 }
 
 TEST(OriginAttributes, Suffix_appId_inIsolatedMozBrowser)
 {
@@ -29,8 +40,26 @@ TEST(OriginAttributes, Suffix_appId_inIs
   TestSuffix(attrs);
 }
 
 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);
+}