Commit 6eb0d4fb authored by Emma Zuehlcke's avatar Emma Zuehlcke
Browse files

Bug 1714608 - Cleaned up partitionKey base domain helpers. r=timhuang

parent bbce1490
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -1472,13 +1472,8 @@ nsresult imgLoader::RemoveEntriesInternal(nsIPrincipal* aPrincipal,
        return false;
      }

      // Extract the base domain from the partition key and match it.
      nsAutoString partitionKeyBaseDomain;
      bool ok = StoragePrincipalHelper::GetBaseDomainFromPartitionKey(
          attrs.mPartitionKey, partitionKeyBaseDomain);

      return ok &&
             NS_ConvertUTF16toUTF8(partitionKeyBaseDomain).Equals(*aBaseDomain);
      return StoragePrincipalHelper::PartitionKeyHasBaseDomain(
          attrs.mPartitionKey, *aBaseDomain);
    }();

    if (shouldRemove) {
+2 −5
Original line number Diff line number Diff line
@@ -74,12 +74,9 @@ void SharedStyleSheetCache::Clear(nsIPrincipal* aForPrincipal,
      }

      // Clear entries partitioned under aBaseDomain.
      nsAutoString partitionKeyBaseDomain;
      bool ok = StoragePrincipalHelper::GetBaseDomainFromPartitionKey(
      return StoragePrincipalHelper::PartitionKeyHasBaseDomain(
          partitionPrincipal->OriginAttributesRef().mPartitionKey,
          partitionKeyBaseDomain);
      return ok &&
             NS_ConvertUTF16toUTF8(partitionKeyBaseDomain).Equals(*aBaseDomain);
          *aBaseDomain);
    }();

    if (shouldRemove) {
+3 −8
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include "mozilla/Telemetry.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Services.h"
#include "mozilla/StoragePrincipalHelper.h"
#include "nsDirectoryServiceUtils.h"
#include "nsAppDirectoryServiceDefs.h"
#include "private/pprio.h"
@@ -3100,14 +3101,8 @@ nsresult CacheFileIOManager::EvictByContextInternal(

      // Filter by base domain.
      if (!aBaseDomain.IsEmpty()) {
        nsString scheme;
        nsString pkBaseDomain;
        int32_t port;
        bool success = OriginAttributes::ParsePartitionKey(
            info->OriginAttributesPtr()->mPartitionKey, scheme, pkBaseDomain,
            port);

        if (success && pkBaseDomain.Equals(aBaseDomain)) {
        if (StoragePrincipalHelper::PartitionKeyHasBaseDomain(
                info->OriginAttributesPtr()->mPartitionKey, aBaseDomain)) {
          return true;
        }

+6 −13
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include "mozilla/TimeStamp.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Services.h"
#include "mozilla/StoragePrincipalHelper.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/Telemetry.h"
#include "mozilla/StaticPrefs_network.h"
@@ -855,20 +856,12 @@ NS_IMETHODIMP CacheStorageService::ClearBaseDomain(
      nsCOMPtr<nsILoadContextInfo> info =
          CacheFileUtils::ParseKey(globalEntry.GetKey());

      if (info) {
        nsString scheme;
        nsString baseDomain;
        int32_t port;
        bool success = OriginAttributes::ParsePartitionKey(
            info->OriginAttributesPtr()->mPartitionKey, scheme, baseDomain,
            port);
        if (success) {
          if (baseDomain.Equals(aBaseDomain)) {
      if (info &&
          StoragePrincipalHelper::PartitionKeyHasBaseDomain(
              info->OriginAttributesPtr()->mPartitionKey, aBaseDomain)) {
        keys.AppendElement(key);
        continue;
      }
        }
      }

      // If we didn't get a partitionKey match, try to match by entry URI. This
      // requires us to iterate over all entries.
+14 −76
Original line number Diff line number Diff line
@@ -414,92 +414,30 @@ bool StoragePrincipalHelper::GetOriginAttributes(
  return true;
}

bool StoragePrincipalHelper::GetBaseDomainFromPartitionKey(
    const nsAString& aPartitionKey, nsAString& aBaseDomain) {
  if (aPartitionKey.IsEmpty()) {
    return false;
  }

  // PartitionKey contains only the host.
  if (!StaticPrefs::privacy_dynamic_firstparty_use_site()) {
    aBaseDomain = aPartitionKey;
    return true;
  }

  if (aPartitionKey[0] != '(') {
    return false;
  }

  // Bounds of the baseDomain are first ',' until next ',' or end.
  nsAString::const_iterator start, end;

  aPartitionKey.BeginReading(start);
  aPartitionKey.EndReading(end);

  // Find scheme - site delimiter.
  nsAString::const_iterator iter(++start);
  bool found = FindCharInReadable(',', iter, end);
  MOZ_DIAGNOSTIC_ASSERT(found);
  if (!found) {
    return false;
  }
  nsAString::const_iterator baseDomainStart = ++iter;
  if (baseDomainStart == end) {
    return false;
  }

  // Find site - port delimiter, or end.
  if (!FindCharInReadable(',', iter, end)) {
    iter--;
  }

  aBaseDomain.Assign(Substring(baseDomainStart, iter));
  return true;
bool StoragePrincipalHelper::PartitionKeyHasBaseDomain(
    const nsAString& aPartitionKey, const nsACString& aBaseDomain) {
  return PartitionKeyHasBaseDomain(aPartitionKey,
                                   NS_ConvertUTF8toUTF16(aBaseDomain));
}

// static
bool StoragePrincipalHelper::HasMatchingBaseDomain(
    nsIURI* aURI, const nsAString& aPartitionKey) {
  MOZ_DIAGNOSTIC_ASSERT(aURI);
  if (!aURI || aPartitionKey.IsEmpty()) {
    return false;
  }

  nsCOMPtr<nsIEffectiveTLDService> tldService =
      do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
  if (NS_WARN_IF(!tldService)) {
    return false;
  }

  nsAutoCString uriBaseDomain;
  nsresult rv = tldService->GetBaseDomain(aURI, 0, uriBaseDomain);
  if (NS_FAILED(rv)) {
    return false;
  }

  nsAutoString partitionKeyBaseDomain;
  bool ok = StoragePrincipalHelper::GetBaseDomainFromPartitionKey(
      aPartitionKey, partitionKeyBaseDomain);
  if (!ok) {
bool StoragePrincipalHelper::PartitionKeyHasBaseDomain(
    const nsAString& aPartitionKey, const nsAString& aBaseDomain) {
  if (aPartitionKey.IsEmpty() || aBaseDomain.IsEmpty()) {
    return false;
  }

  return uriBaseDomain.Equals(NS_ConvertUTF16toUTF8(partitionKeyBaseDomain));
}
  nsString scheme;
  nsString pkBaseDomain;
  int32_t port;
  bool success = OriginAttributes::ParsePartitionKey(aPartitionKey, scheme,
                                                     pkBaseDomain, port);

// static
bool StoragePrincipalHelper::HasMatchingBaseDomain(
    const nsAString& aOrigin, const nsAString& aPartitionKey) {
  if (aOrigin.IsEmpty() || aPartitionKey.IsEmpty()) {
  if (!success) {
    return false;
  }

  nsCOMPtr<nsIURI> uri;
  nsresult rv = NS_NewURI(getter_AddRefs(uri), aOrigin);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return false;
  }
  return HasMatchingBaseDomain(uri, aPartitionKey);
  return aBaseDomain.Equals(pkBaseDomain);
}

}  // namespace mozilla
Loading