Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: browser extension uses chrome.debugger for CSP-safe eval

Louis Beaumontcbc22591ddac
diff --git a/packages/browser-extension/dist/manifest.json b/packages/browser-extension/dist/manifest.json
index fa431d76c..a89531477 100644
--- a/packages/browser-extension/dist/manifest.json
+++ b/packages/browser-extension/dist/manifest.json
@@ -8,6 +8,7 @@
"type": "module"
},
"permissions": [
+ "debugger",
"scripting",
"activeTab",
"tabs",
diff --git a/packages/browser-extension/dist/worker.js b/packages/browser-extension/dist/worker.js
index 5396128e8..f27f4da3d 100644
--- a/packages/browser-extension/dist/worker.js
+++ b/packages/browser-extension/dist/worker.js
@@ -88,22 +88,39 @@ async function evalInTab(tabId, code) {
if (!tab.url || tab.url.startsWith("chrome://") || tab.url.startsWith("chrome-extension://") || tab.url.startsWith("edge://") || tab.url.startsWith("about:") || tab.url.includes("chromewebstore.google.com")) {
throw new Error(`cannot execute scripts on ${tab.url}`);
}
- const needsReturn = !code.includes("return ") && !code.includes(`return
-`) && !code.trimStart().startsWith("{");
- const wrappedCode = needsReturn ? `return (${code})` : code;
- const [frame] = await chrome.scripting.executeScript({
- target: { tabId },
- world: "MAIN",
- func: async (userCode) => {
- const fn = new Function(userCode);
- const result = fn();
- return result instanceof Promise ? await result : result;
- },
- args: [wrappedCode]
- });
- if (frame?.result !== undefined)
- return frame.result;
- return null;
+ const expression = `(async () => { ${code} })()`;
+ try {
+ await new Promise((resolve, reject) => {
+ chrome.debugger.attach({ tabId }, "1.3", () => {
+ if (chrome.runtime.lastError?.message?.includes("already attached")) {
+ resolve();
+ } else if (chrome.runtime.lastError) {
+ reject(new Error(chrome.runtime.lastError.message));
+ } else {
+ resolve();
+ }
+ });
+ });
+ } catch (e) {
+ if (!e.message?.includes("already attached"))
+ throw e;
+ }
+ try {
+ const evalResult = await new Promise((resolve, reject) => {
+ chrome.debugger.sendCommand({ tabId }, "Runtime.evaluate", { expression, awaitPromise: true, returnByValue: true, userGesture: true }, (result) => {
+ if (chrome.runtime.lastError) {
+ reject(new Error(chrome.runtime.lastError.message));
+ } else {
+ resolve(result);
+ }
+ });
+ });
+ if (evalResult?.exceptionDetails) {
+ const desc = evalResult.exceptionDetails.exception?.description || evalResult.exceptionDetails.text || "evaluation error";
+ throw new Error(desc);
+ }
+ return evalResult?.result?.value ?? null;
+ } finally {}
}
function detectBrowser() {
const ua = navigator.userAgent;
diff --git a/packages/browser-extension/src/worker.ts b/packages/browser-extension/src/worker.ts
index be990bf80..4629e2cf5 100644
--- a/packages/browser-extension/src/worker.ts
+++ b/packages/browser-extension/src/worker.ts
@@ -132,31 +132,58 @@ async function evalInTab(tabId: number, code: string): Promise<unknown> {
throw new Error(`cannot execute scripts on ${tab.url}`);
}
- // executeScript in MAIN world = same context as the page's own JS.
- // Same-origin fetch works, cookies are included, no CORS issues.
+ // Use chrome.debugger Runtime.evaluate — bypasses CSP restrictions,
+ // runs in page context with full cookie access, properly returns
+ // async results via awaitPromise.
//
- // Code is passed to `new Function(code)` which wraps it in a function body.
- // - Use `return expr` to get a value back
- // - Multi-line code with explicit returns works naturally
- // - Bare expressions like `document.title` are auto-wrapped with `return`
- const needsReturn =
- !code.includes("return ") && !code.includes("return\n") && !code.trimStart().startsWith("{");
- const wrappedCode = needsReturn ? `return (${code})` : code;
-
- const [frame] = await chrome.scripting.executeScript({
- target: { tabId },
- world: "MAIN",
- func: async (userCode: string) => {
- const fn = new Function(userCode);
- const result = fn();
- // Await promises so async code works
- return result instanceof Promise ? await result : result;
- },
- args: [wrappedCode],
- });
-
- if (frame?.result !== undefined) return frame.result;
- return null;
+ // Wrap code in an async IIFE so `await` and `return` both work.
+ const expression = `(async () => { ${code} })()`;
+
+ // Attach debugger if not already attached
+ try {
+ await new Promise<void>((resolve, reject) => {
+ chrome.debugger.attach({ tabId }, "1.3", () => {
+ if (chrome.runtime.lastError?.message?.includes("already attached")) {
+ resolve(); // already attached, that's fine
+ } else if (chrome.runtime.lastError) {
+ reject(new Error(chrome.runtime.lastError.message));
+ } else {
+ resolve();
+ }
+ });
+ });
+ } catch (e: any) {
+ if (!e.message?.includes("already attached")) throw e;
+ }
+
+ try {
+ const evalResult = await new Promise<any>((resolve, reject) => {
+ chrome.debugger.sendCommand(
+ { tabId },
+ "Runtime.evaluate",
+ { expression, awaitPromise: true, returnByValue: true, userGesture: true },
+ (result: any) => {
+ if (chrome.runtime.lastError) {
+ reject(new Error(chrome.runtime.lastError.message));
+ } else {
+ resolve(result);
+ }
+ }
+ );
+ });
+
+ if (evalResult?.exceptionDetails) {
+ const desc = evalResult.exceptionDetails.exception?.description
+ || evalResult.exceptionDetails.text
+ || "evaluation error";
+ throw new Error(desc);
+ }
+
+ return evalResult?.result?.value ?? null;
+ } finally {
+ // Don't detach — keep debugger attached for reuse.
+ // It auto-detaches on tab navigation anyway.
+ }
}
// ---------------------------------------------------------------------------