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): strip non-base64 attachment data URLs
Emre Sokullu00ee165bffe4
diff --git a/src/chrome/src/trace/webbrain-share-outbox.js b/src/chrome/src/trace/webbrain-share-outbox.jsindex 975ba697d..e3713fb08 100644--- a/src/chrome/src/trace/webbrain-share-outbox.js+++ b/src/chrome/src/trace/webbrain-share-outbox.js@@ -45,6 +45,13 @@ function scrubText(value, limit = MAX_MESSAGE_CHARS) {/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]+/gi,'[embedded base64 data omitted]',);+ // Data URLs can also carry percent-encoded or unencoded bytes (SVG,+ // PDFs, etc.). Their media type identifies the attachment regardless of+ // encoding. Stop at text/JSON delimiters to preserve surrounding prose.+ text = text.replace(+ /data:(image|audio|video|application|font|model)\/[^,\s"'<>]+,[^\s"'<>]*/gi,+ '[embedded data omitted]',+ );}// Serialized tool results can contain binaries of any size. Scrub named// base64 fields even when their payload is below the bare-blob threshold.@@ -66,9 +73,9 @@ 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') {- // Case-insensitive with optional media-type parameters, matching scrubText.+ // Match binary media types regardless of data-URL encoding or parameters.return /data:/i.test(value)- && /data:(image|audio|video|application)\/[^,]+;base64,/i.test(value);+ && /data:(image|audio|video|application|font|model)\/[^,\s"'<>]+,/i.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 975ba697d..e3713fb08 100644--- a/src/firefox/src/trace/webbrain-share-outbox.js+++ b/src/firefox/src/trace/webbrain-share-outbox.js@@ -45,6 +45,13 @@ function scrubText(value, limit = MAX_MESSAGE_CHARS) {/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]+/gi,'[embedded base64 data omitted]',);+ // Data URLs can also carry percent-encoded or unencoded bytes (SVG,+ // PDFs, etc.). Their media type identifies the attachment regardless of+ // encoding. Stop at text/JSON delimiters to preserve surrounding prose.+ text = text.replace(+ /data:(image|audio|video|application|font|model)\/[^,\s"'<>]+,[^\s"'<>]*/gi,+ '[embedded data omitted]',+ );}// Serialized tool results can contain binaries of any size. Scrub named// base64 fields even when their payload is below the bare-blob threshold.@@ -66,9 +73,9 @@ 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') {- // Case-insensitive with optional media-type parameters, matching scrubText.+ // Match binary media types regardless of data-URL encoding or parameters.return /data:/i.test(value)- && /data:(image|audio|video|application)\/[^,]+;base64,/i.test(value);+ && /data:(image|audio|video|application|font|model)\/[^,\s"'<>]+,/i.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 91ca6fd88..4f86bff17 100644--- a/test/run.js+++ b/test/run.js@@ -12718,6 +12718,43 @@ 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', () => {+ 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',+ 'DATA:IMAGE/SVG+XML;charset=utf-8,%3Csvg%3E%3C/svg%3E',+ 'data:application/pdf,%25PDF-1.7%0Aendobj',+ 'data:application/octet-stream,ABC',+ 'data:audio/wav,%52%49%46%46',+ 'data:video/mp4,%00%00%00%20ftyp',+ 'data:font/woff,%77%4F%46%46',+ 'data:model/gltf+json,%7B%22asset%22:%7B%7D%7D',+ ]) {+ const content = `before "${dataUrl}" after`;+ const expected = 'before "[embedded data omitted]" after';+ const item = outbox.buildShareGenerationItem({+ finalContent: content,+ messages: [+ { role: 'tool', content },+ { role: 'tool', content: ['caption', dataUrl, { type: 'text', text: dataUrl }] },+ { role: 'user', content: [{ type: 'text', text: 'Read this.' }, { type: 'other', source: { url: dataUrl } }] },+ ],+ });+ assert.equal(item.request[0].content, expected, `${label}: ${dataUrl}`);+ assert.deepEqual(item.request[1].content, ['caption', '[embedded data omitted]']);+ assert.deepEqual(item.request[2].content, [{ type: 'text', text: 'Read this.' }]);+ assert.equal(item.response.content, expected);+ }+ const textUrl = 'data:text/plain,ordinary%20text';+ const item = outbox.buildShareGenerationItem({+ finalContent: textUrl,+ messages: [{ role: 'user', content: textUrl }],+ });+ assert.equal(item.request[0].content, textUrl, `${label}: plain text URL changed`);+ assert.equal(item.response.content, textUrl);+ }+});+test('Share-for-research caps count wrapper messages and serialized overhead', () => {const messages = [{ role: 'system', content: 'SYS' }];for (let i = 0; i < 250; i++) messages.push({ role: 'user', content: `cap${i}-` + 'word '.repeat(150) });