Skip to content

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

fix(providers): fully redact SVG and purge shares on revocation

Emre Sokullu95d93220f138
diff --git a/src/chrome/src/providers/manager.js b/src/chrome/src/providers/manager.js
index 33bf390a9..eb03b90f2 100644
--- a/src/chrome/src/providers/manager.js
+++ b/src/chrome/src/providers/manager.js
@@ -24,6 +24,7 @@ import {
webgpuModelRuntime,
} from './webgpu.js';
import { ADDITIONAL_PROVIDER_DEFAULTS } from './provider-catalog.js';
+import { purgeShareGenerations } from '../trace/webbrain-share-outbox.js';
// Static, NOT dynamic: this module runs in the MV3 service worker, where
// `await import()` throws "import() is disallowed on ServiceWorkerGlobalScope".
// The provider modules above already import this statically, so it's in the SW
@@ -1885,6 +1886,12 @@ export class ProviderManager {
}
}
this.providers.set(id, this._createProvider(id, merged));
+ // Revocation is permanent for queued data, even if sharing is enabled
+ // again before another agent run. Await deletion before acknowledging it.
+ // Explicit off updates also retry a previously failed purge.
+ if (Object.hasOwn(updates, 'shareQueriesForResearch') && merged.shareQueriesForResearch !== true) {
+ await purgeShareGenerations(entry => String(entry?.provider_id || '') === id);
+ }
await this.save();
}
@@ -1943,6 +1950,9 @@ export class ProviderManager {
: WEBBRAIN_CLOUD_PROVIDER_ID;
}
try {
+ if (duplicate.config?.shareQueriesForResearch === true) {
+ await purgeShareGenerations(entry => String(entry?.provider_id || '') === id);
+ }
await this.save();
} catch (error) {
this.providers.set(id, duplicate);
diff --git a/src/chrome/src/trace/webbrain-share-outbox.js b/src/chrome/src/trace/webbrain-share-outbox.js
index e3713fb08..314b6e109 100644
--- a/src/chrome/src/trace/webbrain-share-outbox.js
+++ b/src/chrome/src/trace/webbrain-share-outbox.js
@@ -37,26 +37,27 @@ function scrubText(value, limit = MAX_MESSAGE_CHARS) {
// 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]+/gi,
+ /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]',
);
- // 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]',
- );
+ // Unhandled forms (including escaped base64) must not leave a tail of
+ // bytes behind after a partial match.
+ if (containsBinaryBlock(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 (text.includes('"base64"')) {
- text = text.replace(/"base64"\s*:\s*"[A-Za-z0-9+/=\r\n]*"/g, '"base64":"[omitted]"');
+ if (/"base64"/i.test(text)) {
+ text = text.replace(/"base64"\s*:\s*"(?:\\[\s\S]|[^"\\])*"/gi, '"base64":"[omitted]"');
}
// Bare base64 blobs may wrap at MIME line boundaries. Count encoded
// characters, including padding, so wrapping cannot bypass the threshold
@@ -390,6 +391,13 @@ async function flushShareOutboxNow(transportProvider, shouldSend) {
if (!snapshot.length) return 0;
const removeIds = new Set();
for (const entry of snapshot) {
+ // A settings change can permanently purge this snapshot's entries, then
+ // re-enable sharing while an earlier send is still pending. Never revive
+ // a deleted entry merely because its provider currently consents again.
+ await storageQueue.catch(() => {});
+ try {
+ if (!(await readOutbox()).some(queued => queued?.id === entry.id)) continue;
+ } catch { break; }
// Revalidate consent immediately before each delivery: a pre-flush purge
// cannot cover revocation that lands mid-flush, and this loop otherwise
// holds a stale snapshot. Revoked entries are dropped, never sent.
diff --git a/src/firefox/src/providers/manager.js b/src/firefox/src/providers/manager.js
index daa07e4d9..e358c533d 100644
--- a/src/firefox/src/providers/manager.js
+++ b/src/firefox/src/providers/manager.js
@@ -6,6 +6,7 @@ import { VertexAnthropicProvider } from './vertex-anthropic.js';
import { signOutClaude } from './oauth-claude.js';
import { AwsBedrockProvider } from './aws-bedrock.js';
import { ADDITIONAL_PROVIDER_DEFAULTS } from './provider-catalog.js';
+import { purgeShareGenerations } from '../trace/webbrain-share-outbox.js';
import { fetchWithTimeout } from './fetch-timeout.js';
import {
VISION_MODES,
@@ -1513,6 +1514,12 @@ export class ProviderManager {
}
}
this.providers.set(id, this._createProvider(id, merged));
+ // Revocation is permanent for queued data, even if sharing is enabled
+ // again before another agent run. Await deletion before acknowledging it.
+ // Explicit off updates also retry a previously failed purge.
+ if (Object.hasOwn(updates, 'shareQueriesForResearch') && merged.shareQueriesForResearch !== true) {
+ await purgeShareGenerations(entry => String(entry?.provider_id || '') === id);
+ }
await this.save();
}
@@ -1571,6 +1578,9 @@ export class ProviderManager {
: WEBBRAIN_CLOUD_PROVIDER_ID;
}
try {
+ if (duplicate.config?.shareQueriesForResearch === true) {
+ await purgeShareGenerations(entry => String(entry?.provider_id || '') === id);
+ }
await this.save();
} catch (error) {
this.providers.set(id, duplicate);
diff --git a/src/firefox/src/trace/webbrain-share-outbox.js b/src/firefox/src/trace/webbrain-share-outbox.js
index e3713fb08..314b6e109 100644
--- a/src/firefox/src/trace/webbrain-share-outbox.js
+++ b/src/firefox/src/trace/webbrain-share-outbox.js
@@ -37,26 +37,27 @@ function scrubText(value, limit = MAX_MESSAGE_CHARS) {
// 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]+/gi,
+ /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]',
);
- // 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]',
- );
+ // Unhandled forms (including escaped base64) must not leave a tail of
+ // bytes behind after a partial match.
+ if (containsBinaryBlock(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 (text.includes('"base64"')) {
- text = text.replace(/"base64"\s*:\s*"[A-Za-z0-9+/=\r\n]*"/g, '"base64":"[omitted]"');
+ if (/"base64"/i.test(text)) {
+ text = text.replace(/"base64"\s*:\s*"(?:\\[\s\S]|[^"\\])*"/gi, '"base64":"[omitted]"');
}
// Bare base64 blobs may wrap at MIME line boundaries. Count encoded
// characters, including padding, so wrapping cannot bypass the threshold
@@ -390,6 +391,13 @@ async function flushShareOutboxNow(transportProvider, shouldSend) {
if (!snapshot.length) return 0;
const removeIds = new Set();
for (const entry of snapshot) {
+ // A settings change can permanently purge this snapshot's entries, then
+ // re-enable sharing while an earlier send is still pending. Never revive
+ // a deleted entry merely because its provider currently consents again.
+ await storageQueue.catch(() => {});
+ try {
+ if (!(await readOutbox()).some(queued => queued?.id === entry.id)) continue;
+ } catch { break; }
// Revalidate consent immediately before each delivery: a pre-flush purge
// cannot cover revocation that lands mid-flush, and this loop otherwise
// holds a stale snapshot. Revoked entries are dropped, never sent.
diff --git a/test/run.js b/test/run.js
index 4f86bff17..442fc38bf 100644
--- a/test/run.js
+++ b/test/run.js
@@ -12729,9 +12729,13 @@ test('Share-for-research strips non-base64 attachment data URLs in text and nest
'data:video/mp4,%00%00%00%20ftyp',
'data:font/woff,%77%4F%46%46',
'data:model/gltf+json,%7B%22asset%22:%7B%7D%7D',
+ 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"><text>PRIVATE_IMAGE_TEXT</text></svg>',
+ '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',
]) {
const content = `before "${dataUrl}" after`;
- const expected = 'before "[embedded data omitted]" after';
+ const expected = '[binary content omitted]';
const item = outbox.buildShareGenerationItem({
finalContent: content,
messages: [
@@ -12741,7 +12745,7 @@ test('Share-for-research strips non-base64 attachment data URLs in text and nest
],
});
assert.equal(item.request[0].content, expected, `${label}: ${dataUrl}`);
- assert.deepEqual(item.request[1].content, ['caption', '[embedded data omitted]']);
+ assert.deepEqual(item.request[1].content, ['caption', '[binary content omitted]']);
assert.deepEqual(item.request[2].content, [{ type: 'text', text: 'Read this.' }]);
assert.equal(item.response.content, expected);
}
@@ -12752,6 +12756,19 @@ test('Share-for-research strips non-base64 attachment data URLs in text and nest
});
assert.equal(item.request[0].content, textUrl, `${label}: plain text URL changed`);
assert.equal(item.response.content, textUrl);
+ for (const content of [
+ JSON.stringify({ result: 'data:image/svg+xml,<svg><text>SECRET</text></svg>' }),
+ 'data:image/png;base64,QU%4ADRA==',
+ JSON.stringify({ result: 'data:image/png;base64,QUJD\nREVG' }),
+ ]) {
+ const escaped = outbox.buildShareGenerationItem({ finalContent: content, messages: [{ role: 'tool', content }] });
+ assert.equal(escaped.request[0].content, '[binary content omitted]', `${label}: escaped attachment leaked`);
+ assert.equal(escaped.response.content, '[binary content omitted]');
+ }
+ const serialized = JSON.stringify({ base64: 'QUJD\nREVG' });
+ const escapedBase64 = outbox.buildShareGenerationItem({ finalContent: serialized, messages: [{ role: 'tool', content: serialized }] });
+ assert.equal(escapedBase64.request[0].content, '{"base64":"[omitted]"}');
+ assert.equal(escapedBase64.response.content, '{"base64":"[omitted]"}');
}
});
@@ -12939,6 +12956,76 @@ test('Share-for-research revoke purge is keyed by provider instance', async () =
}
});
+test('Provider settings permanently purge queued shares on opt-out, including active flush snapshots', async () => {
+ const originalChrome = globalThis.chrome;
+ const originalBrowser = globalThis.browser;
+ try {
+ for (const [label, PM, outbox] of [['chrome', ProviderManagerCh, SHARE_OUTBOX_CH], ['firefox', ProviderManagerFx, SHARE_OUTBOX_FX]]) {
+ const storage = {};
+ const runtime = { storage: { local: {
+ async get(keys) { return Object.fromEntries(keys.map(key => [key, structuredClone(storage[key])])); },
+ async set(values) { Object.assign(storage, structuredClone(values)); },
+ } } };
+ globalThis.chrome = runtime;
+ globalThis.browser = runtime;
+ const manager = new PM();
+ const defaults = manager._defaultConfigs();
+ manager.providers.set('openai', manager._createProvider('openai', { ...defaults.openai, configured: true, shareQueriesForResearch: true }));
+ const { providerId: duplicateId } = await manager.duplicateProvider('openai');
+ await manager.updateProvider(duplicateId, { shareQueriesForResearch: true });
+ const enqueue = (id, providerId = 'openai') => outbox.enqueueShareGeneration({
+ id, provider_id: providerId, request: [{ role: 'user', content: id }], response: { role: 'assistant', content: 'ok' },
+ });
+ const queuedIds = () => storage[outbox.SHARE_OUTBOX_STORAGE_KEY].map(entry => entry.id);
+ const consented = entry => manager.consentedShareProviderIds().has(entry.provider_id);
+ await enqueue('old');
+ await enqueue('other', duplicateId);
+ await manager.updateProvider('openai', { shareQueriesForResearch: false });
+ assert.deepEqual(queuedIds(), ['other'], `${label}: disabling sharing did not immediately purge its entries`);
+ await manager.updateProvider('openai', { shareQueriesForResearch: true });
+ const sent = [];
+ const transport = { async sendShareGeneration(sessionId, payload) { sent.push(payload.client_share_id); return { ok: true }; } };
+ await outbox.flushShareOutbox(transport, consented);
+ assert.deepEqual(sent, ['other'], `${label}: re-enabling revived an old share or purged the duplicate`);
+
+ await enqueue('in-flight');
+ await enqueue('stale-snapshot');
+ let releaseSend;
+ let signalStarted;
+ const started = new Promise(resolve => { signalStarted = resolve; });
+ const flush = outbox.flushShareOutbox({
+ async sendShareGeneration(sessionId, payload) {
+ sent.push(payload.client_share_id);
+ if (payload.client_share_id === 'in-flight') {
+ signalStarted();
+ await new Promise(resolve => { releaseSend = resolve; });
+ }
+ return { ok: true };
+ },
+ }, consented);
+ await started;
+ try {
+ await manager.updateProvider('openai', { shareQueriesForResearch: false });
+ await manager.updateProvider('openai', { shareQueriesForResearch: true });
+ await enqueue('new-opt-in');
+ } finally {
+ releaseSend();
+ await flush;
+ }
+ assert.deepEqual(sent, ['other', 'in-flight'], `${label}: an active flush revived purged data`);
+ await outbox.flushShareOutbox(transport, consented);
+ assert.deepEqual(sent, ['other', 'in-flight', 'new-opt-in'], `${label}: new consent did not allow new entries`);
+
+ await enqueue('removed-duplicate', duplicateId);
+ await manager.removeDuplicateProvider(duplicateId);
+ assert.deepEqual(queuedIds(), [], `${label}: removing a provider retained its queued data`);
+ }
+ } finally {
+ globalThis.chrome = originalChrome;
+ globalThis.browser = originalBrowser;
+ }
+});
+
test('Share-for-research delivery stays opt-in and mirrored across both builds', () => {
const chromeOutbox = fs.readFileSync(path.join(ROOT, 'src/chrome/src/trace/webbrain-share-outbox.js'), 'utf8');
const firefoxOutbox = fs.readFileSync(path.join(ROOT, 'src/firefox/src/trace/webbrain-share-outbox.js'), 'utf8');