Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Bind hCaptcha User-Agent to target frame and Tencent script

Emre Sokullu2e35d57a9e3f
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 9c5ff5a0c..094269113 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -86,7 +86,7 @@ import { tracesToMarkdown } from './trace-export.js';
import { hcaptchaParamError } from './captcha-hcaptcha-providers.js';
import { getCaptchaCapabilities, prepareNativeCaptchaTasks, solveNativeCaptchaTasks } from './captcha-native-providers.js';
import { applyNativeCaptchaSolution, captureCaptchaDocuments, captchaAnswerDocumentStatus } from './captcha-solution-application.js';
-import { solveCaptchaWithProviders, detectCaptcha, injectToken, captchaParamError, captchaTypesMatch, captchaWebsiteUrl } from './captcha-solver.js';
+import { solveCaptchaWithProviders, detectCaptcha, injectToken, readCaptchaFrameUserAgent, captchaParamError, captchaTypesMatch, captchaWebsiteUrl } from './captcha-solver.js';
import { CAPTCHA_SETTINGS_KEYS, getCaptchaProviders, captchaProviderSupportsType } from './captcha-provider-config.js';
import { captchaChallengeKey, captchaChallengeMatcherOptions, detectChallengeDialog, detectChallengeDialogInPage } from './captcha-gate.js';
import { applyCaptchaFrameVisibility } from './captcha-frame-runtime.js';
@@ -36041,6 +36041,13 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
}
+ let frameUserAgent = null;
+ if (['turnstile', 'hcaptcha'].includes(type) && Number.isInteger(detected?.frameId)) {
+ try { frameUserAgent = await readCaptchaFrameUserAgent(tabId, detected.frameId); } catch {}
+ if (type === 'hcaptcha' && !frameUserAgent) {
+ return noDispatchFailure('solve_captcha: could not read the selected hCaptcha frame User-Agent before dispatch. Ask the user to complete it manually.');
+ }
+ }
const params = {
type,
websiteURL,
@@ -36054,7 +36061,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
...(imageBase64 ? { body: imageBase64 } : {}),
...(type === 'hcaptcha' && rqdata ? { rqdata } : {}),
...(type === 'turnstile' && metadata ? { metadata } : {}),
- ...(['turnstile', 'hcaptcha'].includes(type) && globalThis.navigator?.userAgent ? { userAgent: globalThis.navigator.userAgent } : {}),
+ ...(frameUserAgent ? { userAgent: frameUserAgent } : {}),
};
if (type === 'image_to_text') {
diff --git a/src/chrome/src/agent/captcha-native-providers.js b/src/chrome/src/agent/captcha-native-providers.js
index 38d7a2e96..f51c0dec6 100644
--- a/src/chrome/src/agent/captcha-native-providers.js
+++ b/src/chrome/src/agent/captcha-native-providers.js
@@ -156,7 +156,7 @@ export function buildNativeCaptchaTask(entry) {
const FAMILY_IDENTIFIERS = {
atb: [['appId', 'app_id'], ['apiServer', 'api_server']],
cutcaptcha: [['miseryKey', 'misery_key'], ['apiKey', 'api_key']],
- tencent: [['appId', 'app_id', 'websiteKey']],
+ tencent: [['appId', 'app_id', 'websiteKey'], ['captchaScript', 'captcha_script', 'metadata.captchaUrl']],
lemin: [['captchaId', 'captcha_id'], ['divId', 'div_id']],
vk: [['redirectUri', 'redirect_uri']],
aws_waf: [['websiteKey', 'sitekey', 'awsKey'], ['iv', 'awsIv'], ['context', 'awsContext'],
diff --git a/src/chrome/src/agent/captcha-solver.js b/src/chrome/src/agent/captcha-solver.js
index 098a2cb18..cdd6f2293 100644
--- a/src/chrome/src/agent/captcha-solver.js
+++ b/src/chrome/src/agent/captcha-solver.js
@@ -331,6 +331,17 @@ export async function detectCaptcha(tabId, constraints = {}) {
};
}
+export async function readCaptchaFrameUserAgent(tabId, frameId) {
+ if (!Number.isInteger(frameId)) return null;
+ const results = await chrome.scripting.executeScript({
+ target: { tabId, frameIds: [frameId] },
+ world: 'MAIN',
+ func: () => navigator.userAgent,
+ });
+ const value = results?.find(entry => entry?.frameId === frameId)?.result;
+ return typeof value === 'string' && value.trim() ? value : null;
+}
+
// ─── Token injection ───────────────────────────────────────────────────
export async function injectToken(tabId, {
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 8d00d412f..8ae37cdf8 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -81,7 +81,7 @@ import { tracesToMarkdown } from './trace-export.js';
import { hcaptchaParamError } from './captcha-hcaptcha-providers.js';
import { getCaptchaCapabilities, prepareNativeCaptchaTasks, solveNativeCaptchaTasks } from './captcha-native-providers.js';
import { applyNativeCaptchaSolution, captureCaptchaDocuments, captchaAnswerDocumentStatus } from './captcha-solution-application.js';
-import { solveCaptchaWithProviders, detectCaptcha, injectToken, captchaParamError, captchaTypesMatch, captchaWebsiteUrl } from './captcha-solver.js';
+import { solveCaptchaWithProviders, detectCaptcha, injectToken, readCaptchaFrameUserAgent, captchaParamError, captchaTypesMatch, captchaWebsiteUrl } from './captcha-solver.js';
import { CAPTCHA_SETTINGS_KEYS, getCaptchaProviders, captchaProviderSupportsType } from './captcha-provider-config.js';
import { captchaChallengeKey, captchaChallengeMatcherOptions, detectChallengeDialog, detectChallengeDialogInPage } from './captcha-gate.js';
import { applyCaptchaFrameVisibility } from './captcha-frame-runtime.js';
@@ -33024,6 +33024,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
}
+ let frameUserAgent = null;
+ if (['turnstile', 'hcaptcha'].includes(type) && Number.isInteger(detected?.frameId)) {
+ try { frameUserAgent = await readCaptchaFrameUserAgent(tabId, detected.frameId); } catch {}
+ if (type === 'hcaptcha' && !frameUserAgent) {
+ return noDispatchFailure('solve_captcha: could not read the selected hCaptcha frame User-Agent before dispatch. Ask the user to complete it manually.');
+ }
+ }
const params = {
type,
websiteURL,
@@ -33037,7 +33044,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...(imageBase64 ? { body: imageBase64 } : {}),
...(type === 'hcaptcha' && rqdata ? { rqdata } : {}),
...(type === 'turnstile' && metadata ? { metadata } : {}),
- ...(['turnstile', 'hcaptcha'].includes(type) && globalThis.navigator?.userAgent ? { userAgent: globalThis.navigator.userAgent } : {}),
+ ...(frameUserAgent ? { userAgent: frameUserAgent } : {}),
};
if (type === 'image_to_text') {
diff --git a/src/firefox/src/agent/captcha-native-providers.js b/src/firefox/src/agent/captcha-native-providers.js
index 38d7a2e96..f51c0dec6 100644
--- a/src/firefox/src/agent/captcha-native-providers.js
+++ b/src/firefox/src/agent/captcha-native-providers.js
@@ -156,7 +156,7 @@ export function buildNativeCaptchaTask(entry) {
const FAMILY_IDENTIFIERS = {
atb: [['appId', 'app_id'], ['apiServer', 'api_server']],
cutcaptcha: [['miseryKey', 'misery_key'], ['apiKey', 'api_key']],
- tencent: [['appId', 'app_id', 'websiteKey']],
+ tencent: [['appId', 'app_id', 'websiteKey'], ['captchaScript', 'captcha_script', 'metadata.captchaUrl']],
lemin: [['captchaId', 'captcha_id'], ['divId', 'div_id']],
vk: [['redirectUri', 'redirect_uri']],
aws_waf: [['websiteKey', 'sitekey', 'awsKey'], ['iv', 'awsIv'], ['context', 'awsContext'],
diff --git a/src/firefox/src/agent/captcha-solver.js b/src/firefox/src/agent/captcha-solver.js
index 612e4ee7c..ce4d8aae5 100644
--- a/src/firefox/src/agent/captcha-solver.js
+++ b/src/firefox/src/agent/captcha-solver.js
@@ -396,6 +396,15 @@ export async function detectCaptcha(tabId, constraints = {}) {
};
}
+export async function readCaptchaFrameUserAgent(tabId, frameId) {
+ if (!Number.isInteger(frameId)) return null;
+ const results = await browser.tabs.executeScript(tabId, {
+ frameId, matchAboutBlank: true, code: 'navigator.userAgent',
+ });
+ const value = results?.[0];
+ return typeof value === 'string' && value.trim() ? value : null;
+}
+
export async function injectToken(tabId, {
fieldName,
alsoSet,
diff --git a/test/captcha-native-providers.mjs b/test/captcha-native-providers.mjs
index 38d2ab8bd..0d9339af0 100644
--- a/test/captcha-native-providers.mjs
+++ b/test/captcha-native-providers.mjs
@@ -426,6 +426,8 @@ for (const browser of ['chrome', 'firefox']) {
const actionBoundPairs = [
['DataDome User-Agent', ['2captcha', 'DataDomeSliderTask', { websiteURL: url, captchaUrl: 'https://example.test/captcha', userAgent: 'browser-UA', proxyType: 'http', proxyAddress: 'proxy.test', proxyPort: 8080 }], ['solvecaptcha', 'datadome', { pageurl: url, captcha_url: 'https://example.test/captcha', userAgent: 'browser-UA', proxy: 'proxy.test:8080', proxytype: 'http' }], 'userAgent'],
['DataDome proxy', ['2captcha', 'DataDomeSliderTask', { websiteURL: url, captchaUrl: 'https://example.test/captcha', userAgent: 'browser-UA', proxyType: 'http', proxyAddress: 'proxy.test', proxyPort: 8080 }], ['solvecaptcha', 'datadome', { pageurl: url, captcha_url: 'https://example.test/captcha', userAgent: 'browser-UA', proxy: 'proxy.test:8080', proxytype: 'http' }], 'proxy'],
+ ['Tencent script', ['2captcha', 'TencentTaskProxyless', { websiteURL: url, appId: 'A', captchaScript: 'https://a.test/tcaptcha.js' }], ['solvecaptcha', 'tencent', { pageurl: url, app_id: 'A', captcha_script: 'https://a.test/tcaptcha.js' }], 'captcha_script'],
+ ['Tencent metadata script', ['2captcha', 'TencentTaskProxyless', { websiteURL: url, appId: 'A', captchaScript: 'https://a.test/tcaptcha.js' }], ['capmonster', 'CustomTask:TenDI', { websiteURL: url, websiteKey: 'A', metadata: { captchaUrl: 'https://a.test/tcaptcha.js' } }], 'metadata.captchaUrl'],
['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'],
diff --git a/test/run.js b/test/run.js
index 331bcf868..a9cc4411c 100644
--- a/test/run.js
+++ b/test/run.js
@@ -121815,6 +121815,7 @@ async function withCaptchaFakePage(build, nodes, callback) {
location,
URL,
URLSearchParams,
+ navigator: globalThis.navigator,
innerWidth: 1280,
innerHeight: 720,
getComputedStyle: globalThis.getComputedStyle,
@@ -121987,7 +121988,7 @@ test('NoneCap hCaptcha token is not injected when its User-Agent differs from th
const originalExecute = build === 'chrome' ? api.scripting.executeScript : api.tabs.executeScript;
let injections = 0;
if (build === 'chrome') api.scripting.executeScript = async options => {
- if (options.world === 'MAIN') injections++;
+ if (options.world === 'MAIN' && options.args?.length) injections++;
return originalExecute(options);
};
else api.tabs.executeScript = async (tabId, options) => {
@@ -122012,6 +122013,54 @@ test('NoneCap hCaptcha token is not injected when its User-Agent differs from th
}
});
+test('hCaptcha dispatch and NoneCap validation use the selected frame User-Agent', async () => {
+ for (const [build, AgentClass] of [['chrome', AgentCh], ['firefox', AgentFx]]) {
+ const sitekey = 'f5ab1c2d-7e8f-4a9b-b1c2-d3e4f5a6b7c8';
+ await withCaptchaFakePage(build, [captchaEl('div', { class: 'h-captcha', 'data-sitekey': sitekey })], async () => {
+ const api = build === 'chrome' ? globalThis.chrome : globalThis.browser;
+ api.storage = { local: { get: async () => ({
+ nopechaEnabled: true, nopechaApiKey: 'nopecha_key',
+ nonecapEnabled: true, nonecapApiKey: 'nc_live_' + 'a'.repeat(32),
+ }) } };
+ api.tabs = { ...api.tabs, get: async () => ({ url: 'https://example.test/form' }) };
+ const frameUserAgent = 'target-frame-UA';
+ const originalExecute = build === 'chrome' ? api.scripting.executeScript : api.tabs.executeScript;
+ let injections = 0;
+ if (build === 'chrome') api.scripting.executeScript = async options => {
+ if (options.func?.toString().includes('navigator.userAgent')) {
+ return [{ frameId: 0, result: frameUserAgent }];
+ }
+ if (options.world === 'MAIN' && options.args?.length) injections++;
+ return originalExecute(options);
+ };
+ else api.tabs.executeScript = async (tabId, options) => {
+ if (options.code === 'navigator.userAgent') return [frameUserAgent];
+ if (options.code.includes('injectCaptchaTokenInPage')) injections++;
+ return originalExecute(tabId, options);
+ };
+ const originalFetch = globalThis.fetch;
+ const calls = [];
+ globalThis.fetch = async (requestUrl, options) => {
+ calls.push({ url: requestUrl, body: JSON.parse(options.body) });
+ return requestUrl.includes('nopecha')
+ ? Response.json({ code: 16, message: 'Out of credit' }, { status: 403 })
+ : Response.json({ id: 'solve_test', status: 'solved', token: 'paid-token',
+ user_agent: 'background-UA' });
+ };
+ try {
+ const agent = new AgentClass({});
+ const result = await agent._executeToolImpl(1, 'solve_captcha', {});
+ assert.equal(result.dispatched, true, build);
+ assert.equal(result.injected, false, build);
+ assert.equal(result.manualCompletionRequired, true, build);
+ assert.match(result.error, /different User-Agent/, build);
+ assert.equal(calls[0].body.useragent, frameUserAgent, build);
+ assert.equal(injections, 0, build);
+ } finally { globalThis.fetch = originalFetch; }
+ });
+ }
+});
+
test('CAPTCHA providers: explicit rqdata selects NoneCap Enterprise when frame detection is unavailable', async () => {
for (const [build, AgentClass] of [['chrome', AgentCh], ['firefox', AgentFx]]) {
await withCaptchaFakePage(build, [], async () => {