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: bound the data URL metadata walk in tab-chat persistence
Codex release-state testf390456ddacf
diff --git a/src/chrome/src/ui/tab-chat-persistence.js b/src/chrome/src/ui/tab-chat-persistence.jsindex 281497973..5b8397448 100644--- a/src/chrome/src/ui/tab-chat-persistence.js+++ b/src/chrome/src/ui/tab-chat-persistence.js@@ -40,6 +40,17 @@ function isBase64Code(code) {|| code === 61;}+function isMediaParameterCode(code) {+ return isMimeTypeCode(code)+ || code === 33+ || code === 37+ || code === 39+ || code === 42+ || code === 61+ || code === 95+ || code === 126;+}+function findImageDataUrl(source, start) {const firstCode = IMAGE_DATA_URL_PREFIX.charCodeAt(0);for (let index = start; index <= source.length - IMAGE_DATA_URL_PREFIX.length; index++) {@@ -56,9 +67,13 @@ function imageDataPayloadEnd(source, marker) {if (cursor === mimeStart) return -1;// Walk the short metadata section without applying a regexp to the image- // bytes. A comma before `;base64,` means this is not a base64 data URL.- while (cursor < source.length) {- if (source.charCodeAt(cursor) === 44) return -1;+ // bytes. Only `;name=value` media-type parameters may sit between the MIME+ // type and `;base64,`, so the walk stops at the first character that cannot+ // belong to one (a quote, a tag boundary, whitespace, or the comma that+ // starts a non-base64 payload). Without that bound the scan would run past+ // the attribute it started in and swallow every character up to the next+ // image in the document.+ while (cursor < source.length && source.charCodeAt(cursor) === 59) {if (matchesAsciiIgnoreCase(source, cursor, BASE64_DATA_URL_MARKER)) {cursor += BASE64_DATA_URL_MARKER.length;const payloadStart = cursor;@@ -66,6 +81,7 @@ function imageDataPayloadEnd(source, marker) {return cursor > payloadStart ? cursor : -1;}cursor++;+ while (cursor < source.length && isMediaParameterCode(source.charCodeAt(cursor))) cursor++;}return -1;}diff --git a/src/firefox/src/ui/tab-chat-persistence.js b/src/firefox/src/ui/tab-chat-persistence.jsindex d214620b9..eae3d143f 100644--- a/src/firefox/src/ui/tab-chat-persistence.js+++ b/src/firefox/src/ui/tab-chat-persistence.js@@ -40,6 +40,17 @@ function isBase64Code(code) {|| code === 61;}+function isMediaParameterCode(code) {+ return isMimeTypeCode(code)+ || code === 33+ || code === 37+ || code === 39+ || code === 42+ || code === 61+ || code === 95+ || code === 126;+}+function findImageDataUrl(source, start) {const firstCode = IMAGE_DATA_URL_PREFIX.charCodeAt(0);for (let index = start; index <= source.length - IMAGE_DATA_URL_PREFIX.length; index++) {@@ -56,9 +67,13 @@ function imageDataPayloadEnd(source, marker) {if (cursor === mimeStart) return -1;// Walk the short metadata section without applying a regexp to the image- // bytes. A comma before `;base64,` means this is not a base64 data URL.- while (cursor < source.length) {- if (source.charCodeAt(cursor) === 44) return -1;+ // bytes. Only `;name=value` media-type parameters may sit between the MIME+ // type and `;base64,`, so the walk stops at the first character that cannot+ // belong to one (a quote, a tag boundary, whitespace, or the comma that+ // starts a non-base64 payload). Without that bound the scan would run past+ // the attribute it started in and swallow every character up to the next+ // image in the document.+ while (cursor < source.length && source.charCodeAt(cursor) === 59) {if (matchesAsciiIgnoreCase(source, cursor, BASE64_DATA_URL_MARKER)) {cursor += BASE64_DATA_URL_MARKER.length;const payloadStart = cursor;@@ -66,6 +81,7 @@ function imageDataPayloadEnd(source, marker) {return cursor > payloadStart ? cursor : -1;}cursor++;+ while (cursor < source.length && isMediaParameterCode(source.charCodeAt(cursor))) cursor++;}return -1;}diff --git a/test/run.js b/test/run.jsindex bb153e84b..8337b6239 100644--- a/test/run.js+++ b/test/run.js@@ -43802,6 +43802,30 @@ test('tab-chat persistence strips large and mixed-case image data URLs without cassert.equal(chromeResult, firefoxResult, 'chrome/firefox image compaction diverged');});+test('tab-chat persistence keeps markup between a bare image MIME mention and a later payload', () => {+ const input = [+ '<div>paste as data:image/png here</div>',+ '<b>kept</b>',+ `<img src="data:image/gif;base64,${'A'.repeat(64)}">`,+ ].join('');+ const expected = [+ '<div>paste as data:image/png here</div>',+ '<b>kept</b>',+ `<img src="${TabChatPersistenceCh.TRANSPARENT_PIXEL_PNG_DATA_URL}">`,+ ].join('');++ for (const [label, persistence] of [+ ['chrome', TabChatPersistenceCh],+ ['firefox', TabChatPersistenceFx],+ ]) {+ assert.equal(+ persistence.stripImagePayloadsForPersist(input),+ expected,+ `${label}: markup between a bare MIME mention and a later image payload was swallowed`,+ );+ }+});+test('tab-chat persistence never evicts other chats when shared quota remains exhausted', async () => {for (const [label, persistence] of [['chrome', TabChatPersistenceCh],