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
Keep native CAPTCHA answers available until first mutation
Emre Sokullue2b6ce9c3d06
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex aff48a169..e9ef67e45 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -35728,7 +35728,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agaiconst record = this._nativeCaptchaSolutions?.get(tabId);const applicationRetryable = async () => {if (record?.solution === undefined || record.applied || Date.now() - record.createdAt > 180_000) return false;- try { return (await chrome.tabs.get(tabId))?.url === record.pageUrl; } catch { return false; }+ // A transient tab read cannot consume the answer; the next apply+ // rechecks the URL and document before any page mutation.+ try { return (await chrome.tabs.get(tabId))?.url === record.pageUrl; } catch { return true; }};try {const result = await applyNativeCaptchaSolution(tabId, record, args, chrome);diff --git a/src/chrome/src/agent/captcha-solution-application.js b/src/chrome/src/agent/captcha-solution-application.jsindex 7f3540fb5..0bd6b6967 100644--- a/src/chrome/src/agent/captcha-solution-application.js+++ b/src/chrome/src/agent/captcha-solution-application.js@@ -162,14 +162,15 @@ export async function applyNativeCaptchaSolution(tabId, record, application, apiconst stores = cookies.length ? await api.cookies.getAllCookieStores() : [];const storeId = stores.find(store => store.tabIds.includes(tabId))?.id;if (cookies.length && !storeId) throw new Error('Could not identify this tab’s cookie store.');- record.applied = true;// Install cookies before callbacks or response-field events can submit.for (const cookie of cookies) {const current = await api.tabs.get(tabId);if (current.url !== record.pageUrl) throw new Error('Page changed before CAPTCHA cookie application.');const saved = await api.cookies.set({ url: record.pageUrl, name: cookie.name, value: cookie.value, path: '/', secure: record.pageUrl.startsWith('https:'), storeId });if (!saved) throw new Error('CAPTCHA cookie could not be set.');+ record.applied = true;}+ if (!cookies.length) record.applied = true;const applied = await execute(false);if (!applied?.success) return applied || { success: false, error: 'No application result from the selected frame.' };return { ...applied, cookiesUpdated: cookies.length, note: 'Solution applied; verify fresh page state. Do not request another paid solve.' };diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 23404d45a..fcf03d766 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -32714,7 +32714,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst record = this._nativeCaptchaSolutions?.get(tabId);const applicationRetryable = async () => {if (record?.solution === undefined || record.applied || Date.now() - record.createdAt > 180_000) return false;- try { return (await browser.tabs.get(tabId))?.url === record.pageUrl; } catch { return false; }+ // A transient tab read cannot consume the answer; the next apply+ // rechecks the URL and document before any page mutation.+ try { return (await browser.tabs.get(tabId))?.url === record.pageUrl; } catch { return true; }};try {const result = await applyNativeCaptchaSolution(tabId, record, args, browser);diff --git a/src/firefox/src/agent/captcha-solution-application.js b/src/firefox/src/agent/captcha-solution-application.jsindex 7f3540fb5..0bd6b6967 100644--- a/src/firefox/src/agent/captcha-solution-application.js+++ b/src/firefox/src/agent/captcha-solution-application.js@@ -162,14 +162,15 @@ export async function applyNativeCaptchaSolution(tabId, record, application, apiconst stores = cookies.length ? await api.cookies.getAllCookieStores() : [];const storeId = stores.find(store => store.tabIds.includes(tabId))?.id;if (cookies.length && !storeId) throw new Error('Could not identify this tab’s cookie store.');- record.applied = true;// Install cookies before callbacks or response-field events can submit.for (const cookie of cookies) {const current = await api.tabs.get(tabId);if (current.url !== record.pageUrl) throw new Error('Page changed before CAPTCHA cookie application.');const saved = await api.cookies.set({ url: record.pageUrl, name: cookie.name, value: cookie.value, path: '/', secure: record.pageUrl.startsWith('https:'), storeId });if (!saved) throw new Error('CAPTCHA cookie could not be set.');+ record.applied = true;}+ if (!cookies.length) record.applied = true;const applied = await execute(false);if (!applied?.success) return applied || { success: false, error: 'No application result from the selected frame.' };return { ...applied, cookiesUpdated: cookies.length, note: 'Solution applied; verify fresh page state. Do not request another paid solve.' };diff --git a/test/captcha-native-providers.mjs b/test/captcha-native-providers.mjsindex 38887220d..061405010 100644--- a/test/captcha-native-providers.mjs+++ b/test/captcha-native-providers.mjs@@ -110,7 +110,7 @@ test('native CAPTCHA modules stay mirrored',async()=>{for (const browser of ['chrome', 'firefox']) {const native = await import(`../src/${browser}/src/agent/captcha-native-providers.js`);- const { prepareCaptchaApplication } = await import(`../src/${browser}/src/agent/captcha-solution-application.js`);+ const { prepareCaptchaApplication, applyNativeCaptchaSolution } = await import(`../src/${browser}/src/agent/captcha-solution-application.js`);test(`${browser}: documented cookie responses bind only the requested host-only value`, () => {const result = prepareCaptchaApplication({ cookie: 'datadome=ANSWER==; Path=/; Secure; Domain=.other.test; SameSite=Lax' }, { cookies: [{ name: 'datadome', path: 'cookie' }] });assert.deepEqual(result.cookies, [{ name: 'datadome', value: 'ANSWER==' }]);@@ -118,6 +118,62 @@ for (const browser of ['chrome', 'firefox']) {assert.throws(() => prepareCaptchaApplication({ cookie }, { cookies: [{ name: 'datadome', path: 'cookie' }] }), /Invalid CAPTCHA cookie/);}});+ test(`${browser}: a failed read or first cookie write keeps the paid answer reusable`, async () => {+ for (const failure of ['tab-read', 'cookie-write']) {+ const record = { pageUrl: url, createdAt: Date.now(), applied: false,+ documents: [{ frameId: 0, url, timeOrigin: 1000 }], solution: { cookie: 'clearance' } };+ let tabReads = 0;+ let cookieWrites = 0;+ let pageMutations = 0;+ const api = {+ tabs: {+ get: async () => {+ if (failure === 'tab-read' && ++tabReads === 2) throw new Error('Temporary tab read failure');+ return { url };+ },+ executeScript: async (_tabId, options) => {+ if (/false\]\)$/.test(options.code)) pageMutations++;+ return [{ success: true }];+ },+ },+ scripting: browser === 'chrome' ? { executeScript: async options => {+ if (options.args.at(-1) === false) pageMutations++;+ return [{ frameId: 0, result: { success: true } }];+ } } : undefined,+ webNavigation: { getAllFrames: async () => [{ frameId: 0, url }] },+ cookies: {+ getAllCookieStores: async () => [{ id: 'store', tabIds: [1] }],+ set: async value => {+ if (failure === 'cookie-write' && ++cookieWrites === 1) return null;+ return value;+ },+ },+ };+ const binding = { frameId: 0, frameUrl: url, cookies: [{ name: 'cf_clearance', path: 'cookie' }] };+ await assert.rejects(applyNativeCaptchaSolution(1, record, binding, api), /Temporary tab read failure|cookie could not be set/);+ assert.equal(record.applied, false, failure);+ assert.equal(pageMutations, 0, failure);+ assert.equal((await applyNativeCaptchaSolution(1, record, binding, api)).success, true, failure);+ assert.equal(record.applied, true, failure);+ assert.equal(pageMutations, 1, failure);+ }+ });+ test(`${browser}: a successful first cookie write consumes the answer before later failures`, async () => {+ const record = { pageUrl: url, createdAt: Date.now(), applied: false,+ documents: [{ frameId: 0, url, timeOrigin: 1000 }], solution: { first: 'one', second: 'two' } };+ let writes = 0;+ const api = {+ tabs: { get: async () => ({ url }), executeScript: async () => [{ success: true }] },+ scripting: browser === 'chrome' ? { executeScript: async () => [{ frameId: 0, result: { success: true } }] } : undefined,+ webNavigation: { getAllFrames: async () => [{ frameId: 0, url }] },+ cookies: { getAllCookieStores: async () => [{ id: 'store', tabIds: [1] }],+ set: async value => ++writes === 1 ? value : null },+ };+ await assert.rejects(applyNativeCaptchaSolution(1, record, { frameId: 0, frameUrl: url,+ cookies: [{ name: 'first', path: 'first' }, { name: 'second', path: 'second' }] }, api), /cookie could not be set/);+ assert.equal(record.applied, true);+ assert.equal(writes, 2);+ });for (const [method, answer, expected, binding] of [['grid', 'click:3/a/g/', [3, 10, 16], { mode: 'grid', rows: 4, columns: 4 }],['coordinates', 'coordinate:x=0,y=59;x=252,y=72', [{ x: 0, y: 59 }, { x: 252, y: 72 }], { mode: 'coordinates', sourceWidth: 300, sourceHeight: 300 }],diff --git a/test/run.js b/test/run.jsindex fb252f1d8..ea1f88ba7 100644--- a/test/run.js+++ b/test/run.js@@ -122023,6 +122023,12 @@ test('CAPTCHA native methods dispatch through the real agent, preserve structureassert.equal(pendingAfterPreflight.solveFailed, undefined, build);assert.equal(agent._captchaGateBlockResult(1, 'apply_captcha_solution', {})?.denied, undefined, build);assert.equal(calls.length, 1, build);+ const getTab = api.tabs.get;+ api.tabs.get = async () => { throw new Error('Temporary tab read failure'); };+ const transientRead = await agent._executeToolImpl(1, 'apply_captcha_solution', { frameId: 0, frameUrl: 'https://example.test/form' });+ assert.equal(transientRead.applicationRetryable, true, build);+ assert.equal(agent._captchaSolveGateAfterTool(1, 'apply_captcha_solution', transientRead).status, 'verification_pending', build);+ api.tabs.get = getTab;if (build === 'chrome') api.scripting.executeScript = async () => [{ frameId: 0, result: { success: true } }];else api.tabs.executeScript = async () => [{ success: true }];const applied = await agent._executeToolImpl(1, 'apply_captcha_solution', { frameId: 0, frameUrl: 'https://example.test/form', callback: { name: 'captcha.done', path: '' } });