@whotracksme/reporting 5.1.26 → 5.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,44 +128,48 @@ describe('RequestReporter', function () {
128
128
 
129
129
  context('0001-empty-page', function () {
130
130
  it('detects no 3rd parties', async function () {
131
- await playScenario(chrome, {
131
+ const { seenTabIds } = await playScenario(chrome, {
132
132
  scenarioName: this.test.parent.title,
133
133
  scenarioRelease: '2024-08-02',
134
134
  });
135
135
  await clock.runToLast();
136
136
  expect(
137
- reporter.webRequestPipeline.pageStore.tabs.countNonExpiredKeys(),
138
- ).to.be.equal(1);
139
- const [tab] = reporter.webRequestPipeline.pageStore.tabs.values();
137
+ reporter.webRequestPipeline.pageStore.checkIfEmpty(),
138
+ ).to.be.false;
139
+ expect(seenTabIds).to.have.property('size', 1);
140
+ const tabId = seenTabIds.values().next().value;
141
+ const tab = reporter.webRequestPipeline.pageStore.getPageForRequest({ tabId, frameId: 0 });
140
142
  expect(tab.requestStats).to.be.empty;
141
143
  });
142
144
  });
143
145
 
144
146
  context('0002-3rd-party', function () {
145
147
  it('detects 3rd parties', async function () {
146
- await playScenario(chrome, {
148
+ const { seenTabIds } = await playScenario(chrome, {
147
149
  scenarioName: this.test.parent.title,
148
150
  scenarioRelease: '2024-08-02',
149
151
  });
150
152
  await clock.runToLast();
151
153
  expect(
152
- reporter.webRequestPipeline.pageStore.tabs.countNonExpiredKeys(),
153
- ).to.be.equal(1);
154
- const [tab] = reporter.webRequestPipeline.pageStore.tabs.values();
154
+ reporter.webRequestPipeline.pageStore.checkIfEmpty(),
155
+ ).to.be.false;
156
+ expect(seenTabIds).to.have.property('size', 1);
157
+ const tabId = seenTabIds.values().next().value;
158
+ const tab = reporter.webRequestPipeline.pageStore.getPageForRequest({ tabId, frameId: 0 });
155
159
  expect(tab.requestStats).to.have.keys([
156
160
  'script.localhost',
157
161
  ]);
158
162
  });
159
163
 
160
164
  it('reports 3rd parties', async function () {
161
- await playScenario(chrome, {
165
+ const { seenTabIds } = await playScenario(chrome, {
162
166
  scenarioName: this.test.parent.title,
163
167
  scenarioRelease: '2024-08-02',
164
168
  });
165
169
  await clock.runToLast();
166
170
  const eventPromise = new Promise((resolve) => communicationEmiter.once('send', resolve));
167
171
  // force stage all pages
168
- reporter.webRequestPipeline.pageStore.tabs.forEach((page) => reporter.webRequestPipeline.pageStore.stagePage(page));
172
+ seenTabIds.forEach(tabId => chrome.tabs.onRemoved.dispatch(tabId));
169
173
  await clock.runToLast();
170
174
  const event = await eventPromise;
171
175
  expect(event).to.deep.include({
@@ -198,14 +202,14 @@ describe('RequestReporter', function () {
198
202
 
199
203
  context('0005-preload', function () {
200
204
  it('reports 3rd parties', async function () {
201
- await playScenario(chrome, {
205
+ const { seenTabIds } = await playScenario(chrome, {
202
206
  scenarioName: this.test.parent.title,
203
207
  scenarioRelease: '2024-08-02',
204
208
  });
205
209
  await clock.runToLast();
206
210
  const eventPromise = new Promise((resolve) => communicationEmiter.once('send', resolve));
207
211
  // force stage all pages
208
- reporter.webRequestPipeline.pageStore.tabs.forEach((page) => reporter.webRequestPipeline.pageStore.stagePage(page));
212
+ seenTabIds.forEach(tabId => chrome.tabs.onRemoved.dispatch(tabId));
209
213
  await clock.runToLast();
210
214
  const event = await eventPromise;
211
215
  expect(event).to.deep.include({
@@ -220,7 +224,7 @@ describe('RequestReporter', function () {
220
224
  context('0008-navigation', function () {
221
225
  it('reports 3rd parties', async function () {
222
226
  const eventPromise1 = new Promise((resolve) => communicationEmiter.once('send', resolve));
223
- await playScenario(chrome, {
227
+ const { seenTabIds } = await playScenario(chrome, {
224
228
  scenarioName: this.test.parent.title,
225
229
  scenarioRelease: '2024-08-02-2',
226
230
  });
@@ -234,7 +238,7 @@ describe('RequestReporter', function () {
234
238
  ]);
235
239
  const eventPromise2 = new Promise((resolve) => communicationEmiter.once('send', resolve));
236
240
  // force stage all pages
237
- reporter.webRequestPipeline.pageStore.tabs.forEach((page) => reporter.webRequestPipeline.pageStore.stagePage(page));
241
+ seenTabIds.forEach(tabId => chrome.tabs.onRemoved.dispatch(tabId));
238
242
  await clock.runToLast();
239
243
  const event2 = await eventPromise2;
240
244
  expect(event2).to.deep.include({
@@ -36,7 +36,7 @@ describe('WebRequestPipeline', function () {
36
36
  it('starts empty', function () {
37
37
  const pipeline = new WebRequestPipeline();
38
38
  pipeline.init();
39
- expect(pipeline.pageStore.tabs.countNonExpiredKeys()).to.be.equal(0);
39
+ expect(pipeline.pageStore.checkIfEmpty()).to.be.true;
40
40
  });
41
41
 
42
42
  context('on webRequest.onBeforeRequest', function () {
@@ -50,12 +50,13 @@ describe('WebRequestPipeline', function () {
50
50
  });
51
51
  const details = {
52
52
  tabId: 1,
53
+ frameId: 0,
53
54
  url: 'https://example.com',
54
55
  type: 'main_frame',
55
56
  };
56
57
  chrome.webRequest.onBeforeRequest.dispatch(details);
57
- expect(pipeline.pageStore.tabs.has(details.tabId)).to.be.true;
58
- expect(pipeline.pageStore.tabs.get(details.tabId)).to.deep.include({
58
+ const tab = pipeline.pageStore.getPageForRequest(details);
59
+ expect(tab).to.deep.include({
59
60
  url: details.url,
60
61
  });
61
62
  });
@@ -91,13 +92,17 @@ describe('WebRequestPipeline', function () {
91
92
 
92
93
  context('0001-quick-close', function () {
93
94
  it('runs without a crash', async function () {
94
- await playScenario(chrome, {
95
+ const { seenTabIds } = await playScenario(chrome, {
95
96
  scenarioName: '0001-quick-close',
96
97
  scenarioRelease: '2024-09-27',
97
98
  });
98
- expect(pipeline.pageStore.tabs.countNonExpiredKeys()).to.be.equal(1);
99
- const [tabId] = pipeline.pageStore.tabs.keys().toArray();
100
- const tab = pipeline.pageStore.tabs.get(tabId);
99
+ expect(pipeline.pageStore.checkIfEmpty()).to.be.false;
100
+ expect(seenTabIds).to.have.property('size', 1);
101
+ const tabId = seenTabIds.values().next().value;
102
+ const tab = pipeline.pageStore.getPageForRequest({
103
+ tabId,
104
+ frameId: 0,
105
+ });
101
106
  expect(tab).to.deep.include({
102
107
  url: 'https://ghosterysearch.com/',
103
108
  });
@@ -109,13 +114,17 @@ describe('WebRequestPipeline', function () {
109
114
 
110
115
  context('0002-quick-navigation', function () {
111
116
  it('runs without a crash', async function () {
112
- await playScenario(chrome, {
117
+ const { seenTabIds } = await playScenario(chrome, {
113
118
  scenarioName: '0002-quick-navigation',
114
119
  scenarioRelease: '2024-09-27',
115
120
  });
116
- expect(pipeline.pageStore.tabs.countNonExpiredKeys()).to.be.equal(1);
117
- const [tabId] = pipeline.pageStore.tabs.keys().toArray();
118
- const tab = pipeline.pageStore.tabs.get(tabId);
121
+ expect(pipeline.pageStore.checkIfEmpty()).to.be.false;
122
+ expect(seenTabIds).to.have.property('size', 1);
123
+ const tabId = seenTabIds.values().next().value;
124
+ const tab = pipeline.pageStore.getPageForRequest({
125
+ tabId,
126
+ frameId: 0,
127
+ });
119
128
  expect(tab).to.deep.include({
120
129
  url: 'https://www.facebook.com/login/?next=https%3A%2F%2Fwww.facebook.com%2F',
121
130
  });
@@ -127,13 +136,17 @@ describe('WebRequestPipeline', function () {
127
136
 
128
137
  context('0003-prefetch', function () {
129
138
  it('runs without a crash', async function () {
130
- await playScenario(chrome, {
139
+ const { seenTabIds } = await playScenario(chrome, {
131
140
  scenarioName: '0003-prefetch',
132
141
  scenarioRelease: '2024-09-27',
133
142
  });
134
- expect(pipeline.pageStore.tabs.countNonExpiredKeys()).to.be.equal(1);
135
- const [tabId] = pipeline.pageStore.tabs.keys().toArray();
136
- const tab = pipeline.pageStore.tabs.get(tabId);
143
+ expect(pipeline.pageStore.checkIfEmpty()).to.be.false;
144
+ expect(seenTabIds).to.have.property('size', 1);
145
+ const tabId = seenTabIds.values().next().value;
146
+ const tab = pipeline.pageStore.getPageForRequest({
147
+ tabId,
148
+ frameId: 0,
149
+ });
137
150
  expect(tab).to.deep.include({
138
151
  url: 'http://localhost:8080/',
139
152
  });
@@ -10,11 +10,10 @@
10
10
  */
11
11
 
12
12
  import chrome from 'sinon-chrome';
13
- import { mock } from 'sinon';
13
+ import { mock, match } from 'sinon';
14
14
  import { expect } from 'chai';
15
15
 
16
16
  import PageStore from '../../src/webrequest-pipeline/page-store.js';
17
- import { PAGE_LOADING_STATE } from '../../src/webrequest-pipeline/page.js';
18
17
 
19
18
  describe('PageStore', function () {
20
19
  before(function () {
@@ -25,6 +24,7 @@ describe('PageStore', function () {
25
24
  beforeEach(function () {
26
25
  chrome.flush();
27
26
  chrome.storage.session.get.yields({});
27
+ chrome.tabs.query.returns([]);
28
28
  });
29
29
 
30
30
  after(function () {
@@ -35,7 +35,7 @@ describe('PageStore', function () {
35
35
  it('starts with empty tabs', async function () {
36
36
  const store = new PageStore({});
37
37
  await store.init();
38
- expect(store.tabs.countNonExpiredKeys()).to.be.equal(0);
38
+ expect(store.checkIfEmpty()).to.be.true;
39
39
  });
40
40
 
41
41
  context('on chrome.tabs.onCreated', function () {
@@ -44,8 +44,10 @@ describe('PageStore', function () {
44
44
  await store.init();
45
45
  const tab = { id: 1 };
46
46
  chrome.tabs.onCreated.dispatch(tab);
47
- expect(store.tabs.has(tab.id)).to.be.true;
48
- expect(store.tabs.get(tab.id)).to.deep.include({
47
+
48
+ expect(
49
+ store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
50
+ ).to.deep.include({
49
51
  id: tab.id,
50
52
  });
51
53
  });
@@ -57,8 +59,9 @@ describe('PageStore', function () {
57
59
  await store.init();
58
60
  const tab = { id: 1 };
59
61
  chrome.tabs.onUpdated.dispatch(tab.id, {}, tab);
60
- expect(store.tabs.has(tab.id)).to.be.true;
61
- expect(store.tabs.get(tab.id)).to.deep.include({
62
+ expect(
63
+ store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
64
+ ).to.deep.include({
62
65
  id: tab.id,
63
66
  });
64
67
  });
@@ -68,13 +71,18 @@ describe('PageStore', function () {
68
71
  await store.init();
69
72
  const tab = { id: 1 };
70
73
  chrome.tabs.onCreated.dispatch(tab);
71
- expect(store.tabs.has(tab.id)).to.be.true;
72
- expect(store.tabs.get(tab.id)).to.deep.include({
74
+ expect(
75
+ store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
76
+ ).to.deep.include({
73
77
  id: tab.id,
74
78
  });
75
- expect(store.tabs.get(tab.id)).to.have.property('url', undefined);
79
+ expect(
80
+ store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
81
+ ).to.have.property('url', undefined);
76
82
  chrome.tabs.onUpdated.dispatch(tab.id, { url: 'about:blank' }, tab);
77
- expect(store.tabs.get(tab.id)).to.have.property('url', 'about:blank');
83
+ expect(
84
+ store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
85
+ ).to.have.property('url', 'about:blank');
78
86
  });
79
87
  });
80
88
 
@@ -84,8 +92,7 @@ describe('PageStore', function () {
84
92
  await store.init();
85
93
  const details = { tabId: 1, frameId: 0, url: 'about:blank' };
86
94
  chrome.webNavigation.onBeforeNavigate.dispatch(details);
87
- expect(store.tabs.has(details.tabId)).to.be.true;
88
- expect(store.tabs.get(details.tabId)).to.deep.include({
95
+ expect(store.getPageForRequest(details)).to.deep.include({
89
96
  id: details.tabId,
90
97
  url: details.url,
91
98
  });
@@ -103,13 +110,22 @@ describe('PageStore', function () {
103
110
  };
104
111
  chrome.webNavigation.onBeforeNavigate.dispatch(details);
105
112
  expect(listener).to.not.have.been.called;
106
- const page = store.tabs.get(details.tabId);
107
- page.state = PAGE_LOADING_STATE.COMPLETE;
113
+ const page = store.getPageForRequest(details);
114
+
115
+ // sets PAGE_LOADING_STATE.COMPLETE;
116
+ chrome.webNavigation.onCompleted.dispatch(details);
117
+
108
118
  chrome.webNavigation.onBeforeNavigate.dispatch({
109
119
  ...details,
110
120
  timeStamp: details.timeStamp + 300,
111
121
  });
112
- expect(listener).to.have.been.calledWith(page);
122
+ expect(listener).to.have.been.calledWith(
123
+ match({
124
+ id: page.id,
125
+ url: details.url,
126
+ created: details.timeStamp,
127
+ }),
128
+ );
113
129
  });
114
130
 
115
131
  it('ignore duplicates', async function () {
@@ -124,7 +140,10 @@ describe('PageStore', function () {
124
140
  };
125
141
  chrome.webNavigation.onBeforeNavigate.dispatch(details);
126
142
  expect(listener).to.not.have.been.called;
127
- store.tabs.get(details.tabId).state = PAGE_LOADING_STATE.COMPLETE;
143
+
144
+ // sets PAGE_LOADING_STATE.COMPLETE;
145
+ chrome.webNavigation.onCompleted.dispatch(details);
146
+
128
147
  chrome.webNavigation.onBeforeNavigate.dispatch({
129
148
  ...details,
130
149
  timeStamp: details.timeStamp + 1,
@@ -1,24 +0,0 @@
1
- /**
2
- * WhoTracks.Me
3
- * https://whotracks.me/
4
- *
5
- * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
- *
7
- * This Source Code Form is subject to the terms of the Mozilla Public
8
- * License, v. 2.0. If a copy of the MPL was not distributed with this
9
- * file, You can obtain one at http://mozilla.org/MPL/2.0
10
- */
11
-
12
- /**
13
- * Extacts values from the DNT Tk header, given a list of response headers for a request
14
- * https://www.w3.org/TR/tracking-dnt/#dfn-tk
15
- * @param responseHeaders
16
- */
17
- export default function getTrackingStatus(state) {
18
- const status = state.getResponseHeader('tk');
19
- if (status) {
20
- const [value, statusId] = status.split(';');
21
- return { value, statusId };
22
- }
23
- return null;
24
- }
@@ -1,117 +0,0 @@
1
- /**
2
- * WhoTracks.Me
3
- * https://whotracks.me/
4
- *
5
- * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
- *
7
- * This Source Code Form is subject to the terms of the Mozilla Public
8
- * License, v. 2.0. If a copy of the MPL was not distributed with this
9
- * file, You can obtain one at http://mozilla.org/MPL/2.0
10
- */
11
-
12
- export const PAGE_LOADING_STATE = {
13
- CREATED: 'created',
14
- NAVIGATING: 'navigating',
15
- COMMITTED: 'committed',
16
- COMPLETE: 'complete',
17
- };
18
-
19
- export function create({ id, active, url, incognito, created }) {
20
- const page = {};
21
- page.id = id || 0;
22
- page.url = url;
23
- page.isRedirect = false;
24
- page.isPrivate = incognito;
25
- page.isPrivateServer = false;
26
- page.created = created || Date.now();
27
- page.destroyed = null;
28
- page.lastRequestId = null;
29
- page.frames = {
30
- 0: {
31
- parentFrameId: -1,
32
- url,
33
- },
34
- };
35
- page.state = PAGE_LOADING_STATE.CREATED;
36
-
37
- page.activeTime = 0;
38
- page.activeFrom = active ? Date.now() : 0;
39
-
40
- page.requestStats = {};
41
- page.annotations = {};
42
- page.counter = 0;
43
-
44
- page.tsv = '';
45
- page.tsvId = undefined;
46
- return page;
47
- }
48
-
49
- export function setActive(page, active) {
50
- if (active && page.activeFrom === 0) {
51
- page.activeFrom = Date.now();
52
- } else if (!active && page.activeFrom > 0) {
53
- page.activeTime += Date.now() - page.activeFrom;
54
- page.activeFrom = 0;
55
- }
56
- }
57
-
58
- export function getStatsForDomain(page, domain) {
59
- let stats = page.requestStats[domain];
60
- if (!stats) {
61
- stats = {};
62
- page.requestStats[domain] = stats;
63
- }
64
- return stats;
65
- }
66
-
67
- export function getFrameAncestors(page, { parentFrameId }) {
68
- const ancestors = [];
69
-
70
- // Reconstruct frame ancestors
71
- let currentFrameId = parentFrameId;
72
- while (currentFrameId !== -1) {
73
- const frame = page.frames[currentFrameId];
74
-
75
- // If `frame` if undefined, this means we do not have any information
76
- // about the frame associated with `currentFrameId`. This can happen if
77
- // the event for `main_frame` or `sub_frame` was not emitted from the
78
- // webRequest API for this frame; this can happen when Service Workers
79
- // are used. In this case, we consider that the parent frame is the main
80
- // frame (which is very likely the case).
81
- if (frame === undefined) {
82
- ancestors.push({
83
- frameId: 0,
84
- url: page.url,
85
- });
86
- break;
87
- }
88
-
89
- // Continue going up the ancestors chain
90
- ancestors.push({
91
- frameId: currentFrameId,
92
- url: frame.url,
93
- });
94
- currentFrameId = frame.parentFrameId;
95
- }
96
-
97
- return ancestors;
98
- }
99
-
100
- /**
101
- * Return the URL of the frame.
102
- */
103
- export function getFrameUrl(page, context) {
104
- const { frameId } = context;
105
-
106
- const frame = page.frames[frameId];
107
-
108
- // In some cases, frame creation does not trigger a webRequest event (e.g.:
109
- // if the iframe is specified in the HTML of the page directly). In this
110
- // case we try to fall-back to something else: documentUrl, originUrl,
111
- // initiator.
112
- if (frame === undefined) {
113
- return context.documentUrl || context.originUrl || context.initiator;
114
- }
115
-
116
- return frame.url;
117
- }