Bug 1534339 - make OriginAttributes deserializable; r=baku
authorLiang-Heng Chen <xeonchen@gmail.com>
Wed, 04 Dec 2019 09:46:51 +0000
changeset 505289 6ee737ea06ad42c9c5a5bf06253b49ad16b204af
parent 505288 eb47fe3b844605edd50d4c587a77658debc60ca6
child 505290 c4b38fbe9db01dba1b52e02f5b81c135cfd6e63d
push id36881
push userdvarga@mozilla.com
push dateWed, 04 Dec 2019 16:22:31 +0000
treeherdermozilla-central@13fb375eaf14 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1534339
milestone73.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 1534339 - make OriginAttributes deserializable; r=baku `CreateSuffix` is irreversible by `PopulateFromSuffix` because it uses a multi-to-one mapping. Since only ':' will happen in a IPv6 format, we can make it a 1-to-1 mapping so that the `firstPartyDomain` is consistent after `CreateSuffix` and `PopulateFromSuffix`. Differential Revision: https://phabricator.services.mozilla.com/D47910
caps/OriginAttributes.cpp
caps/tests/gtest/TestOriginAttributes.cpp
--- a/caps/OriginAttributes.cpp
+++ b/caps/OriginAttributes.cpp
@@ -8,16 +8,19 @@
 #include "mozilla/Preferences.h"
 #include "mozilla/dom/BlobURLProtocolHandler.h"
 #include "mozilla/dom/URLSearchParams.h"
 #include "mozilla/dom/quota/QuotaManager.h"
 #include "nsIEffectiveTLDService.h"
 #include "nsIURI.h"
 #include "nsURLHelper.h"
 
+static const char kSourceChar = ':';
+static const char kSanitizedChar = '+';
+
 namespace mozilla {
 
 using dom::URLParams;
 
 void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
                                            nsIURI* aURI, bool aForced) {
   bool isFirstPartyEnabled = IsFirstPartyEnabled();
 
@@ -140,27 +143,26 @@ void OriginAttributes::CreateSuffix(nsAC
   if (mPrivateBrowsingId) {
     value.Truncate();
     value.AppendInt(mPrivateBrowsingId);
     params.Set(NS_LITERAL_STRING("privateBrowsingId"), value);
   }
 
   if (!mFirstPartyDomain.IsEmpty()) {
     nsAutoString sanitizedFirstPartyDomain(mFirstPartyDomain);
-    sanitizedFirstPartyDomain.ReplaceChar(
-        dom::quota::QuotaManager::kReplaceChars, '+');
+    sanitizedFirstPartyDomain.ReplaceChar(kSourceChar, kSanitizedChar);
 
     params.Set(NS_LITERAL_STRING("firstPartyDomain"),
                sanitizedFirstPartyDomain);
   }
 
   if (!mGeckoViewSessionContextId.IsEmpty()) {
     nsAutoString sanitizedGeckoViewUserContextId(mGeckoViewSessionContextId);
     sanitizedGeckoViewUserContextId.ReplaceChar(
-        dom::quota::QuotaManager::kReplaceChars, '+');
+        dom::quota::QuotaManager::kReplaceChars, kSanitizedChar);
 
     params.Set(NS_LITERAL_STRING("geckoViewUserContextId"),
                sanitizedGeckoViewUserContextId);
   }
 
   aStr.Truncate();
 
   params.Serialize(value);
@@ -238,17 +240,19 @@ class MOZ_STACK_CLASS PopulateFromSuffix
       NS_ENSURE_TRUE(val >= 0 && val <= UINT32_MAX, false);
       mOriginAttributes->mPrivateBrowsingId = static_cast<uint32_t>(val);
 
       return true;
     }
 
     if (aName.EqualsLiteral("firstPartyDomain")) {
       MOZ_RELEASE_ASSERT(mOriginAttributes->mFirstPartyDomain.IsEmpty());
-      mOriginAttributes->mFirstPartyDomain.Assign(aValue);
+      nsAutoString firstPartyDomain(aValue);
+      firstPartyDomain.ReplaceChar(kSanitizedChar, kSourceChar);
+      mOriginAttributes->mFirstPartyDomain.Assign(firstPartyDomain);
       return true;
     }
 
     if (aName.EqualsLiteral("geckoViewUserContextId")) {
       MOZ_RELEASE_ASSERT(
           mOriginAttributes->mGeckoViewSessionContextId.IsEmpty());
       mOriginAttributes->mGeckoViewSessionContextId.Assign(aValue);
       return true;
--- a/caps/tests/gtest/TestOriginAttributes.cpp
+++ b/caps/tests/gtest/TestOriginAttributes.cpp
@@ -21,16 +21,18 @@ static void TestSuffix(const OriginAttri
 }
 
 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));
+
+  TestSuffix(attrs);
 }
 
 TEST(OriginAttributes, Suffix_default)
 {
   OriginAttributes attrs;
   TestSuffix(attrs);
 }