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 CAPTCHA answer retention and v2 fallback identity
Emre Sokullu62cf5c53ae58
diff --git a/src/chrome/src/agent/captcha-native-providers.js b/src/chrome/src/agent/captcha-native-providers.jsindex 7ccd177c3..2a2a874e4 100644--- a/src/chrome/src/agent/captcha-native-providers.js+++ b/src/chrome/src/agent/captcha-native-providers.js@@ -124,6 +124,8 @@ const FAMILY_IDENTIFIERS = {captchafox: [['apiServer', 'api_server']],altcha: [['challengeURL', 'challenge_url'], ['challengeJSON', 'challenge_json']],friendly: [['version'], ['moduleScript', 'module_script'], ['nomoduleScript', 'nomodule_script']],+ recaptcha_v2: [['recaptchaDataSValue', 'enterprisePayload.s', 'data-s', 'data.s']],+ recaptcha_v2_enterprise: [['recaptchaDataSValue', 'enterprisePayload.s', 'data-s', 'data.s']],recaptcha_v3: [['pageAction', 'action', 'data.action']],recaptcha_v3_enterprise: [['pageAction', 'action', 'data.action']],turnstile: [diff --git a/src/chrome/src/agent/captcha-solution-application.js b/src/chrome/src/agent/captcha-solution-application.jsindex 0bd6b6967..a207eb5cb 100644--- a/src/chrome/src/agent/captcha-solution-application.js+++ b/src/chrome/src/agent/captcha-solution-application.js@@ -133,7 +133,7 @@ export function applyCaptchaValuesInPage(expectedUrl, fields, callback, clicks =}export async function applyNativeCaptchaSolution(tabId, record, application, api = globalThis.browser || globalThis.chrome) {- if (!record || record.applied) throw new Error('No unapplied CAPTCHA solution is available. Do not request another solve.');+ if (!record || record.applied || record.applying) throw new Error('No unapplied CAPTCHA solution is available. Do not request another solve.');const tab = await api.tabs.get(tabId);if (tab.url !== record.pageUrl) throw new Error('The page changed after the CAPTCHA solve.');if (Date.now() - record.createdAt > 180_000) throw new Error('The stored CAPTCHA solution has expired.');@@ -159,19 +159,39 @@ export async function applyNativeCaptchaSolution(tabId, record, application, api};const preflight = await execute(true);if (!preflight?.success) return preflight || { success: false, error: 'No result from the selected CAPTCHA frame.' };- const 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.');- // 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.');+ if (record.applied || record.applying) throw new Error('No unapplied CAPTCHA solution is available. Do not request another solve.');+ record.applying = true;+ try {+ const 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.');+ // 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;+ }+ let applied;+ try {+ applied = await execute(false);+ } catch (error) {+ // The page may have changed after a field event or callback. Its state is+ // indeterminate, so never replay the paid answer in that case.+ record.applied = true;+ throw error;+ }+ if (!applied) {+ record.applied = true;+ return { success: false, error: 'No application result from the selected frame.' };+ }+ // The page helper returns failures only from validation before mutation.+ // Keep a field/callback/click answer available when that second check fails.+ if (!applied.success) return applied;record.applied = true;+ return { ...applied, cookiesUpdated: cookies.length, note: 'Solution applied; verify fresh page state. Do not request another paid solve.' };+ } finally {+ record.applying = false;}- 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/captcha-native-providers.js b/src/firefox/src/agent/captcha-native-providers.jsindex 7ccd177c3..2a2a874e4 100644--- a/src/firefox/src/agent/captcha-native-providers.js+++ b/src/firefox/src/agent/captcha-native-providers.js@@ -124,6 +124,8 @@ const FAMILY_IDENTIFIERS = {captchafox: [['apiServer', 'api_server']],altcha: [['challengeURL', 'challenge_url'], ['challengeJSON', 'challenge_json']],friendly: [['version'], ['moduleScript', 'module_script'], ['nomoduleScript', 'nomodule_script']],+ recaptcha_v2: [['recaptchaDataSValue', 'enterprisePayload.s', 'data-s', 'data.s']],+ recaptcha_v2_enterprise: [['recaptchaDataSValue', 'enterprisePayload.s', 'data-s', 'data.s']],recaptcha_v3: [['pageAction', 'action', 'data.action']],recaptcha_v3_enterprise: [['pageAction', 'action', 'data.action']],turnstile: [diff --git a/src/firefox/src/agent/captcha-solution-application.js b/src/firefox/src/agent/captcha-solution-application.jsindex 0bd6b6967..a207eb5cb 100644--- a/src/firefox/src/agent/captcha-solution-application.js+++ b/src/firefox/src/agent/captcha-solution-application.js@@ -133,7 +133,7 @@ export function applyCaptchaValuesInPage(expectedUrl, fields, callback, clicks =}export async function applyNativeCaptchaSolution(tabId, record, application, api = globalThis.browser || globalThis.chrome) {- if (!record || record.applied) throw new Error('No unapplied CAPTCHA solution is available. Do not request another solve.');+ if (!record || record.applied || record.applying) throw new Error('No unapplied CAPTCHA solution is available. Do not request another solve.');const tab = await api.tabs.get(tabId);if (tab.url !== record.pageUrl) throw new Error('The page changed after the CAPTCHA solve.');if (Date.now() - record.createdAt > 180_000) throw new Error('The stored CAPTCHA solution has expired.');@@ -159,19 +159,39 @@ export async function applyNativeCaptchaSolution(tabId, record, application, api};const preflight = await execute(true);if (!preflight?.success) return preflight || { success: false, error: 'No result from the selected CAPTCHA frame.' };- const 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.');- // 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.');+ if (record.applied || record.applying) throw new Error('No unapplied CAPTCHA solution is available. Do not request another solve.');+ record.applying = true;+ try {+ const 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.');+ // 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;+ }+ let applied;+ try {+ applied = await execute(false);+ } catch (error) {+ // The page may have changed after a field event or callback. Its state is+ // indeterminate, so never replay the paid answer in that case.+ record.applied = true;+ throw error;+ }+ if (!applied) {+ record.applied = true;+ return { success: false, error: 'No application result from the selected frame.' };+ }+ // The page helper returns failures only from validation before mutation.+ // Keep a field/callback/click answer available when that second check fails.+ if (!applied.success) return applied;record.applied = true;+ return { ...applied, cookiesUpdated: cookies.length, note: 'Solution applied; verify fresh page state. Do not request another paid solve.' };+ } finally {+ record.applying = false;}- 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 1792df88a..e3fc1c897 100644--- a/test/captcha-native-providers.mjs+++ b/test/captcha-native-providers.mjs@@ -174,6 +174,38 @@ for (const browser of ['chrome', 'firefox']) {assert.equal(record.applied, true);assert.equal(writes, 2);});+ test(`${browser}: a non-cookie answer survives a second pre-mutation frame check`, async () => {+ const record = { pageUrl: url, createdAt: Date.now(), applied: false,+ documents: [{ frameId: 0, url, timeOrigin: 1000 }], solution: { token: 'answer' } };+ let attempts = 0;+ const result = () => ++attempts === 2+ ? { success: false, error: 'Observed CAPTCHA callback is unavailable.' }+ : { success: true };+ const api = {+ tabs: { get: async () => ({ url }), executeScript: async () => [result()] },+ scripting: browser === 'chrome' ? { executeScript: async () => [{ frameId: 0, result: result() }] } : undefined,+ webNavigation: { getAllFrames: async () => [{ frameId: 0, url }] },+ };+ const binding = { frameId: 0, frameUrl: url, fields: [{ selector: '#response', path: 'token' }] };+ assert.equal((await applyNativeCaptchaSolution(1, record, binding, api)).success, false);+ assert.equal(record.applied, false);+ assert.equal((await applyNativeCaptchaSolution(1, record, binding, api)).success, true);+ assert.equal(record.applied, true);+ });+ test(`${browser}: an indeterminate non-cookie application consumes the answer`, async () => {+ const record = { pageUrl: url, createdAt: Date.now(), applied: false,+ documents: [{ frameId: 0, url, timeOrigin: 1000 }], solution: { token: 'answer' } };+ let attempts = 0;+ const result = () => { if (++attempts === 2) throw new Error('Frame execution failed'); return { success: true }; };+ const api = {+ tabs: { get: async () => ({ url }), executeScript: async () => [result()] },+ scripting: browser === 'chrome' ? { executeScript: async () => [{ frameId: 0, result: result() }] } : undefined,+ webNavigation: { getAllFrames: async () => [{ frameId: 0, url }] },+ };+ await assert.rejects(applyNativeCaptchaSolution(1, record, { frameId: 0, frameUrl: url,+ fields: [{ selector: '#response', path: 'token' }] }, api), /Frame execution failed/);+ assert.equal(record.applied, true);+ });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 }],@@ -211,6 +243,9 @@ for (const browser of ['chrome', 'firefox']) {assert.equal(calls.length, 0);});const actionBoundPairs = [+ ['reCAPTCHA v2 data-s', ['2captcha', 'RecaptchaV2TaskProxyless', { websiteURL: url, websiteKey: 'site', recaptchaDataSValue: 'observed' }], ['solvecaptcha', 'recaptcha_v2', { pageurl: url, googlekey: 'site', 'data-s': 'observed' }], 'data-s'],+ ['reCAPTCHA v2 enterprise data-s', ['2captcha', 'RecaptchaV2EnterpriseTaskProxyless', { websiteURL: url, websiteKey: 'site', enterprisePayload: { s: 'observed' } }], ['solvecaptcha', 'recaptcha_v2_enterprise', { pageurl: url, googlekey: 'site', 'data-s': 'observed' }], 'data-s'],+ ['reCAPTCHA v2 nested data-s', ['2captcha', 'RecaptchaV2TaskProxyless', { websiteURL: url, websiteKey: 'site', recaptchaDataSValue: 'observed' }], ['nopecha', 'token/recaptcha2', { url, sitekey: 'site', data: { s: 'observed' } }], 'data.s'],['reCAPTCHA v3 action', ['2captcha', 'RecaptchaV3TaskProxyless', { websiteURL: url, websiteKey: 'site', minScore: 0.3, pageAction: 'login' }], ['solvecaptcha', 'recaptcha_v3', { pageurl: url, googlekey: 'site', action: 'login' }], 'action'],['reCAPTCHA v3 enterprise action', ['capsolver', 'ReCaptchaV3EnterpriseTaskProxyLess', { websiteURL: url, websiteKey: 'site', pageAction: 'login' }], ['nopecha', 'token/recaptcha3:enterprise', { url, sitekey: 'site', data: { action: 'login' } }], 'data.action'],['Turnstile action', ['capsolver', 'AntiTurnstileTaskProxyLess', { websiteURL: url, websiteKey: 'site', metadata: { action: 'login', cdata: 'widget' } }], ['anti-captcha', 'TurnstileTaskProxyless', { websiteURL: url, websiteKey: 'site', action: 'login', cData: 'widget' }], 'action'],@@ -231,6 +266,14 @@ for (const browser of ['chrome', 'firefox']) {assert.equal(calls.length, 0);});for (const [label, entries] of [+ ['reCAPTCHA v2 omitted data-s', [+ { provider: '2captcha', method: 'RecaptchaV2TaskProxyless', parameters: { websiteURL: url, websiteKey: 'site', recaptchaDataSValue: 'observed' } },+ { provider: 'solvecaptcha', method: 'recaptcha_v2', parameters: { pageurl: url, googlekey: 'site' } },+ ]],+ ['reCAPTCHA v2 enterprise omitted data-s', [+ { provider: '2captcha', method: 'RecaptchaV2EnterpriseTaskProxyless', parameters: { websiteURL: url, websiteKey: 'site', enterprisePayload: { s: 'observed' } } },+ { provider: 'solvecaptcha', method: 'recaptcha_v2_enterprise', parameters: { pageurl: url, googlekey: 'site' } },+ ]],['reCAPTCHA v3 omitted action', [{ provider: '2captcha', method: 'RecaptchaV3TaskProxyless', parameters: { websiteURL: url, websiteKey: 'site', minScore: 0.3, pageAction: 'delete' } },{ provider: 'solvecaptcha', method: 'recaptcha_v3', parameters: { pageurl: url, googlekey: 'site' } },