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
Match recognition constraints across CAPTCHA fallbacks
Emre Sokullu87a384bda366
diff --git a/src/chrome/src/agent/captcha-native-providers.js b/src/chrome/src/agent/captcha-native-providers.jsindex 70f598b3a..0ca75966f 100644--- a/src/chrome/src/agent/captcha-native-providers.js+++ b/src/chrome/src/agent/captcha-native-providers.js@@ -273,6 +273,39 @@ function validateFallbackIdentifiers(built) {return JSON.stringify([media[0], parts, instructions[0] ?? null]);});if (new Set(signatures).size > 1) throw new Error('Fallback recognition tasks must use the same observed challenge media and instructions.');+ // Worker instructions may match while their answer contract differs.+ // Compare only documented challenge constraints, including provider aliases.+ const constraints = [+ { aliases: ['minClicks', 'min_clicks'] },+ { aliases: ['maxClicks', 'max_clicks'] },+ { aliases: ['minLength', 'min_len'] },+ { aliases: ['maxLength', 'max_len'] },+ { aliases: ['case', 'regsense'], boolean: true },+ { aliases: ['phrase'], boolean: true },+ { aliases: ['math', 'calc'], boolean: true },+ { aliases: ['numeric'] },+ { aliases: ['canNoAnswer', 'can_no_answer'], boolean: true },+ { aliases: ['angle'] },+ ];+ for (const { aliases, boolean } of constraints) {+ const values = built.map(({ task }) => {+ const found = aliases.map(path => at(task, path)).filter(value => value !== undefined);+ if (found.length > 1 && new Set(found.map(String)).size > 1) {+ throw new Error(`Fallback recognition tasks must use the same ${aliases[0]} constraint.`);+ }+ const value = found[0];+ return boolean && value !== undefined ? Number(value) : value;+ });+ if (new Set(values.map(value => JSON.stringify(value))).size > 1) {+ throw new Error(`Fallback recognition tasks must use the same ${aliases[0]} constraint.`);+ }+ }+ if (family === 'recaptcha_recognition') {+ const grids = built.map(({ task }) => task.grid ?? task.metadata?.Grid+ ?? (task.rows !== undefined || task.columns !== undefined+ ? `${task.rows ?? '?'}x${task.columns ?? '?'}` : null));+ if (new Set(grids.filter(Boolean)).size > 1) throw new Error('Fallback recognition tasks must use the same grid dimensions.');+ }}if (['recaptcha_v2', 'recaptcha_v2_enterprise'].includes(family)) {const modes = built.map(({ task }) => task.isInvisible ?? task.invisible);diff --git a/src/firefox/src/agent/captcha-native-providers.js b/src/firefox/src/agent/captcha-native-providers.jsindex 70f598b3a..0ca75966f 100644--- a/src/firefox/src/agent/captcha-native-providers.js+++ b/src/firefox/src/agent/captcha-native-providers.js@@ -273,6 +273,39 @@ function validateFallbackIdentifiers(built) {return JSON.stringify([media[0], parts, instructions[0] ?? null]);});if (new Set(signatures).size > 1) throw new Error('Fallback recognition tasks must use the same observed challenge media and instructions.');+ // Worker instructions may match while their answer contract differs.+ // Compare only documented challenge constraints, including provider aliases.+ const constraints = [+ { aliases: ['minClicks', 'min_clicks'] },+ { aliases: ['maxClicks', 'max_clicks'] },+ { aliases: ['minLength', 'min_len'] },+ { aliases: ['maxLength', 'max_len'] },+ { aliases: ['case', 'regsense'], boolean: true },+ { aliases: ['phrase'], boolean: true },+ { aliases: ['math', 'calc'], boolean: true },+ { aliases: ['numeric'] },+ { aliases: ['canNoAnswer', 'can_no_answer'], boolean: true },+ { aliases: ['angle'] },+ ];+ for (const { aliases, boolean } of constraints) {+ const values = built.map(({ task }) => {+ const found = aliases.map(path => at(task, path)).filter(value => value !== undefined);+ if (found.length > 1 && new Set(found.map(String)).size > 1) {+ throw new Error(`Fallback recognition tasks must use the same ${aliases[0]} constraint.`);+ }+ const value = found[0];+ return boolean && value !== undefined ? Number(value) : value;+ });+ if (new Set(values.map(value => JSON.stringify(value))).size > 1) {+ throw new Error(`Fallback recognition tasks must use the same ${aliases[0]} constraint.`);+ }+ }+ if (family === 'recaptcha_recognition') {+ const grids = built.map(({ task }) => task.grid ?? task.metadata?.Grid+ ?? (task.rows !== undefined || task.columns !== undefined+ ? `${task.rows ?? '?'}x${task.columns ?? '?'}` : null));+ if (new Set(grids.filter(Boolean)).size > 1) throw new Error('Fallback recognition tasks must use the same grid dimensions.');+ }}if (['recaptcha_v2', 'recaptcha_v2_enterprise'].includes(family)) {const modes = built.map(({ task }) => task.isInvisible ?? task.invisible);diff --git a/test/captcha-native-providers.mjs b/test/captcha-native-providers.mjsindex db8d8ef40..35f7d8e51 100644--- a/test/captcha-native-providers.mjs+++ b/test/captcha-native-providers.mjs@@ -400,6 +400,38 @@ for (const browser of ['chrome', 'firefox']) {['coordinate image instruction', ['2captcha', 'CoordinatesTask', { body: 'image-A', imgInstructions: 'cars' }], ['solvecaptcha', 'coordinates', { body: 'image-A', textinstructions: 'cars' }], task => { task.textinstructions = 'bicycles'; }],['coordinate lowercase image instruction', ['2captcha', 'CoordinatesTask', { body: 'image-A', imgInstructions: 'cars' }], ['solvecaptcha', 'coordinates', { body: 'image-A', imginstructions: 'cars' }], task => { task.imginstructions = 'bicycles'; }],];+ test(`${browser}: recognition fallback compares click counts, text classes, and answer lengths`, t => {+ const calls = mockApi(t, () => { throw new Error('Must not dispatch'); });+ const enabled = ['2captcha', 'solvecaptcha', 'nopecha'].map(id => ({ id, apiKey: 'key' }));+ const coordinates = [+ { provider: '2captcha', method: 'CoordinatesTask', parameters: { body: 'image-A', minClicks: 1, maxClicks: 1 } },+ { provider: 'solvecaptcha', method: 'coordinates', parameters: { body: 'image-A', min_clicks: 1, max_clicks: 1 } },+ ];+ assert.equal(native.prepareNativeCaptchaTasks(enabled, coordinates).length, 2);+ for (const changed of [{ min_clicks: 5 }, { max_clicks: 5 }, { min_clicks: undefined }]) {+ const parameters = { ...coordinates[1].parameters, ...changed };+ if (changed.min_clicks === undefined) delete parameters.min_clicks;+ assert.throws(() => native.prepareNativeCaptchaTasks(enabled, [coordinates[0],+ { ...coordinates[1], parameters }]), /same minClicks|same maxClicks/);+ }+ const textTasks = [+ { provider: '2captcha', method: 'ImageToTextTask', parameters: { body: 'image-A',+ case: true, phrase: false, math: false, numeric: 2, minLength: 4, maxLength: 6 } },+ { provider: 'solvecaptcha', method: 'base64', parameters: { body: 'image-A',+ regsense: 1, phrase: 0, calc: 0, numeric: 2, min_len: 4, max_len: 6 } },+ ];+ assert.equal(native.prepareNativeCaptchaTasks(enabled, textTasks).length, 2);+ for (const changed of [{ regsense: 0 }, { numeric: 1 }, { min_len: 5 }, { max_len: 8 }, { calc: 1 }, { phrase: 1 }]) {+ assert.throws(() => native.prepareNativeCaptchaTasks(enabled, [textTasks[0],+ { ...textTasks[1], parameters: { ...textTasks[1].parameters, ...changed } }]), /same .* constraint/);+ }+ const grids = [+ { provider: '2captcha', method: 'GridTask:recaptcha_recognition', parameters: { body: 'image-A', rows: 3, columns: 3, comment: 'cars' } },+ { provider: 'nopecha', method: 'recognition/recaptcha', parameters: { image_data: ['image-A'], grid: '4x4', task: 'cars' } },+ ];+ assert.throws(() => native.prepareNativeCaptchaTasks(enabled, grids), /same grid dimensions/);+ assert.equal(calls.length, 0);+ });for (const [label, a, b, change] of recognitionPairs) test(`${browser}: ${label} must match before paid fallback`, t => {const calls = mockApi(t, () => { throw new Error('Must not dispatch'); });const entries = [a, b].map(([provider, method, parameters]) => ({ provider, method, parameters: structuredClone(parameters) }));