Skip to content

Commit 7625948

Browse files
committed
Bug 1968644 - Part 3: Populate the triggeringFirstPartyClassificationFlags and triggeringThirdPartyClassificationFlags for fetch requests. r=valentin,smaug
Differential Revision: https://phabricator.services.mozilla.com/D252444
1 parent 2557f2f commit 7625948

File tree

10 files changed

+95
-30
lines changed

10 files changed

+95
-30
lines changed

dom/base/Document.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4488,9 +4488,10 @@ nsresult Document::Dispatch(already_AddRefed<nsIRunnable>&& aRunnable) const {
44884488
}
44894489

44904490
void Document::NoteScriptTrackingStatus(const nsACString& aURL,
4491-
bool aIsTracking) {
4492-
if (aIsTracking) {
4493-
mTrackingScripts.Insert(aURL);
4491+
net::ClassificationFlags& aFlags) {
4492+
// If the script is not tracking, we don't need to do anything.
4493+
if (aFlags.firstPartyFlags || aFlags.thirdPartyFlags) {
4494+
mTrackingScripts.InsertOrUpdate(aURL, aFlags);
44944495
}
44954496
// Ideally, whether a given script is tracking or not should be consistent,
44964497
// but there is a race so that it is not, when loading real sites in debug
@@ -4503,7 +4504,27 @@ bool Document::IsScriptTracking(JSContext* aCx) const {
45034504
if (!JS::DescribeScriptedCaller(&filename, aCx)) {
45044505
return false;
45054506
}
4506-
return mTrackingScripts.Contains(nsDependentCString(filename.get()));
4507+
4508+
auto entry = mTrackingScripts.Lookup(nsDependentCString(filename.get()));
4509+
if (!entry) {
4510+
return false;
4511+
}
4512+
4513+
return net::UrlClassifierCommon::IsTrackingClassificationFlag(
4514+
entry.Data().thirdPartyFlags, IsInPrivateBrowsing());
4515+
}
4516+
4517+
net::ClassificationFlags Document::GetScriptTrackingFlags() const {
4518+
if (auto loc = JSCallingLocation::Get()) {
4519+
if (auto entry = mTrackingScripts.Lookup(loc.FileName())) {
4520+
return entry.Data();
4521+
}
4522+
}
4523+
4524+
// If the currently executing script is not a tracker, return the
4525+
// classification flags of the document.
4526+
4527+
return mClassificationFlags;
45074528
}
45084529

45094530
void Document::GetContentType(nsAString& aContentType) {

dom/base/Document.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include "nsHashKeys.h"
7272
#include "nsIChannel.h"
7373
#include "nsIChannelEventSink.h"
74+
#include "nsIClassifiedChannel.h"
7475
#include "nsID.h"
7576
#include "nsIDocumentViewer.h"
7677
#include "nsIInterfaceRequestor.h"
@@ -3888,11 +3889,17 @@ class Document : public nsINode,
38883889
// The URLs passed to this function should match what
38893890
// JS::DescribeScriptedCaller() returns, since this API is used to
38903891
// determine whether some code is being called from a tracking script.
3891-
void NoteScriptTrackingStatus(const nsACString& aURL, bool isTracking);
3892+
void NoteScriptTrackingStatus(const nsACString& aURL,
3893+
net::ClassificationFlags& aFlags);
38923894
// The JSContext passed to this method represents the context that we want to
38933895
// determine if it belongs to a tracker.
38943896
bool IsScriptTracking(JSContext* aCx) const;
38953897

3898+
// Acquires the script tracking flags for the currently executing script. If
3899+
// the currently executing script is not a tracker, it will return the
3900+
// classification flags of the document.
3901+
net::ClassificationFlags GetScriptTrackingFlags() const;
3902+
38963903
// ResizeObserver usage.
38973904
void AddResizeObserver(ResizeObserver& aObserver) {
38983905
MOZ_ASSERT(!mResizeObservers.Contains(&aObserver));
@@ -5299,10 +5306,10 @@ class Document : public nsINode,
52995306

53005307
RefPtr<nsCommandManager> mMidasCommandManager;
53015308

5302-
// The set of all the tracking script URLs. URLs are added to this set by
5309+
// The hashmap of all the tracking script URLs. URLs are added to this map by
53035310
// calling NoteScriptTrackingStatus(). Currently we assume that a URL not
5304-
// existing in the set means the corresponding script isn't a tracking script.
5305-
nsTHashSet<nsCString> mTrackingScripts;
5311+
// existing in the map means the corresponding script isn't a tracking script.
5312+
nsTHashMap<nsCStringHashKey, net::ClassificationFlags> mTrackingScripts;
53065313

53075314
// Pointer to our parser if we're currently in the process of being
53085315
// parsed into.

dom/fetch/Fetch.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mozilla/ipc/PBackgroundChild.h"
1616
#include "mozilla/ipc/PBackgroundSharedTypes.h"
1717
#include "mozilla/ipc/IPCStreamUtils.h"
18+
#include "nsIClassifiedChannel.h"
1819
#include "nsIGlobalObject.h"
1920

2021
#include "nsDOMString.h"
@@ -450,7 +451,8 @@ class MainThreadFetchRunnable : public Runnable {
450451
fetch = new FetchDriver(mRequest.clonePtr(), principal, loadGroup,
451452
workerPrivate->MainThreadEventTarget(),
452453
workerPrivate->CookieJarSettings(),
453-
workerPrivate->GetPerformanceStorage(), false);
454+
workerPrivate->GetPerformanceStorage(),
455+
net::ClassificationFlags({0, 0}));
454456
nsAutoCString spec;
455457
if (proxy->GetWorkerPrivate()->GetBaseURI()) {
456458
proxy->GetWorkerPrivate()->GetBaseURI()->GetAsciiSpec(spec);
@@ -560,7 +562,7 @@ already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
560562
nsCOMPtr<nsILoadGroup> loadGroup;
561563
nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
562564
nsIPrincipal* principal;
563-
bool isTrackingFetch = false;
565+
net::ClassificationFlags trackingFlags = {0, 0};
564566
if (window) {
565567
doc = window->GetExtantDoc();
566568
if (!doc) {
@@ -571,7 +573,7 @@ already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
571573
loadGroup = doc->GetDocumentLoadGroup();
572574
cookieJarSettings = doc->CookieJarSettings();
573575

574-
isTrackingFetch = doc->IsScriptTracking(cx);
576+
trackingFlags = doc->GetScriptTrackingFlags();
575577
} else {
576578
principal = aGlobal->PrincipalOrNull();
577579
if (NS_WARN_IF(!principal)) {
@@ -596,7 +598,7 @@ already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
596598
new FetchDriver(std::move(internalRequest), principal, loadGroup,
597599
aGlobal->SerialEventTarget(), cookieJarSettings,
598600
nullptr, // PerformanceStorage
599-
isTrackingFetch);
601+
trackingFlags);
600602
fetch->SetDocument(doc);
601603
resolver->SetLoadGroup(loadGroup);
602604
aRv = fetch->Fetch(signalImpl, resolver);
@@ -1694,7 +1696,7 @@ void FetchBody<Derived>::MaybeTeeReadableStreamBody(
16941696
return;
16951697
}
16961698

1697-
nsTArray<RefPtr<ReadableStream> > branches;
1699+
nsTArray<RefPtr<ReadableStream>> branches;
16981700
MOZ_KnownLive(mReadableStreamBody)->Tee(aCx, branches, aRv);
16991701
if (aRv.Failed()) {
17001702
return;

dom/fetch/FetchDriver.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ FetchDriver::FetchDriver(SafeRefPtr<InternalRequest> aRequest,
341341
nsIEventTarget* aMainThreadEventTarget,
342342
nsICookieJarSettings* aCookieJarSettings,
343343
PerformanceStorage* aPerformanceStorage,
344-
bool aIsTrackingFetch)
344+
net::ClassificationFlags aTrackingFlags)
345345
: mPrincipal(aPrincipal),
346346
mLoadGroup(aLoadGroup),
347347
mRequest(std::move(aRequest)),
@@ -350,7 +350,7 @@ FetchDriver::FetchDriver(SafeRefPtr<InternalRequest> aRequest,
350350
mCookieJarSettings(aCookieJarSettings),
351351
mPerformanceStorage(aPerformanceStorage),
352352
mNeedToObserveOnDataAvailable(false),
353-
mIsTrackingFetch(aIsTrackingFetch),
353+
mTrackingFlags(aTrackingFlags),
354354
mIsOn3PCBExceptionList(false),
355355
mOnStopRequestCalled(false)
356356
#ifdef DEBUG
@@ -364,6 +364,9 @@ FetchDriver::FetchDriver(SafeRefPtr<InternalRequest> aRequest,
364364
MOZ_ASSERT(mRequest);
365365
MOZ_ASSERT(aPrincipal);
366366
MOZ_ASSERT(aMainThreadEventTarget);
367+
368+
mIsTrackingFetch = net::UrlClassifierCommon::IsTrackingClassificationFlag(
369+
mTrackingFlags.thirdPartyFlags, false);
367370
}
368371

369372
FetchDriver::~FetchDriver() {
@@ -693,6 +696,14 @@ nsresult FetchDriver::HttpFetch(
693696
NS_ENSURE_SUCCESS(rv, rv);
694697
}
695698

699+
nsCOMPtr<nsILoadInfo> loadInfo = chan->LoadInfo();
700+
rv = loadInfo->SetTriggeringFirstPartyClassificationFlags(
701+
mTrackingFlags.firstPartyFlags);
702+
NS_ENSURE_SUCCESS(rv, rv);
703+
rv = loadInfo->SetTriggeringThirdPartyClassificationFlags(
704+
mTrackingFlags.thirdPartyFlags);
705+
NS_ENSURE_SUCCESS(rv, rv);
706+
696707
// If the fetch is created by FetchEvent.request or NavigationPreload request,
697708
// corresponding InterceptedHttpChannel information need to propagate to the
698709
// channel of the fetch.

dom/fetch/FetchDriver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define mozilla_dom_FetchDriver_h
99

1010
#include "nsIChannelEventSink.h"
11+
#include "nsIClassifiedChannel.h"
1112
#include "nsIInterfaceRequestor.h"
1213
#include "nsINetworkInterceptController.h"
1314
#include "nsIStreamListener.h"
@@ -112,7 +113,8 @@ class FetchDriver final : public nsIChannelEventSink,
112113
FetchDriver(SafeRefPtr<InternalRequest> aRequest, nsIPrincipal* aPrincipal,
113114
nsILoadGroup* aLoadGroup, nsIEventTarget* aMainThreadEventTarget,
114115
nsICookieJarSettings* aCookieJarSettings,
115-
PerformanceStorage* aPerformanceStorage, bool aIsTrackingFetch);
116+
PerformanceStorage* aPerformanceStorage,
117+
net::ClassificationFlags aTrackingFlags);
116118

117119
nsresult Fetch(AbortSignalImpl* aSignalImpl, FetchDriverObserver* aObserver);
118120

@@ -191,6 +193,7 @@ class FetchDriver final : public nsIChannelEventSink,
191193
bool mNeedToObserveOnDataAvailable;
192194

193195
bool mIsTrackingFetch;
196+
net::ClassificationFlags mTrackingFlags;
194197

195198
// Indicates whether the fetch request is from a third-party context.
196199
Maybe<bool> mIsThirdPartyContext;

dom/fetch/FetchService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ RefPtr<FetchServicePromises> FetchService::FetchInstance::Fetch() {
265265
// tracking fetch to false, but for Keepalive
266266
// requests from main thread this needs to be
267267
// changed. See Bug 1892406
268-
false // IsTrackingFetch
268+
net::ClassificationFlags({0, 0}) // TrackingFlags
269269
);
270270

271271
if (mArgsType == FetchArgsType::WorkerFetch) {

dom/script/ScriptLoadContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ScriptLoadContext::ScriptLoadContext(
6161
mIsNonAsyncScriptInserted(false),
6262
mIsXSLT(false),
6363
mInCompilingList(false),
64-
mIsTracking(false),
64+
mClassificationFlags({0, 0}),
6565
mWasCompiledOMT(false),
6666
mLineNo(1),
6767
mColumnNo(0),

dom/script/ScriptLoadContext.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "mozilla/Maybe.h"
2727
#include "mozilla/MaybeOneOf.h"
2828
#include "mozilla/Mutex.h"
29+
#include "mozilla/net/UrlClassifierCommon.h"
2930
#include "mozilla/PreloaderBase.h"
3031
#include "mozilla/RefPtr.h"
3132
#include "mozilla/StaticPrefs_dom.h"
@@ -35,6 +36,7 @@
3536
#include "mozilla/Vector.h"
3637
#include "nsCOMPtr.h"
3738
#include "nsCycleCollectionParticipant.h"
39+
#include "nsIClassifiedChannel.h"
3840
#include "nsIScriptElement.h"
3941

4042
class nsICacheInfoChannel;
@@ -151,10 +153,12 @@ class ScriptLoadContext : public JS::loader::LoadContextBase,
151153

152154
bool CompileStarted() const;
153155

154-
bool IsTracking() const { return mIsTracking; }
155-
void SetIsTracking() {
156-
MOZ_ASSERT(!mIsTracking);
157-
mIsTracking = true;
156+
net::ClassificationFlags& GetClassificationFlags() {
157+
return mClassificationFlags;
158+
}
159+
void SetClassificationFlags(
160+
const net::ClassificationFlags& aClassificationFlags) {
161+
mClassificationFlags = aClassificationFlags;
158162
}
159163

160164
void BlockOnload(Document* aDocument);
@@ -290,11 +294,11 @@ class ScriptLoadContext : public JS::loader::LoadContextBase,
290294
bool mIsNonAsyncScriptInserted; // True if we live in
291295
// mNonAsyncExternalScriptInsertedRequests
292296
bool mIsXSLT; // True if we live in mXSLTRequests.
293-
bool mInCompilingList; // True if we are in mOffThreadCompilingRequests.
294-
bool mIsTracking; // True if the script comes from a source on our
295-
// tracking protection list.
296-
bool mWasCompiledOMT; // True if the script has been compiled off main
297-
// thread.
297+
bool mInCompilingList; // True if we are in mOffThreadCompilingRequests.
298+
net::ClassificationFlags // Classification flags
299+
mClassificationFlags; // of the source of the script.
300+
bool mWasCompiledOMT; // True if the script has been compiled off main
301+
// thread.
298302

299303
// Task that performs off-thread compilation or off-thread decode.
300304
// This field is used to take the result of the task, or cancel the task.

dom/script/ScriptLoader.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,11 @@ nsresult ScriptLoader::StartLoadInternal(
10011001

10021002
LOG(("ScriptLoadRequest (%p): mode=%u tracking=%d", aRequest,
10031003
unsigned(aRequest->GetScriptLoadContext()->mScriptMode),
1004-
aRequest->GetScriptLoadContext()->IsTracking()));
1004+
net::UrlClassifierCommon::IsTrackingClassificationFlag(
1005+
aRequest->GetScriptLoadContext()
1006+
->GetClassificationFlags()
1007+
.thirdPartyFlags,
1008+
NS_UsePrivateBrowsing(channel))));
10051009

10061010
PrepareRequestPriorityAndRequestDependencies(channel, aRequest);
10071011

@@ -2593,7 +2597,8 @@ nsresult ScriptLoader::FillCompileOptionsForRequest(
25932597

25942598
if (mDocument) {
25952599
mDocument->NoteScriptTrackingStatus(
2596-
aRequest->mURL, aRequest->GetScriptLoadContext()->IsTracking());
2600+
aRequest->mURL,
2601+
aRequest->GetScriptLoadContext()->GetClassificationFlags());
25972602
}
25982603

25992604
const char* introductionType;
@@ -4392,7 +4397,10 @@ nsresult ScriptLoader::PrepareLoadedRequest(ScriptLoadRequest* aRequest,
43924397
MOZ_ASSERT(classifiedChannel);
43934398
if (classifiedChannel &&
43944399
classifiedChannel->IsThirdPartyTrackingResource()) {
4395-
aRequest->GetScriptLoadContext()->SetIsTracking();
4400+
net::ClassificationFlags flags{
4401+
classifiedChannel->GetFirstPartyClassificationFlags(),
4402+
classifiedChannel->GetThirdPartyClassificationFlags()};
4403+
aRequest->GetScriptLoadContext()->SetClassificationFlags(flags);
43964404
}
43974405
}
43984406

dom/xhr/XMLHttpRequestMainThread.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "nsStringStream.h"
6666
#include "nsIAuthPrompt.h"
6767
#include "nsIAuthPrompt2.h"
68+
#include "nsIClassifiedChannel.h"
6869
#include "nsIClassOfService.h"
6970
#include "nsIHttpChannel.h"
7071
#include "nsISupportsPriority.h"
@@ -2636,6 +2637,14 @@ nsresult XMLHttpRequestMainThread::CreateChannel() {
26362637
NS_ENSURE_SUCCESS(rv, rv);
26372638
}
26382639

2640+
if (nsCOMPtr<Document> doc = GetDocumentIfCurrent()) {
2641+
net::ClassificationFlags flags = doc->GetScriptTrackingFlags();
2642+
nsCOMPtr<nsILoadInfo> loadInfo = mChannel->LoadInfo();
2643+
2644+
loadInfo->SetTriggeringFirstPartyClassificationFlags(flags.firstPartyFlags);
2645+
loadInfo->SetTriggeringThirdPartyClassificationFlags(flags.thirdPartyFlags);
2646+
}
2647+
26392648
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(mChannel));
26402649
if (httpChannel) {
26412650
rv = httpChannel->SetRequestMethod(mRequestMethod);

0 commit comments

Comments
 (0)