Skip to content

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

download_resource_from_page: bound blob reads, refuse streaming playlists

Emre Sokullu5a1e2b4ff58a
diff --git a/src/chrome/src/network/network-tools.js b/src/chrome/src/network/network-tools.js
index 10dc070f1..c9298234c 100644
--- a/src/chrome/src/network/network-tools.js
+++ b/src/chrome/src/network/network-tools.js
@@ -2550,49 +2550,85 @@ function retainGenericDownloadStage(downloadId, releaseToken, signal) {
findDownloadItem(downloadId).then(item => { if (['complete', 'interrupted'].includes(item?.state)) void finish(); }).catch(() => {});
}
+// download_resource_from_page reads the element's resource in the page. A
+// blob is read into memory for chrome.downloads (which cannot follow blob:
+// URLs), so it is bounded and cancelled past maxBytes. A streaming playlist
+// (HLS .m3u8, DASH .mpd) is not the media it plays: saving it would save
+// only its text, so it is refused. Runs in the page: self-contained.
+export async function readPageResourceForDownload(selector, maxBytes) {
+ const playlistError = 'This element plays a streaming playlist (HLS .m3u8 or DASH .mpd), not a media file: saving it would save only the playlist text. Tell the user this stream cannot be saved as one file, or use a media-download skill for the site if one is enabled.';
+ const playlistType = /^(?:application\/(?:vnd\.apple\.mpegurl|x-mpegurl|dash\+xml)|audio\/(?:x-)?mpegurl)\b/i;
+ const el = document.querySelector(selector);
+ if (!el) return { ok: false, error: 'element not found' };
+ // Try every common URL-bearing attribute.
+ const url = el.src || el.href || el.currentSrc ||
+ el.getAttribute('data-src') || el.getAttribute('data-url') || '';
+ if (!url) return { ok: false, error: 'element has no src/href/currentSrc/data-src' };
+ if (/\.(?:m3u8|mpd)(?:[?#]|$)/i.test(url)) return { ok: false, streamingPlaylist: true, error: playlistError };
+ if (url.startsWith('blob:')) {
+ try {
+ const res = await fetch(url);
+ const type = res.headers.get('content-type') || '';
+ if (playlistType.test(type)) return { ok: false, streamingPlaylist: true, error: playlistError };
+ const tooLarge = { ok: false, tooLarge: true, error: `The blob is larger than ${Math.round(maxBytes / 1048576)} MB, more than WebBrain reads into memory; nothing was saved.` };
+ const declared = Number(res.headers.get('content-length'));
+ if (Number.isFinite(declared) && declared > maxBytes) return tooLarge;
+ const chunks = [];
+ let total = 0;
+ if (res.body && typeof res.body.getReader === 'function') {
+ const reader = res.body.getReader();
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ total += value.byteLength;
+ if (total > maxBytes) {
+ try { await reader.cancel(); } catch (e) {}
+ return tooLarge;
+ }
+ chunks.push(value);
+ }
+ } else {
+ const whole = await res.blob();
+ if (whole.size > maxBytes) return tooLarge;
+ chunks.push(whole);
+ }
+ const blob = new Blob(chunks, { type });
+ const dataUrl = await new Promise((resolve, reject) => {
+ const fr = new FileReader();
+ fr.onload = () => resolve(fr.result);
+ fr.onerror = () => reject(fr.error);
+ fr.readAsDataURL(blob);
+ });
+ return { ok: true, url: dataUrl, isBlob: true, mime: blob.type, size: blob.size };
+ } catch (e) {
+ return { ok: false, error: 'failed to read blob URL: ' + e.message };
+ }
+ }
+ // Flag cross-origin resources: the permission gate charged this
+ // download to the PAGE host, but a cross-origin resource is fetched
+ // from a different host β€” route those through download_files, which
+ // gates on the resource's own host.
+ let crossOrigin = false;
+ try { crossOrigin = new URL(url, location.href).origin !== location.origin; } catch (e) {}
+ return { ok: true, url, isBlob: false, crossOrigin };
+}
+
+const PAGE_BLOB_MAX_BYTES = 25 * 1024 * 1024;
+
export async function downloadResourceFromPage(tabId, args = {}, ctx = {}) {
const { selector, filename } = args;
if (!selector) return { success: false, error: 'selector is required' };
try {
const results = await chrome.scripting.executeScript({
target: { tabId },
- func: async (sel) => {
- const el = document.querySelector(sel);
- if (!el) return { ok: false, error: 'element not found' };
- // Try every common URL-bearing attribute.
- let url = el.src || el.href || el.currentSrc ||
- el.getAttribute('data-src') || el.getAttribute('data-url') || '';
- if (!url) return { ok: false, error: 'element has no src/href/currentSrc/data-src' };
-
- // Blob URLs need to be read into a data URL because chrome.downloads
- // can't follow blob:// from background context.
- if (url.startsWith('blob:')) {
- try {
- const res = await fetch(url);
- const blob = await res.blob();
- const dataUrl = await new Promise((resolve, reject) => {
- const fr = new FileReader();
- fr.onload = () => resolve(fr.result);
- fr.onerror = () => reject(fr.error);
- fr.readAsDataURL(blob);
- });
- return { ok: true, url: dataUrl, isBlob: true, mime: blob.type, size: blob.size };
- } catch (e) {
- return { ok: false, error: 'failed to read blob URL: ' + e.message };
- }
- }
- // Flag cross-origin resources: the permission gate charged this
- // download to the PAGE host, but a cross-origin resource is fetched
- // from a different host β€” route those through download_files, which
- // gates on the resource's own host.
- let crossOrigin = false;
- try { crossOrigin = new URL(url, location.href).origin !== location.origin; } catch (e) {}
- return { ok: true, url, isBlob: false, crossOrigin };
- },
- args: [selector],
+ func: readPageResourceForDownload,
+ args: [selector, PAGE_BLOB_MAX_BYTES],
});
const r = results?.[0]?.result;
- if (!r?.ok) return { success: false, error: r?.error || 'extraction failed' };
+ if (!r?.ok) {
+ return { success: false, error: r?.error || 'extraction failed',
+ ...(r?.streamingPlaylist ? { streamingPlaylist: true } : {}), ...(r?.tooLarge ? { tooLarge: true } : {}) };
+ }
if (!r.isBlob && r.crossOrigin) {
return {
success: false,
diff --git a/src/firefox/src/network/network-tools.js b/src/firefox/src/network/network-tools.js
index 939248abb..324428c3e 100644
--- a/src/firefox/src/network/network-tools.js
+++ b/src/firefox/src/network/network-tools.js
@@ -2504,44 +2504,82 @@ function retainGenericDownloadStage(downloadId, releaseToken, signal) {
findDownloadItem(downloadId).then(item => { if (['complete', 'interrupted'].includes(item?.state)) void finish(); }).catch(() => {});
}
+// download_resource_from_page reads the element's resource in the page. A
+// blob is read into memory for chrome.downloads (which cannot follow blob:
+// URLs), so it is bounded and cancelled past maxBytes. A streaming playlist
+// (HLS .m3u8, DASH .mpd) is not the media it plays: saving it would save
+// only its text, so it is refused. Runs in the page: self-contained.
+export async function readPageResourceForDownload(selector, maxBytes) {
+ const playlistError = 'This element plays a streaming playlist (HLS .m3u8 or DASH .mpd), not a media file: saving it would save only the playlist text. Tell the user this stream cannot be saved as one file, or use a media-download skill for the site if one is enabled.';
+ const playlistType = /^(?:application\/(?:vnd\.apple\.mpegurl|x-mpegurl|dash\+xml)|audio\/(?:x-)?mpegurl)\b/i;
+ const el = document.querySelector(selector);
+ if (!el) return { ok: false, error: 'element not found' };
+ // Try every common URL-bearing attribute.
+ const url = el.src || el.href || el.currentSrc ||
+ el.getAttribute('data-src') || el.getAttribute('data-url') || '';
+ if (!url) return { ok: false, error: 'element has no src/href/currentSrc/data-src' };
+ if (/\.(?:m3u8|mpd)(?:[?#]|$)/i.test(url)) return { ok: false, streamingPlaylist: true, error: playlistError };
+ if (url.startsWith('blob:')) {
+ try {
+ const res = await fetch(url);
+ const type = res.headers.get('content-type') || '';
+ if (playlistType.test(type)) return { ok: false, streamingPlaylist: true, error: playlistError };
+ const tooLarge = { ok: false, tooLarge: true, error: `The blob is larger than ${Math.round(maxBytes / 1048576)} MB, more than WebBrain reads into memory; nothing was saved.` };
+ const declared = Number(res.headers.get('content-length'));
+ if (Number.isFinite(declared) && declared > maxBytes) return tooLarge;
+ const chunks = [];
+ let total = 0;
+ if (res.body && typeof res.body.getReader === 'function') {
+ const reader = res.body.getReader();
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ total += value.byteLength;
+ if (total > maxBytes) {
+ try { await reader.cancel(); } catch (e) {}
+ return tooLarge;
+ }
+ chunks.push(value);
+ }
+ } else {
+ const whole = await res.blob();
+ if (whole.size > maxBytes) return tooLarge;
+ chunks.push(whole);
+ }
+ const blob = new Blob(chunks, { type });
+ const dataUrl = await new Promise((resolve, reject) => {
+ const fr = new FileReader();
+ fr.onload = () => resolve(fr.result);
+ fr.onerror = () => reject(fr.error);
+ fr.readAsDataURL(blob);
+ });
+ return { ok: true, url: dataUrl, isBlob: true, mime: blob.type, size: blob.size };
+ } catch (e) {
+ return { ok: false, error: 'failed to read blob URL: ' + e.message };
+ }
+ }
+ // Flag cross-origin resources: the permission gate charged this
+ // download to the PAGE host, but a cross-origin resource is fetched
+ // from a different host β€” route those through download_files, which
+ // gates on the resource's own host.
+ let crossOrigin = false;
+ try { crossOrigin = new URL(url, location.href).origin !== location.origin; } catch (e) {}
+ return { ok: true, url, isBlob: false, crossOrigin };
+}
+
+const PAGE_BLOB_MAX_BYTES = 25 * 1024 * 1024;
+
export async function downloadResourceFromPage(tabId, args = {}, ctx = {}) {
const { selector, filename } = args;
if (!selector) return { success: false, error: 'selector is required' };
try {
- const code = `
- (async () => {
- const el = document.querySelector(${JSON.stringify(selector)});
- if (!el) return { ok: false, error: 'element not found' };
- let url = el.src || el.href || el.currentSrc ||
- el.getAttribute('data-src') || el.getAttribute('data-url') || '';
- if (!url) return { ok: false, error: 'element has no src/href/currentSrc/data-src' };
- if (url.startsWith('blob:')) {
- try {
- const res = await fetch(url);
- const blob = await res.blob();
- const dataUrl = await new Promise((resolve, reject) => {
- const fr = new FileReader();
- fr.onload = () => resolve(fr.result);
- fr.onerror = () => reject(fr.error);
- fr.readAsDataURL(blob);
- });
- return { ok: true, url: dataUrl, isBlob: true, mime: blob.type, size: blob.size };
- } catch (e) {
- return { ok: false, error: 'failed to read blob URL: ' + e.message };
- }
- }
- // Flag cross-origin resources: the permission gate charged this
- // download to the PAGE host, but a cross-origin resource is fetched
- // from a different host β€” route those through download_files, which
- // gates on the resource's own host.
- let crossOrigin = false;
- try { crossOrigin = new URL(url, location.href).origin !== location.origin; } catch (e) {}
- return { ok: true, url, isBlob: false, crossOrigin };
- })()
- `;
+ const code = `(${readPageResourceForDownload.toString()})(${JSON.stringify(selector)}, ${PAGE_BLOB_MAX_BYTES})`;
const results = await browser.tabs.executeScript(tabId, { code });
const r = results?.[0];
- if (!r?.ok) return { success: false, error: r?.error || 'extraction failed' };
+ if (!r?.ok) {
+ return { success: false, error: r?.error || 'extraction failed',
+ ...(r?.streamingPlaylist ? { streamingPlaylist: true } : {}), ...(r?.tooLarge ? { tooLarge: true } : {}) };
+ }
if (!r.isBlob && r.crossOrigin) {
return {
success: false,
diff --git a/test/page-resource-download-browser.mjs b/test/page-resource-download-browser.mjs
new file mode 100644
index 000000000..b79c813ca
--- /dev/null
+++ b/test/page-resource-download-browser.mjs
@@ -0,0 +1,61 @@
+// download_resource_from_page reads the element's resource in the page
+// (network-tools.js readPageResourceForDownload), in real Chromium for both
+// builds' copies: a blob is read up to a size limit and its reader cancelled
+// past it, and a streaming playlist is refused rather than saved as media.
+import assert from 'node:assert/strict';
+import http from 'node:http';
+import { test } from 'node:test';
+import { chromium } from 'playwright';
+
+const api = { runtime: { getURL: value => value, onMessage: { addListener() {} } }, storage: { local: { get: async () => ({}) }, onChanged: { addListener() {} } }, downloads: { onChanged: { addListener() {} } } };
+globalThis.chrome ??= api;
+globalThis.browser ??= api;
+
+const server = http.createServer((request, response) => {
+ response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
+ response.end('<!doctype html><meta charset="utf-8"><title>Resources</title><body></body>');
+});
+await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+const origin = `http://127.0.0.1:${server.address().port}`;
+test.after(() => new Promise(resolve => server.close(resolve)));
+
+for (const build of ['chrome', 'firefox']) {
+ test(`${build}: a page resource is read within its limit, and a streaming playlist is not saved as media`, async () => {
+ const { readPageResourceForDownload } = await import(`../src/${build}/src/network/network-tools.js`);
+ const browser = await chromium.launch({ channel: 'chromium' });
+ try {
+ const page = await (await browser.newContext()).newPage();
+ await page.goto(`${origin}/page`);
+ await page.evaluate(() => {
+ const add = (id, url, tag = 'video') => { const el = document.createElement(tag); el.id = id; el.src = url; document.body.append(el); };
+ add('small', URL.createObjectURL(new Blob([new Uint8Array(2048)], { type: 'video/mp4' })));
+ add('large', URL.createObjectURL(new Blob([new Uint8Array(300 * 1024)], { type: 'video/mp4' })));
+ add('hls-blob', URL.createObjectURL(new Blob(['#EXTM3U\n'], { type: 'application/vnd.apple.mpegurl' })));
+ add('hls', '/stream/master.m3u8?token=1');
+ add('dash', '/stream/manifest.mpd');
+ add('file', '/media/clip.mp4');
+ });
+ const read = (selector, maxBytes) => page.evaluate(({ source, selector, maxBytes }) => {
+ // eslint-disable-next-line no-new-func
+ const fn = new Function(`return (${source})`)();
+ return fn(selector, maxBytes);
+ }, { source: readPageResourceForDownload.toString(), selector, maxBytes });
+ const small = await read('#small', 100 * 1024);
+ assert.equal(small.ok, true, JSON.stringify(small).slice(0, 200));
+ assert.equal(small.size, 2048);
+ assert.match(small.url, /^data:video\/mp4;base64,/);
+ const large = await read('#large', 100 * 1024);
+ assert.deepEqual({ ok: large.ok, tooLarge: large.tooLarge }, { ok: false, tooLarge: true });
+ for (const selector of ['#hls-blob', '#hls', '#dash']) {
+ const playlist = await read(selector, 100 * 1024);
+ assert.equal(playlist.ok, false, selector);
+ assert.equal(playlist.streamingPlaylist, true, selector);
+ assert.match(playlist.error, /streaming playlist/);
+ }
+ const file = await read('#file', 100 * 1024);
+ assert.deepEqual({ ok: file.ok, isBlob: file.isBlob, crossOrigin: file.crossOrigin }, { ok: true, isBlob: false, crossOrigin: false });
+ } finally {
+ await browser.close();
+ }
+ });
+}