@whotracksme/reporting 8.0.2 → 9.0.0
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.
- package/CHANGELOG.md +19 -0
- package/package.json +1 -1
- package/reporting/scenarios/README.md +11 -15
- package/reporting/snapshots/0001/snapshot.json +48 -24
- package/reporting/snapshots/0003/snapshot.json +22 -22
- package/reporting/snapshots/0005/snapshot.json +140 -124
- package/reporting/src/popularity-vote-sanitizer.js +59 -1
- package/reporting/src/request/index.js +14 -0
- package/reporting/src/request/page-store.js +217 -209
- package/reporting/src/request/utils/webrequest.js +0 -5
- package/reporting/test/helpers/scenarios.js +11 -0
- package/reporting/test/popularity-vote-sanitizer.spec.js +54 -12
- package/reporting/test/request/index.spec.js +29 -5
- package/reporting/test/request/index.test.js +107 -116
- package/reporting/test/request/page-store.test.js +251 -83
- package/reporting/test/request/test-pages.js +116 -0
- package/reporting/snapshots/0002/events.log.br +0 -0
- package/reporting/snapshots/0002/snapshot.json +0 -2511
- package/reporting/snapshots/0004/events.log.br +0 -0
- package/reporting/snapshots/0004/snapshot.json +0 -759
- package/reporting/snapshots/0006/events.log.br +0 -0
- package/reporting/snapshots/0006/snapshot.json +0 -5489
|
@@ -9,13 +9,18 @@
|
|
|
9
9
|
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import logger from '../logger.js';
|
|
12
13
|
import ChromeStorageMap from './utils/chrome-storage-map.js';
|
|
13
14
|
|
|
14
15
|
const PAGE_TTL = 1000 * 60 * 60; // 1 hour
|
|
15
16
|
|
|
17
|
+
const BFCACHE_TTL_MS = 10 * 60 * 1000;
|
|
18
|
+
// Min gap between sweeps driven by webNavigation.onCompleted, so a
|
|
19
|
+
// burst of navigations doesn't trigger N tabs.query/getAllFrames
|
|
20
|
+
// roundtrips back-to-back.
|
|
21
|
+
const FLUSH_THROTTLE_MS = 30 * 1000;
|
|
22
|
+
|
|
16
23
|
const PAGE_LOADING_STATE = {
|
|
17
|
-
CREATED: 'created',
|
|
18
|
-
NAVIGATING: 'navigating',
|
|
19
24
|
COMMITTED: 'committed',
|
|
20
25
|
COMPLETE: 'complete',
|
|
21
26
|
};
|
|
@@ -29,76 +34,80 @@ function makePageActive(page, active) {
|
|
|
29
34
|
}
|
|
30
35
|
}
|
|
31
36
|
|
|
32
|
-
function
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
function createPage({ tabId, documentId, url, isPrivate, active }) {
|
|
38
|
+
return {
|
|
39
|
+
id: tabId,
|
|
40
|
+
documentId,
|
|
41
|
+
documentIds: [documentId],
|
|
42
|
+
url,
|
|
43
|
+
isPrivate: !!isPrivate,
|
|
44
|
+
isPrivateServer: false,
|
|
45
|
+
created: Date.now(),
|
|
46
|
+
destroyed: null,
|
|
47
|
+
// Wall-clock instant at which the next sweep should stage this
|
|
48
|
+
// page. null while the page is live; set to `now + BFCACHE_TTL_MS`
|
|
49
|
+
// when a sweep first finds no live doc; 0 when the tab is
|
|
50
|
+
// closed/discarded; cleared on bfcache restore.
|
|
51
|
+
stageAfter: null,
|
|
52
|
+
state: PAGE_LOADING_STATE.COMMITTED,
|
|
53
|
+
activeTime: 0,
|
|
54
|
+
activeFrom: active ? Date.now() : 0,
|
|
55
|
+
requestStats: {},
|
|
56
|
+
annotations: {},
|
|
57
|
+
counter: 0,
|
|
46
58
|
};
|
|
47
|
-
page.state = PAGE_LOADING_STATE.CREATED;
|
|
48
|
-
|
|
49
|
-
page.activeTime = 0;
|
|
50
|
-
page.activeFrom = active ? Date.now() : 0;
|
|
51
|
-
|
|
52
|
-
page.requestStats = {};
|
|
53
|
-
page.annotations = {};
|
|
54
|
-
page.counter = 0;
|
|
55
|
-
page.previous = null;
|
|
56
|
-
return page;
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
export default class PageStore {
|
|
60
62
|
#notifyPageStageListeners;
|
|
63
|
+
// rootDocumentId -> page; persisted across SW restarts.
|
|
61
64
|
#pages;
|
|
65
|
+
// Any documentId (root or sub-frame) -> owning page. Rebuilt on init.
|
|
66
|
+
#documentIndex;
|
|
67
|
+
// tabId -> { isPrivate, active }; used at commit time so a new page
|
|
68
|
+
// inherits the current tab's flags.
|
|
69
|
+
#tabContext;
|
|
70
|
+
#lastFlush = 0;
|
|
62
71
|
|
|
63
72
|
constructor({ notifyPageStageListeners }) {
|
|
64
73
|
this.#pages = new ChromeStorageMap({
|
|
65
|
-
storageKey: 'wtm-
|
|
74
|
+
storageKey: 'wtm-request-reporting:page-store:pages',
|
|
66
75
|
ttlInMs: PAGE_TTL,
|
|
67
76
|
});
|
|
77
|
+
this.#documentIndex = new Map();
|
|
78
|
+
this.#tabContext = new Map();
|
|
68
79
|
this.#notifyPageStageListeners = notifyPageStageListeners;
|
|
69
80
|
}
|
|
70
81
|
|
|
71
82
|
async init() {
|
|
72
83
|
await this.#pages.isReady;
|
|
84
|
+
for (const page of this.#pages.values()) {
|
|
85
|
+
for (const docId of page.documentIds) {
|
|
86
|
+
this.#documentIndex.set(docId, page);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Seed tabContext before subscribing so a concurrent onCommitted
|
|
90
|
+
// can't create a page with default isPrivate/active flags.
|
|
91
|
+
for (const tab of await chrome.tabs.query({})) {
|
|
92
|
+
this.#onTabCreated(tab);
|
|
93
|
+
}
|
|
73
94
|
chrome.tabs.onCreated.addListener(this.#onTabCreated);
|
|
74
95
|
chrome.tabs.onUpdated.addListener(this.#onTabUpdated);
|
|
75
96
|
chrome.tabs.onRemoved.addListener(this.#onTabRemoved);
|
|
76
97
|
chrome.tabs.onActivated.addListener(this.#onTabActivated);
|
|
77
|
-
chrome.webNavigation.onBeforeNavigate.addListener(this.#onBeforeNavigate);
|
|
78
98
|
chrome.webNavigation.onCommitted.addListener(this.#onNavigationCommitted);
|
|
79
99
|
chrome.webNavigation.onCompleted.addListener(this.#onNavigationCompleted);
|
|
80
|
-
|
|
81
|
-
// Note: not available on Firefox Android
|
|
100
|
+
// Not available on Firefox Android.
|
|
82
101
|
chrome.windows?.onFocusChanged?.addListener(this.#onWindowFocusChanged);
|
|
83
|
-
|
|
84
|
-
// popupate initially open tabs
|
|
85
|
-
(await chrome.tabs.query({})).forEach((tab) => this.#onTabCreated(tab));
|
|
86
102
|
}
|
|
87
103
|
|
|
88
104
|
unload() {
|
|
89
|
-
this.#
|
|
90
|
-
|
|
91
|
-
this.#stagePage(page);
|
|
92
|
-
});
|
|
93
|
-
this.#pages.clear();
|
|
94
|
-
|
|
105
|
+
this.#documentIndex.clear();
|
|
106
|
+
this.#tabContext.clear();
|
|
95
107
|
chrome.tabs.onCreated.removeListener(this.#onTabCreated);
|
|
96
108
|
chrome.tabs.onUpdated.removeListener(this.#onTabUpdated);
|
|
97
109
|
chrome.tabs.onRemoved.removeListener(this.#onTabRemoved);
|
|
98
110
|
chrome.tabs.onActivated.removeListener(this.#onTabActivated);
|
|
99
|
-
chrome.webNavigation.onBeforeNavigate.removeListener(
|
|
100
|
-
this.#onBeforeNavigate,
|
|
101
|
-
);
|
|
102
111
|
chrome.webNavigation.onCommitted.removeListener(
|
|
103
112
|
this.#onNavigationCommitted,
|
|
104
113
|
);
|
|
@@ -109,214 +118,213 @@ export default class PageStore {
|
|
|
109
118
|
}
|
|
110
119
|
|
|
111
120
|
checkIfEmpty() {
|
|
112
|
-
// this
|
|
121
|
+
// this operation can potentially be expensive
|
|
113
122
|
return this.#pages.countNonExpiredKeys() === 0;
|
|
114
123
|
}
|
|
115
124
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
page
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
this.#notifyPageStageListeners(page);
|
|
125
|
+
// Test helper: returns the first stored page for `tabId`.
|
|
126
|
+
findPageForTab(tabId) {
|
|
127
|
+
for (const page of this.#pages.values()) {
|
|
128
|
+
if (page.id === tabId) return page;
|
|
129
|
+
}
|
|
130
|
+
return undefined;
|
|
123
131
|
}
|
|
124
132
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
133
|
+
// Stage pages whose tab is gone or whose docs have been absent
|
|
134
|
+
// from the live set for BFCACHE_TTL_MS.
|
|
135
|
+
async flush() {
|
|
136
|
+
const live = await this.#collectLiveDocumentIds();
|
|
137
|
+
const now = Date.now();
|
|
138
|
+
this.#lastFlush = now;
|
|
139
|
+
for (const page of this.#pages.values()) {
|
|
140
|
+
if (page.documentIds.some((d) => live.has(d))) {
|
|
141
|
+
if (page.stageAfter !== null) {
|
|
142
|
+
page.stageAfter = null;
|
|
143
|
+
this.#pages.set(page.documentId, page);
|
|
144
|
+
}
|
|
145
|
+
} else if (page.stageAfter === null) {
|
|
146
|
+
page.stageAfter = now + BFCACHE_TTL_MS;
|
|
147
|
+
this.#pages.set(page.documentId, page);
|
|
148
|
+
} else if (now >= page.stageAfter) {
|
|
149
|
+
this.#stagePage(page);
|
|
150
|
+
}
|
|
139
151
|
}
|
|
152
|
+
}
|
|
140
153
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
154
|
+
async #collectLiveDocumentIds() {
|
|
155
|
+
const live = new Set();
|
|
156
|
+
for (const tab of await chrome.tabs.query({})) {
|
|
157
|
+
try {
|
|
158
|
+
const frames = await chrome.webNavigation.getAllFrames({
|
|
159
|
+
tabId: tab.id,
|
|
160
|
+
});
|
|
161
|
+
for (const frame of frames) {
|
|
162
|
+
if (frame.documentId) live.add(frame.documentId);
|
|
163
|
+
}
|
|
164
|
+
} catch (e) {
|
|
165
|
+
// Tab can disappear between tabs.query and getAllFrames.
|
|
166
|
+
// Skip it — its docs will look gone, which is fine.
|
|
167
|
+
logger.debug('PageStore: getAllFrames failed for tab', tab.id, e);
|
|
168
|
+
}
|
|
146
169
|
}
|
|
170
|
+
return live;
|
|
171
|
+
}
|
|
147
172
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Remove tab context for `tabId`.
|
|
153
|
-
*/
|
|
154
|
-
#onTabRemoved = (tabId) => {
|
|
155
|
-
const page = this.#pages.get(tabId);
|
|
156
|
-
if (!page) {
|
|
157
|
-
return;
|
|
173
|
+
#stagePage(page) {
|
|
174
|
+
for (const docId of page.documentIds) {
|
|
175
|
+
this.#documentIndex.delete(docId);
|
|
158
176
|
}
|
|
159
|
-
|
|
160
|
-
|
|
177
|
+
this.#pages.delete(page.documentId);
|
|
178
|
+
const snapshot = structuredClone(page);
|
|
179
|
+
makePageActive(snapshot, false);
|
|
180
|
+
snapshot.destroyed = Date.now();
|
|
181
|
+
this.#notifyPageStageListeners(snapshot);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
#indexDocument(page, documentId) {
|
|
185
|
+
if (!page.documentIds.includes(documentId)) {
|
|
186
|
+
page.documentIds.push(documentId);
|
|
161
187
|
}
|
|
162
|
-
this.#
|
|
163
|
-
}
|
|
188
|
+
this.#documentIndex.set(documentId, page);
|
|
189
|
+
}
|
|
164
190
|
|
|
165
|
-
#
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
this.#pages.set(page.id, page);
|
|
191
|
+
#setTabActive(tabId, active) {
|
|
192
|
+
const ctx = this.#tabContext.get(tabId);
|
|
193
|
+
if (ctx) ctx.active = active;
|
|
194
|
+
for (const page of this.#pages.values()) {
|
|
195
|
+
if (page.id === tabId) {
|
|
196
|
+
makePageActive(page, active);
|
|
197
|
+
this.#pages.set(page.documentId, page);
|
|
173
198
|
}
|
|
174
|
-
} else if (this.#pages.has(previousTabId)) {
|
|
175
|
-
const previousPage = this.#pages.get(previousTabId);
|
|
176
|
-
makePageActive(previousPage, false);
|
|
177
|
-
this.#pages.set(previousPage.id, previousPage);
|
|
178
199
|
}
|
|
200
|
+
}
|
|
179
201
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
202
|
+
#onTabCreated = (tab) => {
|
|
203
|
+
this.#tabContext.set(tab.id, {
|
|
204
|
+
isPrivate: !!tab.incognito,
|
|
205
|
+
active: !!tab.active,
|
|
206
|
+
});
|
|
185
207
|
};
|
|
186
208
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
209
|
+
#onTabUpdated = (tabId, info, tab) => {
|
|
210
|
+
if (info.discarded) {
|
|
211
|
+
// Memory-discarded tab: docs are gone but onRemoved won't fire
|
|
212
|
+
// until the user closes the tab.
|
|
213
|
+
this.#markTabGone(tabId);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const ctx = {
|
|
217
|
+
isPrivate: !!tab.incognito,
|
|
218
|
+
active: !!tab.active,
|
|
219
|
+
};
|
|
220
|
+
this.#tabContext.set(tabId, ctx);
|
|
221
|
+
for (const page of this.#pages.values()) {
|
|
222
|
+
if (page.id === tabId) {
|
|
223
|
+
page.isPrivate = ctx.isPrivate;
|
|
224
|
+
makePageActive(page, ctx.active);
|
|
225
|
+
this.#pages.set(page.documentId, page);
|
|
194
226
|
}
|
|
195
|
-
makePageActive(page, windowId === focusedWindowId);
|
|
196
|
-
this.#pages.set(id, page);
|
|
197
227
|
}
|
|
198
228
|
};
|
|
199
229
|
|
|
200
|
-
#
|
|
201
|
-
|
|
230
|
+
#onTabRemoved = (tabId) => {
|
|
231
|
+
this.#tabContext.delete(tabId);
|
|
232
|
+
this.#markTabGone(tabId);
|
|
233
|
+
};
|
|
202
234
|
|
|
203
|
-
|
|
204
|
-
|
|
235
|
+
#markTabGone(tabId) {
|
|
236
|
+
for (const page of this.#pages.values()) {
|
|
237
|
+
if (page.id === tabId) {
|
|
238
|
+
page.stageAfter = 0;
|
|
239
|
+
this.#pages.set(page.documentId, page);
|
|
240
|
+
}
|
|
205
241
|
}
|
|
242
|
+
// Tab close is a definitive end-of-life signal; flush eagerly
|
|
243
|
+
// (no throttle) so the page emits before the SW can suspend.
|
|
244
|
+
this.flush().catch((e) =>
|
|
245
|
+
logger.debug('PageStore: flush after tab gone failed', e),
|
|
246
|
+
);
|
|
247
|
+
}
|
|
206
248
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
page.url === url &&
|
|
214
|
-
page.created + 200 > timeStamp
|
|
215
|
-
) {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
// We are starting a navigation to a new page - if the previous page is complete (i.e. fully
|
|
219
|
-
// loaded), stage it before we create the new page info.
|
|
220
|
-
if (page.state === PAGE_LOADING_STATE.COMPLETE) {
|
|
221
|
-
this.#stagePage(page);
|
|
249
|
+
#onTabActivated = ({ previousTabId, tabId }) => {
|
|
250
|
+
if (previousTabId) {
|
|
251
|
+
this.#setTabActive(previousTabId, false);
|
|
252
|
+
} else {
|
|
253
|
+
for (const otherTabId of this.#tabContext.keys()) {
|
|
254
|
+
if (otherTabId !== tabId) this.#setTabActive(otherTabId, false);
|
|
222
255
|
}
|
|
223
256
|
}
|
|
224
|
-
|
|
225
|
-
// create a new page for the navigation
|
|
226
|
-
this.#pages.delete(tabId);
|
|
227
|
-
|
|
228
|
-
const nextPage = createPageFromTab({
|
|
229
|
-
id: tabId,
|
|
230
|
-
active: false,
|
|
231
|
-
url,
|
|
232
|
-
incognito: page ? page.isPrivate : false,
|
|
233
|
-
created: timeStamp,
|
|
234
|
-
});
|
|
235
|
-
nextPage.previous = page;
|
|
236
|
-
nextPage.state = PAGE_LOADING_STATE.NAVIGATING;
|
|
237
|
-
this.#pages.set(tabId, nextPage);
|
|
257
|
+
this.#setTabActive(tabId, true);
|
|
238
258
|
};
|
|
239
259
|
|
|
240
|
-
#
|
|
241
|
-
const {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (!page) {
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
if (frameId === 0) {
|
|
249
|
-
page.state = PAGE_LOADING_STATE.COMMITTED;
|
|
250
|
-
this.#pages.set(tabId, page);
|
|
251
|
-
} else if (!page.frames[frameId]) {
|
|
252
|
-
// frame created without request
|
|
253
|
-
this.onSubFrame(details);
|
|
260
|
+
#onWindowFocusChanged = async (focusedWindowId) => {
|
|
261
|
+
for (const { id, windowId } of await chrome.tabs.query({ active: true })) {
|
|
262
|
+
this.#setTabActive(id, windowId === focusedWindowId);
|
|
254
263
|
}
|
|
255
264
|
};
|
|
256
265
|
|
|
257
|
-
#
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
266
|
+
#onNavigationCommitted = ({
|
|
267
|
+
frameId,
|
|
268
|
+
tabId,
|
|
269
|
+
documentId,
|
|
270
|
+
parentDocumentId,
|
|
271
|
+
url,
|
|
272
|
+
}) => {
|
|
273
|
+
if (frameId !== 0) {
|
|
274
|
+
if (this.#documentIndex.has(documentId)) return;
|
|
275
|
+
const owner =
|
|
276
|
+
parentDocumentId && this.#documentIndex.get(parentDocumentId);
|
|
277
|
+
if (!owner) return;
|
|
278
|
+
this.#indexDocument(owner, documentId);
|
|
279
|
+
this.#pages.set(owner.documentId, owner);
|
|
261
280
|
return;
|
|
262
281
|
}
|
|
263
|
-
|
|
264
|
-
|
|
282
|
+
// Main-frame. Reuse an existing record for a bfcache re-commit
|
|
283
|
+
// so accumulated stats survive forward/back navigation; preserve
|
|
284
|
+
// its state, since onCompleted is not guaranteed to re-fire on
|
|
285
|
+
// restore and we'd otherwise downgrade a complete page back to
|
|
286
|
+
// committed and silently drop it at staging.
|
|
287
|
+
let page = this.#pages.get(documentId);
|
|
288
|
+
if (!page) {
|
|
289
|
+
const ctx = this.#tabContext.get(tabId) || {};
|
|
290
|
+
page = createPage({ tabId, documentId, url, ...ctx });
|
|
265
291
|
}
|
|
266
|
-
|
|
292
|
+
page.url = url;
|
|
293
|
+
page.stageAfter = null;
|
|
294
|
+
this.#indexDocument(page, documentId);
|
|
295
|
+
this.#pages.set(documentId, page);
|
|
267
296
|
};
|
|
268
297
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const page = this.#pages.get(
|
|
272
|
-
if (
|
|
273
|
-
|
|
298
|
+
#onNavigationCompleted = ({ frameId, documentId }) => {
|
|
299
|
+
if (frameId !== 0) return;
|
|
300
|
+
const page = this.#pages.get(documentId);
|
|
301
|
+
if (page) {
|
|
302
|
+
page.state = PAGE_LOADING_STATE.COMPLETE;
|
|
303
|
+
this.#pages.set(documentId, page);
|
|
274
304
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
parentFrameId,
|
|
278
|
-
url,
|
|
279
|
-
};
|
|
280
|
-
this.#pages.set(tabId, page);
|
|
305
|
+
if (Date.now() - this.#lastFlush < FLUSH_THROTTLE_MS) return;
|
|
306
|
+
this.flush().catch((e) => logger.debug('PageStore: flush failed', e));
|
|
281
307
|
};
|
|
282
308
|
|
|
283
|
-
getPageForRequest(
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (
|
|
287
|
-
|
|
309
|
+
getPageForRequest({ documentId, parentDocumentId, documentLifecycle }) {
|
|
310
|
+
// Prerender docs were never seen by the user.
|
|
311
|
+
if (documentLifecycle === 'prerender') return null;
|
|
312
|
+
if (documentId) {
|
|
313
|
+
const direct = this.#documentIndex.get(documentId);
|
|
314
|
+
if (direct) return direct;
|
|
288
315
|
}
|
|
289
|
-
|
|
290
|
-
//
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
if (
|
|
294
|
-
|
|
316
|
+
// Sub-frame request can fire before its own onCommitted, or
|
|
317
|
+
// without a documentId (iframe HTML fetch). Fall back to parent.
|
|
318
|
+
if (parentDocumentId) {
|
|
319
|
+
const owner = this.#documentIndex.get(parentDocumentId);
|
|
320
|
+
if (owner) {
|
|
321
|
+
if (documentId) {
|
|
322
|
+
this.#indexDocument(owner, documentId);
|
|
323
|
+
this.#pages.set(owner.documentId, owner);
|
|
324
|
+
}
|
|
325
|
+
return owner;
|
|
295
326
|
}
|
|
296
|
-
return null;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
const couldBePreviousPage =
|
|
300
|
-
frameId === 0 && type !== 'main_frame' && page.previous;
|
|
301
|
-
|
|
302
|
-
// for main frame requests: check if the origin url is from the previous page (Firefox)
|
|
303
|
-
if (
|
|
304
|
-
couldBePreviousPage &&
|
|
305
|
-
page.url !== originUrl &&
|
|
306
|
-
page.previous.url === originUrl
|
|
307
|
-
) {
|
|
308
|
-
return page.previous;
|
|
309
|
-
}
|
|
310
|
-
// on Chrome we have `initiator` which only contains the origin. In this case, check for a
|
|
311
|
-
// different origin
|
|
312
|
-
if (
|
|
313
|
-
couldBePreviousPage &&
|
|
314
|
-
initiator &&
|
|
315
|
-
!page.url.startsWith(initiator) &&
|
|
316
|
-
page.previous.url.startsWith(initiator)
|
|
317
|
-
) {
|
|
318
|
-
return page.previous;
|
|
319
327
|
}
|
|
320
|
-
return
|
|
328
|
+
return null;
|
|
321
329
|
}
|
|
322
330
|
}
|
|
@@ -125,11 +125,6 @@ export class WebRequestContext {
|
|
|
125
125
|
return null;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
// Sub frames book keeping
|
|
129
|
-
if (context.type === 'sub_frame') {
|
|
130
|
-
pageStore.onSubFrame(context);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
128
|
// Get context on this page
|
|
134
129
|
const page = pageStore.getPageForRequest(context);
|
|
135
130
|
|
|
@@ -140,6 +140,17 @@ function playEvents(chrome, events, options = {}) {
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
|
+
// Replaying with a documentIdPrefix lets the test simulate a
|
|
144
|
+
// fresh browsing session: identical events, but documentIds
|
|
145
|
+
// the reporter has never seen, so documentId-keyed dedupe
|
|
146
|
+
// doesn't suppress the replay.
|
|
147
|
+
if (options.documentIdPrefix) {
|
|
148
|
+
for (const prop of ['documentId', 'parentDocumentId']) {
|
|
149
|
+
if (event[prop]) {
|
|
150
|
+
event[prop] = options.documentIdPrefix + event[prop];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
143
154
|
return event;
|
|
144
155
|
});
|
|
145
156
|
if (events.api === 'tabs' && event.event === 'onCreated') {
|
|
@@ -542,28 +542,70 @@ describe('#sanitizePathSegment', function () {
|
|
|
542
542
|
}
|
|
543
543
|
});
|
|
544
544
|
|
|
545
|
+
describe('should not crash on arbitrary URL path segments', function () {
|
|
546
|
+
for (const segment of [
|
|
547
|
+
'%F0%90%80%80%F0%90%80%82%F0%90%A0%81IbFe',
|
|
548
|
+
'b---344%F0%9F%90%80%F0%9F%90%818880-88e',
|
|
549
|
+
'01-%F0%9F%90%80-687%F0%9F%90%80ca%F0%9F%90%80-%F0%9F%90%80b-0245',
|
|
550
|
+
]) {
|
|
551
|
+
it(`- <<${segment}>>`, function () {
|
|
552
|
+
sanitizePathSegment(segment); // should not throw
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
describe('should safely truncate long strings', function () {
|
|
558
|
+
// %F0%9F%90%80 decodes to U+1F400, which JavaScript stores as the
|
|
559
|
+
// surrogate pair '🐀' (two UTF-16 code units). By growing
|
|
560
|
+
// the prefix one char at a time, we eventually reach a length at
|
|
561
|
+
// which the internal truncation at MAX_TEXT_LENGTH - MASKED.length
|
|
562
|
+
// lands between the high and low surrogate of that pair. The
|
|
563
|
+
// truncated text then carries a lone surrogate, which
|
|
564
|
+
// `encodeURIComponent` rejects with "URI malformed" once the
|
|
565
|
+
// backport step runs.
|
|
566
|
+
it('should not throw when a surrogate pair straddles the truncation boundary', function () {
|
|
567
|
+
for (let i = 0; i < 300; i++) {
|
|
568
|
+
sanitizePathSegment('a'.repeat(i) + '%F0%9F%90%80aaa');
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
});
|
|
572
|
+
|
|
545
573
|
describe('[property based testing]', function () {
|
|
574
|
+
function checkSanitizedPathSegment(pathSegment) {
|
|
575
|
+
const sanitized = sanitizePathSegment(pathSegment);
|
|
576
|
+
if (!sanitized.includes('#??#')) {
|
|
577
|
+
return sanitized === pathSegment;
|
|
578
|
+
}
|
|
579
|
+
if (sanitized.length > pathSegment.length) {
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
const nonMasked = sanitized.replaceAll('#??#', '');
|
|
583
|
+
return textCanBeDerivedByRemovingChars({
|
|
584
|
+
fullText: pathSegment,
|
|
585
|
+
reducedTo: nonMasked,
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
|
|
546
589
|
it('should only remove, but never add characters', function () {
|
|
547
590
|
fc.assert(
|
|
548
591
|
fc.property(fc.webUrl(), (url) => {
|
|
549
592
|
const { pathname } = new URL(url);
|
|
550
593
|
for (const pathSegment of pathname.split('/').filter((x) => x)) {
|
|
551
|
-
|
|
552
|
-
if (!sanitized.includes('#??#')) {
|
|
553
|
-
return sanitized === pathSegment;
|
|
554
|
-
}
|
|
555
|
-
if (sanitized.length > pathSegment.length) {
|
|
556
|
-
return false;
|
|
557
|
-
}
|
|
558
|
-
const nonMasked = sanitized.replaceAll('#??#', '');
|
|
559
|
-
return textCanBeDerivedByRemovingChars({
|
|
560
|
-
fullText: pathSegment,
|
|
561
|
-
reducedTo: nonMasked,
|
|
562
|
-
});
|
|
594
|
+
return checkSanitizedPathSegment(pathSegment);
|
|
563
595
|
}
|
|
564
596
|
}),
|
|
565
597
|
);
|
|
566
598
|
});
|
|
599
|
+
|
|
600
|
+
it('should handle arbitrary, long strings', function () {
|
|
601
|
+
fc.assert(
|
|
602
|
+
fc.property(
|
|
603
|
+
fc.string({ unit: 'binary', minLength: 1, maxLength: 2000 }),
|
|
604
|
+
checkSanitizedPathSegment,
|
|
605
|
+
),
|
|
606
|
+
{ numRuns: 5 },
|
|
607
|
+
);
|
|
608
|
+
});
|
|
567
609
|
});
|
|
568
610
|
});
|
|
569
611
|
|