From c52ca9c7d5c56764e695667356eced96ae9e1a64 Mon Sep 17 00:00:00 2001 From: Dimi Lee Date: Thu, 23 Apr 2020 14:24:56 +0000 Subject: [PATCH] Bug 1616775 - P1. Add IsThirdPartyWindow and IsThirdPartyTrackingResourceWindow fields to WindowContext. r=timhuang,baku,farre We have to add "IsThirdPartyWindow" in WindowContext because we need to know if a BrowsingContext is third-party (The browsing context may be not in-process). Differential Revision: https://phabricator.services.mozilla.com/D71010 --- docshell/base/WindowContext.cpp | 12 ++++++++++++ docshell/base/WindowContext.h | 14 +++++++++++++- dom/base/nsGlobalWindowOuter.cpp | 15 ++++++++++++++- .../components/antitracking/ContentBlocking.cpp | 8 ++------ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/docshell/base/WindowContext.cpp b/docshell/base/WindowContext.cpp index d86243d0e3c6..24b35ffb26ed 100644 --- a/docshell/base/WindowContext.cpp +++ b/docshell/base/WindowContext.cpp @@ -61,6 +61,18 @@ void WindowContext::SendCommitTransaction(ContentChild* aChild, aChild->SendCommitWindowContextTransaction(this, aTxn, aEpoch); } +bool WindowContext::CanSet(FieldIndex, + const bool& IsThirdPartyWindow, + ContentParent* aSource) { + return mBrowsingContext->CheckOnlyOwningProcessCanSet(aSource); +} + +bool WindowContext::CanSet(FieldIndex, + const bool& aIsThirdPartyTrackingResourceWindow, + ContentParent* aSource) { + return mBrowsingContext->CheckOnlyOwningProcessCanSet(aSource); +} + already_AddRefed WindowContext::Create( WindowGlobalChild* aWindow) { MOZ_RELEASE_ASSERT(XRE_IsContentProcess(), diff --git a/docshell/base/WindowContext.h b/docshell/base/WindowContext.h index 52c0f371dd7a..b8abf0921166 100644 --- a/docshell/base/WindowContext.h +++ b/docshell/base/WindowContext.h @@ -17,7 +17,13 @@ namespace dom { #define MOZ_EACH_WC_FIELD(FIELD) \ FIELD(OuterWindowId, uint64_t) \ FIELD(CookieJarSettings, Maybe) \ - FIELD(HasStoragePermission, bool) + FIELD(HasStoragePermission, bool) \ + /* Whether the given window hierarchy is third party. See \ + * ThirdPartyUtil::IsThirdPartyWindow for details */ \ + FIELD(IsThirdPartyWindow, bool) \ + /* Whether this window's channel has been marked as a third-party \ + * tracking resource */ \ + FIELD(IsThirdPartyTrackingResourceWindow, bool) class WindowContext : public nsISupports, public nsWrapperCache { MOZ_DECL_SYNCED_CONTEXT(WindowContext, MOZ_EACH_WC_FIELD) @@ -89,6 +95,12 @@ class WindowContext : public nsISupports, public nsWrapperCache { return true; } + bool CanSet(FieldIndex, + const bool& IsThirdPartyWindow, ContentParent* aSource); + bool CanSet(FieldIndex, + const bool& aIsThirdPartyTrackingResourceWindow, + ContentParent* aSource); + // Overload `DidSet` to get notifications for a particular field being set. // // You can also overload the variant that gets the old value if you need it. diff --git a/dom/base/nsGlobalWindowOuter.cpp b/dom/base/nsGlobalWindowOuter.cpp index 1c01b9d57f7f..365ce751fb50 100644 --- a/dom/base/nsGlobalWindowOuter.cpp +++ b/dom/base/nsGlobalWindowOuter.cpp @@ -2507,6 +2507,9 @@ nsresult nsGlobalWindowOuter::SetNewDocument(Document* aDocument, ReportLargeAllocStatus(); mLargeAllocStatus = LargeAllocStatus::NONE; + bool isThirdPartyTrackingResourceWindow = + nsContentUtils::IsThirdPartyTrackingResourceWindow(newInnerWindow); + // Set the cookie jar settings to the window context. if (newInnerWindow) { net::CookieJarSettingsArgs cookieJarSettings; @@ -2516,6 +2519,16 @@ nsresult nsGlobalWindowOuter::SetNewDocument(Document* aDocument, newInnerWindow->GetWindowGlobalChild() ->WindowContext() ->SetCookieJarSettings(Some(cookieJarSettings)); + + newInnerWindow->GetWindowGlobalChild() + ->WindowContext() + ->SetIsThirdPartyWindow(nsContentUtils::IsThirdPartyWindowOrChannel( + newInnerWindow, nullptr, nullptr)); + + newInnerWindow->GetWindowGlobalChild() + ->WindowContext() + ->SetIsThirdPartyTrackingResourceWindow( + isThirdPartyTrackingResourceWindow); } mHasStorageAccess = false; @@ -2539,7 +2552,7 @@ nsresult nsGlobalWindowOuter::SetNewDocument(Document* aDocument, cookieBehavior == nsICookieService::BEHAVIOR_REJECT_TRACKER || cookieBehavior == nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN); - if (nsContentUtils::IsThirdPartyTrackingResourceWindow(newInnerWindow)) { + if (isThirdPartyTrackingResourceWindow) { checkStorageAccess = true; } } diff --git a/toolkit/components/antitracking/ContentBlocking.cpp b/toolkit/components/antitracking/ContentBlocking.cpp index 0b834fb9fae3..e63fed570bec 100644 --- a/toolkit/components/antitracking/ContentBlocking.cpp +++ b/toolkit/components/antitracking/ContentBlocking.cpp @@ -306,19 +306,15 @@ ContentBlocking::AllowAccessFor( } else { // We should be a 3rd party source. - // Make sure we are either a third-party tracker or a third-party - // window (depends upon the cookie bahavior). if (behavior == nsICookieService::BEHAVIOR_REJECT_TRACKER && - !nsContentUtils::IsThirdPartyTrackingResourceWindow( - parentInnerWindow)) { + !parentWindowContext->GetIsThirdPartyTrackingResourceWindow()) { LOG(("Our window isn't a third-party tracking window")); return StorageAccessGrantPromise::CreateAndReject(false, __func__); } else if ((CookieJarSettings::IsRejectThirdPartyWithExceptions(behavior) || behavior == nsICookieService:: BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN) && - !nsContentUtils::IsThirdPartyWindowOrChannel(parentInnerWindow, - nullptr, nullptr)) { + !parentWindowContext->GetIsThirdPartyWindow()) { LOG(("Our window isn't a third-party window")); return StorageAccessGrantPromise::CreateAndReject(false, __func__); }