1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "ImageCacheKey.h"
10 #include "mozilla/AntiTrackingUtils.h"
11 #include "mozilla/ContentBlocking.h"
12 #include "mozilla/HashFunctions.h"
13 #include "mozilla/StorageAccess.h"
14 #include "mozilla/StoragePrincipalHelper.h"
15 #include "mozilla/Unused.h"
16 #include "mozilla/dom/Document.h"
17 #include "mozilla/dom/File.h"
18 #include "mozilla/dom/ServiceWorkerManager.h"
19 #include "mozilla/StaticPrefs_privacy.h"
20 #include "nsContentUtils.h"
21 #include "nsHashKeys.h"
22 #include "nsLayoutUtils.h"
23 #include "nsPrintfCString.h"
32 ImageCacheKey::ImageCacheKey(nsIURI
* aURI
, const OriginAttributes
& aAttrs
,
35 mOriginAttributes(aAttrs
),
36 mControlledDocument(GetSpecialCaseDocumentToken(aDocument
)),
37 mIsolationKey(GetIsolationKey(aDocument
, aURI
)),
39 if (mURI
->SchemeIs("chrome")) {
44 ImageCacheKey::ImageCacheKey(const ImageCacheKey
& aOther
)
46 mOriginAttributes(aOther
.mOriginAttributes
),
47 mControlledDocument(aOther
.mControlledDocument
),
48 mIsolationKey(aOther
.mIsolationKey
),
50 mIsChrome(aOther
.mIsChrome
) {}
52 ImageCacheKey::ImageCacheKey(ImageCacheKey
&& aOther
)
53 : mURI(std::move(aOther
.mURI
)),
54 mOriginAttributes(aOther
.mOriginAttributes
),
55 mControlledDocument(aOther
.mControlledDocument
),
56 mIsolationKey(aOther
.mIsolationKey
),
58 mIsChrome(aOther
.mIsChrome
) {}
60 bool ImageCacheKey::operator==(const ImageCacheKey
& aOther
) const {
61 // Don't share the image cache between a controlled document and anything
63 if (mControlledDocument
!= aOther
.mControlledDocument
) {
66 // Don't share the image cache between two top-level documents of different
68 if (!mIsolationKey
.Equals(aOther
.mIsolationKey
,
69 nsCaseInsensitiveCStringComparator
)) {
72 // The origin attributes always have to match.
73 if (mOriginAttributes
!= aOther
.mOriginAttributes
) {
77 // For non-blob URIs, compare the URIs.
79 nsresult rv
= mURI
->Equals(aOther
.mURI
, &equals
);
80 return NS_SUCCEEDED(rv
) && equals
;
83 void ImageCacheKey::EnsureHash() const {
84 MOZ_ASSERT(mHash
.isNothing());
85 PLDHashNumber hash
= 0;
87 // Since we frequently call Hash() several times in a row on the same
88 // ImageCacheKey, as an optimization we compute our hash once and store it.
90 nsPrintfCString
ptr("%p", mControlledDocument
);
92 mOriginAttributes
.CreateSuffix(suffix
);
95 Unused
<< mURI
->GetSpec(spec
);
96 hash
= HashString(spec
);
98 hash
= AddToHash(hash
, HashString(suffix
), HashString(mIsolationKey
),
104 void* ImageCacheKey::GetSpecialCaseDocumentToken(Document
* aDocument
) {
105 // Cookie-averse documents can never have storage granted to them. Since they
106 // may not have inner windows, they would require special handling below, so
107 // just bail out early here.
108 if (!aDocument
|| aDocument
->IsCookieAverse()) {
112 // For controlled documents, we cast the pointer into a void* to avoid
113 // dereferencing it (since we only use it for comparisons).
114 RefPtr
<ServiceWorkerManager
> swm
= ServiceWorkerManager::GetInstance();
115 if (swm
&& aDocument
->GetController().isSome()) {
123 nsCString
ImageCacheKey::GetIsolationKey(Document
* aDocument
, nsIURI
* aURI
) {
124 if (!aDocument
|| !aDocument
->GetInnerWindow()) {
128 // Network-state isolation
129 if (StaticPrefs::privacy_partition_network_state()) {
131 StoragePrincipalHelper::GetOriginAttributesForNetworkState(aDocument
, oa
);
133 nsAutoCString suffix
;
134 oa
.CreateSuffix(suffix
);
136 return std::move(suffix
);
139 // If the window is 3rd party resource, let's see if first-party storage
140 // access is granted for this image.
141 if (AntiTrackingUtils::IsThirdPartyWindow(aDocument
->GetInnerWindow(),
143 uint32_t rejectedReason
= 0;
144 Unused
<< rejectedReason
;
145 return StorageDisabledByAntiTracking(aDocument
, aURI
, rejectedReason
)
146 ? aDocument
->GetBaseDomain()
150 // Another scenario is if this image is a 3rd party resource loaded by a
151 // first party context. In this case, we should check if the nsIChannel has
152 // been marked as tracking resource, but we don't have the channel yet at
153 // this point. The best approach here is to be conservative: if we are sure
154 // that the permission is granted, let's return 0. Otherwise, let's make a
155 // unique image cache per the top-level document eTLD+1.
156 if (!ContentBlocking::ApproximateAllowAccessForWithoutChannel(
157 aDocument
->GetInnerWindow(), aURI
)) {
158 // If we are here, the image is a 3rd-party resource loaded by a first-party
159 // context. We can just use the document's base domain as the key because it
160 // should be the same as the top-level document's base domain.
162 ->GetBaseDomain(); // because we don't have anything better!
169 } // namespace mozilla