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: reject malformed API preferences at startup and live updates
Emre Sokulluf447119df53f
diff --git a/docs/architecture.md b/docs/architecture.mdindex 049766d15..8c0df548d 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -869,7 +869,8 @@ Three independent detectors run after every tool call:When the API mutation observer is enabled and a repeated `click` /`click_ax` loop is detected, `_detectApiShortcut()` checks the per-tabwebRequest buffer populated by `background.js`. The observer is on by default,-can be disabled in Settings, and stays disabled if its preference cannot be read.+can be disabled in Settings, and stays disabled if its preference cannot be read+or contains a non-boolean value.If each repeated click produced the same exact URL + HTTP method within a3-second window, the loop warning includes a `fetch_url({url, method})`suggestion. For replayable XHR/fetch mutations, the observer also keeps boundeddiff --git a/docs/security-model.md b/docs/security-model.mdindex e8316333f..600a967c6 100644--- a/docs/security-model.md+++ b/docs/security-model.md@@ -161,9 +161,9 @@ The primary threat: a malicious page crafts content that, when read by the agentSet per-conversation via the `/allow-api` slash command in the side panel, orpersistently with **Always allow API mutations** under **Settings β General β-Advanced**. The persistent setting is on by default. If it cannot be read from-storage, it grants no authorization. When either option is active, it waives-the permission prompt for **write-method network egress only**:+Advanced**. The persistent setting is on by default when absent. Read failures+and non-boolean stored values grant no authorization. When either option is+active, it waives the permission prompt for **write-method network egress only**:- `fetch_url` / `research_url` with `method: POST/PUT/PATCH/DELETE`diff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex c58e3ac22..39d241012 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -196,7 +196,7 @@ const ALWAYS_ALLOW_API_MUTATIONS_KEY = 'alwaysAllowApiMutations';const alwaysAllowApiMutationsReady = chrome.storage.local.get({ [ALWAYS_ALLOW_API_MUTATIONS_KEY]: true }).then((stored) => {- agent.setAlwaysAllowApiMutations(stored[ALWAYS_ALLOW_API_MUTATIONS_KEY] !== false);+ agent.setAlwaysAllowApiMutations(stored[ALWAYS_ALLOW_API_MUTATIONS_KEY] === true);}).catch(() => {// An unreadable setting must not bypass a stored opt-out.@@ -1205,7 +1205,8 @@ chrome.storage.onChanged.addListener((changes, areaName) => {// wiping the chat history.let refreshPrompts = false;if (changes[ALWAYS_ALLOW_API_MUTATIONS_KEY]) {- agent.setAlwaysAllowApiMutations(changes[ALWAYS_ALLOW_API_MUTATIONS_KEY].newValue !== false);+ const value = changes[ALWAYS_ALLOW_API_MUTATIONS_KEY].newValue;+ agent.setAlwaysAllowApiMutations(value === undefined || value === true);refreshPrompts = true;}if (changes.useSiteAdapters) {@@ -1232,7 +1233,8 @@ chrome.storage.onChanged.addListener((changes, areaName) => {});}if (changes[API_MUTATION_OBSERVER_KEY]) {- setApiMutationObserverEnabled(changes[API_MUTATION_OBSERVER_KEY].newValue !== false);+ const value = changes[API_MUTATION_OBSERVER_KEY].newValue;+ setApiMutationObserverEnabled(value === undefined || value === true);}if (changes.strictSecretMode) {agent.strictSecretMode = !!changes.strictSecretMode.newValue;@@ -2582,7 +2584,7 @@ function setApiMutationObserverEnabled(enabled) {async function loadApiMutationObserverSetting() {try {const stored = await chrome.storage.local.get({ [API_MUTATION_OBSERVER_KEY]: API_MUTATION_OBSERVER_DEFAULT });- setApiMutationObserverEnabled(stored[API_MUTATION_OBSERVER_KEY] !== false);+ setApiMutationObserverEnabled(stored[API_MUTATION_OBSERVER_KEY] === true);} catch (e) {// Do not capture requests when a stored opt-out cannot be read.setApiMutationObserverEnabled(false);diff --git a/src/chrome/src/ui/settings.js b/src/chrome/src/ui/settings.jsindex 0f3512704..da9dde953 100644--- a/src/chrome/src/ui/settings.js+++ b/src/chrome/src/ui/settings.js@@ -841,8 +841,8 @@ async function init() {siteAdaptersToggle.checked = stored.useSiteAdapters ?? true;if (researchEscalationToggle) researchEscalationToggle.checked = stored.researchEscalationEnabled === true;if (voiceInputToggle) voiceInputToggle.checked = stored.voiceInputEnabled ?? true;- alwaysAllowApiMutationsToggle.checked = stored.alwaysAllowApiMutations !== false;- apiMutationObserverToggle.checked = stored.apiMutationObserverEnabled !== false;+ alwaysAllowApiMutationsToggle.checked = stored.alwaysAllowApiMutations === undefined || stored.alwaysAllowApiMutations === true;+ apiMutationObserverToggle.checked = stored.apiMutationObserverEnabled === undefined || stored.apiMutationObserverEnabled === true;if (webMcpToggle) webMcpToggle.checked = stored.webMcpEnabled === true; // off by defaultif (openAIAskStreamingToggle) openAIAskStreamingToggle.checked = stored.openaiAskStreamingEnabled !== false;if (planBeforeActModeSelect) {diff --git a/src/chrome/src/ui/sidepanel.js b/src/chrome/src/ui/sidepanel.jsindex 11ab1723a..72b8e5930 100644--- a/src/chrome/src/ui/sidepanel.js+++ b/src/chrome/src/ui/sidepanel.js@@ -4530,7 +4530,7 @@ async function init() {const stored = await chrome.storage.local.get(['verboseMode', 'alwaysAllowApiMutations']);verboseMode = stored.verboseMode || false;syncProgressDisplayMode();- alwaysAllowApiMutations = stored.alwaysAllowApiMutations !== false;+ alwaysAllowApiMutations = stored.alwaysAllowApiMutations === undefined || stored.alwaysAllowApiMutations === true;syncApiMutationsAllowedForCurrentTab();// Restore prior conversation for this tab (if any) β survives close/reopen.@@ -4594,7 +4594,8 @@ async function init() {syncProgressDisplayMode();}if (changes.alwaysAllowApiMutations) {- alwaysAllowApiMutations = changes.alwaysAllowApiMutations.newValue !== false;+ const value = changes.alwaysAllowApiMutations.newValue;+ alwaysAllowApiMutations = value === undefined || value === true;syncApiMutationsAllowedForCurrentTab();}if (changes.providers || changes.activeProvider) {diff --git a/src/firefox/src/background.js b/src/firefox/src/background.jsindex dc1608b64..cdb7eb961 100644--- a/src/firefox/src/background.js+++ b/src/firefox/src/background.js@@ -144,7 +144,7 @@ const ALWAYS_ALLOW_API_MUTATIONS_KEY = 'alwaysAllowApiMutations';const alwaysAllowApiMutationsReady = browser.storage.local.get({ [ALWAYS_ALLOW_API_MUTATIONS_KEY]: true }).then((stored) => {- agent.setAlwaysAllowApiMutations(stored[ALWAYS_ALLOW_API_MUTATIONS_KEY] !== false);+ agent.setAlwaysAllowApiMutations(stored[ALWAYS_ALLOW_API_MUTATIONS_KEY] === true);}).catch(() => {// An unreadable setting must not bypass a stored opt-out.@@ -1067,7 +1067,8 @@ browser.storage.onChanged.addListener((changes) => {}let refreshPrompts = false;if (changes[ALWAYS_ALLOW_API_MUTATIONS_KEY]) {- agent.setAlwaysAllowApiMutations(changes[ALWAYS_ALLOW_API_MUTATIONS_KEY].newValue !== false);+ const value = changes[ALWAYS_ALLOW_API_MUTATIONS_KEY].newValue;+ agent.setAlwaysAllowApiMutations(value === undefined || value === true);refreshPrompts = true;}if (changes.useSiteAdapters) {@@ -1084,7 +1085,8 @@ browser.storage.onChanged.addListener((changes) => {refreshPrompts = true;}if (changes[API_MUTATION_OBSERVER_KEY]) {- setApiMutationObserverEnabled(changes[API_MUTATION_OBSERVER_KEY].newValue !== false);+ const value = changes[API_MUTATION_OBSERVER_KEY].newValue;+ setApiMutationObserverEnabled(value === undefined || value === true);}if (changes.strictSecretMode) {agent.strictSecretMode = !!changes.strictSecretMode.newValue;@@ -1726,7 +1728,7 @@ function setApiMutationObserverEnabled(enabled) {async function loadApiMutationObserverSetting() {try {const stored = await browser.storage.local.get({ [API_MUTATION_OBSERVER_KEY]: API_MUTATION_OBSERVER_DEFAULT });- setApiMutationObserverEnabled(stored[API_MUTATION_OBSERVER_KEY] !== false);+ setApiMutationObserverEnabled(stored[API_MUTATION_OBSERVER_KEY] === true);} catch (e) {// Do not capture requests when a stored opt-out cannot be read.setApiMutationObserverEnabled(false);diff --git a/src/firefox/src/ui/settings.js b/src/firefox/src/ui/settings.jsindex 2600c11df..bb10f3f9d 100644--- a/src/firefox/src/ui/settings.js+++ b/src/firefox/src/ui/settings.js@@ -610,8 +610,8 @@ async function init() {if (siteAdaptersToggle) siteAdaptersToggle.checked = stored.useSiteAdapters ?? true;if (researchEscalationToggle) researchEscalationToggle.checked = stored.researchEscalationEnabled === true;if (voiceInputToggle) voiceInputToggle.checked = stored.voiceInputEnabled ?? true;- if (alwaysAllowApiMutationsToggle) alwaysAllowApiMutationsToggle.checked = stored.alwaysAllowApiMutations !== false;- if (apiMutationObserverToggle) apiMutationObserverToggle.checked = stored.apiMutationObserverEnabled !== false;+ if (alwaysAllowApiMutationsToggle) alwaysAllowApiMutationsToggle.checked = stored.alwaysAllowApiMutations === undefined || stored.alwaysAllowApiMutations === true;+ if (apiMutationObserverToggle) apiMutationObserverToggle.checked = stored.apiMutationObserverEnabled === undefined || stored.apiMutationObserverEnabled === true;if (openAIAskStreamingToggle) openAIAskStreamingToggle.checked = stored.openaiAskStreamingEnabled !== false;if (planBeforeActModeSelect) planBeforeActModeSelect.value = normalizePlanBeforeActMode(stored);if (planReviewModeSelect) planReviewModeSelect.value = normalizePlanReviewMode(stored);diff --git a/src/firefox/src/ui/sidepanel.js b/src/firefox/src/ui/sidepanel.jsindex 624bb4e67..65e1983e9 100644--- a/src/firefox/src/ui/sidepanel.js+++ b/src/firefox/src/ui/sidepanel.js@@ -4378,7 +4378,7 @@ async function init() {const stored = await browser.storage.local.get(['verboseMode', 'alwaysAllowApiMutations']);verboseMode = stored.verboseMode || false;syncProgressDisplayMode();- alwaysAllowApiMutations = stored.alwaysAllowApiMutations !== false;+ alwaysAllowApiMutations = stored.alwaysAllowApiMutations === undefined || stored.alwaysAllowApiMutations === true;syncApiMutationsAllowedForCurrentTab();// Restore prior conversation for this tab (if any) β survives close/reopen.@@ -4440,7 +4440,8 @@ async function init() {syncProgressDisplayMode();}if (changes.alwaysAllowApiMutations) {- alwaysAllowApiMutations = changes.alwaysAllowApiMutations.newValue !== false;+ const value = changes.alwaysAllowApiMutations.newValue;+ alwaysAllowApiMutations = value === undefined || value === true;syncApiMutationsAllowedForCurrentTab();}if (changes.providers || changes.activeProvider) {diff --git a/test/run.js b/test/run.jsindex bfe0958fa..c6bbbc733 100644--- a/test/run.js+++ b/test/run.js@@ -46626,7 +46626,7 @@ test('API mutation observer setting defaults on and controls the request observeassert.match(html, /id="toggle-api-mutation-observer"/, `${label}: settings toggle missing`);assert.doesNotMatch(html, /id="toggle-api-mutation-observer"\s+checked/, `${label}: observer toggle state should be set from storage`);assert.match(settings, /apiMutationObserverEnabled/, `${label}: settings should persist observer toggle`);- assert.match(settings, /apiMutationObserverToggle\.checked = stored\.apiMutationObserverEnabled !== false/, `${label}: observer should load on by default`);+ assert.match(settings, /apiMutationObserverToggle\.checked = stored\.apiMutationObserverEnabled/, `${label}: observer should load its stored preference`);assert.match(settings, /apiMutationObserverEnabled:\s*apiMutationObserverToggle\.checked/, `${label}: observer toggle should save storage`);assert.match(bg, /const API_MUTATION_OBSERVER_KEY = 'apiMutationObserverEnabled';/, `${label}: storage key missing`);assert.match(bg, /const API_MUTATION_OBSERVER_DEFAULT = true;/, `${label}: observer default should be explicit and on`);@@ -46639,10 +46639,10 @@ test('API mutation observer setting defaults on and controls the request observeassert.match(bg, /onBeforeRequest\.removeListener\(recordApiRequest\)/, `${label}: observer should unregister when disabled`);assert.match(bg, /onBeforeSendHeaders\?\.removeListener\(recordApiRequestHeaders\)/, `${label}: header observer should unregister when disabled`);assert.match(bg, /storage\.local\.get\(\{ \[API_MUTATION_OBSERVER_KEY\]: API_MUTATION_OBSERVER_DEFAULT \}\)/, `${label}: unset storage should use the explicit default`);- assert.match(bg, /setApiMutationObserverEnabled\(stored\[API_MUTATION_OBSERVER_KEY\] !== false\)/, `${label}: observer should stay on unless explicitly disabled`);+ assert.match(bg, /setApiMutationObserverEnabled\(stored\[API_MUTATION_OBSERVER_KEY\] === true\)/, `${label}: observer should accept only a boolean after applying the missing-key default`);assert.match(bg,- /changes\[API_MUTATION_OBSERVER_KEY\][\s\S]*setApiMutationObserverEnabled\(changes\[API_MUTATION_OBSERVER_KEY\]\.newValue !== false\)/,+ /changes\[API_MUTATION_OBSERVER_KEY\][\s\S]*setApiMutationObserverEnabled\(/,`${label}: storage changes should update observer`,);}@@ -46654,6 +46654,11 @@ for (const label of ['chrome', 'firefox']) {{ name: 'explicit opt-in stays on', stored: { apiMutationObserverEnabled: true }, expected: true },{ name: 'explicit opt-out stays off', stored: { apiMutationObserverEnabled: false }, expected: false },{ name: 'storage read failure keeps capture off', storageError: true, expected: false },+ ...[null, 'false', 'true', 0, 1, {}, []].map(value => ({+ name: `malformed ${JSON.stringify(value)} keeps capture off`,+ stored: { apiMutationObserverEnabled: value },+ expected: false,+ })),]) {test(`${label} API mutation observer startup: ${name}`, async () => {const source = fs.readFileSync(path.join(ROOT, `src/${label}/src/background.js`), 'utf8');@@ -46714,19 +46719,78 @@ test('persistent API mutation permission defaults on, is portable, and mirrored'assert.match(html, /id="always-allow-api-mutations-desc"[^>]*data-i18n="st\.display\.always_allow_api_mutations\.desc"/, `${label}: persistent permission description should expose a stable accessible-description target`);assert.match(html, /id="toggle-always-allow-api-mutations"[^>]*aria-labelledby="always-allow-api-mutations-label"[^>]*aria-describedby="always-allow-api-mutations-desc"/, `${label}: persistent permission toggle should reference its translated label and description`);assert.doesNotMatch(html, /id="toggle-always-allow-api-mutations"\s+checked/, `${label}: persistent permission state should be set from storage`);- assert.match(settings, /alwaysAllowApiMutationsToggle\.checked = stored\.alwaysAllowApiMutations !== false/, `${label}: settings should load on by default`);+ assert.match(settings, /alwaysAllowApiMutationsToggle\.checked = stored\.alwaysAllowApiMutations/, `${label}: settings should load the stored authorization preference`);assert.match(settings, /alwaysAllowApiMutations:\s*alwaysAllowApiMutationsToggle\.checked/, `${label}: settings should save the persistent permission`);assert.match(bg, /const ALWAYS_ALLOW_API_MUTATIONS_KEY = 'alwaysAllowApiMutations';/, `${label}: background storage key missing`);- assert.match(bg, /setAlwaysAllowApiMutations\(stored\[ALWAYS_ALLOW_API_MUTATIONS_KEY\] !== false\)/, `${label}: background should initialize the Agent from storage`);+ assert.match(bg, /setAlwaysAllowApiMutations\(stored\[ALWAYS_ALLOW_API_MUTATIONS_KEY\] === true\)/, `${label}: background should accept only a boolean after applying the missing-key default`);assert.match(bg, /await alwaysAllowApiMutationsReady;/, `${label}: first agent runs should wait for persistent permission hydration`);- assert.match(bg, /changes\[ALWAYS_ALLOW_API_MUTATIONS_KEY\][\s\S]*setAlwaysAllowApiMutations\(changes\[ALWAYS_ALLOW_API_MUTATIONS_KEY\]\.newValue !== false\)/, `${label}: background should apply live revocation`);- assert.match(panel, /alwaysAllowApiMutations = stored\.alwaysAllowApiMutations !== false/, `${label}: side panel should load the persistent authorization state`);+ assert.match(bg, /changes\[ALWAYS_ALLOW_API_MUTATIONS_KEY\][\s\S]*setAlwaysAllowApiMutations\(/, `${label}: background should apply live revocation`);+ assert.match(panel, /alwaysAllowApiMutations = stored\.alwaysAllowApiMutations/, `${label}: side panel should load the persistent authorization state`);assert.match(panel, /alwaysAllowApiMutations \|\| isApiMutationsAllowedForTab\(currentTabId\)/, `${label}: authorization should combine persistent and conversation permission`);assert.match(config, /alwaysAllowApiMutations:\s*true/, `${label}: config export default should be on`);assert.match(config, /'alwaysAllowApiMutations'/, `${label}: config import should validate the boolean setting`);}});+for (const [label, AgentClass] of [['chrome', AgentCh], ['firefox', AgentFx]]) {+ test(`${label} API preference live updates and UI hydration reject malformed values`, () => {+ const background = fs.readFileSync(path.join(ROOT, `src/${label}/src/background.js`), 'utf8');+ const settings = fs.readFileSync(path.join(ROOT, `src/${label}/src/ui/settings.js`), 'utf8');+ const panel = fs.readFileSync(path.join(ROOT, `src/${label}/src/ui/sidepanel.js`), 'utf8');+ const blocks = {+ authorization: background.match(/if \(changes\[ALWAYS_ALLOW_API_MUTATIONS_KEY\]\) \{[\s\S]*?\n \}/)?.[0],+ observer: background.match(/if \(changes\[API_MUTATION_OBSERVER_KEY\]\) \{[\s\S]*?\n \}/)?.[0],+ settingsAuthorization: settings.match(/alwaysAllowApiMutationsToggle\.checked = [^;]+;/)?.[0],+ settingsObserver: settings.match(/apiMutationObserverToggle\.checked = [^;]+;/)?.[0],+ panelHydration: panel.match(/alwaysAllowApiMutations = stored\.[^;]+;/)?.[0],+ panelUpdate: panel.match(/if \(changes\.alwaysAllowApiMutations\) \{[\s\S]*?\n \}/)?.[0],+ };+ for (const [name, block] of Object.entries(blocks)) assert.ok(block, `${label}: missing ${name}`);+ for (const [value, expected] of [+ [undefined, true], [true, true], [false, false], [null, false],+ ['false', false], ['true', false], [0, false], [1, false], [{}, false], [[], false],+ ]) {+ const detail = `${label}: ${JSON.stringify(value)}`;+ const agent = new AgentClass({});+ agent.setAlwaysAllowApiMutations(true);+ const observerUpdates = [];+ const changes = {+ alwaysAllowApiMutations: { oldValue: true },+ apiMutationObserverEnabled: { oldValue: true },+ };+ const stored = {};+ if (value !== undefined) {+ for (const key of Object.keys(changes)) {+ changes[key].newValue = value;+ stored[key] = value;+ }+ }+ const context = {+ agent, changes, stored,+ ALWAYS_ALLOW_API_MUTATIONS_KEY: 'alwaysAllowApiMutations',+ API_MUTATION_OBSERVER_KEY: 'apiMutationObserverEnabled',+ refreshPrompts: false,+ setApiMutationObserverEnabled: enabled => observerUpdates.push(enabled),+ alwaysAllowApiMutationsToggle: { checked: true },+ apiMutationObserverToggle: { checked: true },+ alwaysAllowApiMutations: true,+ syncApiMutationsAllowedForCurrentTab() {},+ };+ for (const name of ['authorization', 'observer', 'settingsAuthorization', 'settingsObserver', 'panelHydration']) {+ vm.runInNewContext(blocks[name], context);+ }+ assert.equal(agent.isApiMutationsAllowed(4897), expected, `${detail}: background permission`);+ assert.deepEqual(observerUpdates, [expected], `${detail}: observer registration`);+ assert.equal(context.alwaysAllowApiMutationsToggle.checked, expected, `${detail}: authorization toggle`);+ assert.equal(context.apiMutationObserverToggle.checked, expected, `${detail}: observer toggle`);+ assert.equal(context.alwaysAllowApiMutations, expected, `${detail}: sidepanel hydration`);+ context.alwaysAllowApiMutations = true;+ vm.runInNewContext(blocks.panelUpdate, context);+ assert.equal(context.alwaysAllowApiMutations, expected, `${detail}: sidepanel live update`);+ }+ });+}+test('settings async test controls surface rejected background results', () => {for (const [label, settingsRel, htmlRel] of [['chrome', 'src/chrome/src/ui/settings.js', 'src/chrome/src/ui/settings.html'],@@ -73946,6 +74010,11 @@ for (const [label, AgentClass] of [['chrome', AgentCh], ['firefox', AgentFx]]) {{ name: 'explicit opt-in stays on', stored: { alwaysAllowApiMutations: true }, expected: true },{ name: 'explicit opt-out stays off', stored: { alwaysAllowApiMutations: false }, expected: false },{ name: 'storage read failure keeps authorization off', storageError: true, expected: false },+ ...[null, 'false', 'true', 0, 1, {}, []].map(value => ({+ name: `malformed ${JSON.stringify(value)} keeps authorization off`,+ stored: { alwaysAllowApiMutations: value },+ expected: false,+ })),]) {test(`${label} persistent API mutation startup: ${name}`, async () => {const source = fs.readFileSync(path.join(ROOT, `src/${label}/src/background.js`), 'utf8');