| File: | root/firefox-clang/obj-x86_64-pc-linux-gnu/toolkit/components/antitracking/./../../../../toolkit/components/antitracking/StorageAccess.cpp |
| Warning: | line 803, column 5 Value stored to 'rv' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 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 "nsIClassifiedChannel.h" |
| 26 | #include "nsNetUtil.h" |
| 27 | #include "nsScriptSecurityManager.h" |
| 28 | #include "nsSandboxFlags.h" |
| 29 | #include "AntiTrackingUtils.h" |
| 30 | #include "AntiTrackingLog.h" |
| 31 | #include "ContentBlockingAllowList.h" |
| 32 | #include "mozIThirdPartyUtil.h" |
| 33 | |
| 34 | using namespace mozilla; |
| 35 | using namespace mozilla::dom; |
| 36 | using mozilla::net::CookieJarSettings; |
| 37 | |
| 38 | // This internal method returns ACCESS_DENY if the access is denied, |
| 39 | // ACCESS_DEFAULT if unknown, some other access code if granted. |
| 40 | uint32_t mozilla::detail::CheckCookiePermissionForPrincipal( |
| 41 | nsICookieJarSettings* aCookieJarSettings, nsIPrincipal* aPrincipal) { |
| 42 | 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" , 42); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aCookieJarSettings" ")"); do { MOZ_CrashSequence(__null, 42); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 43 | 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" , 43); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")" ); do { MOZ_CrashSequence(__null, 43); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 44 | |
| 45 | uint32_t cookiePermission = nsICookiePermission::ACCESS_DEFAULT; |
| 46 | if (!aPrincipal->GetIsContentPrincipal()) { |
| 47 | return cookiePermission; |
| 48 | } |
| 49 | |
| 50 | nsresult rv = |
| 51 | aCookieJarSettings->CookiePermission(aPrincipal, &cookiePermission); |
| 52 | 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" , 52)) { |
| 53 | return nsICookiePermission::ACCESS_DEFAULT; |
| 54 | } |
| 55 | |
| 56 | // If we have a custom cookie permission, let's use it. |
| 57 | return cookiePermission; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Checks if storage for a given principal is permitted by the user's |
| 62 | * preferences. |
| 63 | * |
| 64 | * Ways this function is used: |
| 65 | * - aPrincipal, aWindow, optional aURI, others don't care: does this principal |
| 66 | * have storage access, testing this window's sandboxing and if it is |
| 67 | * third-party. If aURI is provided, we use that for the window's third party |
| 68 | * comparisons. |
| 69 | * - aPrincipal, aChannel, aWindow=nullptr, others don't care: does this |
| 70 | * principal have storage access, testing if this channel is third-party. Note |
| 71 | * that this ignores aURI. |
| 72 | * - aPrincipal, optional aCookieJarSettings, aWindow=nullptr, aChannel=nullptr, |
| 73 | * aURI don't care: does this principal have storage access (assuming it is in a |
| 74 | * first-party context and not sandboxed). If we aren't given a |
| 75 | * cookieJarSettings, we build one with the principal. |
| 76 | * |
| 77 | * In all of these cases, we test: |
| 78 | * - if aPrincipal is a NullPrincipal, denying |
| 79 | * - if this is for an about URI, allowing (maybe with private browsing |
| 80 | * constraints) We test the aWindow's extant doc's URI's, aURI's, and |
| 81 | * aPrincipal's scheme to be "about". |
| 82 | * |
| 83 | * We also send a decision to the ContentBlockingNotifier if we have aWindow or |
| 84 | * aChannel and didn't stop at the NullPrincipal or about: checks. |
| 85 | * |
| 86 | * Used in the implementation of StorageAllowedForWindow, |
| 87 | * StorageAllowedForDocument, StorageAllowedForChannel and |
| 88 | * StorageAllowedForServiceWorker. |
| 89 | */ |
| 90 | static StorageAccess InternalStorageAllowedCheck( |
| 91 | nsIPrincipal* aPrincipal, nsPIDOMWindowInner* aWindow, nsIURI* aURI, |
| 92 | nsIChannel* aChannel, nsICookieJarSettings* aCookieJarSettings, |
| 93 | uint32_t& aRejectedReason) { |
| 94 | 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" , 94); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")" ); do { MOZ_CrashSequence(__null, 94); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 95 | |
| 96 | aRejectedReason = 0; |
| 97 | |
| 98 | StorageAccess access = StorageAccess::eAllow; |
| 99 | |
| 100 | // We don't allow storage on the null principal, in general. Even if the |
| 101 | // calling context is chrome. |
| 102 | if (aPrincipal->GetIsNullPrincipal()) { |
| 103 | return StorageAccess::eDeny; |
| 104 | } |
| 105 | |
| 106 | nsCOMPtr<nsIURI> documentURI; |
| 107 | if (aWindow) { |
| 108 | // If the document is sandboxed, then it is not permitted to use storage |
| 109 | Document* document = aWindow->GetExtantDoc(); |
| 110 | if (document && document->GetSandboxFlags() & SANDBOXED_ORIGIN) { |
| 111 | return StorageAccess::eDeny; |
| 112 | } |
| 113 | |
| 114 | // Check if we are in private browsing, and record that fact |
| 115 | if (document && document->IsInPrivateBrowsing()) { |
| 116 | access = StorageAccess::ePrivateBrowsing; |
| 117 | } |
| 118 | |
| 119 | // Get the document URI for the below about: URI check. |
| 120 | documentURI = document ? document->GetDocumentURI() : nullptr; |
| 121 | } |
| 122 | |
| 123 | // About URIs are allowed to access storage, even if they don't have chrome |
| 124 | // privileges. If this is not desired, than the consumer will have to |
| 125 | // implement their own restriction functionality. |
| 126 | // |
| 127 | // This is due to backwards-compatibility and the state of storage access |
| 128 | // before the introducton of InternalStorageAllowedCheck: |
| 129 | // |
| 130 | // BEFORE: |
| 131 | // localStorage, caches: allowed in 3rd-party iframes always |
| 132 | // IndexedDB: allowed in 3rd-party iframes only if 3rd party URI is an about: |
| 133 | // URI within a specific allowlist |
| 134 | // |
| 135 | // AFTER: |
| 136 | // localStorage, caches: allowed in 3rd-party iframes by default. Preference |
| 137 | // can be set to disable in 3rd-party, which will not disallow in about: |
| 138 | // URIs. |
| 139 | // IndexedDB: allowed in 3rd-party iframes by default. Preference can be set |
| 140 | // to disable in 3rd-party, which will disallow in about: URIs, unless they |
| 141 | // are within a specific allowlist. |
| 142 | // |
| 143 | // This means that behavior for storage with internal about: URIs should not |
| 144 | // be affected, which is desireable due to the lack of automated testing for |
| 145 | // about: URIs with these preferences set, and the importance of the correct |
| 146 | // functioning of these URIs even with custom preferences. |
| 147 | // |
| 148 | // We need to check the aURI or the document URI here instead of only checking |
| 149 | // the URI from the principal. Because the principal might not have a URI if |
| 150 | // it is a system principal. |
| 151 | if ((aURI && aURI->SchemeIs("about") && |
| 152 | !NS_IsContentAccessibleAboutURI(aURI)) || |
| 153 | (documentURI && documentURI->SchemeIs("about") && |
| 154 | !NS_IsContentAccessibleAboutURI(documentURI)) || |
| 155 | aPrincipal->SchemeIs("about")) { |
| 156 | return access; |
| 157 | } |
| 158 | |
| 159 | bool disabled = true; |
| 160 | if (aWindow) { |
| 161 | nsIURI* documentURI = aURI ? aURI : aWindow->GetDocumentURI(); |
| 162 | disabled = !documentURI || !ShouldAllowAccessFor(aWindow, documentURI, true, |
| 163 | &aRejectedReason); |
| 164 | |
| 165 | // If the window is a third-party tracker, we should set the rejected reason |
| 166 | // to partitioned tracker. |
| 167 | uint32_t rejectedReason = aRejectedReason; |
| 168 | if (aRejectedReason == |
| 169 | static_cast<uint32_t>( |
| 170 | nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN) && |
| 171 | nsContentUtils::IsThirdPartyTrackingResourceWindow(aWindow)) { |
| 172 | rejectedReason = |
| 173 | nsIWebProgressListener::STATE_COOKIES_PARTITIONED_TRACKER; |
| 174 | } |
| 175 | |
| 176 | ContentBlockingNotifier::OnDecision( |
| 177 | aWindow, |
| 178 | disabled ? ContentBlockingNotifier::BlockingDecision::eBlock |
| 179 | : ContentBlockingNotifier::BlockingDecision::eAllow, |
| 180 | rejectedReason); |
| 181 | } else if (aChannel) { |
| 182 | disabled = false; |
| 183 | nsCOMPtr<nsIURI> uri; |
| 184 | nsresult rv = aChannel->GetURI(getter_AddRefs(uri)); |
| 185 | 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" , 185)) { |
| 186 | disabled = !ShouldAllowAccessFor(aChannel, uri, &aRejectedReason); |
| 187 | } |
| 188 | |
| 189 | // If the channel is a third-party tracker, we should set the rejected |
| 190 | // reason to partitioned tracker. |
| 191 | uint32_t rejectedReason = aRejectedReason; |
| 192 | nsCOMPtr<nsIClassifiedChannel> classifiedChannel = |
| 193 | do_QueryInterface(aChannel); |
| 194 | |
| 195 | if (classifiedChannel && |
| 196 | classifiedChannel->IsThirdPartyTrackingResource() && |
| 197 | aRejectedReason == |
| 198 | static_cast<uint32_t>( |
| 199 | nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN)) { |
| 200 | rejectedReason = |
| 201 | nsIWebProgressListener::STATE_COOKIES_PARTITIONED_TRACKER; |
| 202 | } |
| 203 | |
| 204 | ContentBlockingNotifier::OnDecision( |
| 205 | aChannel, |
| 206 | disabled ? ContentBlockingNotifier::BlockingDecision::eBlock |
| 207 | : ContentBlockingNotifier::BlockingDecision::eAllow, |
| 208 | rejectedReason); |
| 209 | } else { |
| 210 | 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" , 210); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")" ); do { MOZ_CrashSequence(__null, 210); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 211 | nsCOMPtr<nsICookieJarSettings> cookieJarSettings = aCookieJarSettings; |
| 212 | if (!cookieJarSettings) { |
| 213 | cookieJarSettings = net::CookieJarSettings::Create(aPrincipal); |
| 214 | } |
| 215 | disabled = !ShouldAllowAccessFor(aPrincipal, aCookieJarSettings); |
| 216 | } |
| 217 | |
| 218 | if (!disabled) { |
| 219 | return access; |
| 220 | } |
| 221 | |
| 222 | // We want to have a partitioned storage only for trackers. |
| 223 | // XXX: We should probably remove the check here because this was added for |
| 224 | // partitioned tracker only. This was never shipped. |
| 225 | if (aRejectedReason == |
| 226 | static_cast<uint32_t>( |
| 227 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER) || |
| 228 | aRejectedReason == |
| 229 | static_cast<uint32_t>( |
| 230 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_SOCIALTRACKER)) { |
| 231 | return StorageAccess::ePartitionTrackersOrDeny; |
| 232 | } |
| 233 | |
| 234 | // We want to have a partitioned storage for all third parties. |
| 235 | if (aRejectedReason == |
| 236 | static_cast<uint32_t>( |
| 237 | nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN)) { |
| 238 | return StorageAccess::ePartitionForeignOrDeny; |
| 239 | } |
| 240 | |
| 241 | return StorageAccess::eDeny; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Wrapper around InternalStorageAllowedCheck which caches the check result on |
| 246 | * the inner window to improve performance. nsGlobalWindowInner is responsible |
| 247 | * for invalidating the cache state if storage access changes during window |
| 248 | * lifetime. |
| 249 | */ |
| 250 | static StorageAccess InternalStorageAllowedCheckCached( |
| 251 | nsIPrincipal* aPrincipal, nsPIDOMWindowInner* aWindow, nsIURI* aURI, |
| 252 | nsIChannel* aChannel, nsICookieJarSettings* aCookieJarSettings, |
| 253 | uint32_t& aRejectedReason) { |
| 254 | // If enabled, check if we have already computed the storage access field |
| 255 | // for this window. This avoids repeated calls to |
| 256 | // InternalStorageAllowedCheck. |
| 257 | nsGlobalWindowInner* win = nullptr; |
| 258 | if (aWindow) { |
| 259 | win = nsGlobalWindowInner::Cast(aWindow); |
| 260 | |
| 261 | Maybe<StorageAccess> storageAccess = |
| 262 | win->GetStorageAllowedCache(aRejectedReason); |
| 263 | if (storageAccess.isSome()) { |
| 264 | return storageAccess.value(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | StorageAccess result = InternalStorageAllowedCheck( |
| 269 | aPrincipal, aWindow, aURI, aChannel, aCookieJarSettings, aRejectedReason); |
| 270 | if (win) { |
| 271 | // Remember check result for the lifetime of the window. It's the windows |
| 272 | // responsibility to invalidate this field if storage access changes |
| 273 | // because a storage access permission is granted. |
| 274 | win->SetStorageAllowedCache(result, aRejectedReason); |
| 275 | } |
| 276 | |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | namespace mozilla { |
| 281 | |
| 282 | StorageAccess StorageAllowedForWindow(nsPIDOMWindowInner* aWindow, |
| 283 | uint32_t* aRejectedReason) { |
| 284 | uint32_t rejectedReason; |
| 285 | if (!aRejectedReason) { |
| 286 | aRejectedReason = &rejectedReason; |
| 287 | } |
| 288 | |
| 289 | *aRejectedReason = 0; |
| 290 | |
| 291 | if (Document* document = aWindow->GetExtantDoc()) { |
| 292 | nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal(); |
| 293 | // Note that GetChannel() below may return null, but that's OK, since the |
| 294 | // callee is able to deal with a null channel argument, and if passed |
| 295 | // null, will only fail to notify the UI in case storage gets blocked. |
| 296 | nsIChannel* channel = document->GetChannel(); |
| 297 | return InternalStorageAllowedCheckCached( |
| 298 | principal, aWindow, nullptr, channel, document->CookieJarSettings(), |
| 299 | *aRejectedReason); |
| 300 | } |
| 301 | |
| 302 | // No document? Try checking Private Browsing Mode without document |
| 303 | if (const nsCOMPtr<nsIGlobalObject> global = aWindow->AsGlobal()) { |
| 304 | if (const nsCOMPtr<nsIPrincipal> principal = global->PrincipalOrNull()) { |
| 305 | if (principal->GetIsInPrivateBrowsing()) { |
| 306 | return StorageAccess::ePrivateBrowsing; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Everything failed? Let's return a generic rejected reason. |
| 312 | return StorageAccess::eDeny; |
| 313 | } |
| 314 | |
| 315 | StorageAccess StorageAllowedForDocument(const Document* aDoc) { |
| 316 | StorageAccess cookieAllowed = CookieAllowedForDocument(aDoc); |
| 317 | if (!aDoc->IsTopLevelContentDocument() && |
| 318 | cookieAllowed > StorageAccess::eDeny) { |
| 319 | return StorageAccess::ePartitionForeignOrDeny; |
| 320 | } |
| 321 | return cookieAllowed; |
| 322 | } |
| 323 | |
| 324 | StorageAccess CookieAllowedForDocument(const Document* aDoc) { |
| 325 | 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" , 325); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aDoc" ")"); do { MOZ_CrashSequence(__null, 325); __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
| 326 | |
| 327 | if (nsPIDOMWindowInner* inner = aDoc->GetInnerWindow()) { |
| 328 | nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal(); |
| 329 | // Note that GetChannel() below may return null, but that's OK, since the |
| 330 | // callee is able to deal with a null channel argument, and if passed |
| 331 | // null, will only fail to notify the UI in case storage gets blocked. |
| 332 | nsIChannel* channel = aDoc->GetChannel(); |
| 333 | |
| 334 | uint32_t rejectedReason = 0; |
| 335 | return InternalStorageAllowedCheckCached( |
| 336 | principal, inner, nullptr, channel, |
| 337 | const_cast<Document*>(aDoc)->CookieJarSettings(), rejectedReason); |
| 338 | } |
| 339 | |
| 340 | return StorageAccess::eDeny; |
| 341 | } |
| 342 | |
| 343 | StorageAccess StorageAllowedForNewWindow(nsIPrincipal* aPrincipal, nsIURI* aURI, |
| 344 | nsPIDOMWindowInner* aParent) { |
| 345 | 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" , 345); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")" ); do { MOZ_CrashSequence(__null, 345); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 346 | 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" , 346); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aURI" ")"); do { MOZ_CrashSequence(__null, 346); __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
| 347 | // parent may be nullptr |
| 348 | |
| 349 | uint32_t rejectedReason = 0; |
| 350 | nsCOMPtr<nsICookieJarSettings> cjs; |
| 351 | if (aParent && aParent->GetExtantDoc()) { |
| 352 | cjs = aParent->GetExtantDoc()->CookieJarSettings(); |
| 353 | } else { |
| 354 | cjs = net::CookieJarSettings::Create(aPrincipal); |
| 355 | } |
| 356 | return InternalStorageAllowedCheck(aPrincipal, aParent, aURI, nullptr, cjs, |
| 357 | rejectedReason); |
| 358 | } |
| 359 | |
| 360 | StorageAccess StorageAllowedForChannel(nsIChannel* aChannel) { |
| 361 | 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" , 361); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "nsContentUtils::GetSecurityManager()" ")"); do { MOZ_CrashSequence(__null, 361); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 362 | 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" , 362); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "aChannel" ")"); do { MOZ_CrashSequence(__null, 362); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 363 | |
| 364 | nsCOMPtr<nsIPrincipal> principal; |
| 365 | (void)nsContentUtils::GetSecurityManager()->GetChannelResultPrincipal( |
| 366 | aChannel, getter_AddRefs(principal)); |
| 367 | 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" , 367); return StorageAccess::eDeny; } } while (false); |
| 368 | |
| 369 | nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo(); |
| 370 | nsCOMPtr<nsICookieJarSettings> cookieJarSettings; |
| 371 | nsresult rv = |
| 372 | loadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings)); |
| 373 | 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" , 373); return StorageAccess::eDeny; } } while (false); |
| 374 | |
| 375 | uint32_t rejectedReason = 0; |
| 376 | StorageAccess result = InternalStorageAllowedCheck( |
| 377 | principal, nullptr, nullptr, aChannel, cookieJarSettings, rejectedReason); |
| 378 | |
| 379 | return result; |
| 380 | } |
| 381 | |
| 382 | StorageAccess StorageAllowedForServiceWorker( |
| 383 | nsIPrincipal* aPrincipal, nsICookieJarSettings* aCookieJarSettings) { |
| 384 | uint32_t rejectedReason = 0; |
| 385 | return InternalStorageAllowedCheck(aPrincipal, nullptr, nullptr, nullptr, |
| 386 | aCookieJarSettings, rejectedReason); |
| 387 | } |
| 388 | |
| 389 | bool ShouldPartitionStorage(StorageAccess aAccess) { |
| 390 | return aAccess == StorageAccess::ePartitionTrackersOrDeny || |
| 391 | aAccess == StorageAccess::ePartitionForeignOrDeny; |
| 392 | } |
| 393 | |
| 394 | bool ShouldPartitionStorage(uint32_t aRejectedReason) { |
| 395 | return aRejectedReason == |
| 396 | static_cast<uint32_t>( |
| 397 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER) || |
| 398 | aRejectedReason == |
| 399 | static_cast<uint32_t>( |
| 400 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_SOCIALTRACKER) || |
| 401 | aRejectedReason == |
| 402 | static_cast<uint32_t>( |
| 403 | nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN); |
| 404 | } |
| 405 | |
| 406 | bool StoragePartitioningEnabled(StorageAccess aAccess, |
| 407 | nsICookieJarSettings* aCookieJarSettings) { |
| 408 | return aAccess == StorageAccess::ePartitionForeignOrDeny && |
| 409 | aCookieJarSettings->GetCookieBehavior() == |
| 410 | nsICookieService::BEHAVIOR_PARTITION_FOREIGN; |
| 411 | } |
| 412 | |
| 413 | bool StoragePartitioningEnabled(uint32_t aRejectedReason, |
| 414 | nsICookieJarSettings* aCookieJarSettings) { |
| 415 | return aRejectedReason == |
| 416 | static_cast<uint32_t>( |
| 417 | nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN) && |
| 418 | aCookieJarSettings->GetCookieBehavior() == |
| 419 | nsICookieService::BEHAVIOR_PARTITION_FOREIGN; |
| 420 | } |
| 421 | |
| 422 | int32_t CookiesBehavior(Document* a3rdPartyDocument) { |
| 423 | 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" , 423); AnnotateMozCrashReason("MOZ_ASSERT" "(" "a3rdPartyDocument" ")"); do { MOZ_CrashSequence(__null, 423); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 424 | |
| 425 | // WebExtensions principals always get BEHAVIOR_ACCEPT as cookieBehavior |
| 426 | // (See Bug 1406675 and Bug 1525917 for rationale). |
| 427 | if (BasePrincipal::Cast(a3rdPartyDocument->NodePrincipal())->AddonPolicy()) { |
| 428 | return nsICookieService::BEHAVIOR_ACCEPT; |
| 429 | } |
| 430 | |
| 431 | return a3rdPartyDocument->CookieJarSettings()->GetCookieBehavior(); |
| 432 | } |
| 433 | |
| 434 | bool CookiesBehaviorRejectsThirdPartyContexts(Document* aDocument) { |
| 435 | 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" , 435); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aDocument" ")" ); do { MOZ_CrashSequence(__null, 435); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 436 | |
| 437 | // WebExtensions principals always get BEHAVIOR_ACCEPT as cookieBehavior |
| 438 | // (See Bug 1406675 and Bug 1525917 for rationale). |
| 439 | if (BasePrincipal::Cast(aDocument->NodePrincipal())->AddonPolicy()) { |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | return aDocument->CookieJarSettings()->GetRejectThirdPartyContexts(); |
| 444 | } |
| 445 | |
| 446 | int32_t CookiesBehavior(nsILoadInfo* aLoadInfo, nsIURI* a3rdPartyURI) { |
| 447 | 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" , 447); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aLoadInfo" ")" ); do { MOZ_CrashSequence(__null, 447); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 448 | 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" , 448); AnnotateMozCrashReason("MOZ_ASSERT" "(" "a3rdPartyURI" ")"); do { MOZ_CrashSequence(__null, 448); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 449 | |
| 450 | // WebExtensions 3rd party URI always get BEHAVIOR_ACCEPT as cookieBehavior, |
| 451 | // this is semantically equivalent to the principal having a AddonPolicy(). |
| 452 | if (a3rdPartyURI->SchemeIs("moz-extension")) { |
| 453 | return nsICookieService::BEHAVIOR_ACCEPT; |
| 454 | } |
| 455 | |
| 456 | nsCOMPtr<nsICookieJarSettings> cookieJarSettings; |
| 457 | nsresult rv = |
| 458 | aLoadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings)); |
| 459 | 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" , 459)) { |
| 460 | return nsICookieService::BEHAVIOR_REJECT; |
| 461 | } |
| 462 | |
| 463 | return cookieJarSettings->GetCookieBehavior(); |
| 464 | } |
| 465 | |
| 466 | int32_t CookiesBehavior(nsIPrincipal* aPrincipal, |
| 467 | nsICookieJarSettings* aCookieJarSettings) { |
| 468 | 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" , 468); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")" ); do { MOZ_CrashSequence(__null, 468); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 469 | 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" , 469); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aCookieJarSettings" ")"); do { MOZ_CrashSequence(__null, 469); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 470 | |
| 471 | // WebExtensions principals always get BEHAVIOR_ACCEPT as cookieBehavior |
| 472 | // (See Bug 1406675 for rationale). |
| 473 | if (BasePrincipal::Cast(aPrincipal)->AddonPolicy()) { |
| 474 | return nsICookieService::BEHAVIOR_ACCEPT; |
| 475 | } |
| 476 | |
| 477 | return aCookieJarSettings->GetCookieBehavior(); |
| 478 | } |
| 479 | |
| 480 | bool ShouldAllowAccessFor(nsPIDOMWindowInner* aWindow, nsIURI* aURI, |
| 481 | bool aCookies, uint32_t* aRejectedReason) { |
| 482 | 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" , 482); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aWindow" ")" ); do { MOZ_CrashSequence(__null, 482); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 483 | 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" , 483); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aURI" ")"); do { MOZ_CrashSequence(__null, 483); __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
| 484 | |
| 485 | // Let's avoid a null check on aRejectedReason everywhere else. |
| 486 | uint32_t rejectedReason = 0; |
| 487 | if (!aRejectedReason) { |
| 488 | aRejectedReason = &rejectedReason; |
| 489 | } |
| 490 | |
| 491 | 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) |
| 492 | 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); |
| 493 | |
| 494 | nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(aWindow); |
| 495 | Document* document = innerWindow->GetExtantDoc(); |
| 496 | if (!document) { |
| 497 | 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); |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | uint32_t cookiePermission = detail::CheckCookiePermissionForPrincipal( |
| 502 | document->CookieJarSettings(), document->NodePrincipal()); |
| 503 | if (cookiePermission != nsICookiePermission::ACCESS_DEFAULT) { |
| 504 | 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) |
| 505 | ("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) |
| 506 | "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) |
| 507 | 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) |
| 508 | 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) |
| 509 | : "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); |
| 510 | if (cookiePermission != nsICookiePermission::ACCESS_DENY) { |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | *aRejectedReason = |
| 515 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION; |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | int32_t behavior = CookiesBehavior(document); |
| 520 | if (behavior == nsICookieService::BEHAVIOR_ACCEPT) { |
| 521 | 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); |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | if (ContentBlockingAllowList::Check(aWindow)) { |
| 526 | return true; |
| 527 | } |
| 528 | |
| 529 | if (behavior == nsICookieService::BEHAVIOR_REJECT) { |
| 530 | 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); |
| 531 | *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_ALL; |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | // As a performance optimization, we only perform this check for |
| 536 | // BEHAVIOR_REJECT_FOREIGN and BEHAVIOR_LIMIT_FOREIGN. For |
| 537 | // BEHAVIOR_REJECT_TRACKER and |
| 538 | // BEHAVIOR_PARTITION_FOREIGN, third-partiness is |
| 539 | // implicily checked later below. |
| 540 | if (behavior != nsICookieService::BEHAVIOR_REJECT_TRACKER && |
| 541 | behavior != nsICookieService::BEHAVIOR_PARTITION_FOREIGN) { |
| 542 | // Let's check if this is a 3rd party context. |
| 543 | if (!AntiTrackingUtils::IsThirdPartyWindow(aWindow, aURI)) { |
| 544 | 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); |
| 545 | return true; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if (behavior == nsICookieService::BEHAVIOR_REJECT_FOREIGN || |
| 550 | behavior == nsICookieService::BEHAVIOR_LIMIT_FOREIGN) { |
| 551 | // XXX For non-cookie forms of storage, we handle BEHAVIOR_LIMIT_FOREIGN |
| 552 | // by simply rejecting the request to use the storage. In the future, if |
| 553 | // we change the meaning of BEHAVIOR_LIMIT_FOREIGN to be one which makes |
| 554 | // sense for non-cookie storage types, this may change. |
| 555 | 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); |
| 556 | *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN; |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | // The document has been allowlisted. We can return from here directly. |
| 561 | if (document->HasStorageAccessPermissionGrantedByAllowList()) { |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | 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" , 566); AnnotateMozCrashReason("MOZ_ASSERT" "(" "behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN" ")"); do { MOZ_CrashSequence(__null, 566); __attribute__((nomerge )) ::abort(); } while (false); } } while (false) |
| 566 | 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" , 566); AnnotateMozCrashReason("MOZ_ASSERT" "(" "behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN" ")"); do { MOZ_CrashSequence(__null, 566); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 567 | |
| 568 | uint32_t blockedReason = |
| 569 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER; |
| 570 | |
| 571 | if (behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER) { |
| 572 | if (!nsContentUtils::IsThirdPartyTrackingResourceWindow(aWindow)) { |
| 573 | 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); |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | nsCOMPtr<nsIClassifiedChannel> classifiedChannel = |
| 578 | do_QueryInterface(document->GetChannel()); |
| 579 | if (classifiedChannel) { |
| 580 | uint32_t classificationFlags = |
| 581 | classifiedChannel->GetThirdPartyClassificationFlags(); |
| 582 | if (classificationFlags & nsIClassifiedChannel::ClassificationFlags:: |
| 583 | CLASSIFIED_SOCIALTRACKING) { |
| 584 | blockedReason = |
| 585 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_SOCIALTRACKER; |
| 586 | } |
| 587 | } |
| 588 | } else if (behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN) { |
| 589 | if (nsContentUtils::IsThirdPartyTrackingResourceWindow(aWindow) || |
| 590 | AntiTrackingUtils::IsThirdPartyWindow(aWindow, aURI)) { |
| 591 | 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); |
| 592 | // fall through, but remember that we're partitioning. |
| 593 | blockedReason = nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN; |
| 594 | } else { |
| 595 | 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); |
| 596 | return true; |
| 597 | } |
| 598 | } else { |
| 599 | 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" , 601); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible " "here." ")"); do { MOZ_CrashSequence(__null, 601); __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false) |
| 600 | "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" , 601); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible " "here." ")"); do { MOZ_CrashSequence(__null, 601); __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false) |
| 601 | "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" , 601); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible " "here." ")"); do { MOZ_CrashSequence(__null, 601); __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false); |
| 602 | } |
| 603 | |
| 604 | Document* doc = aWindow->GetExtantDoc(); |
| 605 | // Make sure storage access isn't disabled |
| 606 | if (doc && (doc->StorageAccessSandboxed())) { |
| 607 | 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); |
| 608 | *aRejectedReason = blockedReason; |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | // "Storage access granted" only affects cookie access for third party |
| 613 | // documents. So if we are looking if we should allow access for cookies, |
| 614 | // then test if that permission is enabled on this document. |
| 615 | // Document::UsingStorageAccess first checks if storage access granted is |
| 616 | // cached in the inner window, if no, it then checks the storage permission |
| 617 | // flag in the channel's loadinfo |
| 618 | bool allowed = aCookies && document->UsingStorageAccess(); |
| 619 | |
| 620 | if (!allowed) { |
| 621 | *aRejectedReason = blockedReason; |
| 622 | } else { |
| 623 | if (MOZ_LOG_TEST(gAntiTrackingLog, mozilla::LogLevel::Debug)(__builtin_expect(!!(mozilla::detail::log_test(gAntiTrackingLog , mozilla::LogLevel::Debug)), 0)) && |
| 624 | aWindow->UsingStorageAccess()) { |
| 625 | 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); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return allowed; |
| 630 | } |
| 631 | |
| 632 | bool ShouldAllowAccessFor(nsIChannel* aChannel, nsIURI* aURI, |
| 633 | uint32_t* aRejectedReason) { |
| 634 | 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" , 634); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aURI" ")"); do { MOZ_CrashSequence(__null, 634); __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
| 635 | 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" , 635); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aChannel" ")" ); do { MOZ_CrashSequence(__null, 635); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 636 | |
| 637 | // Let's avoid a null check on aRejectedReason everywhere else. |
| 638 | uint32_t rejectedReason = 0; |
| 639 | if (!aRejectedReason) { |
| 640 | aRejectedReason = &rejectedReason; |
| 641 | } |
| 642 | |
| 643 | nsIScriptSecurityManager* ssm = |
| 644 | nsScriptSecurityManager::GetScriptSecurityManager(); |
| 645 | 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" , 645); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ssm" ")"); do { MOZ_CrashSequence(__null, 645); __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
| 646 | |
| 647 | nsCOMPtr<nsIURI> channelURI; |
| 648 | nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(channelURI)); |
| 649 | if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) { |
| 650 | 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); |
| 651 | return true; |
| 652 | } |
| 653 | 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) |
| 654 | ("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) |
| 655 | 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); |
| 656 | |
| 657 | nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo(); |
| 658 | nsCOMPtr<nsICookieJarSettings> cookieJarSettings; |
| 659 | rv = loadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings)); |
| 660 | 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" , 660)) { |
| 661 | 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) |
| 662 | ("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) |
| 663 | "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); |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | nsCOMPtr<nsIPrincipal> channelPrincipal; |
| 668 | rv = ssm->GetChannelURIPrincipal(aChannel, getter_AddRefs(channelPrincipal)); |
| 669 | 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" , 669)) { |
| 670 | 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); |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | uint32_t cookiePermission = detail::CheckCookiePermissionForPrincipal( |
| 675 | cookieJarSettings, channelPrincipal); |
| 676 | if (cookiePermission != nsICookiePermission::ACCESS_DEFAULT) { |
| 677 | 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) |
| 678 | ("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) |
| 679 | "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) |
| 680 | 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) |
| 681 | 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) |
| 682 | : "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); |
| 683 | if (cookiePermission != nsICookiePermission::ACCESS_DENY) { |
| 684 | return true; |
| 685 | } |
| 686 | |
| 687 | *aRejectedReason = |
| 688 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION; |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | if (!channelURI) { |
| 693 | 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); |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | int32_t behavior = CookiesBehavior(loadInfo, channelURI); |
| 698 | if (behavior == nsICookieService::BEHAVIOR_ACCEPT) { |
| 699 | 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); |
| 700 | return true; |
| 701 | } |
| 702 | |
| 703 | nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel); |
| 704 | |
| 705 | if (httpChannel && ContentBlockingAllowList::Check(httpChannel)) { |
| 706 | return true; |
| 707 | } |
| 708 | |
| 709 | if (behavior == nsICookieService::BEHAVIOR_REJECT) { |
| 710 | 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); |
| 711 | *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_ALL; |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil = |
| 716 | components::ThirdPartyUtil::Service(); |
| 717 | if (!thirdPartyUtil) { |
| 718 | 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); |
| 719 | return true; |
| 720 | } |
| 721 | |
| 722 | bool thirdParty = false; |
| 723 | rv = thirdPartyUtil->IsThirdPartyChannel(aChannel, aURI, &thirdParty); |
| 724 | // Grant if it's not a 3rd party. |
| 725 | // Be careful to check the return value of IsThirdPartyChannel, since |
| 726 | // IsThirdPartyChannel() will fail if the channel's loading principal is the |
| 727 | // system principal... |
| 728 | if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && !thirdParty) { |
| 729 | 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); |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | if (behavior == nsICookieService::BEHAVIOR_REJECT_FOREIGN || |
| 734 | behavior == nsICookieService::BEHAVIOR_LIMIT_FOREIGN) { |
| 735 | // XXX For non-cookie forms of storage, we handle BEHAVIOR_LIMIT_FOREIGN |
| 736 | // by simply rejecting the request to use the storage. In the future, if |
| 737 | // we change the meaning of BEHAVIOR_LIMIT_FOREIGN to be one which makes |
| 738 | // sense for non-cookie storage types, this may change. |
| 739 | 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); |
| 740 | *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN; |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | // The channel has been allowlisted. We can return from here. |
| 745 | if (loadInfo->GetStoragePermission() == |
| 746 | nsILoadInfo::StoragePermissionAllowListed) { |
| 747 | return true; |
| 748 | } |
| 749 | |
| 750 | 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" , 751); AnnotateMozCrashReason("MOZ_ASSERT" "(" "behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN" ")"); do { MOZ_CrashSequence(__null, 751); __attribute__((nomerge )) ::abort(); } while (false); } } while (false) |
| 751 | 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" , 751); AnnotateMozCrashReason("MOZ_ASSERT" "(" "behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN" ")"); do { MOZ_CrashSequence(__null, 751); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 752 | |
| 753 | uint32_t blockedReason = |
| 754 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER; |
| 755 | |
| 756 | // Not a tracker. |
| 757 | nsCOMPtr<nsIClassifiedChannel> classifiedChannel = |
| 758 | do_QueryInterface(aChannel); |
| 759 | if (behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER) { |
| 760 | if (classifiedChannel) { |
| 761 | if (!classifiedChannel->IsThirdPartyTrackingResource()) { |
| 762 | 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); |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | uint32_t classificationFlags = |
| 767 | classifiedChannel->GetThirdPartyClassificationFlags(); |
| 768 | if (classificationFlags & nsIClassifiedChannel::ClassificationFlags:: |
| 769 | CLASSIFIED_SOCIALTRACKING) { |
| 770 | blockedReason = |
| 771 | nsIWebProgressListener::STATE_COOKIES_BLOCKED_SOCIALTRACKER; |
| 772 | } |
| 773 | } |
| 774 | } else if (behavior == nsICookieService::BEHAVIOR_PARTITION_FOREIGN) { |
| 775 | if ((classifiedChannel && |
| 776 | classifiedChannel->IsThirdPartyTrackingResource()) || |
| 777 | AntiTrackingUtils::IsThirdPartyChannel(aChannel)) { |
| 778 | 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); |
| 779 | // fall through but remember that we're partitioning. |
| 780 | blockedReason = nsIWebProgressListener::STATE_COOKIES_PARTITIONED_FOREIGN; |
| 781 | } else { |
| 782 | 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); |
| 783 | return true; |
| 784 | } |
| 785 | } else { |
| 786 | 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" , 788); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible " "here." ")"); do { MOZ_CrashSequence(__null, 788); __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false) |
| 787 | "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" , 788); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible " "here." ")"); do { MOZ_CrashSequence(__null, 788); __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false) |
| 788 | "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" , 788); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "This should be an exhaustive list of cookie behaviors possible " "here." ")"); do { MOZ_CrashSequence(__null, 788); __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false); |
| 789 | } |
| 790 | |
| 791 | RefPtr<BrowsingContext> targetBC; |
| 792 | rv = loadInfo->GetTargetBrowsingContext(getter_AddRefs(targetBC)); |
| 793 | 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" , 793)) { |
| 794 | 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); |
| 795 | return false; |
| 796 | } |
| 797 | |
| 798 | // If we cannot get the target browsing context from the loadInfo, the |
| 799 | // channel could be a fetch request from a worker scope. In this case, we |
| 800 | // get the target browsing context from the worker associated browsing |
| 801 | // context instead. |
| 802 | if (!targetBC) { |
| 803 | rv = loadInfo->GetAssociatedBrowsingContext(getter_AddRefs(targetBC)); |
Value stored to 'rv' is never read | |
| 804 | } |
| 805 | |
| 806 | // We could have no target BC for the channel if it's for loading the script |
| 807 | // for remote workers, i.e. shared workers and service workers. In this |
| 808 | // case, we also don't have document, so we can skip the sandbox and the |
| 809 | // document check. |
| 810 | if (!targetBC) { |
| 811 | 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); |
| 812 | } |
| 813 | |
| 814 | if (targetBC && |
| 815 | Document::StorageAccessSandboxed(targetBC->GetSandboxFlags())) { |
| 816 | 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); |
| 817 | *aRejectedReason = blockedReason; |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | // Let's see if we have to grant the access for this particular channel. |
| 822 | |
| 823 | // UsingStorageAccess only applies to channels that load |
| 824 | // documents, for sub-resources loads, just returns the result from |
| 825 | // loadInfo. |
| 826 | bool isDocument = false; |
| 827 | aChannel->GetIsDocument(&isDocument); |
| 828 | |
| 829 | if (targetBC && isDocument) { |
| 830 | nsCOMPtr<nsPIDOMWindowInner> inner = |
| 831 | AntiTrackingUtils::GetInnerWindow(targetBC); |
| 832 | if (inner && inner->UsingStorageAccess()) { |
| 833 | 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); |
| 834 | return true; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | nsILoadInfo::StoragePermissionState storageAccess = |
| 839 | loadInfo->GetStoragePermission(); |
| 840 | bool allowed = storageAccess == nsILoadInfo::HasStoragePermission || |
| 841 | storageAccess == nsILoadInfo::StoragePermissionAllowListed; |
| 842 | if (!allowed) { |
| 843 | *aRejectedReason = blockedReason; |
| 844 | } |
| 845 | |
| 846 | return allowed; |
| 847 | } |
| 848 | |
| 849 | bool ShouldAllowAccessFor(nsIPrincipal* aPrincipal, |
| 850 | nsICookieJarSettings* aCookieJarSettings) { |
| 851 | 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" , 851); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrincipal" ")" ); do { MOZ_CrashSequence(__null, 851); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 852 | 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" , 852); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aCookieJarSettings" ")"); do { MOZ_CrashSequence(__null, 852); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
| 853 | |
| 854 | uint32_t access = |
| 855 | detail::CheckCookiePermissionForPrincipal(aCookieJarSettings, aPrincipal); |
| 856 | |
| 857 | if (access != nsICookiePermission::ACCESS_DEFAULT) { |
| 858 | return access != nsICookiePermission::ACCESS_DENY; |
| 859 | } |
| 860 | |
| 861 | int32_t behavior = CookiesBehavior(aPrincipal, aCookieJarSettings); |
| 862 | return behavior != nsICookieService::BEHAVIOR_REJECT; |
| 863 | } |
| 864 | |
| 865 | } // namespace mozilla |