Bug Summary

File:root/firefox-clang/obj-x86_64-pc-linux-gnu/toolkit/components/antitracking/./../../../../toolkit/components/antitracking/StorageAccess.cpp
Warning:line 783, column 5
Value stored to 'rv' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name Unified_cpp_antitracking0.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/toolkit/components/antitracking -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/toolkit/components/antitracking -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -D _GLIBCXX_ASSERTIONS=1 -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D MOZ_HAS_MOZGLUE -D MOZILLA_INTERNAL_API -D IMPL_LIBXUL -D MOZ_SUPPORT_LEAKCHECKING -D STATIC_EXPORTABLE_JS_API -I /root/firefox-clang/toolkit/components/antitracking -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/toolkit/components/antitracking -I /root/firefox-clang/netwerk/base -I /root/firefox-clang/netwerk/protocol/http -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/ipc/ipdl/_ipdlheaders -I /root/firefox-clang/ipc/chromium/src -I /root/firefox-clang/third_party/abseil-cpp -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/x86_64-linux-gnu/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/backward -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-character-conversion -std=gnu++20 -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-11-12-001017-1011516-1 -x c++ Unified_cpp_antitracking0.cpp
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#include "StorageAccess.h"
6
7#include "mozilla/BasePrincipal.h"
8#include "mozilla/Components.h"
9#include "mozilla/dom/Document.h"
10#include "mozilla/net/CookieJarSettings.h"
11#include "mozilla/PermissionManager.h"
12#include "mozilla/StaticPrefs_browser.h"
13#include "mozilla/StaticPrefs_network.h"
14#include "mozilla/StaticPrefs_privacy.h"
15#include "mozilla/StorageAccess.h"
16#include "nsAboutProtocolUtils.h"
17#include "nsContentUtils.h"
18#include "nsGlobalWindowInner.h"
19#include "nsICookiePermission.h"
20#include "nsICookieService.h"
21#include "nsICookieJarSettings.h"
22#include "nsIHttpChannel.h"
23#include "nsIPermission.h"
24#include "nsIWebProgressListener.h"
25#include "nsNetUtil.h"
26#include "nsScriptSecurityManager.h"
27#include "nsSandboxFlags.h"
28#include "AntiTrackingUtils.h"
29#include "AntiTrackingLog.h"
30#include "ContentBlockingAllowList.h"
31#include "mozIThirdPartyUtil.h"
32
33using namespace mozilla;
34using namespace mozilla::dom;
35using mozilla::net::CookieJarSettings;
36
37// This internal method returns ACCESS_DENY if the access is denied,
38// ACCESS_DEFAULT if unknown, some other access code if granted.
39uint32_t mozilla::detail::CheckCookiePermissionForPrincipal(
40 nsICookieJarSettings* aCookieJarSettings, nsIPrincipal* aPrincipal) {
41 MOZ_ASSERT(aCookieJarSettings)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aCookieJarSettings)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aCookieJarSettings))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("aCookieJarSettings"
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 41); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aCookieJarSettings"
")"); do { MOZ_CrashSequence(__null, 41); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
42 MOZ_ASSERT(aPrincipal)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPrincipal)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPrincipal))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aPrincipal", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 42); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")"
); do { MOZ_CrashSequence(__null, 42); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
43
44 uint32_t cookiePermission = nsICookiePermission::ACCESS_DEFAULT;
45 if (!aPrincipal->GetIsContentPrincipal()) {
46 return cookiePermission;
47 }
48
49 nsresult rv =
50 aCookieJarSettings->CookiePermission(aPrincipal, &cookiePermission);
51 if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))), "NS_FAILED(rv)", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 51)
) {
52 return nsICookiePermission::ACCESS_DEFAULT;
53 }
54
55 // If we have a custom cookie permission, let's use it.
56 return cookiePermission;
57}
58
59/*
60 * Checks if storage for a given principal is permitted by the user's
61 * preferences.
62 *
63 * Ways this function is used:
64 * - aPrincipal, aWindow, optional aURI, others don't care: does this principal
65 * have storage access, testing this window's sandboxing and if it is
66 * third-party. If aURI is provided, we use that for the window's third party
67 * comparisons.
68 * - aPrincipal, aChannel, aWindow=nullptr, others don't care: does this
69 * principal have storage access, testing if this channel is third-party. Note
70 * that this ignores aURI.
71 * - aPrincipal, optional aCookieJarSettings, aWindow=nullptr, aChannel=nullptr,
72 * aURI don't care: does this principal have storage access (assuming it is in a
73 * first-party context and not sandboxed). If we aren't given a
74 * cookieJarSettings, we build one with the principal.
75 *
76 * In all of these cases, we test:
77 * - if aPrincipal is a NullPrincipal, denying
78 * - if this is for an about URI, allowing (maybe with private browsing
79 * constraints) We test the aWindow's extant doc's URI's, aURI's, and
80 * aPrincipal's scheme to be "about".
81 *
82 * We also send a decision to the ContentBlockingNotifier if we have aWindow or
83 * aChannel and didn't stop at the NullPrincipal or about: checks.
84 *
85 * Used in the implementation of StorageAllowedForWindow,
86 * StorageAllowedForDocument, StorageAllowedForChannel and
87 * StorageAllowedForServiceWorker.
88 */
89static StorageAccess InternalStorageAllowedCheck(
90 nsIPrincipal* aPrincipal, nsPIDOMWindowInner* aWindow, nsIURI* aURI,
91 nsIChannel* aChannel, nsICookieJarSettings* aCookieJarSettings,
92 uint32_t& aRejectedReason) {
93 MOZ_ASSERT(aPrincipal)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPrincipal)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPrincipal))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aPrincipal", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 93); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")"
); do { MOZ_CrashSequence(__null, 93); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
94
95 aRejectedReason = 0;
96
97 StorageAccess access = StorageAccess::eAllow;
98
99 // We don't allow storage on the null principal, in general. Even if the
100 // calling context is chrome.
101 if (aPrincipal->GetIsNullPrincipal()) {
102 return StorageAccess::eDeny;
103 }
104
105 nsCOMPtr<nsIURI> documentURI;
106 if (aWindow) {
107 // If the document is sandboxed, then it is not permitted to use storage
108 Document* document = aWindow->GetExtantDoc();
109 if (document && document->GetSandboxFlags() & SANDBOXED_ORIGIN) {
110 return StorageAccess::eDeny;
111 }
112
113 // Check if we are in private browsing, and record that fact
114 if (document && document->IsInPrivateBrowsing()) {
115 access = StorageAccess::ePrivateBrowsing;
116 }
117
118 // Get the document URI for the below about: URI check.
119 documentURI = document ? document->GetDocumentURI() : nullptr;
120 }
121
122 // About URIs are allowed to access storage, even if they don't have chrome
123 // privileges. If this is not desired, than the consumer will have to
124 // implement their own restriction functionality.
125 //
126 // This is due to backwards-compatibility and the state of storage access
127 // before the introducton of InternalStorageAllowedCheck:
128 //
129 // BEFORE:
130 // localStorage, caches: allowed in 3rd-party iframes always
131 // IndexedDB: allowed in 3rd-party iframes only if 3rd party URI is an about:
132 // URI within a specific allowlist
133 //
134 // AFTER:
135 // localStorage, caches: allowed in 3rd-party iframes by default. Preference
136 // can be set to disable in 3rd-party, which will not disallow in about:
137 // URIs.
138 // IndexedDB: allowed in 3rd-party iframes by default. Preference can be set
139 // to disable in 3rd-party, which will disallow in about: URIs, unless they
140 // are within a specific allowlist.
141 //
142 // This means that behavior for storage with internal about: URIs should not
143 // be affected, which is desireable due to the lack of automated testing for
144 // about: URIs with these preferences set, and the importance of the correct
145 // functioning of these URIs even with custom preferences.
146 //
147 // We need to check the aURI or the document URI here instead of only checking
148 // the URI from the principal. Because the principal might not have a URI if
149 // it is a system principal.
150 if ((aURI && aURI->SchemeIs("about") &&
151 !NS_IsContentAccessibleAboutURI(aURI)) ||
152 (documentURI && documentURI->SchemeIs("about") &&
153 !NS_IsContentAccessibleAboutURI(documentURI)) ||
154 aPrincipal->SchemeIs("about")) {
155 return access;
156 }
157
158 bool disabled = true;
159 if (aWindow) {
160 nsIURI* documentURI = aURI ? aURI : aWindow->GetDocumentURI();
161 disabled = !documentURI || !ShouldAllowAccessFor(aWindow, documentURI, true,
162 &aRejectedReason);
163
164 // If the window is a third-party tracker, we should set the rejected reason
165 // to partitioned tracker.
166 uint32_t rejectedReason = aRejectedReason;
167 if (aRejectedReason ==
168 static_cast<uint32_t>(
169 nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN) &&
170 nsContentUtils::IsThirdPartyTrackingResourceWindow(aWindow)) {
171 rejectedReason =
172 nsIWebProgressListener::STATE_COOKIES_PARTITIONED_TRACKER;
173 }
174
175 ContentBlockingNotifier::OnDecision(
176 aWindow,
177 disabled ? ContentBlockingNotifier::BlockingDecision::eBlock
178 : ContentBlockingNotifier::BlockingDecision::eAllow,
179 rejectedReason);
180 } else if (aChannel) {
181 disabled = false;
182 nsCOMPtr<nsIURI> uri;
183 nsresult rv = aChannel->GetURI(getter_AddRefs(uri));
184 if (!NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))), "NS_FAILED(rv)", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 184)
) {
185 disabled = !ShouldAllowAccessFor(aChannel, uri, &aRejectedReason);
186 }
187
188 // If the channel is a third-party tracker, we should set the rejected
189 // reason to partitioned tracker.
190 uint32_t rejectedReason = aRejectedReason;
191 if (AntiTrackingUtils::IsThirdPartyTrackingChannel(aChannel) &&
192 aRejectedReason ==
193 static_cast<uint32_t>(
194 nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN)) {
195 rejectedReason =
196 nsIWebProgressListener::STATE_COOKIES_PARTITIONED_TRACKER;
197 }
198
199 ContentBlockingNotifier::OnDecision(
200 aChannel,
201 disabled ? ContentBlockingNotifier::BlockingDecision::eBlock
202 : ContentBlockingNotifier::BlockingDecision::eAllow,
203 rejectedReason);
204 } else {
205 MOZ_ASSERT(aPrincipal)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPrincipal)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPrincipal))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aPrincipal", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 205); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")"
); do { MOZ_CrashSequence(__null, 205); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
206 nsCOMPtr<nsICookieJarSettings> cookieJarSettings = aCookieJarSettings;
207 if (!cookieJarSettings) {
208 cookieJarSettings = net::CookieJarSettings::Create(aPrincipal);
209 }
210 disabled = !ShouldAllowAccessFor(aPrincipal, aCookieJarSettings);
211 }
212
213 if (!disabled) {
214 return access;
215 }
216
217 // We want to have a partitioned storage only for trackers.
218 // XXX: We should probably remove the check here because this was added for
219 // partitioned tracker only. This was never shipped.
220 if (aRejectedReason ==
221 static_cast<uint32_t>(
222 nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER) ||
223 aRejectedReason ==
224 static_cast<uint32_t>(
225 nsIWebProgressListener::STATE_COOKIES_BLOCKED_SOCIALTRACKER)) {
226 return StorageAccess::ePartitionTrackersOrDeny;
227 }
228
229 // We want to have a partitioned storage for all third parties.
230 if (aRejectedReason ==
231 static_cast<uint32_t>(
232 nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN)) {
233 return StorageAccess::ePartitionForeignOrDeny;
234 }
235
236 return StorageAccess::eDeny;
237}
238
239/**
240 * Wrapper around InternalStorageAllowedCheck which caches the check result on
241 * the inner window to improve performance. nsGlobalWindowInner is responsible
242 * for invalidating the cache state if storage access changes during window
243 * lifetime.
244 */
245static StorageAccess InternalStorageAllowedCheckCached(
246 nsIPrincipal* aPrincipal, nsPIDOMWindowInner* aWindow, nsIURI* aURI,
247 nsIChannel* aChannel, nsICookieJarSettings* aCookieJarSettings,
248 uint32_t& aRejectedReason) {
249 // If enabled, check if we have already computed the storage access field
250 // for this window. This avoids repeated calls to
251 // InternalStorageAllowedCheck.
252 nsGlobalWindowInner* win = nullptr;
253 if (aWindow) {
254 win = nsGlobalWindowInner::Cast(aWindow);
255
256 Maybe<StorageAccess> storageAccess =
257 win->GetStorageAllowedCache(aRejectedReason);
258 if (storageAccess.isSome()) {
259 return storageAccess.value();
260 }
261 }
262
263 StorageAccess result = InternalStorageAllowedCheck(
264 aPrincipal, aWindow, aURI, aChannel, aCookieJarSettings, aRejectedReason);
265 if (win) {
266 // Remember check result for the lifetime of the window. It's the windows
267 // responsibility to invalidate this field if storage access changes
268 // because a storage access permission is granted.
269 win->SetStorageAllowedCache(result, aRejectedReason);
270 }
271
272 return result;
273}
274
275namespace mozilla {
276
277StorageAccess StorageAllowedForWindow(nsPIDOMWindowInner* aWindow,
278 uint32_t* aRejectedReason) {
279 uint32_t rejectedReason;
280 if (!aRejectedReason) {
281 aRejectedReason = &rejectedReason;
282 }
283
284 *aRejectedReason = 0;
285
286 if (Document* document = aWindow->GetExtantDoc()) {
287 nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal();
288 // Note that GetChannel() below may return null, but that's OK, since the
289 // callee is able to deal with a null channel argument, and if passed
290 // null, will only fail to notify the UI in case storage gets blocked.
291 nsIChannel* channel = document->GetChannel();
292 return InternalStorageAllowedCheckCached(
293 principal, aWindow, nullptr, channel, document->CookieJarSettings(),
294 *aRejectedReason);
295 }
296
297 // No document? Try checking Private Browsing Mode without document
298 if (const nsCOMPtr<nsIGlobalObject> global = aWindow->AsGlobal()) {
299 if (const nsCOMPtr<nsIPrincipal> principal = global->PrincipalOrNull()) {
300 if (principal->GetIsInPrivateBrowsing()) {
301 return StorageAccess::ePrivateBrowsing;
302 }
303 }
304 }
305
306 // Everything failed? Let's return a generic rejected reason.
307 return StorageAccess::eDeny;
308}
309
310StorageAccess StorageAllowedForDocument(const Document* aDoc) {
311 StorageAccess cookieAllowed = CookieAllowedForDocument(aDoc);
312 if (!aDoc->IsTopLevelContentDocument() &&
313 cookieAllowed > StorageAccess::eDeny) {
314 return StorageAccess::ePartitionForeignOrDeny;
315 }
316 return cookieAllowed;
317}
318
319StorageAccess CookieAllowedForDocument(const Document* aDoc) {
320 MOZ_ASSERT(aDoc)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aDoc)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(aDoc))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("aDoc", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 320); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aDoc" ")"); do
{ MOZ_CrashSequence(__null, 320); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
321
322 if (nsPIDOMWindowInner* inner = aDoc->GetInnerWindow()) {
323 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
324 // Note that GetChannel() below may return null, but that's OK, since the
325 // callee is able to deal with a null channel argument, and if passed
326 // null, will only fail to notify the UI in case storage gets blocked.
327 nsIChannel* channel = aDoc->GetChannel();
328
329 uint32_t rejectedReason = 0;
330 return InternalStorageAllowedCheckCached(
331 principal, inner, nullptr, channel,
332 const_cast<Document*>(aDoc)->CookieJarSettings(), rejectedReason);
333 }
334
335 return StorageAccess::eDeny;
336}
337
338StorageAccess StorageAllowedForNewWindow(nsIPrincipal* aPrincipal, nsIURI* aURI,
339 nsPIDOMWindowInner* aParent) {
340 MOZ_ASSERT(aPrincipal)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPrincipal)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPrincipal))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aPrincipal", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 340); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")"
); do { MOZ_CrashSequence(__null, 340); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
341 MOZ_ASSERT(aURI)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aURI)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(aURI))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("aURI", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 341); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aURI" ")"); do
{ MOZ_CrashSequence(__null, 341); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
342 // parent may be nullptr
343
344 uint32_t rejectedReason = 0;
345 nsCOMPtr<nsICookieJarSettings> cjs;
346 if (aParent && aParent->GetExtantDoc()) {
347 cjs = aParent->GetExtantDoc()->CookieJarSettings();
348 } else {
349 cjs = net::CookieJarSettings::Create(aPrincipal);
350 }
351 return InternalStorageAllowedCheck(aPrincipal, aParent, aURI, nullptr, cjs,
352 rejectedReason);
353}
354
355StorageAccess StorageAllowedForChannel(nsIChannel* aChannel) {
356 MOZ_DIAGNOSTIC_ASSERT(nsContentUtils::GetSecurityManager())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(nsContentUtils::GetSecurityManager())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(nsContentUtils::GetSecurityManager
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("nsContentUtils::GetSecurityManager()", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 356); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "nsContentUtils::GetSecurityManager()"
")"); do { MOZ_CrashSequence(__null, 356); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
357 MOZ_DIAGNOSTIC_ASSERT(aChannel)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aChannel)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aChannel))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aChannel", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 357); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "aChannel"
")"); do { MOZ_CrashSequence(__null, 357); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
358
359 nsCOMPtr<nsIPrincipal> principal;
360 (void)nsContentUtils::GetSecurityManager()->GetChannelResultPrincipal(
361 aChannel, getter_AddRefs(principal));
362 NS_ENSURE_TRUE(principal, StorageAccess::eDeny)do { if ((__builtin_expect(!!(!(principal)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "principal" ") failed", nullptr
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 362); return StorageAccess::eDeny; } } while (false)
;
363
364 nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
365 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
366 nsresult rv =
367 loadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings));
368 NS_ENSURE_SUCCESS(rv, StorageAccess::eDeny)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "StorageAccess::eDeny", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 368); return StorageAccess::eDeny; } } while (false)
;
369
370 uint32_t rejectedReason = 0;
371 StorageAccess result = InternalStorageAllowedCheck(
372 principal, nullptr, nullptr, aChannel, cookieJarSettings, rejectedReason);
373
374 return result;
375}
376
377StorageAccess StorageAllowedForServiceWorker(
378 nsIPrincipal* aPrincipal, nsICookieJarSettings* aCookieJarSettings) {
379 uint32_t rejectedReason = 0;
380 return InternalStorageAllowedCheck(aPrincipal, nullptr, nullptr, nullptr,
381 aCookieJarSettings, rejectedReason);
382}
383
384bool ShouldPartitionStorage(StorageAccess aAccess) {
385 return aAccess == StorageAccess::ePartitionTrackersOrDeny ||
386 aAccess == StorageAccess::ePartitionForeignOrDeny;
387}
388
389bool ShouldPartitionStorage(uint32_t aRejectedReason) {
390 return aRejectedReason ==
391 static_cast<uint32_t>(
392 nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER) ||
393 aRejectedReason ==
394 static_cast<uint32_t>(
395 nsIWebProgressListener::STATE_COOKIES_BLOCKED_SOCIALTRACKER) ||
396 aRejectedReason ==
397 static_cast<uint32_t>(
398 nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN);
399}
400
401bool StoragePartitioningEnabled(StorageAccess aAccess,
402 nsICookieJarSettings* aCookieJarSettings) {
403 return aAccess == StorageAccess::ePartitionForeignOrDeny &&
404 aCookieJarSettings->GetCookieBehavior() ==
405 nsICookieService::BEHAVIOR_PARTITION_FOREIGN;
406}
407
408bool StoragePartitioningEnabled(uint32_t aRejectedReason,
409 nsICookieJarSettings* aCookieJarSettings) {
410 return aRejectedReason ==
411 static_cast<uint32_t>(
412 nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN) &&
413 aCookieJarSettings->GetCookieBehavior() ==
414 nsICookieService::BEHAVIOR_PARTITION_FOREIGN;
415}
416
417int32_t CookiesBehavior(Document* a3rdPartyDocument) {
418 MOZ_ASSERT(a3rdPartyDocument)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(a3rdPartyDocument)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(a3rdPartyDocument))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("a3rdPartyDocument"
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 418); AnnotateMozCrashReason("MOZ_ASSERT" "(" "a3rdPartyDocument"
")"); do { MOZ_CrashSequence(__null, 418); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
419
420 // WebExtensions principals always get BEHAVIOR_ACCEPT as cookieBehavior
421 // (See Bug 1406675 and Bug 1525917 for rationale).
422 if (BasePrincipal::Cast(a3rdPartyDocument->NodePrincipal())->AddonPolicy()) {
423 return nsICookieService::BEHAVIOR_ACCEPT;
424 }
425
426 return a3rdPartyDocument->CookieJarSettings()->GetCookieBehavior();
427}
428
429bool CookiesBehaviorRejectsThirdPartyContexts(Document* aDocument) {
430 MOZ_ASSERT(aDocument)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aDocument)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aDocument))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aDocument", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 430); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aDocument" ")"
); do { MOZ_CrashSequence(__null, 430); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
431
432 // WebExtensions principals always get BEHAVIOR_ACCEPT as cookieBehavior
433 // (See Bug 1406675 and Bug 1525917 for rationale).
434 if (BasePrincipal::Cast(aDocument->NodePrincipal())->AddonPolicy()) {
435 return false;
436 }
437
438 return aDocument->CookieJarSettings()->GetRejectThirdPartyContexts();
439}
440
441int32_t CookiesBehavior(nsILoadInfo* aLoadInfo, nsIURI* a3rdPartyURI) {
442 MOZ_ASSERT(aLoadInfo)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aLoadInfo)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aLoadInfo))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aLoadInfo", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 442); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aLoadInfo" ")"
); do { MOZ_CrashSequence(__null, 442); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
443 MOZ_ASSERT(a3rdPartyURI)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(a3rdPartyURI)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(a3rdPartyURI))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("a3rdPartyURI", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 443); AnnotateMozCrashReason("MOZ_ASSERT" "(" "a3rdPartyURI"
")"); do { MOZ_CrashSequence(__null, 443); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
444
445 // WebExtensions 3rd party URI always get BEHAVIOR_ACCEPT as cookieBehavior,
446 // this is semantically equivalent to the principal having a AddonPolicy().
447 if (a3rdPartyURI->SchemeIs("moz-extension")) {
448 return nsICookieService::BEHAVIOR_ACCEPT;
449 }
450
451 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
452 nsresult rv =
453 aLoadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings));
454 if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))), "NS_FAILED(rv)", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 454)
) {
455 return nsICookieService::BEHAVIOR_REJECT;
456 }
457
458 return cookieJarSettings->GetCookieBehavior();
459}
460
461int32_t CookiesBehavior(nsIPrincipal* aPrincipal,
462 nsICookieJarSettings* aCookieJarSettings) {
463 MOZ_ASSERT(aPrincipal)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPrincipal)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPrincipal))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aPrincipal", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 463); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")"
); do { MOZ_CrashSequence(__null, 463); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
464 MOZ_ASSERT(aCookieJarSettings)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aCookieJarSettings)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aCookieJarSettings))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("aCookieJarSettings"
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 464); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aCookieJarSettings"
")"); do { MOZ_CrashSequence(__null, 464); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
465
466 // WebExtensions principals always get BEHAVIOR_ACCEPT as cookieBehavior
467 // (See Bug 1406675 for rationale).
468 if (BasePrincipal::Cast(aPrincipal)->AddonPolicy()) {
469 return nsICookieService::BEHAVIOR_ACCEPT;
470 }
471
472 return aCookieJarSettings->GetCookieBehavior();
473}
474
475bool ShouldAllowAccessFor(nsPIDOMWindowInner* aWindow, nsIURI* aURI,
476 bool aCookies, uint32_t* aRejectedReason) {
477 MOZ_ASSERT(aWindow)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aWindow)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aWindow))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aWindow", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 477); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aWindow" ")"
); do { MOZ_CrashSequence(__null, 477); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
478 MOZ_ASSERT(aURI)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aURI)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(aURI))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("aURI", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 478); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aURI" ")"); do
{ MOZ_CrashSequence(__null, 478); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
479
480 // Let's avoid a null check on aRejectedReason everywhere else.
481 uint32_t rejectedReason = 0;
482 if (!aRejectedReason) {
483 aRejectedReason = &rejectedReason;
484 }
485
486 LOG_SPEC(("Computing whether window %p has access to URI %s", aWindow, _spec),do { if ((__builtin_expect(!!(mozilla::detail::log_test(gAntiTrackingLog
, mozilla::LogLevel::Debug)), 0))) { nsAutoCString _specStr("(null)"_ns
); if (aURI) { _specStr = (aURI)->GetSpecOrDefault(); } _specStr
.Truncate(std::min(_specStr.Length(), sMaxSpecLength)); const
char* _spec = _specStr.get(); do { const ::mozilla::LogModule
* moz_real_module = gAntiTrackingLog; if ((__builtin_expect(!
!(mozilla::detail::log_test(moz_real_module, mozilla::LogLevel
::Debug)), 0))) { mozilla::detail::log_print(moz_real_module,
mozilla::LogLevel::Debug, "Computing whether window %p has access to URI %s"
, aWindow, _spec); } } while (0); } } while (0)
487 aURI)do { if ((__builtin_expect(!!(mozilla::detail::log_test(gAntiTrackingLog
, mozilla::LogLevel::Debug)), 0))) { nsAutoCString _specStr("(null)"_ns
); if (aURI) { _specStr = (aURI)->GetSpecOrDefault(); } _specStr
.Truncate(std::min(_specStr.Length(), sMaxSpecLength)); const
char* _spec = _specStr.get(); do { const ::mozilla::LogModule
* moz_real_module = gAntiTrackingLog; if ((__builtin_expect(!
!(mozilla::detail::log_test(moz_real_module, mozilla::LogLevel
::Debug)), 0))) { mozilla::detail::log_print(moz_real_module,
mozilla::LogLevel::Debug, "Computing whether window %p has access to URI %s"
, aWindow, _spec); } } while (0); } } while (0)
;
488
489 nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(aWindow);
490 Document* document = innerWindow->GetExtantDoc();
491 if (!document) {
492 LOG(("Our window has no document"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our window has no document"
); } } while (0)
;
493 return false;
494 }
495
496 uint32_t cookiePermission = detail::CheckCookiePermissionForPrincipal(
497 document->CookieJarSettings(), document->NodePrincipal());
498 if (cookiePermission != nsICookiePermission::ACCESS_DEFAULT) {
499 LOG(do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for window's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
500 ("CheckCookiePermissionForPrincipal() returned a non-default access "do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for window's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
501 "code (%d) for window's principal, returning %s",do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for window's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
502 int(cookiePermission),do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for window's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
503 cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for window's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
504 : "failure"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for window's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
;
505 if (cookiePermission != nsICookiePermission::ACCESS_DENY) {
506 return true;
507 }
508
509 *aRejectedReason =
510 nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION;
511 return false;
512 }
513
514 int32_t behavior = CookiesBehavior(document);
515 if (behavior == nsICookieService::BEHAVIOR_ACCEPT) {
516 LOG(("The cookie behavior pref mandates accepting all cookies!"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "The cookie behavior pref mandates accepting all cookies!"
); } } while (0)
;
517 return true;
518 }
519
520 if (ContentBlockingAllowList::Check(aWindow)) {
521 return true;
522 }
523
524 if (behavior == nsICookieService::BEHAVIOR_REJECT) {
525 LOG(("The cookie behavior pref mandates rejecting all cookies!"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "The cookie behavior pref mandates rejecting all cookies!"
); } } while (0)
;
526 *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_ALL;
527 return false;
528 }
529
530 // As a performance optimization, we only perform this check for
531 // BEHAVIOR_REJECT_FOREIGN and BEHAVIOR_LIMIT_FOREIGN. For
532 // BEHAVIOR_REJECT_TRACKER and
533 // BEHAVIOR_PARTITION_FOREIGN, third-partiness is
534 // implicily checked later below.
535 if (behavior != nsICookieService::BEHAVIOR_REJECT_TRACKER &&
536 behavior != nsICookieService::BEHAVIOR_PARTITION_FOREIGN) {
537 // Let's check if this is a 3rd party context.
538 if (!AntiTrackingUtils::IsThirdPartyWindow(aWindow, aURI)) {
539 LOG(("Our window isn't a third-party window"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our window isn't a third-party window"
); } } while (0)
;
540 return true;
541 }
542 }
543
544 if (behavior == nsICookieService::BEHAVIOR_REJECT_FOREIGN ||
545 behavior == nsICookieService::BEHAVIOR_LIMIT_FOREIGN) {
546 // XXX For non-cookie forms of storage, we handle BEHAVIOR_LIMIT_FOREIGN
547 // by simply rejecting the request to use the storage. In the future, if
548 // we change the meaning of BEHAVIOR_LIMIT_FOREIGN to be one which makes
549 // sense for non-cookie storage types, this may change.
550 LOG(("Nothing more to do due to the behavior code %d", int(behavior)))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Nothing more to do due to the behavior code %d"
, int(behavior)); } } while (0)
;
551 *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN;
552 return false;
553 }
554
555 // The document has been allowlisted. We can return from here directly.
556 if (document->HasStorageAccessPermissionGrantedByAllowList()) {
557 return true;
558 }
559
560 MOZ_ASSERT(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER ||do { static_assert( mozilla::detail::AssertionConditionType<
decltype(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER
|| behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)
>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER
|| behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN"
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 561); AnnotateMozCrashReason("MOZ_ASSERT" "(" "behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN"
")"); do { MOZ_CrashSequence(__null, 561); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
561 behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER
|| behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)
>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER
|| behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN"
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 561); AnnotateMozCrashReason("MOZ_ASSERT" "(" "behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN"
")"); do { MOZ_CrashSequence(__null, 561); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
562
563 uint32_t blockedReason =
564 nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER;
565
566 if (behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER) {
567 if (!nsContentUtils::IsThirdPartyTrackingResourceWindow(aWindow)) {
568 LOG(("Our window isn't a third-party tracking window"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our window isn't a third-party tracking window"
); } } while (0)
;
569 return true;
570 }
571
572 if (AntiTrackingUtils::IsThirdPartySocialTrackingChannel(
573 document->GetChannel())) {
574 blockedReason =
575 nsIWebProgressListener::STATE_COOKIES_BLOCKED_SOCIALTRACKER;
576 }
577 } else if (behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN) {
578 if (nsContentUtils::IsThirdPartyTrackingResourceWindow(aWindow) ||
579 AntiTrackingUtils::IsThirdPartyWindow(aWindow, aURI)) {
580 LOG(("We're in the third-party context, storage should be partitioned"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "We're in the third-party context, storage should be partitioned"
); } } while (0)
;
581 // fall through, but remember that we're partitioning.
582 blockedReason = nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN;
583 } else {
584 LOG(("Our window isn't a third-party window, storage is allowed"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our window isn't a third-party window, storage is allowed"
); } } while (0)
;
585 return true;
586 }
587 } else {
588 MOZ_ASSERT_UNREACHABLE(do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"This should be an exhaustive list of cookie behaviors possible "
"here." ")", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 590); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible "
"here." ")"); do { MOZ_CrashSequence(__null, 590); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
589 "This should be an exhaustive list of cookie behaviors possible "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"This should be an exhaustive list of cookie behaviors possible "
"here." ")", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 590); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible "
"here." ")"); do { MOZ_CrashSequence(__null, 590); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
590 "here.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"This should be an exhaustive list of cookie behaviors possible "
"here." ")", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 590); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible "
"here." ")"); do { MOZ_CrashSequence(__null, 590); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
;
591 }
592
593 Document* doc = aWindow->GetExtantDoc();
594 // Make sure storage access isn't disabled
595 if (doc && (doc->StorageAccessSandboxed())) {
596 LOG(("Our document is sandboxed"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our document is sandboxed"
); } } while (0)
;
597 *aRejectedReason = blockedReason;
598 return false;
599 }
600
601 // "Storage access granted" only affects cookie access for third party
602 // documents. So if we are looking if we should allow access for cookies,
603 // then test if that permission is enabled on this document.
604 // Document::UsingStorageAccess first checks if storage access granted is
605 // cached in the inner window, if no, it then checks the storage permission
606 // flag in the channel's loadinfo
607 bool allowed = aCookies && document->UsingStorageAccess();
608
609 if (!allowed) {
610 *aRejectedReason = blockedReason;
611 } else {
612 if (MOZ_LOG_TEST(gAntiTrackingLog, mozilla::LogLevel::Debug)(__builtin_expect(!!(mozilla::detail::log_test(gAntiTrackingLog
, mozilla::LogLevel::Debug)), 0))
&&
613 aWindow->UsingStorageAccess()) {
614 LOG(("Permission stored in the window. All good."))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Permission stored in the window. All good."
); } } while (0)
;
615 }
616 }
617
618 return allowed;
619}
620
621bool ShouldAllowAccessFor(nsIChannel* aChannel, nsIURI* aURI,
622 uint32_t* aRejectedReason) {
623 MOZ_ASSERT(aURI)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aURI)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(aURI))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("aURI", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 623); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aURI" ")"); do
{ MOZ_CrashSequence(__null, 623); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
624 MOZ_ASSERT(aChannel)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aChannel)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aChannel))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aChannel", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 624); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aChannel" ")"
); do { MOZ_CrashSequence(__null, 624); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
625
626 // Let's avoid a null check on aRejectedReason everywhere else.
627 uint32_t rejectedReason = 0;
628 if (!aRejectedReason) {
629 aRejectedReason = &rejectedReason;
630 }
631
632 nsIScriptSecurityManager* ssm =
633 nsScriptSecurityManager::GetScriptSecurityManager();
634 MOZ_ASSERT(ssm)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(ssm)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(ssm))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("ssm", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 634); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ssm" ")"); do
{ MOZ_CrashSequence(__null, 634); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
635
636 nsCOMPtr<nsIURI> channelURI;
637 nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(channelURI));
638 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
639 LOG(("Failed to get the channel final URI, bail out early"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Failed to get the channel final URI, bail out early"
); } } while (0)
;
640 return true;
641 }
642 LOG_SPEC(do { if ((__builtin_expect(!!(mozilla::detail::log_test(gAntiTrackingLog
, mozilla::LogLevel::Debug)), 0))) { nsAutoCString _specStr("(null)"_ns
); if (channelURI) { _specStr = (channelURI)->GetSpecOrDefault
(); } _specStr.Truncate(std::min(_specStr.Length(), sMaxSpecLength
)); const char* _spec = _specStr.get(); do { const ::mozilla::
LogModule* moz_real_module = gAntiTrackingLog; if ((__builtin_expect
(!!(mozilla::detail::log_test(moz_real_module, mozilla::LogLevel
::Debug)), 0))) { mozilla::detail::log_print(moz_real_module,
mozilla::LogLevel::Debug, "Computing whether channel %p has access to URI %s"
, aChannel, _spec); } } while (0); } } while (0)
643 ("Computing whether channel %p has access to URI %s", aChannel, _spec),do { if ((__builtin_expect(!!(mozilla::detail::log_test(gAntiTrackingLog
, mozilla::LogLevel::Debug)), 0))) { nsAutoCString _specStr("(null)"_ns
); if (channelURI) { _specStr = (channelURI)->GetSpecOrDefault
(); } _specStr.Truncate(std::min(_specStr.Length(), sMaxSpecLength
)); const char* _spec = _specStr.get(); do { const ::mozilla::
LogModule* moz_real_module = gAntiTrackingLog; if ((__builtin_expect
(!!(mozilla::detail::log_test(moz_real_module, mozilla::LogLevel
::Debug)), 0))) { mozilla::detail::log_print(moz_real_module,
mozilla::LogLevel::Debug, "Computing whether channel %p has access to URI %s"
, aChannel, _spec); } } while (0); } } while (0)
644 channelURI)do { if ((__builtin_expect(!!(mozilla::detail::log_test(gAntiTrackingLog
, mozilla::LogLevel::Debug)), 0))) { nsAutoCString _specStr("(null)"_ns
); if (channelURI) { _specStr = (channelURI)->GetSpecOrDefault
(); } _specStr.Truncate(std::min(_specStr.Length(), sMaxSpecLength
)); const char* _spec = _specStr.get(); do { const ::mozilla::
LogModule* moz_real_module = gAntiTrackingLog; if ((__builtin_expect
(!!(mozilla::detail::log_test(moz_real_module, mozilla::LogLevel
::Debug)), 0))) { mozilla::detail::log_print(moz_real_module,
mozilla::LogLevel::Debug, "Computing whether channel %p has access to URI %s"
, aChannel, _spec); } } while (0); } } while (0)
;
645
646 nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
647 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
648 rv = loadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings));
649 if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))), "NS_FAILED(rv)", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 649)
) {
650 LOG(do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Failed to get the cookie jar settings from the loadinfo, bail out "
"early"); } } while (0)
651 ("Failed to get the cookie jar settings from the loadinfo, bail out "do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Failed to get the cookie jar settings from the loadinfo, bail out "
"early"); } } while (0)
652 "early"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Failed to get the cookie jar settings from the loadinfo, bail out "
"early"); } } while (0)
;
653 return true;
654 }
655
656 nsCOMPtr<nsIPrincipal> channelPrincipal;
657 rv = ssm->GetChannelURIPrincipal(aChannel, getter_AddRefs(channelPrincipal));
658 if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))), "NS_FAILED(rv)", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 658)
) {
659 LOG(("No channel principal, bail out early"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "No channel principal, bail out early"
); } } while (0)
;
660 return false;
661 }
662
663 uint32_t cookiePermission = detail::CheckCookiePermissionForPrincipal(
664 cookieJarSettings, channelPrincipal);
665 if (cookiePermission != nsICookiePermission::ACCESS_DEFAULT) {
666 LOG(do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for channel's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
667 ("CheckCookiePermissionForPrincipal() returned a non-default access "do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for channel's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
668 "code (%d) for channel's principal, returning %s",do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for channel's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
669 int(cookiePermission),do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for channel's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
670 cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for channel's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
671 : "failure"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "CheckCookiePermissionForPrincipal() returned a non-default access "
"code (%d) for channel's principal, returning %s", int(cookiePermission
), cookiePermission != nsICookiePermission::ACCESS_DENY ? "success"
: "failure"); } } while (0)
;
672 if (cookiePermission != nsICookiePermission::ACCESS_DENY) {
673 return true;
674 }
675
676 *aRejectedReason =
677 nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION;
678 return false;
679 }
680
681 if (!channelURI) {
682 LOG(("No channel uri, bail out early"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "No channel uri, bail out early"
); } } while (0)
;
683 return false;
684 }
685
686 int32_t behavior = CookiesBehavior(loadInfo, channelURI);
687 if (behavior == nsICookieService::BEHAVIOR_ACCEPT) {
688 LOG(("The cookie behavior pref mandates accepting all cookies!"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "The cookie behavior pref mandates accepting all cookies!"
); } } while (0)
;
689 return true;
690 }
691
692 nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
693
694 if (httpChannel && ContentBlockingAllowList::Check(httpChannel)) {
695 return true;
696 }
697
698 if (behavior == nsICookieService::BEHAVIOR_REJECT) {
699 LOG(("The cookie behavior pref mandates rejecting all cookies!"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "The cookie behavior pref mandates rejecting all cookies!"
); } } while (0)
;
700 *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_ALL;
701 return false;
702 }
703
704 nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
705 components::ThirdPartyUtil::Service();
706 if (!thirdPartyUtil) {
707 LOG(("No thirdPartyUtil, bail out early"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "No thirdPartyUtil, bail out early"
); } } while (0)
;
708 return true;
709 }
710
711 bool thirdParty = false;
712 rv = thirdPartyUtil->IsThirdPartyChannel(aChannel, aURI, &thirdParty);
713 // Grant if it's not a 3rd party.
714 // Be careful to check the return value of IsThirdPartyChannel, since
715 // IsThirdPartyChannel() will fail if the channel's loading principal is the
716 // system principal...
717 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && !thirdParty) {
718 LOG(("Our channel isn't a third-party channel"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our channel isn't a third-party channel"
); } } while (0)
;
719 return true;
720 }
721
722 if (behavior == nsICookieService::BEHAVIOR_REJECT_FOREIGN ||
723 behavior == nsICookieService::BEHAVIOR_LIMIT_FOREIGN) {
724 // XXX For non-cookie forms of storage, we handle BEHAVIOR_LIMIT_FOREIGN
725 // by simply rejecting the request to use the storage. In the future, if
726 // we change the meaning of BEHAVIOR_LIMIT_FOREIGN to be one which makes
727 // sense for non-cookie storage types, this may change.
728 LOG(("Nothing more to do due to the behavior code %d", int(behavior)))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Nothing more to do due to the behavior code %d"
, int(behavior)); } } while (0)
;
729 *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN;
730 return false;
731 }
732
733 // The channel has been allowlisted. We can return from here.
734 if (loadInfo->GetStoragePermission() ==
735 nsILoadInfo::StoragePermissionAllowListed) {
736 return true;
737 }
738
739 MOZ_ASSERT(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER ||do { static_assert( mozilla::detail::AssertionConditionType<
decltype(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER
|| behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)
>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER
|| behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN"
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 740); AnnotateMozCrashReason("MOZ_ASSERT" "(" "behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN"
")"); do { MOZ_CrashSequence(__null, 740); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
740 behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER
|| behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)
>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER
|| behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN)
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN"
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 740); AnnotateMozCrashReason("MOZ_ASSERT" "(" "behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN"
")"); do { MOZ_CrashSequence(__null, 740); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
741
742 uint32_t blockedReason =
743 nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER;
744
745 if (behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER) {
746 if (!AntiTrackingUtils::IsThirdPartyTrackingChannel(aChannel)) {
747 LOG(("Our channel isn't a third-party tracking channel"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our channel isn't a third-party tracking channel"
); } } while (0)
;
748 return true;
749 }
750
751 if (AntiTrackingUtils::IsThirdPartySocialTrackingChannel(aChannel)) {
752 blockedReason =
753 nsIWebProgressListener::STATE_COOKIES_BLOCKED_SOCIALTRACKER;
754 }
755 } else if (behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN) {
756 if (AntiTrackingUtils::IsThirdPartyTrackingChannel(aChannel) ||
757 AntiTrackingUtils::IsThirdPartyChannel(aChannel)) {
758 LOG(("We're in the third-party context, storage should be partitioned"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "We're in the third-party context, storage should be partitioned"
); } } while (0)
;
759 // fall through but remember that we're partitioning.
760 blockedReason = nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN;
761 } else {
762 LOG(("Our channel isn't a third-party channel, storage is allowed"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our channel isn't a third-party channel, storage is allowed"
); } } while (0)
;
763 return true;
764 }
765 } else {
766 MOZ_ASSERT_UNREACHABLE(do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"This should be an exhaustive list of cookie behaviors possible "
"here." ")", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 768); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible "
"here." ")"); do { MOZ_CrashSequence(__null, 768); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
767 "This should be an exhaustive list of cookie behaviors possible "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"This should be an exhaustive list of cookie behaviors possible "
"here." ")", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 768); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible "
"here." ")"); do { MOZ_CrashSequence(__null, 768); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
768 "here.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"This should be an exhaustive list of cookie behaviors possible "
"here." ")", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 768); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible "
"here." ")"); do { MOZ_CrashSequence(__null, 768); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
;
769 }
770
771 RefPtr<BrowsingContext> targetBC;
772 rv = loadInfo->GetTargetBrowsingContext(getter_AddRefs(targetBC));
773 if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))), "NS_FAILED(rv)", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 773)
) {
774 LOG(("Failed to get the channel's target browsing context"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Failed to get the channel's target browsing context"
); } } while (0)
;
775 return false;
776 }
777
778 // If we cannot get the target browsing context from the loadInfo, the
779 // channel could be a fetch request from a worker scope. In this case, we
780 // get the target browsing context from the worker associated browsing
781 // context instead.
782 if (!targetBC) {
783 rv = loadInfo->GetAssociatedBrowsingContext(getter_AddRefs(targetBC));
Value stored to 'rv' is never read
784 }
785
786 // We could have no target BC for the channel if it's for loading the script
787 // for remote workers, i.e. shared workers and service workers. In this
788 // case, we also don't have document, so we can skip the sandbox and the
789 // document check.
790 if (!targetBC) {
791 LOG(("No browsing context is available for the channel."))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "No browsing context is available for the channel."
); } } while (0)
;
792 }
793
794 if (targetBC &&
795 Document::StorageAccessSandboxed(targetBC->GetSandboxFlags())) {
796 LOG(("Our document is sandboxed"))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Our document is sandboxed"
); } } while (0)
;
797 *aRejectedReason = blockedReason;
798 return false;
799 }
800
801 // Let's see if we have to grant the access for this particular channel.
802
803 // UsingStorageAccess only applies to channels that load
804 // documents, for sub-resources loads, just returns the result from
805 // loadInfo.
806 bool isDocument = false;
807 aChannel->GetIsDocument(&isDocument);
808
809 if (targetBC && isDocument) {
810 nsCOMPtr<nsPIDOMWindowInner> inner =
811 AntiTrackingUtils::GetInnerWindow(targetBC);
812 if (inner && inner->UsingStorageAccess()) {
813 LOG(("Permission stored in the window. All good."))do { const ::mozilla::LogModule* moz_real_module = gAntiTrackingLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, mozilla::LogLevel::Debug)), 0))) { mozilla::detail::log_print
(moz_real_module, mozilla::LogLevel::Debug, "Permission stored in the window. All good."
); } } while (0)
;
814 return true;
815 }
816 }
817
818 nsILoadInfo::StoragePermissionState storageAccess =
819 loadInfo->GetStoragePermission();
820 bool allowed = storageAccess == nsILoadInfo::HasStoragePermission ||
821 storageAccess == nsILoadInfo::StoragePermissionAllowListed;
822 if (!allowed) {
823 *aRejectedReason = blockedReason;
824 }
825
826 return allowed;
827}
828
829bool ShouldAllowAccessFor(nsIPrincipal* aPrincipal,
830 nsICookieJarSettings* aCookieJarSettings) {
831 MOZ_ASSERT(aPrincipal)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPrincipal)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPrincipal))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aPrincipal", "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 831); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")"
); do { MOZ_CrashSequence(__null, 831); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
832 MOZ_ASSERT(aCookieJarSettings)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aCookieJarSettings)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aCookieJarSettings))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("aCookieJarSettings"
, "./../../../../toolkit/components/antitracking/StorageAccess.cpp"
, 832); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aCookieJarSettings"
")"); do { MOZ_CrashSequence(__null, 832); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
833
834 uint32_t access =
835 detail::CheckCookiePermissionForPrincipal(aCookieJarSettings, aPrincipal);
836
837 if (access != nsICookiePermission::ACCESS_DEFAULT) {
838 return access != nsICookiePermission::ACCESS_DENY;
839 }
840
841 int32_t behavior = CookiesBehavior(aPrincipal, aCookieJarSettings);
842 return behavior != nsICookieService::BEHAVIOR_REJECT;
843}
844
845} // namespace mozilla