rentgen/problematic.js

96 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-04-23 14:56:41 +02:00
console.log("PROBLEMATIC REQUESTS");
2021-05-21 22:28:19 +02:00
let memory = {};
2021-06-29 21:07:50 +02:00
function gethost(url) {
return new URL(request.url).host;
}
function getshorthost(host) {
console.log("getshort", host);
return host.split(".").slice(-2).join(".");
}
2021-05-16 07:54:41 +02:00
// 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));
2021-06-29 21:07:50 +02:00
/* console.log(request_url.ho, origin_url, request_url.includes(origin_url)); */
console.log(
request_url.host,
origin_url.host,
request_url.host.includes(origin_url.host)
);
if (request_url.host.includes(origin_url.host)) {
return false;
}
if (getshorthost(request_url.host) == getshorthost(origin_url.host)) {
return false;
}
2021-05-16 07:54:41 +02:00
return (
request_url.origin != origin_url.origin ||
request.urlClassification.thirdParty.length > 0
);
}
2021-04-23 14:56:41 +02:00
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;
2021-05-16 07:54:41 +02:00
2021-04-23 14:56:41 +02:00
const getOrigin = async (arg) => {
let url;
2021-05-21 22:28:19 +02:00
if (arg.tabId && arg.tabId >= 0) {
2021-04-23 14:56:41 +02:00
const tab = await browser.tabs.get(arg.tabId);
url = tab.url;
} else {
url = arg.frameAncestors[0].url;
}
2021-05-16 07:54:41 +02:00
return url;
2021-04-23 14:56:41 +02:00
};
const exposesOrigin = async (arg) => {
2021-05-16 07:54:41 +02:00
return getReferer(arg).includes(new URL(await getOrigin(arg)).host);
2021-04-23 14:56:41 +02:00
};
browser.webRequest.onBeforeSendHeaders.addListener(
async (request) => {
if (
2021-05-16 07:54:41 +02:00
(await isThirdParty(request)) &&
2021-04-23 14:56:41 +02:00
hasReferer(request) &&
(await exposesOrigin(request))
) {
const has_cookie = hasCookie(request);
2021-05-21 22:28:19 +02:00
if (!memory[request.tabId]) {
memory[request.tabId] = {};
}
2021-06-29 21:07:50 +02:00
const shorthost = getshorthost(new URL(request.url).host);
2021-05-21 22:28:19 +02:00
if (!memory[request.tabId][shorthost]) {
memory[request.tabId][shorthost] = [];
}
2021-06-29 21:07:50 +02:00
memory[request.tabId][shorthost].push({
url: request.url,
has_cookie,
cookie: request.requestHeaders.find((h) => h.name == "Cookie")?.value,
});
2021-04-23 14:56:41 +02:00
}
},
{ urls: ["<all_urls>"] },
["requestHeaders"]
);
2021-05-21 22:28:19 +02:00
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 = {};
}
});