Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
fix(providers): omit complete text values containing attachment URLs
Emre Sokullu7849097a3003
diff --git a/src/chrome/src/trace/webbrain-share-outbox.js b/src/chrome/src/trace/webbrain-share-outbox.jsindex 314b6e109..fae2a3b20 100644--- a/src/chrome/src/trace/webbrain-share-outbox.js+++ b/src/chrome/src/trace/webbrain-share-outbox.js@@ -11,6 +11,7 @@ const MAX_OUTBOX_ITEMS = 100;const MAX_MESSAGE_CHARS = 10_000;const MAX_REQUEST_BUDGET = 150_000;const MAX_RESPONSE_CHARS = 40_000;+const BINARY_DATA_URL = /data:(image|audio|video|application|font|model)\/[^\s;,]+(?:\s*;[^,]*)?\s*,/i;let storageQueue = Promise.resolve();let flushQueue = Promise.resolve();let fallbackRunCounter = 0;@@ -34,26 +35,10 @@ function clampText(value, limit = MAX_MESSAGE_CHARS) {function scrubText(value, limit = MAX_MESSAGE_CHARS) {if (typeof value !== 'string') return value;let text = value;- // Non-canonical forms count too: uppercase scheme (DATA:...), extra- // media-type parameters (...;charset=utf-8;base64,...). Case-insensitive.- if (/data:/i.test(text)) {- const headers = text.match(/data:(image|audio|video|application|font|model)\/[^,\s"'<>]+,/gi) || [];- // Non-base64 data URLs may contain raw markup, quotes, and whitespace.- // Their end cannot be distinguished reliably from surrounding text, so- // omit the whole value instead of risking a partial attachment upload.- if (headers.some(header => !/;base64,$/i.test(header))) return '[binary content omitted]';- // Detect data URIs regardless of surrounding string or payload length:- // even a 1x1 canvas/QR thumbnail is image bytes the UI promises to strip.- // Newlines (but not spaces/words) are allowed inside the payload so- // wrapped base64 is still removed while surrounding prose survives.- text = text.replace(- /data:(image|audio|video|application|font|model)\/[a-zA-Z0-9+.-]+(?:;[a-zA-Z0-9!#$&^_.+-]+(?:=[a-zA-Z0-9!#$&^_.+-]+)?)*;base64,[A-Za-z0-9+/=\r\n]+(?=$|[\s"'<>])/gi,- '[embedded base64 data omitted]',- );- // Unhandled forms (including escaped base64) must not leave a tail of- // bytes behind after a partial match.- if (containsBinaryBlock(text)) return '[binary content omitted]';- }+ // Data URL payloads can contain markup, quotes, whitespace, and escapes,+ // even with base64 encoding. Omit the entire value: guessing its end can+ // leave image/file bytes behind. The media type is enough to identify it.+ if (BINARY_DATA_URL.test(text)) return '[binary content omitted]';// Serialized tool results can contain binaries of any size. Scrub named// base64 fields even when their payload is below the bare-blob threshold.if (/"base64"/i.test(text)) {@@ -74,9 +59,7 @@ function containsBinaryBlock(value) {// screenshot bytes echoed into a tool result). Check anywhere in the// string, at any length: even tiny thumbnails are image bytes.if (typeof value === 'string') {- // Match binary media types regardless of data-URL encoding or parameters.- return /data:/i.test(value)- && /data:(image|audio|video|application|font|model)\/[^,\s"'<>]+,/i.test(value);+ return BINARY_DATA_URL.test(value);}if (!value || typeof value !== 'object') return false;const type = typeof value.type === 'string' ? value.type.toLowerCase() : '';diff --git a/src/firefox/src/trace/webbrain-share-outbox.js b/src/firefox/src/trace/webbrain-share-outbox.jsindex 314b6e109..fae2a3b20 100644--- a/src/firefox/src/trace/webbrain-share-outbox.js+++ b/src/firefox/src/trace/webbrain-share-outbox.js@@ -11,6 +11,7 @@ const MAX_OUTBOX_ITEMS = 100;const MAX_MESSAGE_CHARS = 10_000;const MAX_REQUEST_BUDGET = 150_000;const MAX_RESPONSE_CHARS = 40_000;+const BINARY_DATA_URL = /data:(image|audio|video|application|font|model)\/[^\s;,]+(?:\s*;[^,]*)?\s*,/i;let storageQueue = Promise.resolve();let flushQueue = Promise.resolve();let fallbackRunCounter = 0;@@ -34,26 +35,10 @@ function clampText(value, limit = MAX_MESSAGE_CHARS) {function scrubText(value, limit = MAX_MESSAGE_CHARS) {if (typeof value !== 'string') return value;let text = value;- // Non-canonical forms count too: uppercase scheme (DATA:...), extra- // media-type parameters (...;charset=utf-8;base64,...). Case-insensitive.- if (/data:/i.test(text)) {- const headers = text.match(/data:(image|audio|video|application|font|model)\/[^,\s"'<>]+,/gi) || [];- // Non-base64 data URLs may contain raw markup, quotes, and whitespace.- // Their end cannot be distinguished reliably from surrounding text, so- // omit the whole value instead of risking a partial attachment upload.- if (headers.some(header => !/;base64,$/i.test(header))) return '[binary content omitted]';- // Detect data URIs regardless of surrounding string or payload length:- // even a 1x1 canvas/QR thumbnail is image bytes the UI promises to strip.- // Newlines (but not spaces/words) are allowed inside the payload so- // wrapped base64 is still removed while surrounding prose survives.- text = text.replace(- /data:(image|audio|video|application|font|model)\/[a-zA-Z0-9+.-]+(?:;[a-zA-Z0-9!#$&^_.+-]+(?:=[a-zA-Z0-9!#$&^_.+-]+)?)*;base64,[A-Za-z0-9+/=\r\n]+(?=$|[\s"'<>])/gi,- '[embedded base64 data omitted]',- );- // Unhandled forms (including escaped base64) must not leave a tail of- // bytes behind after a partial match.- if (containsBinaryBlock(text)) return '[binary content omitted]';- }+ // Data URL payloads can contain markup, quotes, whitespace, and escapes,+ // even with base64 encoding. Omit the entire value: guessing its end can+ // leave image/file bytes behind. The media type is enough to identify it.+ if (BINARY_DATA_URL.test(text)) return '[binary content omitted]';// Serialized tool results can contain binaries of any size. Scrub named// base64 fields even when their payload is below the bare-blob threshold.if (/"base64"/i.test(text)) {@@ -74,9 +59,7 @@ function containsBinaryBlock(value) {// screenshot bytes echoed into a tool result). Check anywhere in the// string, at any length: even tiny thumbnails are image bytes.if (typeof value === 'string') {- // Match binary media types regardless of data-URL encoding or parameters.- return /data:/i.test(value)- && /data:(image|audio|video|application|font|model)\/[^,\s"'<>]+,/i.test(value);+ return BINARY_DATA_URL.test(value);}if (!value || typeof value !== 'object') return false;const type = typeof value.type === 'string' ? value.type.toLowerCase() : '';diff --git a/test/run.js b/test/run.jsindex 442fc38bf..4905f09e0 100644--- a/test/run.js+++ b/test/run.js@@ -12578,7 +12578,7 @@ test('Share-for-research scrub removes embedded data URIs and keeps repeated-ansconst serialized = JSON.stringify(item);assert.equal(serialized.includes('iVBORw0KGgoAAAANSUhEUg'), false, `${label}: tool-result data URI escaped the scrub`);assert.equal(serialized.includes('ABCD1234abcd'), false, `${label}: array string data URI escaped the scrub`);- assert.match(serialized, /embedded base64 data omitted/, `${label}: data-URI placeholder missing`);+ assert.match(serialized, /binary content omitted/, `${label}: data-URI placeholder missing`);// Tiny thumbnails in short strings must be scrubbed too, not just long payloads.const tiny = outbox.buildShareGenerationItem({runId: `run-share-tiny-${label}`,@@ -12718,7 +12718,7 @@ test('Share-for-research scrubs wrapped bare base64 without counting line breaks}});-test('Share-for-research strips non-base64 attachment data URLs in text and nested content', () => {+test('Share-for-research omits text and nested content containing attachment data URLs', () => {for (const [label, outbox] of [['chrome', SHARE_OUTBOX_CH], ['firefox', SHARE_OUTBOX_FX]]) {for (const dataUrl of ['data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3C/svg%3E',@@ -12733,6 +12733,10 @@ test('Share-for-research strips non-base64 attachment data URLs in text and nest'data:image/svg+xml,<?xml version="1.0"?>\n<svg><rect width="10" height="10"/></svg>','data:image/svg+xml,<svg viewBox="0 0 10 10"/>','data:image/svg+xml,<svg><text>INCOMPLETE_IMAGE',+ 'data:image/png;base64,QU JD',+ 'data:image/png;base64,QU\tJD',+ 'data:image/png;name="sample";base64,QUJD',+ 'data:image/png;name="sample,file";base64,QUJD',]) {const content = `before "${dataUrl}" after`;const expected = '[binary content omitted]';