Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

2.5 KB
Raw Blame
diff --git a/lib/server/chromium/crBrowser.js b/lib/server/chromium/crBrowser.js
index 104789ba12c6d547e056dfe468fa9f04bbcc638c..5f348b33ca323e3f44da8b3bf3e4db2357e47e00 100644
--- a/lib/server/chromium/crBrowser.js
+++ b/lib/server/chromium/crBrowser.js
@@ -90,6 +90,9 @@ class CRBrowser extends import_browser.Browser {
       return browser;
     }
     browser._defaultContext = new CRBrowserContext(browser, void 0, options.persistent);
+    // Karakeep patch: remember Chrome's real default browser context so that
+    // targets in contexts created by other CDP clients aren't adopted below.
+    browser._defaultBrowserContextId = (await session.send("Target.getBrowserContexts").catch(() => ({}))).defaultBrowserContextId;
     await Promise.all([
       session.send("Target.setAutoAttach", { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }).then(async () => {
         await session.send("Target.getTargetInfo");
@@ -147,9 +150,29 @@ class CRBrowser extends import_browser.Browser {
     if (targetInfo.type === "browser")
       return;
     const session = this._session.createChildSession(sessionId);
+    if (targetInfo.type === "service_worker" && !targetInfo.browserContextId) {
+      // Karakeep patch: Chrome can report an already running service worker
+      // without a browserContextId (e.g. one whose context is gone). Detaching
+      // resumes it if it is paused and releases the session instead of failing
+      // the assertion below, which crashes the process.
+      session.detach().catch(() => {
+      });
+      return;
+    }
     (0, import_assert.assert)(targetInfo.browserContextId, "targetInfo: " + JSON.stringify(targetInfo, null, 2));
     let context = this._contexts.get(targetInfo.browserContextId) || null;
     if (!context) {
+      if (this._defaultBrowserContextId && targetInfo.browserContextId !== this._defaultBrowserContextId) {
+        // Karakeep patch: the target belongs to a browser context another CDP
+        // client created. Adopting it as a default-context page would enable
+        // Fetch with handleAuthRequests on it (patchright always intercepts),
+        // and Chrome hands the target's proxy auth challenges to the first
+        // client that did so: we'd answer them without credentials and break
+        // the owner's proxy authentication. Detaching resumes the target.
+        session.detach().catch(() => {
+        });
+        return;
+      }
       context = this._defaultContext;
     }
     if (targetInfo.type === "other" && targetInfo.url.startsWith("devtools://devtools") && this._devtools) {