Skip to content

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

fix(chrome): retry PDF.js import, derive viewer path, guard double response

Codex release-state teste2ccc2e92a6f
diff --git a/src/chrome/src/agent/pdf-extraction.js b/src/chrome/src/agent/pdf-extraction.js
index 70a8d89ca..27c951545 100644
--- a/src/chrome/src/agent/pdf-extraction.js
+++ b/src/chrome/src/agent/pdf-extraction.js
@@ -11,9 +11,17 @@ export const PDF_EXTRACTION_READY_MESSAGE = 'offscreen-pdf-extract-ready';
export const PDF_PASSTHROUGH_MAX_BYTES = 16 * 1024 * 1024;
const ALLOWED_PDF_PROTOCOLS = new Set(['http:', 'https:', 'file:']);
-const PDF_HANDLER_PAGE = 'src/ui/pdf-handler.html';
+const DEFAULT_PDF_HANDLER_PAGE = 'src/ui/pdf-handler.html';
const BASE64_MAX_INPUT_BYTES = 32 * 1024 * 1024;
+// Derived from the manifest for the same reason as the background path below:
+// renaming the viewer page would otherwise make read_pdf report a bogus
+// "must use http:, https:, or file:" on our own PDF tabs.
+function pdfHandlerPage(runtime) {
+ const manifest = typeof runtime?.getManifest === 'function' ? runtime.getManifest() : null;
+ return manifest?.mime_types_handler?.['application/pdf']?.handler_url || DEFAULT_PDF_HANDLER_PAGE;
+}
+
// When the native MIME handler owns a PDF tab, the tab URL is our own viewer
// page wrapping the real URL in ?url=. read_pdf falls back to the tab URL when
// called without an explicit one, so unwrap it before the scheme check.
@@ -22,7 +30,7 @@ function unwrapPdfHandlerUrl(url, runtime = globalThis.chrome?.runtime) {
if (typeof runtime?.getURL !== 'function') return url;
let handler;
try {
- handler = new URL(runtime.getURL(PDF_HANDLER_PAGE));
+ handler = new URL(runtime.getURL(pdfHandlerPage(runtime)));
} catch {
return url;
}
diff --git a/src/chrome/src/offscreen/pdf-extraction-host.js b/src/chrome/src/offscreen/pdf-extraction-host.js
index 26332406b..6287eb1bc 100644
--- a/src/chrome/src/offscreen/pdf-extraction-host.js
+++ b/src/chrome/src/offscreen/pdf-extraction-host.js
@@ -8,6 +8,9 @@ import {
isTrustedPdfExtractionSender,
normalizePdfUrl,
} from '../agent/pdf-extraction.js';
+
+const PDF_MESSAGE_TYPES = new Set([PDF_EXTRACTION_MESSAGE, PDF_EXTRACTION_READY_MESSAGE]);
+
let pdfjsPromise = null;
function getPdfjs() {
@@ -17,18 +20,38 @@ function getPdfjs() {
chrome.runtime.getURL('vendor/pdfjs/pdf.worker.mjs');
return pdfjs;
});
+ // The offscreen document outlives any single read, so never memoize a
+ // rejection: one transient import failure would otherwise make every
+ // later read_pdf fail until the document is torn down.
+ pdfjsPromise.catch(() => { pdfjsPromise = null; });
}
return pdfjsPromise;
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
- if (![PDF_EXTRACTION_MESSAGE, PDF_EXTRACTION_READY_MESSAGE].includes(message?.type)) return false;
+ if (!PDF_MESSAGE_TYPES.has(message?.type)) return false;
+
+ // A 16 MB PDF answers with ~21 MB of base64, and an oversized or
+ // unserializable reply makes sendResponse throw. Without this guard that
+ // throw would reach the catch below and call sendResponse a second time on
+ // an already-closed channel, hiding the original failure.
+ let responded = false;
+ const respond = (payload) => {
+ if (responded) return;
+ responded = true;
+ try {
+ sendResponse(payload);
+ } catch (error) {
+ console.warn('[pdf-extraction-host] failed to deliver response', error);
+ }
+ };
+
if (!isTrustedPdfExtractionSender(sender)) {
- sendResponse({ ok: false, ready: false, error: 'Unauthorized PDF extraction sender.' });
+ respond({ ok: false, ready: false, error: 'Unauthorized PDF extraction sender.' });
return false;
}
if (message.type === PDF_EXTRACTION_READY_MESSAGE) {
- sendResponse({ ok: true, ready: true });
+ respond({ ok: true, ready: true });
return false;
}
@@ -47,9 +70,9 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const pdfjs = await getPdfjs();
const result = await extractPdfTextFromBytes(pdfjs, bytes, message.options || {});
if (passthrough) result._pdfBase64 = bytesToBase64(passthrough);
- sendResponse({ ok: true, result });
+ respond({ ok: true, result });
})().catch((error) => {
- sendResponse({ ok: false, error: error?.message || String(error) });
+ respond({ ok: false, error: error?.message || String(error) });
});
return true;
diff --git a/test/pdf-read.mjs b/test/pdf-read.mjs
index 88e876701..32742a4a4 100644
--- a/test/pdf-read.mjs
+++ b/test/pdf-read.mjs
@@ -27,6 +27,9 @@ async function testMv3PdfExtractionUsesTheSharedOffscreenHost() {
assert.match(host, /extractPdfTextFromBytes/);
assert.match(host, /isTrustedPdfExtractionSender\(sender\)/);
assert.match(offscreenHtml, /<script type="module" src="pdf-extraction-host\.js"><\/script>/);
+ // The offscreen document outlives any single read, so a failed PDF.js import
+ // must not be memoized into every later read_pdf.
+ assert.match(host, /pdfjsPromise\.catch\(\(\) => \{ pdfjsPromise = null; \}\);/);
}
async function testPdfUrlAndSenderBoundaries() {
@@ -74,6 +77,22 @@ async function testPdfUrlAndSenderBoundaries() {
/must use http.*https.*file/i,
'An unwrapped inner URL must still pass the scheme allowlist.');
+ // The viewer page follows the manifest too, so renaming it cannot start
+ // rejecting our own PDF tabs with a bogus scheme error.
+ const renamedHandlerRuntime = {
+ ...runtime,
+ getManifest: () => ({ mime_types_handler: { 'application/pdf': { handler_url: 'src/ui/viewer.html' } } }),
+ };
+ assert.equal(
+ normalizePdfUrl(
+ `${runtime.getURL('src/ui/viewer.html')}?url=${encodeURIComponent('https://example.test/paper.pdf')}&tabId=7`,
+ renamedHandlerRuntime),
+ 'https://example.test/paper.pdf');
+ assert.throws(
+ () => normalizePdfUrl(handlerUrl, renamedHandlerRuntime),
+ /must use http.*https.*file/i,
+ 'Only the manifest-declared viewer page may unwrap an inner URL.');
+
let fetchCalls = 0;
const originalFetch = globalThis.fetch;
globalThis.fetch = async () => {