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
Preserve hCaptcha cookies and GeeTest service identity in fallback
Emre Sokulluc432a76d9bf6
diff --git a/src/chrome/src/agent/captcha-native-providers.js b/src/chrome/src/agent/captcha-native-providers.jsindex 0fd52cc69..1a40fea7a 100644--- a/src/chrome/src/agent/captcha-native-providers.js+++ b/src/chrome/src/agent/captcha-native-providers.js@@ -203,12 +203,12 @@ function proxyIdentity(task) {}return JSON.stringify([scheme.toLowerCase(), String(host).toLowerCase(), Number(port), String(login), String(password)]);}-function funcaptchaServiceHost(value) {+function captchaServiceHost(value, family) {try {const url = new URL(value.includes('://') ? value : `https://${value}`);if (!['http:', 'https:'].includes(url.protocol) || !url.hostname || url.pathname !== '/' || url.search || url.hash) throw new Error();return url.host.toLowerCase();- } catch { throw new Error('Fallback FunCaptcha tasks must use comparable service hosts.'); }+ } catch { throw new Error(`Fallback ${family} tasks must use comparable service hosts.`); }}function cloudflareChallengeSignature({ contract, task }) {const mode = contract.provider === 'capsolver' && contract.method === 'AntiCloudflareTask'@@ -268,6 +268,12 @@ function validateFallbackIdentifiers(built) {&& new Set(built.map(recaptchaCookieSignature)).size > 1) {throw new Error('Fallback reCAPTCHA tasks must use the same observed cookie set.');}+ // Only NopeCHA accepts hCaptcha cookies. A later provider cannot continue+ // the same page session when those cookies are part of the observed task.+ if (family === 'hcaptcha' && built.length > 1+ && built.some(({ task }) => usable(task.cookie))) {+ throw new Error('Fallback hCaptcha tasks cannot preserve the observed cookie set across providers.');+ }if (family === 'cloudflare_challenge' && built.length > 1&& new Set(built.map(cloudflareChallengeSignature)).size > 1) {throw new Error('Fallback Cloudflare tasks must use the same challenge mode and observed page snapshot.');@@ -276,7 +282,7 @@ function validateFallbackIdentifiers(built) {const services = built.map(({ task }) => task.funcaptchaApiJSSubdomain || task.surl);if (services.some(usable)) {if (services.some(value => !usable(value))- || new Set(services.map(funcaptchaServiceHost)).size > 1) {+ || new Set(services.map(value => captchaServiceHost(value, 'FunCaptcha'))).size > 1) {throw new Error('Fallback FunCaptcha tasks must use the same observed service host.');}}@@ -362,6 +368,13 @@ function validateFallbackIdentifiers(built) {const versions = built.map(({contract, task}) => task.version === 4 || task.captchaId|| task.captcha_id || task.initParameters?.captcha_id || contract.method === 'geetest_v4' ? 4 : 3);if (new Set(versions).size > 1) throw new Error('Fallback tasks must use the same GeeTest version.');+ if (built.length > 1) {+ const services = built.map(({ task }) => task.geetestApiServerSubdomain ?? task.api_server);+ if (services.some(usable) && (services.some(value => !usable(value))+ || new Set(services.map(value => captchaServiceHost(value, 'GeeTest'))).size > 1)) {+ throw new Error('Fallback GeeTest tasks must use the same observed service host.');+ }+ }groups.push({ aliases: versions[0] === 4 ? ['captchaId', 'captcha_id', 'initParameters.captcha_id', 'gt'] : ['gt'], requireAll: true });if (versions[0] === 3) groups.push({ aliases: ['challenge'], requireAll: true });}diff --git a/src/firefox/src/agent/captcha-native-providers.js b/src/firefox/src/agent/captcha-native-providers.jsindex 0fd52cc69..1a40fea7a 100644--- a/src/firefox/src/agent/captcha-native-providers.js+++ b/src/firefox/src/agent/captcha-native-providers.js@@ -203,12 +203,12 @@ function proxyIdentity(task) {}return JSON.stringify([scheme.toLowerCase(), String(host).toLowerCase(), Number(port), String(login), String(password)]);}-function funcaptchaServiceHost(value) {+function captchaServiceHost(value, family) {try {const url = new URL(value.includes('://') ? value : `https://${value}`);if (!['http:', 'https:'].includes(url.protocol) || !url.hostname || url.pathname !== '/' || url.search || url.hash) throw new Error();return url.host.toLowerCase();- } catch { throw new Error('Fallback FunCaptcha tasks must use comparable service hosts.'); }+ } catch { throw new Error(`Fallback ${family} tasks must use comparable service hosts.`); }}function cloudflareChallengeSignature({ contract, task }) {const mode = contract.provider === 'capsolver' && contract.method === 'AntiCloudflareTask'@@ -268,6 +268,12 @@ function validateFallbackIdentifiers(built) {&& new Set(built.map(recaptchaCookieSignature)).size > 1) {throw new Error('Fallback reCAPTCHA tasks must use the same observed cookie set.');}+ // Only NopeCHA accepts hCaptcha cookies. A later provider cannot continue+ // the same page session when those cookies are part of the observed task.+ if (family === 'hcaptcha' && built.length > 1+ && built.some(({ task }) => usable(task.cookie))) {+ throw new Error('Fallback hCaptcha tasks cannot preserve the observed cookie set across providers.');+ }if (family === 'cloudflare_challenge' && built.length > 1&& new Set(built.map(cloudflareChallengeSignature)).size > 1) {throw new Error('Fallback Cloudflare tasks must use the same challenge mode and observed page snapshot.');@@ -276,7 +282,7 @@ function validateFallbackIdentifiers(built) {const services = built.map(({ task }) => task.funcaptchaApiJSSubdomain || task.surl);if (services.some(usable)) {if (services.some(value => !usable(value))- || new Set(services.map(funcaptchaServiceHost)).size > 1) {+ || new Set(services.map(value => captchaServiceHost(value, 'FunCaptcha'))).size > 1) {throw new Error('Fallback FunCaptcha tasks must use the same observed service host.');}}@@ -362,6 +368,13 @@ function validateFallbackIdentifiers(built) {const versions = built.map(({contract, task}) => task.version === 4 || task.captchaId|| task.captcha_id || task.initParameters?.captcha_id || contract.method === 'geetest_v4' ? 4 : 3);if (new Set(versions).size > 1) throw new Error('Fallback tasks must use the same GeeTest version.');+ if (built.length > 1) {+ const services = built.map(({ task }) => task.geetestApiServerSubdomain ?? task.api_server);+ if (services.some(usable) && (services.some(value => !usable(value))+ || new Set(services.map(value => captchaServiceHost(value, 'GeeTest'))).size > 1)) {+ throw new Error('Fallback GeeTest tasks must use the same observed service host.');+ }+ }groups.push({ aliases: versions[0] === 4 ? ['captchaId', 'captcha_id', 'initParameters.captcha_id', 'gt'] : ['gt'], requireAll: true });if (versions[0] === 3) groups.push({ aliases: ['challenge'], requireAll: true });}diff --git a/test/captcha-native-providers.mjs b/test/captcha-native-providers.mjsindex 4e5309e2e..61f864b95 100644--- a/test/captcha-native-providers.mjs+++ b/test/captcha-native-providers.mjs@@ -130,6 +130,41 @@ for (const browser of ['chrome','firefox']) {parameters: { ...capsolver.parameters, cookies: 'SID=A' } }), /must be array/);assert.equal(calls.length, 0);});+ test(`${browser}: hCaptcha fallback rejects cookies that a later provider cannot preserve`, t => {+ const calls = mockApi(t, () => { throw new Error('Must not dispatch'); });+ const nopecha = { provider: 'nopecha', method: 'token/hcaptcha', parameters: {+ sitekey: 'site', url,+ cookie: [{ name: 'SID', value: 'session', domain: 'example.test', path: '/' }],+ } };+ const nonecap = { provider: 'nonecap', method: 'hcaptcha', parameters: { sitekey: 'site', url } };+ assert.equal(native.prepareNativeCaptchaTasks(providers, [+ { ...nopecha, parameters: { sitekey: 'site', url } }, nonecap,+ ]).length, 2);+ assert.equal(native.prepareNativeCaptchaTasks(providers, [nopecha]).length, 1);+ assert.throws(() => native.prepareNativeCaptchaTasks(providers, [nopecha, nonecap]),+ /cannot preserve the observed cookie set/);+ assert.equal(calls.length, 0);+ });+ test(`${browser}: GeeTest fallback matches provider-specific API service hosts`, t => {+ const calls = mockApi(t, () => { throw new Error('Must not dispatch'); });+ const twoCaptcha = { provider: '2captcha', method: 'GeeTestTaskProxyless', parameters: {+ websiteURL: url, gt: 'site', challenge: 'fresh', geetestApiServerSubdomain: 'api-na.geetest.com',+ } };+ const solveCaptcha = { provider: 'solvecaptcha', method: 'geetest', parameters: {+ pageurl: url, gt: 'site', challenge: 'fresh', api_server: 'https://API-NA.GEETEST.COM/',+ } };+ assert.equal(native.prepareNativeCaptchaTasks(providers, [twoCaptcha, solveCaptcha]).length, 2);+ assert.throws(() => native.prepareNativeCaptchaTasks(providers, [twoCaptcha, {+ ...solveCaptcha, parameters: { ...solveCaptcha.parameters, api_server: 'api-eu.geetest.com' },+ }]), /same observed service host/);+ assert.throws(() => native.prepareNativeCaptchaTasks(providers, [twoCaptcha, {+ ...solveCaptcha, parameters: { pageurl: url, gt: 'site', challenge: 'fresh' },+ }]), /same observed service host/);+ assert.throws(() => native.prepareNativeCaptchaTasks(providers, [twoCaptcha, {+ ...solveCaptcha, parameters: { ...solveCaptcha.parameters, api_server: 'https://api-na.geetest.com/other' },+ }]), /comparable service hosts/);+ assert.equal(calls.length, 0);+ });test(`${browser}: AWS WAF fallback compares API, challenge, and CAPTCHA scripts before dispatch`, t => {const calls = mockApi(t, () => { throw new Error('Must not dispatch'); });const twoCaptcha = { provider: '2captcha', method: 'AmazonTaskProxyless', parameters: {