Bug 1473247 - Part 1: Fixing the issue that the IP addresses won't be set for first party domains. r=arthuredelstein,baku
authorTim Huang <tihuang@mozilla.com>
Fri, 13 Jul 2018 19:53:15 +0000
changeset 818978 03e45d7472a01237858a1224fef4fae9066adecd
parent 818679 7764e30046d332307b726617ad9cd413623d9fd2
child 818979 01f390f36d953e54ae4c17126ac7d9764a6e3470
push id116413
push userbgrinstead@mozilla.com
push dateMon, 16 Jul 2018 22:40:17 +0000
reviewersarthuredelstein, baku
bugs1473247
milestone63.0a1
Bug 1473247 - Part 1: Fixing the issue that the IP addresses won't be set for first party domains. r=arthuredelstein,baku Right now, the firstPartyDomain won't be set when using IP addresses as first party domains. It is because of that the TLD service won't accept IP addresses as valid hosts. The patch fixes this problem by detecting that if the host is a IP address. If it is, we will still set the firstPartyDoamin with the IP address. Differential Revision: https://phabricator.services.mozilla.com/D1977
caps/OriginAttributes.cpp
--- a/caps/OriginAttributes.cpp
+++ b/caps/OriginAttributes.cpp
@@ -6,16 +6,17 @@
 
 #include "mozilla/OriginAttributes.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/dom/URLSearchParams.h"
 #include "mozilla/dom/quota/QuotaManager.h"
 #include "nsIEffectiveTLDService.h"
 #include "nsIURI.h"
 #include "nsIURIWithPrincipal.h"
+#include "nsURLHelper.h"
 
 namespace mozilla {
 
 using dom::URLParams;
 
 bool OriginAttributes::sFirstPartyIsolation = false;
 bool OriginAttributes::sRestrictedOpenerAccess = false;
 
@@ -53,16 +54,38 @@ OriginAttributes::SetFirstPartyDomain(co
 
   nsAutoCString baseDomain;
   nsresult rv = tldService->GetBaseDomain(aURI, 0, baseDomain);
   if (NS_SUCCEEDED(rv)) {
     mFirstPartyDomain = NS_ConvertUTF8toUTF16(baseDomain);
     return;
   }
 
+  if (rv == NS_ERROR_HOST_IS_IP_ADDRESS) {
+    // If the host is an IPv4/IPv6 address, we still accept it as a
+    // valid firstPartyDomain.
+    nsAutoCString ipAddr;
+    rv = aURI->GetHost(ipAddr);
+    NS_ENSURE_SUCCESS_VOID(rv);
+
+    if (net_IsValidIPv6Addr(ipAddr.BeginReading(), ipAddr.Length())) {
+      // According to RFC2732, the host of an IPv6 address should be an
+      // IPv6reference. The GetHost() of nsIURI will only return the IPv6
+      // address. So, we need to convert it back to IPv6reference here.
+      mFirstPartyDomain.Truncate();
+      mFirstPartyDomain.AssignLiteral("[");
+      mFirstPartyDomain.Append(NS_ConvertUTF8toUTF16(ipAddr));
+      mFirstPartyDomain.AppendLiteral("]");
+    } else {
+      mFirstPartyDomain = NS_ConvertUTF8toUTF16(ipAddr);
+    }
+
+    return;
+  }
+
   nsAutoCString scheme;
   rv = aURI->GetScheme(scheme);
   NS_ENSURE_SUCCESS_VOID(rv);
   if (scheme.EqualsLiteral("about")) {
     mFirstPartyDomain.AssignLiteral(ABOUT_URI_FIRST_PARTY_DOMAIN);
   } else if (scheme.EqualsLiteral("blob")) {
     nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(aURI);
     if (uriPrinc) {