From 8fc1b33977553f3d281c756b62e3ac038de15efe Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sat, 6 Nov 2021 21:48:25 +0100 Subject: [PATCH] Add option to clear cookies --- extended-request.ts | 2 +- memory.ts | 43 ++++++++++------ request-cluster.ts | 21 ++++++++ sidebar.tsx | 118 ++++++++++++++++++++++++++++++-------------- util.ts | 5 ++ 5 files changed, 138 insertions(+), 51 deletions(-) diff --git a/extended-request.ts b/extended-request.ts index e089dd4..1125fa3 100644 --- a/extended-request.ts +++ b/extended-request.ts @@ -31,7 +31,7 @@ export default class ExtendedRequest { } else { url = (this.data as any).frameAncestors[0].url; } - this.origin = url; + this.origin = new URL(url).origin; } getOrigin(): string { diff --git a/memory.ts b/memory.ts index bc73e09..6d65e5e 100644 --- a/memory.ts +++ b/memory.ts @@ -4,21 +4,23 @@ import { EventEmitter } from "events"; import { RequestCluster } from "./request-cluster"; class Memory extends EventEmitter { - tab_to_history = {} as Record>; + origin_to_history = {} as Record>; async register(request: ExtendedRequest) { await request.init(); - if (request.isThirdParty() && request.exposesOrigin()) { - if (!this.tab_to_history[request.tabId]) { - this.tab_to_history[request.tabId] = {}; - } - const shorthost = getshorthost(new URL(request.url).host); - if (!this.tab_to_history[request.tabId][shorthost]) { - const cluster = new RequestCluster(shorthost); - this.tab_to_history[request.tabId][shorthost] = cluster; - } - this.tab_to_history[request.tabId][shorthost].add(request); - this.emit("change"); + console.log("registering request for", request.origin); + if (!request.isThirdParty()) { + return; } + if (!this.origin_to_history[request.origin]) { + this.origin_to_history[request.origin] = {}; + } + const shorthost = getshorthost(new URL(request.url).host); + if (!this.origin_to_history[request.origin][shorthost]) { + const cluster = new RequestCluster(shorthost); + this.origin_to_history[request.origin][shorthost] = cluster; + } + this.origin_to_history[request.origin][shorthost].add(request); + this.emit("change"); } constructor() { @@ -32,8 +34,21 @@ class Memory extends EventEmitter { ); } - getClustersForTab(tab_id: number): Record { - return this.tab_to_history[tab_id] || {}; + getClustersForOrigin(origin: string): Record { + return this.origin_to_history[origin] || {}; + } + + async removeCookiesFor(origin: string, shorthost?: string): Promise { + const clusters = this.getClustersForOrigin(origin); + await Promise.all( + Object.values(clusters) + .filter((cluster) => !shorthost || cluster.id === shorthost) + .map((cluster) => cluster.removeAllCookies()) + ); + } + + async removeRequestsFor(origin: string) { + this.origin_to_history[origin] = {}; } } diff --git a/request-cluster.ts b/request-cluster.ts index 5ef4a2d..b816473 100644 --- a/request-cluster.ts +++ b/request-cluster.ts @@ -5,9 +5,18 @@ export type Sources = "cookie" | "pathname" | "queryparams" | "header"; import { TCString, TCModel } from "@iabtcf/core"; +const id = (function* id() { + let i = 0; + while (true) { + i++; + yield i; + } +})(); + export class StolenDataEntry { public isIAB = false; public iab: TCModel | null = null; + public id: number; constructor( public request: ExtendedRequest, @@ -20,6 +29,7 @@ export class StolenDataEntry { // console.log(this.iab); this.isIAB = true; } catch (e) {} + this.id = id.next().value as number; } getPriority() { @@ -95,4 +105,15 @@ export class RequestCluster extends EventEmitter { } } } + + async removeAllCookies() { + const cookies = await browser.cookies.getAll({ domain: this.id }); + for (const cookie of cookies) { + console.log("removing cookie", cookie.name, "from", cookie.domain); + await browser.cookies.remove({ + name: cookie.name, + url: `https://${cookie.domain}`, + }); + } + } } diff --git a/sidebar.tsx b/sidebar.tsx index e72fe98..169fb3e 100644 --- a/sidebar.tsx +++ b/sidebar.tsx @@ -2,19 +2,14 @@ import React, { useEffect, useState } from "react"; import ReactDOM from "react-dom"; import memory from "./memory"; import { RequestCluster, Sources } from "./request-cluster"; -import { Tab, useEmitter } from "./util"; - -async function getTabByID(id: number) { - const tabs = await browser.tabs.query({ currentWindow: true }); - return tabs.find((tab) => tab.id == id); -} +import { getshorthost, useEmitter } from "./util"; async function getCurrentTab() { const [tab] = await browser.tabs.query({ active: true, windowId: browser.windows.WINDOW_ID_CURRENT, }); - return tab.id; + return tab; } const TabDropdown = ({ @@ -46,18 +41,18 @@ const TabDropdown = ({ }; const StolenDataRow = ({ - tabID, + origin, shorthost, minValueLength, cookiesOnly, }: { - tabID: number; + origin: string; shorthost: string; refreshToken: number; minValueLength: number; cookiesOnly: boolean; }) => { - const cluster = memory.getClustersForTab(tabID)[shorthost]; + const cluster = memory.getClustersForOrigin(origin)[shorthost]; const icons: Record = { cookie: "🍪", pathname: "🛣", @@ -68,14 +63,28 @@ const StolenDataRow = ({

{cluster.id} {cluster.hasCookies() ? "🍪" : ""} x - {cluster.requests.length} + {cluster.requests.length}{" "} + cluster.removeAllCookies()} + > + Wyczyść cookiesy +

{cluster .getStolenData({ minValueLength, cookiesOnly }) .map((entry) => ( - + @@ -92,24 +101,20 @@ const StolenDataRow = ({ }; const StolenData = ({ - pickedTab, - refreshToken, + origin, minValueLength, + refreshToken, cookiesOnly, }: { - pickedTab: number | null; + origin: string; refreshToken: number; minValueLength: number; cookiesOnly: boolean; }) => { - const [tab, setTab] = useState(null); - useEffect(() => { - getTabByID(pickedTab).then(setTab); - }, [pickedTab]); - if (!pickedTab || !tab) { + if (!origin) { return
; } - const clusters = Object.values(memory.getClustersForTab(pickedTab)).sort( + const clusters = Object.values(memory.getClustersForOrigin(origin)).sort( RequestCluster.sortCompare ); return ( @@ -117,20 +122,39 @@ const StolenData = ({ {" "}

- {tab.title} + {origin} + +

{clusters .filter((cluster) => !cookiesOnly || cluster.hasCookies()) - .map((cluster) => ( - - ))} + .map((cluster) => { + return ( + + ); + })}
); @@ -167,13 +191,35 @@ const Options = ({ }; const Sidebar = () => { - const [pickedTab, setPickedTab] = useState(null); + const [origin, setOrigin] = useState(null); const [minValueLength, setMinValueLength] = useState(7); const [cookiesOnly, setCookiesOnly] = useState(false); const counter = useEmitter(memory); + + useEffect(() => { + const listener = async (data) => { + console.log("tab change!"); + const tab = await getCurrentTab(); + const url = new URL(tab.url); + console.log( + "NEW ORIGIN", + url.origin, + url.origin.startsWith("moz-extension") + ); + if (url.origin.startsWith("moz-extension")) { + return; + } + setOrigin(url.origin); + }; + browser.tabs.onUpdated.addListener(listener); + return () => { + browser.tabs.onUpdated.removeListener(listener); + }; + }); + return ( <> -
+ {/*
-
+
*/} { setCookiesOnly={setCookiesOnly} /> { {} ); } + +export async function getTabByID(id: number) { + const tabs = await browser.tabs.query({ currentWindow: true }); + return tabs.find((tab) => tab.id == id); +}
{entry.name}