Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

fix: recognize our own PDF handler tabs without a HEAD probe

Codex release-state test21f762dd3d1d
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index c11d96960..85f6cdf54 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -71,7 +71,7 @@ import {
buildClaudeDocumentBlock,
PDF_PASSTHROUGH_MAX_BYTES,
} from './pdf-tools.js';
-import { pdfUrlFromTabUrl } from './pdf-extraction.js';
+import { isPdfHandlerTabUrl, pdfUrlFromTabUrl } from './pdf-extraction.js';
import { normalizePdfOcrResult, PDF_OCR_SYSTEM_PROMPT } from './pdf-ocr.js';
import * as trace from '../trace/recorder.js';
import { buildTerminalRuntimeEvent, enqueueCloudRuntimeEvent, flushCloudRuntimeOutbox } from '../trace/cloud-runtime-outbox.js';
@@ -5359,8 +5359,9 @@ export class Agent extends LoopDetector {
* Decide whether `pageUrl` is a PDF tab the content-script path
* cannot reach. Two paths:
* - Fast path: URL pattern (`isPdfUrl`). Catches `*.pdf` paths and
- * WebBrain's PDF handler URL after unwrapping its `url` parameter.
- * `?file=*.pdf` viewer URLs β€” the bulk of cases.
+ * `?file=*.pdf` viewer URLs β€” the bulk of cases. WebBrain's own
+ * PDF handler URL is unwrapped first, and a handler tab counts as
+ * a PDF tab outright: we only ever open it for a PDF response.
* - Slow path: HEAD probe with credentials. Catches PDFs served
* from endpoints whose URL doesn't reveal the type, e.g.
* `/download?id=42` returning `Content-Type: application/pdf`.
@@ -5380,7 +5381,7 @@ export class Agent extends LoopDetector {
async _isPdfTab(tabId, pageUrl) {
if (!pageUrl) return false;
const pdfUrl = pdfUrlFromTabUrl(pageUrl);
- if (isPdfUrl(pdfUrl)) return true;
+ if (isPdfUrl(pdfUrl) || isPdfHandlerTabUrl(pageUrl)) return true;
// Cache hit?
const cached = this._isPdfTabCache.get(tabId);
@@ -27954,10 +27955,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
try {
let pdfUrl = String(args.url || '').trim();
if (!pdfUrl) {
- // Default to the active tab's URL.
+ // Default to the active tab's URL. On our own viewer page that URL
+ // is the handler wrapping the real one in ?url=; extraction unwraps
+ // it internally, but the document name below is derived from this
+ // string, so unwrap here too.
try {
const tab = await chrome.tabs.get(tabId);
- pdfUrl = tab?.url || '';
+ pdfUrl = pdfUrlFromTabUrl(tab?.url || '');
} catch {}
}
if (!pdfUrl) {
diff --git a/src/chrome/src/agent/pdf-extraction.js b/src/chrome/src/agent/pdf-extraction.js
index c75c7e4b4..2bb0240cd 100644
--- a/src/chrome/src/agent/pdf-extraction.js
+++ b/src/chrome/src/agent/pdf-extraction.js
@@ -54,6 +54,23 @@ export function pdfUrlFromTabUrl(value, runtime = globalThis.chrome?.runtime) {
return unwrapPdfHandlerUrl(url, runtime).href;
}
+// Our viewer page is only ever opened for a response we already identified as
+// a PDF, so a handler tab is a PDF tab by construction. Callers use this to
+// skip the Content-Type probe, which would otherwise decide the question for
+// wrapped URLs like `/download?id=42` β€” and servers routinely answer HEAD with
+// 405, or drop the Content-Type, or burn a one-shot signed download link.
+export function isPdfHandlerTabUrl(value, runtime = globalThis.chrome?.runtime) {
+ let url;
+ try {
+ url = new URL(String(value || ''));
+ } catch {
+ return false;
+ }
+ // unwrapPdfHandlerUrl returns the same object when there was nothing to
+ // unwrap, so identity is the "this was our handler page" signal.
+ return unwrapPdfHandlerUrl(url, runtime) !== url;
+}
+
export function normalizePdfUrl(value, runtime = globalThis.chrome?.runtime) {
let url;
try {
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 42b4ac30a..450fd0b1f 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -59,6 +59,7 @@ import {
import { executeWikipediaSkillTool } from './wikipedia-offline.js';
import {
isPdfUrl,
+ isPdfHandlerTabUrl,
pdfUrlFromTabUrl,
extractPdfText,
providerSupportsPdfPassthrough,
@@ -9187,7 +9188,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
async _isPdfTab(tabId, pageUrl) {
if (!pageUrl) return false;
const pdfUrl = pdfUrlFromTabUrl(pageUrl);
- if (isPdfUrl(pdfUrl)) return true;
+ if (isPdfUrl(pdfUrl) || isPdfHandlerTabUrl(pageUrl)) return true;
const cached = this._isPdfTabCache.get(tabId);
if (cached && cached.url === pageUrl) return cached.isPdf;
@@ -25209,7 +25210,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (!pdfUrl) {
try {
const tab = await browser.tabs.get(tabId);
- pdfUrl = tab?.url || '';
+ // On our own viewer page the tab URL is the handler wrapping the
+ // real URL in ?url=; fetching the handler HTML would hand pdfjs a
+ // document it cannot parse.
+ pdfUrl = pdfUrlFromTabUrl(tab?.url || '');
} catch {}
}
if (!pdfUrl) {
diff --git a/src/firefox/src/agent/pdf-tools.js b/src/firefox/src/agent/pdf-tools.js
index 845113c81..13899a77a 100644
--- a/src/firefox/src/agent/pdf-tools.js
+++ b/src/firefox/src/agent/pdf-tools.js
@@ -59,6 +59,23 @@ export function pdfUrlFromTabUrl(value, runtime = globalThis.browser?.runtime ||
return unwrapPdfHandlerUrl(url, runtime).href;
}
+// Our viewer page is only ever opened for a response we already identified as
+// a PDF, so a handler tab is a PDF tab by construction. Callers use this to
+// skip the Content-Type probe, which would otherwise decide the question for
+// wrapped URLs like `/download?id=42` β€” and servers routinely answer HEAD with
+// 405, or drop the Content-Type, or burn a one-shot signed download link.
+export function isPdfHandlerTabUrl(value, runtime = globalThis.browser?.runtime || globalThis.chrome?.runtime) {
+ let url;
+ try {
+ url = new URL(String(value || ''));
+ } catch {
+ return false;
+ }
+ // unwrapPdfHandlerUrl returns the same object when there was nothing to
+ // unwrap, so identity is the "this was our handler page" signal.
+ return unwrapPdfHandlerUrl(url, runtime) !== url;
+}
+
/**
* Lazy-load pdfjs only on first PDF read. The legacy bundle is ~1 MB
* and the worker is ~2.3 MB; we don't want to pay that startup cost
diff --git a/test/run.js b/test/run.js
index 83845ce51..f5310bde1 100644
--- a/test/run.js
+++ b/test/run.js
@@ -1353,7 +1353,7 @@ function test(name, fn) { tests.push({ name, fn }); }
console.log('\nselection quote');
-test('read_page redirects PDF handler tabs and probes the unwrapped source URL', async () => {
+test('read_page redirects PDF handler tabs to the unwrapped source URL without probing', async () => {
const previousChrome = globalThis.chrome;
const previousBrowser = globalThis.browser;
const previousFetch = globalThis.fetch;
@@ -1371,9 +1371,13 @@ test('read_page redirects PDF handler tabs and probes the unwrapped source URL',
['chrome', AgentCh, 'chrome', 'chrome-extension'],
['firefox', AgentFx, 'browser', 'moz-extension'],
]) {
- for (const { sourceUrl, expectHeadProbe } of [
- { sourceUrl: 'https://papers.example.test/handler-source.pdf', expectHeadProbe: false },
- { sourceUrl: 'https://papers.example.test/download?id=42', expectHeadProbe: true },
+ // The second URL reveals nothing about its type, so it is the case that
+ // would fall back to a Content-Type probe on an ordinary tab. On our own
+ // handler page it must not: we only open that page for a PDF, and the
+ // probe is a HEAD request servers are free to reject.
+ for (const sourceUrl of [
+ 'https://papers.example.test/handler-source.pdf',
+ 'https://papers.example.test/download?id=42',
]) {
const extensionId = `${label}-pdf-routing`;
const handlerUrl = `${scheme}://${extensionId}/src/ui/pdf-handler.html?url=${encodeURIComponent(sourceUrl)}&tabId=73`;
@@ -1398,14 +1402,18 @@ test('read_page redirects PDF handler tabs and probes the unwrapped source URL',
},
};
const headRequests = [];
+ // Answers "not a PDF" so a probe, if one were made, would suppress the
+ // redirect β€” the redirect must come from recognizing the handler page.
globalThis.fetch = async (url, options) => {
headRequests.push({ url: String(url), method: options?.method || 'GET' });
return {
headers: {
- get: name => name.toLowerCase() === 'content-type' ? 'application/pdf' : '',
+ get: name => name.toLowerCase() === 'content-type' ? 'text/html' : '',
},
};
};
+ delete globalThis.chrome;
+ delete globalThis.browser;
globalThis[apiName] = api;
const agent = new AgentClass({});
agent._richTextToolbarToolBlock = async () => null;
@@ -1428,8 +1436,8 @@ test('read_page redirects PDF handler tabs and probes the unwrapped source URL',
}], `${label}: redirect did not preserve the handler's source URL`);
assert.deepEqual(
headRequests,
- expectHeadProbe ? [{ url: sourceUrl, method: 'HEAD' }] : [],
- `${label}: HEAD probe should use the unwrapped source URL`,
+ [],
+ `${label}: a handler tab should be recognized without a Content-Type probe`,
);
}
}