Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,15 @@ import {
UPDATE_INTERVAL_MS,
} from "./config";
import validator_set from "./validator_set.json";
import { isInPartition } from "./webcat/cache";
import { WebcatDatabase } from "./webcat/db";
import { WebcatRequestHandler } from "./webcat/handler";
import { setErrorIcon } from "./webcat/ui";
import { EnrollmentUpdater } from "./webcat/updater";
import { clearBrowserCaches } from "./webcat/utils";

console.log("[webcat] Starting up background");

const db = new WebcatDatabase();

// Not the best performance idea to act on all tab just for this
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
const errorUrl = browser.runtime.getURL("pages/error.html");
if (changeInfo.status === "complete" && tab.url?.startsWith(errorUrl)) {
setErrorIcon(tabId);
}
});

// Grey out and make page action unclickable unless a website is enrolled
browser.tabs.onCreated.addListener((tab) => {
if (tab.id !== undefined) {
browser.pageAction.hide(tab.id);
}
});

// Handle incognito sessions ending
browser.windows.onRemoved.addListener(async () => {
const windows = await browser.windows.getAll();
if (windows.filter((win) => win.incognito).length === 0) {
for (const key of db.origins.keys()) {
if (isInPartition(key, { incognito: true })) {
db.origins.delete(key);
}
}
for (const value of db.nonOrigins.values()) {
if (isInPartition(value, { incognito: true })) {
db.nonOrigins.delete(value);
}
}
}
});

const requestHandler = new WebcatRequestHandler(db);
const updater = new EnrollmentUpdater({
endpoint: endpoint,
Expand Down
36 changes: 34 additions & 2 deletions extension/src/webcat/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
RequestHandler,
} from "../browser/requests";
import { ContentScript } from "../browser/scripting";
import { CacheKey } from "./cache";
import { CacheKey, isInPartition } from "./cache";
import { HookBuilder } from "./hookbuilder";
import { Database } from "./interfaces/database";
import { WebcatError } from "./interfaces/errors";
Expand All @@ -17,7 +17,7 @@ import { OriginStateVerifiedManifest } from "./originstate";
import { validateOrigin } from "./request";
import { FRAME_TYPES } from "./resources";
import { ResponseValidator } from "./response";
import { errorpage } from "./ui";
import { errorpage, setErrorIcon } from "./ui";
import { getFQDN, isExtensionRequest, isNewerSemver } from "./utils";

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
Expand All @@ -44,6 +44,17 @@ export class WebcatRequestHandler extends RequestHandler {
this.#responseValidator = new ResponseValidator(this.#db, this.#hooks);
this.addEventListener("beforerequest", this.#onRequest);
this.addEventListener("headersreceived", this.#onHeaders);
browser.webNavigation.onCommitted.addListener(
this.#onErrorPageNavigation.bind(this),
{
url: [
{
urlPrefix: browser.runtime.getURL("pages/error.html"),
},
],
},
);
browser.windows.onRemoved.addListener(this.#onWindowClosed.bind(this));
}

override async bind(fqdns: string[]): Promise<string[]> {
Expand Down Expand Up @@ -298,4 +309,25 @@ export class WebcatRequestHandler extends RequestHandler {
);
return details.requestId;
}

#onErrorPageNavigation(details: browser.webNavigation._OnCommittedDetails) {
setErrorIcon(details.tabId);
}

async #onWindowClosed() {
// Handle incognito sessions ending
const windows = await browser.windows.getAll();
if (windows.filter((win) => win.incognito).length === 0) {
for (const key of this.#db.origins.keys()) {
if (isInPartition(key, { incognito: true })) {
this.#db.origins.delete(key);
}
}
for (const value of this.#db.nonOrigins.values()) {
if (isInPartition(value, { incognito: true })) {
this.#db.nonOrigins.delete(value);
}
}
}
}
}
Loading