You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB
JavaScript

4 years ago
console.log("PROBLEMATIC REQUESTS");
4 years ago
let memory = {};
4 years ago
// const isThirdParty = (arg) => arg.urlClassification.thirdParty.length > 0;
async function isThirdParty(request) {
const request_url = new URL(request.url);
const origin_url = new URL(await getOrigin(request));
return (
request_url.origin != origin_url.origin ||
request.urlClassification.thirdParty.length > 0
);
}
4 years ago
const hasCookie = (arg) => arg.requestHeaders.some((h) => h.name === "Cookie");
const hasReferer = (arg) =>
arg.requestHeaders.some((h) => h.name === "Referer");
const getReferer = (arg) =>
arg.requestHeaders.filter((h) => h.name === "Referer")[0].value;
4 years ago
4 years ago
const getOrigin = async (arg) => {
let url;
4 years ago
if (arg.tabId && arg.tabId >= 0) {
4 years ago
const tab = await browser.tabs.get(arg.tabId);
url = tab.url;
} else {
url = arg.frameAncestors[0].url;
}
4 years ago
return url;
4 years ago
};
const exposesOrigin = async (arg) => {
4 years ago
return getReferer(arg).includes(new URL(await getOrigin(arg)).host);
4 years ago
};
browser.webRequest.onBeforeSendHeaders.addListener(
async (request) => {
if (
4 years ago
(await isThirdParty(request)) &&
4 years ago
hasReferer(request) &&
(await exposesOrigin(request))
) {
const has_cookie = hasCookie(request);
4 years ago
if (!memory[request.tabId]) {
memory[request.tabId] = {};
}
const shorthost = new URL(request.url).host
.match(/((\.[^.]+){2}$)/)[0]
.slice(1);
if (!memory[request.tabId][shorthost]) {
memory[request.tabId][shorthost] = [];
}
memory[request.tabId][shorthost].push({ url: request.url, has_cookie });
4 years ago
}
},
{ urls: ["<all_urls>"] },
["requestHeaders"]
);
4 years ago
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (sender.tab) {
return;
}
console.log("got message!", request);
if (request?.msg === "get_memory") {
sendResponse(memory);
} else if (request?.msg === "clear_memory") {
console.log("memory cleared");
memory = {};
}
});