-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
508 lines (440 loc) · 16.5 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
let blockedURIsByTab = {}; // global object to hold blocked URIs by tabId
let lastURLByTabId = {}; // global object to hold last URL by tabId
let isPopupOpen = false; // tracks state of logwindow.html
let attachedTabs = {}; // tracks which tabs have the debugger attached
let logStorage = {}; // stores console log messages by tabId
let isCSPRewriteEnabledByTab = {}; // tracks isCSPRewriteEnabled state by tabId and URL
let successfulLoadsByTab = {};
let failedLoadsByTab = {};
let failedLoadCounts = {};
let leakCountsByTab = {}; // global object to track leak counts by tabId
let leakedURLsByTab = {}; // global object to store leaked URLs by tabId
let processedUrlsByTab = {}; // tracks processed URLs to prevent duplicate leaks
let currentTabId = null; // tracks the active tab ID for leak detection
let validTabActive = false; // tracks if the current tab is a valid Archive-It tab
// prefixes globally for reuse
const prefixes = [
"https://wayback.archive-it.org/",
"https://partner.archive-it.org/",
"https://archive-it.org/",
];
// web request listener function for leak detection
function webRequestListenerFunction(details) {
if (
validTabActive &&
details.tabId === currentTabId &&
!prefixes.some((prefix) => details.url.startsWith(prefix))
) {
// ensure processedUrlsByTab[details.tabId] is initialized as a Set
processedUrlsByTab[details.tabId] =
processedUrlsByTab[details.tabId] || new Set();
if (!processedUrlsByTab[details.tabId].has(details.url)) {
processedUrlsByTab[details.tabId].add(details.url);
saveProcessedUrls(details.tabId);
updateLeakCount(details.tabId, 1);
console.log("Leak detected:", details.url);
// save the leak URL for the current tab
leakedURLsByTab[details.tabId] =
leakedURLsByTab[details.tabId] || new Set();
leakedURLsByTab[details.tabId].add(details.url);
// store leak details in local storage
chrome.storage.local.set({
[`leakedURLs_${details.tabId}`]: Array.from(
leakedURLsByTab[details.tabId]
),
});
}
}
}
// update the leak count for a specific tab
function updateLeakCount(tabId, increment = 1) {
leakCountsByTab[tabId] = (leakCountsByTab[tabId] || 0) + increment;
// save the updated leak count in local storage
chrome.storage.local.set(
{ [`leakCount_${tabId}`]: leakCountsByTab[tabId] },
() => {
console.log(
`Updated leak count for tab ${tabId}: ${leakCountsByTab[tabId]}`
);
updateBadgeForActiveTab();
}
);
}
// save processed URLs to local storage for a specific tab
function saveProcessedUrls(tabId) {
const processedUrls = Array.from(processedUrlsByTab[tabId] || []);
chrome.storage.local.set(
{ [`processedUrls_${tabId}`]: processedUrls },
() => {
console.log(`Processed URLs saved for tab ${tabId}`);
}
);
}
// // badge update to show leak counts
// function updateBadgeForActiveTab() {
// chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
// if (tabs.length > 0) {
// const activeTabId = tabs[0].id;
// const leakCount = leakCountsByTab[activeTabId] || 0;
// chrome.action.setBadgeText({
// text: leakCount > 0 ? `${leakCount}` : "",
// tabId: activeTabId,
// });
// }
// });
// }
// reset leaks and processed URLs when a tab is removed
chrome.tabs.onRemoved.addListener((tabId) => {
delete leakCountsByTab[tabId];
delete leakedURLsByTab[tabId];
delete processedUrlsByTab[tabId]; // clear only the specific tab's processed URLs
chrome.storage.local.remove([
`leakCount_${tabId}`,
`leakedURLs_${tabId}`,
`processedUrls_${tabId}`,
]);
console.log(`Tab ${tabId} removed, leak data cleared.`);
});
// listener for web requests to detect leaks
chrome.webRequest.onBeforeRequest.addListener(webRequestListenerFunction, {
urls: ["<all_urls>"],
});
// track the active tab and check if it’s valid for leak detection
chrome.tabs.onActivated.addListener((activeInfo) => {
currentTabId = activeInfo.tabId;
chrome.tabs.get(currentTabId, (tab) => {
validTabActive =
tab && prefixes.some((prefix) => tab.url.startsWith(prefix));
updateBadgeForActiveTab();
});
});
// listener to retrieve resource leaks for a specific tab
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "getResourceLeaks") {
const tabId = message.tabId;
console.log(`Received request for resource leaks of Tab ${tabId}`);
const leakCount = leakCountsByTab[tabId] || 0;
const leakedURLs = leakedURLsByTab[tabId]
? Array.from(leakedURLsByTab[tabId])
: [];
console.log(`Leaked URLs for Tab ${tabId}:`, leakedURLs);
sendResponse({
status: "success",
tabId: tabId,
leakCount: leakCount,
leakedURLs: leakedURLs,
});
return true; // keeps message channel open for async response
}
});
// update badge text with the current CSP violation
function updateBadgeForActiveTab() {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs.length > 0) {
const activeTabId = tabs[0].id;
const blockedCount = blockedURIsByTab[activeTabId]
? blockedURIsByTab[activeTabId].size
: 0;
// update the badge text with the blocked count for the active tab
chrome.action.setBadgeText({
text: blockedCount > 0 ? blockedCount.toString() : "",
tabId: activeTabId,
});
}
});
}
// utility function to save console logs
function saveLogs(tabId, logEntry) {
chrome.storage.local.get([`logs_${tabId}`], (result) => {
const logs = result[`logs_${tabId}`] || [];
logs.push(logEntry);
chrome.storage.local.set({ [`logs_${tabId}`]: logs }, () => {
console.log(`Logs saved for tab ${tabId}`);
});
});
}
// main message listener for handling CSP violations, load failures, and logs
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const tabId = sender.tab ? sender.tab.id : null;
if (message.type === "cspViolation") {
handleCspViolation(message, tabId, sendResponse);
} else if (message.type === "loadFailed") {
handleLoadFailed(message, tabId, sendResponse);
} else if (message.type === "loadSuccessful") {
handleLoadSuccessful(message, tabId, sendResponse);
} else if (message.action === "attachDebugger") {
attachDebugger(message, sendResponse);
} else if (message.action === "popupOpened") {
isPopupOpen = true;
console.log("Popup opened");
} else if (message.action === "popupClosed") {
isPopupOpen = false;
console.log("Popup closed");
} else if (message.action === "getLogs") {
getLogsForTab(message, sendResponse);
}
return true; // keep message channel open for asynchronous responses
});
// handle CSP violations
function handleCspViolation(message, tabId, sendResponse) {
const { blockedURI } = message.details;
if (tabId && blockedURI) {
blockedURIsByTab[tabId] = blockedURIsByTab[tabId] || new Set();
blockedURIsByTab[tabId].add(blockedURI);
const blockedCount = blockedURIsByTab[tabId].size;
chrome.storage.local.set(
{ [`blockedCount_${tabId}`]: blockedCount },
() => {
if (chrome.runtime.lastError) {
console.error(`Storage error: ${chrome.runtime.lastError.message}`);
sendResponse({
status: "error",
message: "Failed to store blocked URIs count.",
});
} else {
console.log(
`Saved blocked URIs count for tab ${tabId}: ${blockedCount}`
);
// update badge text for the current active tab
updateBadgeForActiveTab();
sendResponse({ status: "success", tabId, blockedCount });
}
}
);
} else {
console.error("Error: No blockedURI or tabId provided.");
sendResponse({
status: "error",
message: "No blockedURI or tabId provided.",
});
}
}
// handle load successful
function handleLoadSuccessful(message, tabId, sendResponse) {
const { resourceType, resourceURL, status, originalURL } = message;
console.log(`Successfully loaded ${resourceType}: ${resourceURL}`);
// initialize an array for this tabId if it doesn't exist
successfulLoadsByTab[tabId] = successfulLoadsByTab[tabId] || [];
// add the successful load details to the tab's array
successfulLoadsByTab[tabId].push({
resourceType,
resourceURL,
originalURL,
status,
timestamp: new Date().toISOString(),
});
// optionally save the updated global object back to Chrome storage for persistence
chrome.storage.local.set({ successfulLoadsByTab }, () => {
console.log(`Saved successful load for tab ${tabId}:`, {
resourceType,
resourceURL,
originalURL,
status,
});
// send confirmation response back to content script
sendResponse({ status: "success", tabId, resourceURL });
});
}
// handle load failures
function handleLoadFailed(message, tabId, sendResponse) {
const { resourceType, resourceURL, error, originalURL } = message;
console.log(
`Failed to load ${resourceType} resource: ${resourceURL}. Error: ${error}`
);
// initialize the array if it doesn't exist
failedLoadsByTab[tabId] = failedLoadsByTab[tabId] || [];
failedLoadCounts[tabId] = (failedLoadCounts[tabId] || 0) + 1;
// add failure details to the tab's array
failedLoadsByTab[tabId].push({
resourceType,
resourceURL,
originalURL,
error,
timestamp: new Date().toISOString(),
});
// save the updated global object back to Chrome storage for persistence
chrome.storage.local.set({ failedLoadsByTab, failedLoadCounts }, () => {
console.log(`Saved failed loads and count for tab ${tabId}:`, {
resourceType,
resourceURL,
originalURL,
error,
failedCount: failedLoadCounts[tabId], // Log the updated count
});
// send a response back to the content script to confirm receipt
sendResponse({ status: "received" });
});
}
// attach debugger to the tab
function attachDebugger(request, sendResponse) {
const tabId = request.tabId;
if (attachedTabs[tabId]) {
console.log(`Debugger already attached to tab ${tabId}`);
sendResponse({ success: true });
return;
}
chrome.debugger.attach({ tabId }, "1.3", () => {
if (chrome.runtime.lastError) {
console.warn(
`Failed to attach debugger to tab ${tabId}: ${chrome.runtime.lastError.message}`
);
sendResponse({ success: false, error: chrome.runtime.lastError.message });
return;
}
attachedTabs[tabId] = true;
chrome.debugger.sendCommand({ tabId }, "Log.enable");
chrome.debugger.sendCommand({ tabId }, "Runtime.enable");
chrome.debugger.onEvent.addListener((source, method, params) => {
if (source.tabId === tabId) {
let message = "";
if (method === "Log.entryAdded") {
message = `[${params.entry.level}] ${params.entry.text}`;
} else if (method === "Runtime.consoleAPICalled") {
message = `[${params.type}] ${params.args
.map((arg) => arg.value)
.join(" ")}`;
}
if (message) {
console.log(`Tab ${tabId}: ${message}`);
logStorage[tabId] = logStorage[tabId] || [];
logStorage[tabId].push({ tabId, message });
saveLogs(tabId, { tabId, message });
if (isPopupOpen) {
chrome.runtime.sendMessage({ tabId, log: { message } });
}
}
}
});
sendResponse({ success: true });
});
}
// get logs for the specified tab
function getLogsForTab(request, sendResponse) {
const tabId = request.tabId;
chrome.storage.local.get([`logs_${tabId}`], (result) => {
sendResponse(result[`logs_${tabId}`] || []);
});
}
// listen for tab activation to update the badge for the active tab
chrome.tabs.onActivated.addListener(updateBadgeForActiveTab);
// listen for tab updates (e.g., navigation) to reset badge if necessary
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete" && tab.active) {
updateBadgeForActiveTab();
}
});
// initial call to update the badge when the extension is loaded
updateBadgeForActiveTab();
// when tabs are created, do:
chrome.tabs.onCreated.addListener((tab) => {
chrome.storage.local.set({ cspRewriteEnabled: false });
});
// when tabs are removed, do:
chrome.tabs.onRemoved.addListener((tabId) => {
// check and detach debugger if attached
if (attachedTabs[tabId]) {
chrome.debugger.detach({ tabId }, () => {
if (chrome.runtime.lastError) {
console.warn(
`Failed to detach debugger from tab ${tabId}: ${chrome.runtime.lastError.message}`
);
} else {
console.log(`Debugger detached from tab ${tabId}`);
}
});
delete attachedTabs[tabId];
}
// clean up stored logs and blocked URIs for the tab
delete logStorage[tabId];
delete blockedURIsByTab[tabId];
delete lastURLByTabId[tabId];
delete isCSPRewriteEnabledByTab[tabId];
delete successfulLoadsByTab[tabId];
delete failedLoadsByTab[tabId];
delete failedLoadCounts[tabId];
// clear any local storage entries tied to the tab
chrome.storage.local.remove(
[`logs_${tabId}`, `blockedCount_${tabId}`],
() => {
if (chrome.runtime.lastError) {
console.error(
`Error clearing storage for tab ${tabId}: ${chrome.runtime.lastError.message}`
);
} else {
console.log(`Storage cleared for tab ${tabId}`);
}
}
);
// reset CSP rewrite state for this tab
chrome.storage.local.set({ [`cspRewriteEnabled_${tabId}`]: false }, () => {
console.log(`CSP rewrite state reset for tab ${tabId}`);
});
console.log(`Tab ${tabId} removed, all associated data cleaned up.`);
});
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
if (removeInfo.isWindowClosing) return;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs.length > 0 && tabs[0].id === tabId) {
// clear badge if the active tab with violations is closed
chrome.action.setBadgeText({ text: "" });
}
});
});
// listens for incoming message and instructs logic in content.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "CSP_REWRITE_ENABLED") {
console.log("CSP rewrite enabled");
// send message to content script to enable CSP rewrite and reload the page
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "ENABLE_CSP_REWRITE" });
});
} else if (request.action === "CSP_REWRITE_DISABLED") {
console.log("CSP rewrite disabled");
// send message to content script to disable CSP rewrite and reload the page
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "DISABLE_CSP_REWRITE" });
});
}
});
// listens for navigation events to reset CSP violation count and clear blocked URIs
chrome.webNavigation.onCommitted.addListener(function (details) {
const tabId = details.tabId;
const newURL = details.url;
// check if the new URL is from a different domain or a non-monitored site
if (!newURL.startsWith("https://wayback.archive-it.org/")) {
// reset the CSP violation count and clear blocked URIs for this tab
chrome.storage.local.remove([`blockedCount_${tabId}`], () => {
if (chrome.runtime.lastError) {
console.error(
`Error resetting blockedCount for tab ${tabId}: ${chrome.runtime.lastError.message}`
);
} else {
console.log(`CSP violation count reset for tab ${tabId}`);
blockedURIsByTab[tabId] = new Set(); // clear the in-memory blocked URIs
}
});
}
// optionally, update the last URL visited for this tab
lastURLByTabId[tabId] = newURL;
});
// listener to get CSP violations for the selected tab
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "getCSPViolations") {
const tabId = message.tabId;
console.log(`Received request for CSP violations of Tab ${tabId}`);
chrome.storage.local.get([`blockedCount_${tabId}`], (result) => {
const blockedCount = result[`blockedCount_${tabId}`] || 0;
const blockedURIs = blockedURIsByTab[tabId]
? Array.from(blockedURIsByTab[tabId])
: [];
console.log(`Blocked URIs for Tab ${tabId}:`, blockedURIs);
sendResponse({
status: "success",
tabId: tabId,
blockedCount: blockedCount,
blockedURIs: blockedURIs,
});
});
return true; // keeps message channel open for async response
}
});