Skip to content

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

fix(captcha): preserve callback completion and retries

Codex release-state test10f5546196e3
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index e2743f726..35b73ad19 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -92,7 +92,7 @@ import { advanceHuggingFaceSignupRecovery, normalizeHuggingFaceSignupRecovery, h
import { hcaptchaParamError } from './captcha-hcaptcha-providers.js';
import { AWS_WAF_COOKIE_PATHS, awsWafTaskRoute, describeAwsWafObservation, getCaptchaCapabilities, prepareNativeCaptchaTasks, solveNativeCaptchaTasks } from './captcha-native-providers.js';
import { applyNativeCaptchaSolution, captureCaptchaDocuments, captchaAnswerDocumentStatus, requiresCaptchaUserAgent } from './captcha-solution-application.js';
-import { solveCaptchaWithProviders, detectCaptcha, injectToken, readCaptchaFrameUserAgent, captchaParamError, captchaAutomaticDispatchError, captchaTypesMatch, captchaWebsiteUrl } from './captcha-solver.js';
+import { solveCaptchaWithProviders, detectCaptcha, injectToken, ensureCaptchaCallbackBridge, readCaptchaFrameUserAgent, captchaParamError, captchaAutomaticDispatchError, 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, observeAwsWafChallengeInPage } from './captcha-frame-runtime.js';
@@ -37182,6 +37182,11 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
}
+ if (wantInject && detected) {
+ // Ensure the bridge exists in the selected document, including an
+ // inherited-origin descendant reached through an injectable ancestor.
+ try { await ensureCaptchaCallbackBridge(tabId, detected); } catch (_) {}
+ }
dispatched = true;
const result = await solveCaptchaWithProviders(providers, params);
if (automaticAnswer) {
diff --git a/src/chrome/src/agent/captcha-frame-runtime.js b/src/chrome/src/agent/captcha-frame-runtime.js
index 65a84c39f..08e838635 100644
--- a/src/chrome/src/agent/captcha-frame-runtime.js
+++ b/src/chrome/src/agent/captcha-frame-runtime.js
@@ -1276,6 +1276,12 @@ export function injectCaptchaTokenInPage(payload, scope = null) {
frameUrl,
};
}
+ const callbackOnly = payload?.callbackOnly === true;
+ if (callbackOnly && resolvedFields.some(field => field.primary
+ && (!field.element || field.element.value !== token))) {
+ return { success: false, fieldUpdated: false, staleTarget: true,
+ error: 'The applied CAPTCHA response field changed before callback discovery.', frameUrl };
+ }
const skippedFields = resolvedFields.filter(field => !field.primary && field.error);
const injectableFields = resolvedFields.filter(field => !field.error);
@@ -1389,10 +1395,11 @@ export function injectCaptchaTokenInPage(payload, scope = null) {
};
if (!callbacks.length) collectGoogleCallbacks();
- for (const field of injectableFields) {
+ const appliedFields = callbackOnly ? [] : injectableFields;
+ for (const field of appliedFields) {
setOn(field.name, field.element);
}
- const fieldsTouched = injectableFields.length;
+ const fieldsTouched = appliedFields.length;
let calledCallback = false;
let callbackSource = null;
@@ -1409,8 +1416,8 @@ export function injectCaptchaTokenInPage(payload, scope = null) {
return {
success: true,
- fieldUpdated: true,
- fieldsUpdated: injectableFields.map(field => field.name),
+ fieldUpdated: fieldsTouched > 0,
+ fieldsUpdated: appliedFields.map(field => field.name),
fieldsTouched,
compatibilityFieldSkipped: skippedFields.length > 0,
compatibilityFieldError: skippedFields[0]?.error || null,
diff --git a/src/chrome/src/agent/captcha-solver.js b/src/chrome/src/agent/captcha-solver.js
index bc5936f0b..8dc4fa937 100644
--- a/src/chrome/src/agent/captcha-solver.js
+++ b/src/chrome/src/agent/captcha-solver.js
@@ -383,6 +383,29 @@ export async function readCaptchaFrameUserAgent(tabId, frameId) {
// ─── Token injection ───────────────────────────────────────────────────
+// document_start captures callbacks for widgets rendered during page load.
+// Also ensure the bridge exists for older tabs and inherited-origin fallbacks.
+const CAPTCHA_CALLBACK_BRIDGE_FILE = 'src/content/captcha-callback-bridge.js';
+// Wait once for late renders or callback registration. After a successful
+// field application, the retry only discovers and invokes callbacks.
+const CAPTCHA_BRIDGE_SETTLE_RETRY_MS = 1500;
+
+export async function ensureCaptchaCallbackBridge(tabId, target = null) {
+ try {
+ const targetSpec = Number.isInteger(target?.frameId)
+ ? { tabId, frameIds: [target.frameId] }
+ : { tabId, allFrames: true };
+ await chrome.scripting.executeScript({
+ target: targetSpec,
+ world: 'MAIN',
+ files: [CAPTCHA_CALLBACK_BRIDGE_FILE],
+ });
+ return true;
+ } catch (_) {
+ return false;
+ }
+}
+
export async function injectToken(tabId, {
fieldName,
alsoSet,
@@ -411,26 +434,55 @@ export async function injectToken(tabId, {
const targetSpec = Number.isInteger(target?.frameId)
? { tabId, frameIds: [target.frameId] }
: (target?.frameUrl ? { tabId, allFrames: true } : { tabId });
- const results = await chrome.scripting.executeScript({
- target: targetSpec,
- world: 'MAIN',
- args: [pagePayload],
- func: injectCaptchaTokenInPage,
- });
- const outputs = (results || []).map(entry => ({
- ...(entry?.result || {}),
- frameId: Number.isInteger(entry?.frameId) ? entry.frameId : null,
- }));
- const successes = outputs.filter(output => output.success === true);
- if (successes.length === 1) return successes[0];
- if (successes.length > 1) {
- return {
- success: false,
- ambiguousTarget: true,
- error: 'Token injection matched more than one frame; pass an exact frameUrl.',
- matchedFrames: successes.map(output => ({ frameId: output.frameId, frameUrl: output.frameUrl })),
- };
+ // Best-effort fallback for documents without the early content script.
+ await ensureCaptchaCallbackBridge(tabId, target);
+ const runOnce = async (callbackOnly = false) => {
+ const results = await chrome.scripting.executeScript({
+ target: targetSpec,
+ world: 'MAIN',
+ args: [{ ...pagePayload, callbackOnly }],
+ func: injectCaptchaTokenInPage,
+ });
+ const outputs = (results || []).map(entry => ({
+ ...(entry?.result || {}),
+ frameId: Number.isInteger(entry?.frameId) ? entry.frameId : null,
+ }));
+ const successes = outputs.filter(output => output.success === true);
+ if (successes.length === 1) return successes[0];
+ if (successes.length > 1) {
+ return {
+ success: false,
+ ambiguousTarget: true,
+ error: 'Token injection matched more than one frame; pass an exact frameUrl.',
+ matchedFrames: successes.map(output => ({ frameId: output.frameId, frameUrl: output.frameUrl })),
+ };
+ }
+ return outputs.find(output => !output.skipped)
+ || { success: false, error: 'injection script returned no matching frame result' };
+ };
+ const first = await runOnce();
+ // A clean miss means the widget may be rendering right now: no callback
+ // was invoked and either no callbacks were found or the widget's field /
+ // site key was absent while the frame identity itself matched
+ // (staleTarget without skipped). Frame-identity staleness (skipped),
+ // ambiguity, and target errors cannot resolve by waiting.
+ const cleanMiss = !first?.calledCallback
+ && (first?.callbackCandidates === 0 || first?.staleTarget === true)
+ && !first?.skipped && !first?.ambiguousTarget && !first?.targetRequired;
+ if (cleanMiss) {
+ await new Promise(resolve => setTimeout(resolve, CAPTCHA_BRIDGE_SETTLE_RETRY_MS));
+ if (!first.success) return { ...(await runOnce()), bridgeSettleRetried: true };
+ // Field events may have completed the challenge and removed its widget or
+ // navigated the frame. Preserve that application even if discovery fails.
+ try {
+ const retry = await runOnce(true);
+ if (retry.success) {
+ return { ...retry, fieldUpdated: first.fieldUpdated,
+ fieldsUpdated: first.fieldsUpdated, fieldsTouched: first.fieldsTouched,
+ bridgeSettleRetried: true };
+ }
+ } catch (_) { /* The successful application may have navigated the page. */ }
+ return { ...first, bridgeSettleRetried: true };
}
- return outputs.find(output => !output.skipped)
- || { success: false, error: 'injection script returned no matching frame result' };
+ return first;
}
diff --git a/src/firefox/manifest.json b/src/firefox/manifest.json
index 83d8267e7..803bef35d 100644
--- a/src/firefox/manifest.json
+++ b/src/firefox/manifest.json
@@ -20,6 +20,7 @@
"alarms",
"clipboardWrite",
"clipboardRead",
+ "scripting",
"<all_urls>"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'; connect-src * data: blob: http://* ws://*;",
@@ -124,6 +125,7 @@
"vendor/pdfjs/pdf.worker.mjs",
"src/agent/social-media-downloader.js",
"src/content/file-picker-guard-page.js",
+ "src/content/captcha-callback-bridge.js",
"skills/*"
],
"browser_specific_settings": {
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 6363fffc7..3b6117f2f 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -87,7 +87,7 @@ import { advanceHuggingFaceSignupRecovery, normalizeHuggingFaceSignupRecovery, h
import { hcaptchaParamError } from './captcha-hcaptcha-providers.js';
import { AWS_WAF_COOKIE_PATHS, awsWafTaskRoute, describeAwsWafObservation, getCaptchaCapabilities, prepareNativeCaptchaTasks, solveNativeCaptchaTasks } from './captcha-native-providers.js';
import { applyNativeCaptchaSolution, captureCaptchaDocuments, captchaAnswerDocumentStatus, requiresCaptchaUserAgent } from './captcha-solution-application.js';
-import { solveCaptchaWithProviders, detectCaptcha, injectToken, readCaptchaFrameUserAgent, captchaParamError, captchaAutomaticDispatchError, captchaTypesMatch, captchaWebsiteUrl } from './captcha-solver.js';
+import { solveCaptchaWithProviders, detectCaptcha, injectToken, ensureCaptchaCallbackBridge, readCaptchaFrameUserAgent, captchaParamError, captchaAutomaticDispatchError, 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, observeAwsWafChallengeInPage } from './captcha-frame-runtime.js';
@@ -34207,6 +34207,11 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
}
+ if (wantInject && detected) {
+ // Ensure the bridge exists in the selected document, including an
+ // inherited-origin descendant reached through an injectable ancestor.
+ try { await ensureCaptchaCallbackBridge(tabId, detected); } catch (_) {}
+ }
dispatched = true;
const result = await solveCaptchaWithProviders(providers, params);
if (automaticAnswer) {
diff --git a/src/firefox/src/agent/captcha-frame-runtime.js b/src/firefox/src/agent/captcha-frame-runtime.js
index 65a84c39f..08e838635 100644
--- a/src/firefox/src/agent/captcha-frame-runtime.js
+++ b/src/firefox/src/agent/captcha-frame-runtime.js
@@ -1276,6 +1276,12 @@ export function injectCaptchaTokenInPage(payload, scope = null) {
frameUrl,
};
}
+ const callbackOnly = payload?.callbackOnly === true;
+ if (callbackOnly && resolvedFields.some(field => field.primary
+ && (!field.element || field.element.value !== token))) {
+ return { success: false, fieldUpdated: false, staleTarget: true,
+ error: 'The applied CAPTCHA response field changed before callback discovery.', frameUrl };
+ }
const skippedFields = resolvedFields.filter(field => !field.primary && field.error);
const injectableFields = resolvedFields.filter(field => !field.error);
@@ -1389,10 +1395,11 @@ export function injectCaptchaTokenInPage(payload, scope = null) {
};
if (!callbacks.length) collectGoogleCallbacks();
- for (const field of injectableFields) {
+ const appliedFields = callbackOnly ? [] : injectableFields;
+ for (const field of appliedFields) {
setOn(field.name, field.element);
}
- const fieldsTouched = injectableFields.length;
+ const fieldsTouched = appliedFields.length;
let calledCallback = false;
let callbackSource = null;
@@ -1409,8 +1416,8 @@ export function injectCaptchaTokenInPage(payload, scope = null) {
return {
success: true,
- fieldUpdated: true,
- fieldsUpdated: injectableFields.map(field => field.name),
+ fieldUpdated: fieldsTouched > 0,
+ fieldsUpdated: appliedFields.map(field => field.name),
fieldsTouched,
compatibilityFieldSkipped: skippedFields.length > 0,
compatibilityFieldError: skippedFields[0]?.error || null,
diff --git a/src/firefox/src/agent/captcha-solver.js b/src/firefox/src/agent/captcha-solver.js
index ca05a1c25..92ef826ba 100644
--- a/src/firefox/src/agent/captcha-solver.js
+++ b/src/firefox/src/agent/captcha-solver.js
@@ -445,6 +445,89 @@ export async function readCaptchaFrameUserAgent(tabId, frameId) {
return typeof value === 'string' && value.trim() ? value : null;
}
+// document_start captures callbacks for widgets rendered during page load.
+// Also ensure the bridge exists for older tabs and inherited-origin fallbacks.
+const CAPTCHA_CALLBACK_BRIDGE_FILE = 'src/content/captcha-callback-bridge.js';
+// Wait once for late renders or callback registration. After a successful
+// field application, the retry only discovers and invokes callbacks.
+const CAPTCHA_BRIDGE_SETTLE_RETRY_MS = 1500;
+// scripting.executeScript resolves `files` relative to the calling
+// document on Firefox MV2 (not the extension root), so the file path
+// below is root-absolute. Chrome resolves relative to the root.
+const CAPTCHA_CALLBACK_BRIDGE_SCRIPT_FILE = '/src/content/captcha-callback-bridge.js';
+
+// Self-contained for MAIN-world scripting and the MV2 tag fallback. The
+// injectable frameId can identify an ancestor of a srcdoc/about:blank widget.
+async function installCaptchaCallbackBridgeInPage(target, src) {
+ let frameDocument = document;
+ let frameWindow = window;
+ try {
+ for (const segment of Array.isArray(target?.framePath) ? target.framePath : []) {
+ const element = Array.from(frameDocument.querySelectorAll('iframe'))[segment.index];
+ const nextDocument = element?.contentDocument;
+ const nextWindow = element?.contentWindow;
+ if (!nextDocument || !nextWindow
+ || (segment.frameUrl && String(nextWindow.location.href) !== segment.frameUrl)
+ || (segment.frameName && String(nextWindow.name || element.name || '') !== segment.frameName)) return false;
+ frameDocument = nextDocument;
+ frameWindow = nextWindow;
+ }
+ if (target?.frameUrl && String(frameWindow.location.href) !== target.frameUrl) return false;
+ if (Number.isFinite(target?.documentTimeOrigin)
+ && frameWindow.performance.timeOrigin !== target.documentTimeOrigin) return false;
+ const pageWindow = frameWindow.wrappedJSObject || frameWindow;
+ if (pageWindow.__webbrainCaptchaCallbacks) return true;
+ // Await the script before provider dispatch or token application; merely
+ // appending it leaves a race with render() in the descendant document.
+ return await new Promise(resolve => {
+ const script = frameDocument.createElement('script');
+ const finish = success => { clearTimeout(timer); script.remove(); resolve(success); };
+ const timer = setTimeout(() => finish(false), 2000);
+ script.onload = () => finish(!!pageWindow.__webbrainCaptchaCallbacks);
+ script.onerror = () => finish(false);
+ script.src = src;
+ (frameDocument.head || frameDocument.documentElement).appendChild(script);
+ });
+ } catch (_) {
+ return false;
+ }
+}
+
+export async function ensureCaptchaCallbackBridge(tabId, target = null, api = globalThis.browser || globalThis.chrome) {
+ const frameIds = Number.isInteger(target?.frameId) ? [target.frameId] : null;
+ const hasFramePath = Array.isArray(target?.framePath) && target.framePath.length > 0;
+ try {
+ if (typeof api.scripting?.executeScript === 'function') {
+ const results = await api.scripting.executeScript({
+ target: frameIds ? { tabId, frameIds } : { tabId, allFrames: true },
+ world: 'MAIN',
+ ...(hasFramePath ? {
+ func: installCaptchaCallbackBridgeInPage,
+ args: [target, api.runtime.getURL(CAPTCHA_CALLBACK_BRIDGE_FILE)],
+ } : { files: [CAPTCHA_CALLBACK_BRIDGE_SCRIPT_FILE] }),
+ });
+ if (hasFramePath) return results?.some(entry => entry.result === true) === true;
+ return true;
+ }
+ } catch (_) { /* fall through to tag injection */ }
+ try {
+ // Runtimes without the scripting API: run the bridge through a page
+ // <script> tag so it still executes in MAIN world. Page CSP may block
+ // it; fail-open.
+ const src = api.runtime.getURL(CAPTCHA_CALLBACK_BRIDGE_FILE);
+ const code = `(${installCaptchaCallbackBridgeInPage.toString()})(${JSON.stringify(target)}, ${JSON.stringify(src)})`;
+ let results;
+ if (frameIds) {
+ results = await api.tabs.executeScript(tabId, { frameId: frameIds[0], matchAboutBlank: true, code });
+ } else {
+ results = await api.tabs.executeScript(tabId, { allFrames: true, matchAboutBlank: true, code });
+ }
+ return results?.some(result => result === true) === true;
+ } catch (_) {
+ return false;
+ }
+}
+
export async function injectToken(tabId, {
fieldName,
alsoSet,
@@ -470,9 +553,11 @@ export async function injectToken(tabId, {
callbackHint,
target: target || {},
};
- const code = `(() => {
+ // Best-effort fallback for documents without the early content script.
+ await ensureCaptchaCallbackBridge(tabId, target);
+ const injectionCode = callbackOnly => `(() => {
const inject = ${injectCaptchaTokenInPage.toString()};
- const payload = ${JSON.stringify(pagePayload)};
+ const payload = ${JSON.stringify({ ...pagePayload, callbackOnly })};
let frameDocument = typeof document !== 'undefined' ? document : null;
let frameWindow = typeof window !== 'undefined' ? window : globalThis;
for (const segment of Array.isArray(payload?.target?.framePath) ? payload.target.framePath : []) {
@@ -515,26 +600,54 @@ export async function injectToken(tabId, {
}
return inject(payload, { document: frameDocument, window: frameWindow });
})()`;
- const details = Number.isInteger(target?.frameId)
- ? { code, frameId: target.frameId, matchAboutBlank: true }
- : { code, ...(target?.frameUrl ? { allFrames: true } : {}) };
- const results = await browser.tabs.executeScript(tabId, details);
- const outputs = (results || []).filter(Boolean);
- const successes = outputs.filter(output => output.success === true);
- if (successes.length === 1) {
- return {
- ...successes[0],
- frameId: Number.isInteger(target?.frameId) ? target.frameId : null,
- };
- }
- if (successes.length > 1) {
- return {
- success: false,
- ambiguousTarget: true,
- error: 'Token injection matched more than one frame; pass an exact frameUrl.',
- matchedFrames: successes.map(output => ({ frameUrl: output.frameUrl })),
- };
+ const runOnce = async (callbackOnly = false) => {
+ const code = injectionCode(callbackOnly);
+ const details = Number.isInteger(target?.frameId)
+ ? { code, frameId: target.frameId, matchAboutBlank: true }
+ : { code, ...(target?.frameUrl ? { allFrames: true } : {}) };
+ const results = await browser.tabs.executeScript(tabId, details);
+ const outputs = (results || []).filter(Boolean);
+ const successes = outputs.filter(output => output.success === true);
+ if (successes.length === 1) {
+ return {
+ ...successes[0],
+ frameId: Number.isInteger(target?.frameId) ? target.frameId : null,
+ };
+ }
+ if (successes.length > 1) {
+ return {
+ success: false,
+ ambiguousTarget: true,
+ error: 'Token injection matched more than one frame; pass an exact frameUrl.',
+ matchedFrames: successes.map(output => ({ frameUrl: output.frameUrl })),
+ };
+ }
+ return outputs.find(output => !output.skipped)
+ || { success: false, error: 'injection script returned no matching frame result' };
+ };
+ const first = await runOnce();
+ // A clean miss means the widget may be rendering right now: no callback
+ // was invoked and either no callbacks were found or the widget's field /
+ // site key was absent while the frame identity itself matched
+ // (staleTarget without skipped). Frame-identity staleness (skipped),
+ // ambiguity, and target errors cannot resolve by waiting.
+ const cleanMiss = !first?.calledCallback
+ && (first?.callbackCandidates === 0 || first?.staleTarget === true)
+ && !first?.skipped && !first?.ambiguousTarget && !first?.targetRequired;
+ if (cleanMiss) {
+ await new Promise(resolve => setTimeout(resolve, CAPTCHA_BRIDGE_SETTLE_RETRY_MS));
+ if (!first.success) return { ...(await runOnce()), bridgeSettleRetried: true };
+ // Field events may have completed the challenge and removed its widget or
+ // navigated the frame. Preserve that application even if discovery fails.
+ try {
+ const retry = await runOnce(true);
+ if (retry.success) {
+ return { ...retry, fieldUpdated: first.fieldUpdated,
+ fieldsUpdated: first.fieldsUpdated, fieldsTouched: first.fieldsTouched,
+ bridgeSettleRetried: true };
+ }
+ } catch (_) { /* The successful application may have navigated the page. */ }
+ return { ...first, bridgeSettleRetried: true };
}
- return outputs.find(output => !output.skipped)
- || { success: false, error: 'injection script returned no matching frame result' };
+ return first;
}
diff --git a/test/captcha-callback-bridge-ui.mjs b/test/captcha-callback-bridge-ui.mjs
index e7700c42a..7ae0259cb 100644
--- a/test/captcha-callback-bridge-ui.mjs
+++ b/test/captcha-callback-bridge-ui.mjs
@@ -34,19 +34,27 @@ const server = createServer((req, res) => {
}
if (url.pathname === '/page.js') {
res.setHeader('Content-Type', 'application/javascript');
- res.end(`(() => {
+ // Page-load renders retain closure callbacks before any solver dispatch.
+ res.end(`window.renderWidget = () => {
let accepted = 0;
+ const mode = new URL(location.href).searchParams.get('mode');
+ const fieldMode = mode?.startsWith('field');
const complete = token => {
- if (token !== 'fixture-token' || api.getResponse(widget) !== token) throw new Error('app state was not updated');
+ if (token !== 'fixture-token' || (!fieldMode && api.getResponse(window.renderedWidget) !== token)) throw new Error('app state was not updated');
document.querySelector('#next').disabled=false;
document.querySelector('#challenge').hidden=true;
document.querySelector('#result').textContent='accepted:'+ (++accepted);
+ if (mode === 'field-remove') { document.querySelector('#challenge').remove(); document.querySelector('#other').remove(); }
};
const asyncMode = new URL(location.href).searchParams.get('mode')==='async';
- const widget=api.render('widget', {sitekey:${JSON.stringify(sitekey)}, ...(asyncMode?{}:{callback:complete})});
+ const widget=api.render('widget', {sitekey:${JSON.stringify(sitekey)}, ...(asyncMode || fieldMode?{}:{callback:complete})});
+ window.renderedWidget = widget;
+ if(fieldMode)document.querySelector('#answer-'+widget).addEventListener('change',event=>complete(event.target.value));
api.render('other', {sitekey:${JSON.stringify(sitekey)},callback:()=>{throw new Error('wrong widget');}});
if(asyncMode)api.execute(widget,{async:true}).then(({response})=>complete(response));
- })();`);
+ };
+ window.renderWidgetDelayed = ms => setTimeout(() => window.renderWidget(), ms);
+ if(new URL(location.href).searchParams.get('render')!=='late')window.renderWidget();`);
return;
}
res.setHeader('Content-Type', 'text/html');
@@ -55,10 +63,10 @@ const server = createServer((req, res) => {
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const origin = `http://127.0.0.1:${server.address().port}`;
-const scenarios = [['hcaptcha','callback'],['hcaptcha','async'],['turnstile','callback'],['recaptcha_v2','callback']];
+const scenarios = [['hcaptcha','callback'],['hcaptcha','async'],['turnstile','callback'],['recaptcha_v2','callback'],['hcaptcha','field'],['turnstile','field-remove']];
const payload = type => ({fieldName:type==='hcaptcha'?'h-captcha-response':type==='turnstile'?'cf-turnstile-response':'g-recaptcha-response',token:'fixture-token',
target:{frameId:0,websiteKey:sitekey,type,responseFieldId:'answer-widget-1'}});
-const probe = `JSON.stringify({early:bridgeAtFirstScript,enabled:!document.querySelector('#next').disabled,result:document.querySelector('#result').textContent,hidden:document.querySelector('#challenge').hidden})`;
+const probe = `JSON.stringify({early:bridgeAtFirstScript,bridge:!!window.__webbrainCaptchaCallbacks,enabled:!document.querySelector('#next').disabled,result:document.querySelector('#result').textContent,hidden:document.querySelector('#challenge')?.hidden ?? true})`;
async function verifyGate(tabId, input, api) {
const { Agent } = await import(api.runtime.getURL('src/agent/agent.js'));
const agent = new Agent({ getActive: () => ({ promptTier: 'full' }) });
@@ -72,11 +80,12 @@ async function verifyGate(tabId, input, api) {
return observation.gate;
}
const assertProgress = (build, type, mode, result, state) => {
- assert.equal(result.calledCallback, true, `${build} ${type} ${mode}: ${JSON.stringify(result)}`);
- assert.equal(result.callbackCandidates, 1);
+ assert.equal(result.success, true, `${build} ${type} ${mode}: ${JSON.stringify(result)}`);
+ assert.equal(result.calledCallback, !mode.startsWith('field'), JSON.stringify(result));
+ assert.equal(result.callbackCandidates, mode.startsWith('field') ? 0 : 1);
assert.equal(result.callbackBridgeAvailable, true);
- assert.deepEqual(state, {early:true,enabled:true,result:'accepted:1',hidden:true});
- console.log(`PASS ${build}: ${type} ${mode}, document-start capture, exact widget callback, page continuation and gate clearance`);
+ assert.deepEqual(state, {early:true,bridge:true,enabled:true,result:'accepted:1',hidden:true});
+ console.log(`PASS ${build}: ${type} ${mode}, page-load capture, single completion${mode === 'field-remove' ? ' after widget removal' : ' and gate clearance'}`);
};
try {
const profile = await mkdtemp(join(tmpdir(),'webbrain-captcha-chrome-'));
@@ -92,6 +101,7 @@ try {
for (const [type, mode] of scenarios) {
await page.goto(`${origin}/fixture?type=${type}&mode=${mode}`);
const input = payload(type);input.target.frameUrl=page.url();
+ assert.equal(await page.evaluate('!!window.__webbrainCaptchaCallbacks'), true, 'bridge must capture widgets before the solve');
const outcome = await control.evaluate(async ({input}) => {
const tabs = await chrome.tabs.query({});const tab=tabs.find(tab=>tab.url===input.target.frameUrl);
const {injectToken}=await import(chrome.runtime.getURL('src/agent/captcha-solver.js'));
@@ -104,9 +114,33 @@ try {
{pageUrl:input.target.frameUrl,pageContent:'button Continue'}, {});
return {injection,gate};
},{input});
- assert.equal(outcome.gate?.status, 'cleared', JSON.stringify(outcome));
+ if (mode !== 'field-remove') assert.equal(outcome.gate?.status, 'cleared', JSON.stringify(outcome));
assertProgress('chrome',type,mode,outcome.injection,JSON.parse(await page.evaluate(probe)));
}
+ {
+ // Remediation proof: the widget renders 1200ms after injection starts,
+ // so only the settle retry can capture its closure.
+ const type = 'hcaptcha', mode = 'callback';
+ await page.goto(`${origin}/fixture?type=${type}&mode=${mode}&render=late`);
+ const input = payload(type);input.target.frameUrl=page.url();
+ await page.evaluate('window.renderWidgetDelayed(1200)');
+ const outcome = await control.evaluate(async ({input}) => {
+ const tabs = await chrome.tabs.query({});const tab=tabs.find(tab=>tab.url===input.target.frameUrl);
+ const {injectToken}=await import(chrome.runtime.getURL('src/agent/captcha-solver.js'));
+ const injection = await injectToken(tab.id,input);
+ const { Agent } = await import(chrome.runtime.getURL('src/agent/agent.js'));
+ const agent = new Agent({ getActive: () => ({ promptTier: 'full' }) });
+ agent._captchaGateStates.set(tab.id, { pageUrl: input.target.frameUrl, status: 'verification_pending',
+ publicGate: { status: 'verification_pending', solveAttempted: true }, captchaCandidateIdentity: { ...input.target } });
+ const { gate } = await agent._observeCaptchaChallenge(tab.id, 'get_accessibility_tree',
+ {pageUrl:input.target.frameUrl,pageContent:'button Continue'}, {});
+ return {injection,gate};
+ },{input});
+ if (mode !== 'field-remove') assert.equal(outcome.gate?.status, 'cleared', JSON.stringify(outcome));
+ assert.equal(outcome.injection.calledCallback, true, JSON.stringify(outcome.injection));
+ assert.equal(outcome.injection.bridgeSettleRetried, true, 'late render must be captured by the settle retry');
+ assertProgress('chrome',type,mode+'+late-render',outcome.injection,JSON.parse(await page.evaluate(probe)));
+ }
} finally {await context?.close();await rm(profile,{recursive:true,force:true});}
const profile2 = await mkdtemp(join(tmpdir(),'webbrain-captcha-firefox-'));
@@ -136,6 +170,7 @@ try {
const url=`${origin}/fixture?type=${type}&mode=${mode}`;
await session.send('browsingContext.navigate',{context:page,url,wait:'complete'});
const input=payload(type);input.target.frameUrl=url;
+ assert.equal(await evaluate(page,'!!window.__webbrainCaptchaCallbacks'), true, 'bridge must capture widgets before the solve');
const outcome=JSON.parse(await evaluate(control.context,`(async()=>{
const tabs=await browser.tabs.query({});const tab=tabs.find(tab=>tab.url===${JSON.stringify(url)});
const {injectToken}=await import(browser.runtime.getURL('src/agent/captcha-solver.js'));
@@ -144,8 +179,88 @@ try {
const gate=await (${verifyGate.toString()})(tab.id,input,browser);
return JSON.stringify({injection,gate});
})()`));
- assert.equal(outcome.gate?.status, 'cleared', JSON.stringify(outcome));
+ if (mode !== 'field-remove') assert.equal(outcome.gate?.status, 'cleared', JSON.stringify(outcome));
assertProgress('firefox',type,mode,outcome.injection,JSON.parse(await evaluate(page,probe)));
}
+ {
+ // Remediation proof: the widget renders 1200ms after injection starts,
+ // so only the settle retry can capture its closure.
+ const type = 'hcaptcha', mode = 'callback';
+ const url=`${origin}/fixture?type=${type}&mode=${mode}&render=late`;
+ await session.send('browsingContext.navigate',{context:page,url,wait:'complete'});
+ const input=payload(type);input.target.frameUrl=url;
+ await evaluate(page,'window.renderWidgetDelayed(1200)');
+ const outcome=JSON.parse(await evaluate(control.context,`(async()=>{
+ const tabs=await browser.tabs.query({});const tab=tabs.find(tab=>tab.url===${JSON.stringify(url)});
+ const {injectToken}=await import(browser.runtime.getURL('src/agent/captcha-solver.js'));
+ const input=${JSON.stringify(input)};
+ const injection=await injectToken(tab.id,input);
+ const gate=await (${verifyGate.toString()})(tab.id,input,browser);
+ return JSON.stringify({injection,gate});
+ })()`));
+ if (mode !== 'field-remove') assert.equal(outcome.gate?.status, 'cleared', JSON.stringify(outcome));
+ assert.equal(outcome.injection.calledCallback, true, JSON.stringify(outcome.injection));
+ assert.equal(outcome.injection.bridgeSettleRetried, true, 'late render must be captured by the settle retry');
+ assertProgress('firefox',type,mode+'+late-render',outcome.injection,JSON.parse(await evaluate(page,probe)));
+ }
+ {
+ const url = `${origin}/fixture?type=hcaptcha&mode=callback&render=late`;
+ await session.send('browsingContext.navigate',{context:page,url,wait:'complete'});
+ await evaluate(page, `(async () => {
+ const outer = document.createElement('iframe'); outer.name = 'outer';
+ outer.srcdoc = '<iframe name="inner" srcdoc="&lt;div id=widget data-sitekey=${sitekey}&gt;&lt;/div&gt;"></iframe>';
+ const loaded = new Promise(resolve => outer.onload = resolve);
+ document.body.append(outer); await loaded;
+ const inner = outer.contentDocument.querySelector('iframe');
+ const child = window.descendant = inner.contentWindow;
+ child.resetFixture = () => {
+ // Simulate a document that never had the bridge, including its SDK
+ // property watchers, so reinstalling cannot reuse old registrations.
+ delete child.__webbrainCaptchaCallbacks;
+ delete child.hcaptcha; delete child.turnstile; delete child.grecaptcha;
+ child.document.getElementById('widget').replaceChildren();
+ child.accepted = null;
+ child.hcaptcha = {
+ render(id, params) {
+ const field = child.document.createElement('textarea');
+ field.name = 'h-captcha-response'; field.id = 'descendant-answer';
+ child.document.getElementById(id).append(field); return 'descendant-widget';
+ }, getResponse: () => ''
+ };
+ };
+ child.resetFixture();
+ child.renderFixture = () => child.hcaptcha.render('widget', {
+ sitekey: '${sitekey}', callback: token => { child.accepted = token; }
+ });
+ })()`);
+ const input = {fieldName:'h-captcha-response',token:'fixture-token',target:{
+ frameId:0,frameUrl:'about:srcdoc',websiteKey:sitekey,type:'hcaptcha',
+ responseFieldId:'descendant-answer',framePath:[
+ {index:0,frameUrl:'about:srcdoc',frameName:'outer'},
+ {index:0,frameUrl:'about:srcdoc',frameName:'inner'}
+ ]}};
+ for (const fallback of [false, true]) {
+ // Exercise MAIN scripting and the MV2 fallback in the actual descendant.
+ if (fallback) await evaluate(page, 'descendant.resetFixture()');
+ const installed = await evaluate(control.context, `(async () => {
+ const tab = (await browser.tabs.query({})).find(tab => tab.url === ${JSON.stringify(url)});
+ const {ensureCaptchaCallbackBridge} = await import(browser.runtime.getURL('src/agent/captcha-solver.js'));
+ const api = ${fallback ? '{runtime:browser.runtime,tabs:browser.tabs}' : 'browser'};
+ return ensureCaptchaCallbackBridge(tab.id, ${JSON.stringify(input.target)}, api);
+ })()`);
+ assert.equal(installed, true, `descendant bridge installation (fallback=${fallback})`);
+ assert.equal(await evaluate(page, '!!descendant.__webbrainCaptchaCallbacks'), true);
+ await evaluate(page, 'descendant.renderFixture()');
+ const result = JSON.parse(await evaluate(control.context, `(async () => {
+ const tab = (await browser.tabs.query({})).find(tab => tab.url === ${JSON.stringify(url)});
+ const {injectToken} = await import(browser.runtime.getURL('src/agent/captcha-solver.js'));
+ return JSON.stringify(await injectToken(tab.id, ${JSON.stringify(input)}));
+ })()`));
+ assert.equal(result.success, true, JSON.stringify(result));
+ assert.equal(result.calledCallback, true, JSON.stringify(result));
+ assert.equal(await evaluate(page, 'descendant.accepted'), 'fixture-token');
+ }
+ console.log('PASS firefox: inherited-origin descendant bridge, MAIN and MV2 fallback, selected closure completion');
+ }
} finally {session?.socket?.close();child.kill();await new Promise(resolve=>child.once('exit',resolve));await rm(profile2,{recursive:true,force:true});}
} finally {server.close();}
diff --git a/test/captcha-callback-bridge.mjs b/test/captcha-callback-bridge.mjs
index bb6d92aa1..241357566 100644
--- a/test/captcha-callback-bridge.mjs
+++ b/test/captcha-callback-bridge.mjs
@@ -6,6 +6,7 @@ import { readFile } from 'node:fs/promises';
for (const build of ['chrome', 'firefox']) {
const source = await readFile(new URL(`../src/${build}/src/content/captcha-callback-bridge.js`, import.meta.url), 'utf8');
const { injectCaptchaTokenInPage } = await import(`../src/${build}/src/agent/captcha-frame-runtime.js`);
+ const { injectToken, ensureCaptchaCallbackBridge } = await import(`../src/${build}/src/agent/captcha-solver.js`);
function fixture(family = 'hcaptcha') {
const name = family === 'hcaptcha' ? 'h-captcha-response' : family === 'turnstile' ? 'cf-turnstile-response' : 'g-recaptcha-response';
const hosts = [], fields = [];
@@ -16,7 +17,7 @@ for (const build of ['chrome', 'firefox']) {
: selector.includes('[data-sitekey]') ? hosts
: selector.includes('textarea[name=') ? fields.filter(field => selector.includes(`name="${field.name}"`)) : [],
};
- const context = vm.createContext({ document, URL, URLSearchParams, performance: { timeOrigin: 1000 }, location: { href: 'https://fixture.test/join' }, Event: class {}, answers: [] });
+ const context = vm.createContext({ document, URL, URLSearchParams, performance: { timeOrigin: 1000 }, location: { href: 'https://fixture.test/join' }, Event: class { constructor(type) { this.type = type; } }, answers: [] });
context.window = context;
const evaluate = code => vm.runInContext(code, context);
evaluate(source);
@@ -119,11 +120,100 @@ for (const build of ['chrome', 'firefox']) {
assert.equal(f.inject().calledCallback, false);
assert.equal(f.context.answers.length, 1);
});
- test(`${build}: bridge manifest runs before site scripts in every matching frame`, async () => {
+ for (const mode of ['field', 'removed', 'late-callback', 'navigated']) {
+ test(`${build}: settle retry preserves ${mode} application without replaying field events`, async () => {
+ const f = fixture();
+ f.render('one', 'undefined');
+ let events = 0, attempts = 0;
+ f.fields[0].dispatchEvent = event => {
+ events++;
+ if (event.type === 'change' && mode === 'removed') f.fields.shift();
+ };
+ const input = { fieldName: f.name, token: 'paid-token', callbackHint: 'lateComplete',
+ target: { frameId: 0, frameUrl: f.context.location.href, websiteKey: 'site-key',
+ type: 'hcaptcha', responseFieldId: 'answer-one', documentTimeOrigin: 1000 } };
+ const beforeAttempt = () => {
+ attempts++;
+ if (attempts === 2 && mode === 'navigated') throw new Error('frame navigated');
+ if (attempts === 2 && mode === 'late-callback') f.evaluate('lateComplete = token => answers.push(token)');
+ };
+ const api = {
+ scripting: { executeScript: async options => {
+ if (options.files) return [];
+ beforeAttempt();
+ return [{ frameId: 0, result: options.func(options.args[0], { document: f.context.document, window: f.context }) }];
+ } },
+ tabs: { executeScript: async (_, options) => {
+ beforeAttempt();
+ return [f.evaluate(options.code)];
+ } },
+ };
+ const namespace = build === 'firefox' ? 'browser' : 'chrome';
+ const previous = globalThis[namespace];
+ globalThis[namespace] = api;
+ try {
+ const result = await injectToken(1, input);
+ assert.equal(result.success, true, JSON.stringify(result));
+ assert.equal(result.fieldUpdated, true);
+ assert.equal(result.fieldsTouched, 1);
+ assert.equal(result.bridgeSettleRetried, true);
+ assert.equal(result.calledCallback, mode === 'late-callback');
+ assert.equal(events, 2, 'one input/change pair, even when callback discovery retries');
+ assert.equal(attempts, 2);
+ assert.deepEqual(JSON.parse(JSON.stringify(f.context.answers)), mode === 'late-callback' ? ['paid-token'] : []);
+ } finally {
+ if (previous === undefined) delete globalThis[namespace]; else globalThis[namespace] = previous;
+ }
+ });
+ }
+ if (build === 'firefox') for (const fallback of [false, true]) {
+ test(`firefox: descendant installation validates the full frame path (fallback=${fallback})`, async () => {
+ const f = fixture();
+ f.context.location.href = 'about:srcdoc';
+ f.context.name = 'inner';
+ f.evaluate('delete window.__webbrainCaptchaCallbacks');
+ let loads = 0;
+ f.context.document.createElement = () => ({ remove() {} });
+ f.context.document.head = { appendChild(script) { loads++; f.evaluate(source); script.onload(); } };
+ const outerWindow = { location: { href: 'about:blank' }, name: 'outer' };
+ const outerDocument = { querySelectorAll: () => [{ contentWindow: f.context, contentDocument: f.context.document }] };
+ const rootDocument = { querySelectorAll: () => [{ contentWindow: outerWindow, contentDocument: outerDocument }] };
+ const root = vm.createContext({ document: rootDocument, setTimeout, clearTimeout });
+ root.window = root;
+ root.__webbrainCaptchaCallbacks = { callbacks() {} };
+ const target = { frameId: 0, frameUrl: 'about:srcdoc', documentTimeOrigin: 1000,
+ framePath: [{ index: 0, frameUrl: 'about:blank', frameName: 'outer' },
+ { index: 0, frameUrl: 'about:srcdoc', frameName: 'inner' }] };
+ const api = { runtime: { getURL: file => `moz-extension://fixture/${file}` },
+ ...(fallback ? {} : { scripting: { executeScript: async options => [{ result: await vm.runInContext(
+ `(${options.func.toString()})(...${JSON.stringify(options.args)})`, root) }] } }),
+ tabs: { executeScript: async (_, options) => [await vm.runInContext(options.code, root)] } };
+ assert.equal(await ensureCaptchaCallbackBridge(1, target, api), true);
+ assert.equal(loads, 1, 'load into the descendant even when the ancestor already has a bridge');
+ assert.equal(typeof f.context.__webbrainCaptchaCallbacks.callbacks, 'function');
+ for (const stale of [
+ { ...target, framePath: [{ ...target.framePath[0], index: 1 }] },
+ { ...target, framePath: [{ ...target.framePath[0], frameName: 'changed' }] },
+ { ...target, frameUrl: 'about:blank' },
+ { ...target, documentTimeOrigin: 2000 },
+ ]) assert.equal(await ensureCaptchaCallbackBridge(1, stale, api), false);
+ assert.equal(loads, 1, 'stale paths and documents must not install another bridge');
+ });
+ }
+ test(`${build}: bridge captures page-load widgets and is ensured in the selected document`, async () => {
const manifest = JSON.parse(await readFile(new URL(`../src/${build}/manifest.json`, import.meta.url), 'utf8'));
- const entry = manifest.content_scripts.find(entry => entry.js?.includes('src/content/captcha-callback-bridge.js'));
- assert.equal(entry.world, 'MAIN'); assert.equal(entry.run_at, 'document_start');
- assert.equal(entry.all_frames, true); assert.equal(entry.match_about_blank, true);
+ const entries = (manifest.content_scripts || []).filter(entry => entry.js?.includes('src/content/captcha-callback-bridge.js'));
+ assert.equal(entries.length, 1);
+ assert.equal(entries[0].world, 'MAIN'); assert.equal(entries[0].run_at, 'document_start');
+ assert.equal(entries[0].all_frames, true); assert.equal(entries[0].match_about_blank, true);
+ const solver = await readFile(new URL(`../src/${build}/src/agent/captcha-solver.js`, import.meta.url), 'utf8');
+ assert.match(solver, /export async function ensureCaptchaCallbackBridge\(tabId/);
+ assert.match(solver, /await ensureCaptchaCallbackBridge\(tabId, target\)/);
+ assert.match(solver, /captcha-callback-bridge\.js/);
+ assert.match(solver, /CAPTCHA_BRIDGE_SETTLE_RETRY_MS/);
+ assert.match(solver, /bridgeSettleRetried/);
+ const agent = await readFile(new URL(`../src/${build}/src/agent/agent.js`, import.meta.url), 'utf8');
+ assert.match(agent, /await ensureCaptchaCallbackBridge\(tabId, detected\)/);
});
}
test('callback bridge is identical in Chrome and Firefox', async () => {