Skip to content

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

Preserve paid CAPTCHA answers across worker restarts

Emre Sokullufdf9228d4cfd
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index feece0e5a..542e6d6f3 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -10909,10 +10909,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...withoutLegacyCaptchaVerificationState(activeGate.publicGate),
status: 'verification_pending',
solveAttempted: true,
+ nativeAnswerPending: toolResult.applicationRequired === true || toolResult.applicationRetryable === true,
} : {
...activeGate.publicGate,
status: 'manual_required',
solveFailed: true,
+ nativeAnswerPending: false,
};
this._captchaGateStates.set(tabId, {
...(pending ? withoutLegacyCaptchaVerificationState(activeGate) : activeGate),
@@ -16500,6 +16502,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
});
}
this._restorePersistedRichTextToolbarAudit(tabId, entry.richTextToolbarAudit);
+ const savedAnswer = entry.nativeCaptchaAnswer;
+ const nativeAnswerRestored = savedAnswer && typeof savedAnswer.pageUrl === 'string'
+ && Number.isFinite(savedAnswer.createdAt)
+ && Date.now() >= savedAnswer.createdAt && Date.now() - savedAnswer.createdAt <= 180_000
+ && savedAnswer.applied === false && savedAnswer.solution !== undefined
+ && Array.isArray(savedAnswer.documents)
+ && savedAnswer.documents.some(document => document?.frameId === 0);
+ if (nativeAnswerRestored) {
+ this._nativeCaptchaSolutions ||= new Map();
+ this._nativeCaptchaSolutions.set(tabId, {
+ ...savedAnswer,
+ dispatchedTimeOrigins: new Set(Array.isArray(savedAnswer.dispatchedTimeOrigins)
+ ? savedAnswer.dispatchedTimeOrigins.filter(Number.isFinite) : []),
+ });
+ }
const captchaGateState = entry.captchaGateState;
if (
captchaGateState
@@ -16513,7 +16530,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
&& captchaGateState.cloudflareManagedChallenge !== true
&& captchaGateState.publicGate.cloudflareManagedChallenge !== true
) {
- if (!cloudflareSignal) {
+ if (!cloudflareSignal && (!captchaGateState.publicGate.nativeAnswerPending || nativeAnswerRestored)) {
this._captchaGateStates.set(tabId, normalizeCaptchaGateState(captchaGateState));
}
}
@@ -16557,6 +16574,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
})
: { messages: [], compacted: false, bytes: 2 };
const captchaGateState = this._captchaGateStates.get(tabId) || null;
+ const nativeAnswer = this._nativeCaptchaSolutions?.get(tabId);
+ const savedNativeAnswer = nativeAnswer?.solution !== undefined && nativeAnswer.applied === false
+ && Date.now() - nativeAnswer.createdAt <= 180_000
+ ? { pageUrl: nativeAnswer.pageUrl, createdAt: nativeAnswer.createdAt, applied: false,
+ documents: nativeAnswer.documents, solution: nativeAnswer.solution,
+ dispatchedTimeOrigins: [...(nativeAnswer.dispatchedTimeOrigins || [])] }
+ : null;
+ const nativeCaptchaAnswer = savedNativeAnswer && JSON.stringify(savedNativeAnswer).length <= 250_000
+ ? savedNativeAnswer : null;
return {
mode: this.conversationModes.get(tabId) || 'ask',
messages: serialized.messages,
@@ -16577,6 +16603,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
|| captchaGateState?.publicGate?.cloudflareManagedChallenge === true
? null
: captchaGateState,
+ nativeCaptchaAnswer,
};
}
@@ -26087,6 +26114,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
this.completionInvariants.delete(tabId);
this.readCompletenessStates.delete(tabId);
this._captchaGateStates.delete(tabId);
+ this._nativeCaptchaSolutions?.delete(tabId);
if (preserveRunGuard) {
this._activeCloudflareManagedChallengeGate(tabId);
} else {
@@ -35771,11 +35799,21 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
try {
const result = await applyNativeCaptchaSolution(tabId, record, args, chrome);
if (record && result.success === true) record.applicationSucceeded = true;
- return { ...result, injected: result.success === true, dispatched: record?.applied === true,
+ const outcome = { ...result, injected: result.success === true, dispatched: record?.applied === true,
applicationRetryable: result.success !== true && await applicationRetryable() };
+ if (record?.applied === true) {
+ this._captchaSolveGateAfterTool(tabId, name, outcome);
+ await this._persistNow(tabId);
+ }
+ return outcome;
} catch (error) {
- return { success: false, error: error.message, dispatched: record?.applied === true,
+ const outcome = { success: false, error: error.message, dispatched: record?.applied === true,
applicationRetryable: await applicationRetryable() };
+ if (record?.applied === true) {
+ this._captchaSolveGateAfterTool(tabId, name, outcome);
+ await this._persistNow(tabId);
+ }
+ return outcome;
}
}
if (name === 'solve_captcha') {
@@ -35834,6 +35872,8 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
const result = await solveNativeCaptchaTasks(prepared);
record.solution = result.solution;
record.createdAt = Date.now();
+ this._captchaSolveGateAfterTool(tabId, 'solve_captcha', { success: true, applicationRequired: true });
+ await this._persistNow(tabId);
return { success: true, dispatched: true, type: result.family, provider: result.provider, method: result.method, taskId: result.taskId,
solution: result.solution, ...(result.userAgent ? { solverUserAgent: result.userAgent } : {}),
injected: false, applicationRequired: true,
diff --git a/src/chrome/src/agent/captcha-native-providers.js b/src/chrome/src/agent/captcha-native-providers.js
index 9a6760c46..70f598b3a 100644
--- a/src/chrome/src/agent/captcha-native-providers.js
+++ b/src/chrome/src/agent/captcha-native-providers.js
@@ -126,7 +126,8 @@ const FAMILY_IDENTIFIERS = {
lemin: [['captchaId', 'captcha_id'], ['divId', 'div_id']],
vk: [['redirectUri', 'redirect_uri']],
aws_waf: [['websiteKey', 'sitekey', 'awsKey'], ['iv', 'awsIv'], ['context', 'awsContext'],
- ['challengeScript', 'challenge_script', 'awsChallengeJS']],
+ ['challengeScript', 'challenge_script', 'awsChallengeJS'], ['captchaScript'],
+ ['jsapiScript', 'awsApiJs']],
alibaba: [['sceneId', 'metadata.sceneId'], ['prefix', 'metadata.prefix'],
['userId', 'metadata.userId'], ['userUserId', 'metadata.userUserId'],
['userCertifyId', 'metadata.UserCertifyId']],
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index e3b151fdb..32fd0ecd1 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -5951,6 +5951,21 @@ export class Agent extends LoopDetector {
});
}
this._restorePersistedRichTextToolbarAudit(tabId, entry.richTextToolbarAudit);
+ const savedAnswer = entry.nativeCaptchaAnswer;
+ const nativeAnswerRestored = savedAnswer && typeof savedAnswer.pageUrl === 'string'
+ && Number.isFinite(savedAnswer.createdAt)
+ && Date.now() >= savedAnswer.createdAt && Date.now() - savedAnswer.createdAt <= 180_000
+ && savedAnswer.applied === false && savedAnswer.solution !== undefined
+ && Array.isArray(savedAnswer.documents)
+ && savedAnswer.documents.some(document => document?.frameId === 0);
+ if (nativeAnswerRestored) {
+ this._nativeCaptchaSolutions ||= new Map();
+ this._nativeCaptchaSolutions.set(tabId, {
+ ...savedAnswer,
+ dispatchedTimeOrigins: new Set(Array.isArray(savedAnswer.dispatchedTimeOrigins)
+ ? savedAnswer.dispatchedTimeOrigins.filter(Number.isFinite) : []),
+ });
+ }
const captchaGateState = entry.captchaGateState;
if (
captchaGateState
@@ -5964,7 +5979,7 @@ export class Agent extends LoopDetector {
&& captchaGateState.cloudflareManagedChallenge !== true
&& captchaGateState.publicGate.cloudflareManagedChallenge !== true
) {
- if (!cloudflareSignal) {
+ if (!cloudflareSignal && (!captchaGateState.publicGate.nativeAnswerPending || nativeAnswerRestored)) {
this._captchaGateStates.set(tabId, normalizeCaptchaGateState(captchaGateState));
}
}
@@ -6008,6 +6023,15 @@ export class Agent extends LoopDetector {
})
: { messages: [], compacted: false, bytes: 2 };
const captchaGateState = this._captchaGateStates.get(tabId) || null;
+ const nativeAnswer = this._nativeCaptchaSolutions?.get(tabId);
+ const savedNativeAnswer = nativeAnswer?.solution !== undefined && nativeAnswer.applied === false
+ && Date.now() - nativeAnswer.createdAt <= 180_000
+ ? { pageUrl: nativeAnswer.pageUrl, createdAt: nativeAnswer.createdAt, applied: false,
+ documents: nativeAnswer.documents, solution: nativeAnswer.solution,
+ dispatchedTimeOrigins: [...(nativeAnswer.dispatchedTimeOrigins || [])] }
+ : null;
+ const nativeCaptchaAnswer = savedNativeAnswer && JSON.stringify(savedNativeAnswer).length <= 250_000
+ ? savedNativeAnswer : null;
return {
mode: this.conversationModes.get(tabId) || 'ask',
messages: serialized.messages,
@@ -6028,6 +6052,7 @@ export class Agent extends LoopDetector {
|| captchaGateState?.publicGate?.cloudflareManagedChallenge === true
? null
: captchaGateState,
+ nativeCaptchaAnswer,
};
}
@@ -9523,10 +9548,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...withoutLegacyCaptchaVerificationState(activeGate.publicGate),
status: 'verification_pending',
solveAttempted: true,
+ nativeAnswerPending: toolResult.applicationRequired === true || toolResult.applicationRetryable === true,
} : {
...activeGate.publicGate,
status: 'manual_required',
solveFailed: true,
+ nativeAnswerPending: false,
};
this._captchaGateStates.set(tabId, {
...(pending ? withoutLegacyCaptchaVerificationState(activeGate) : activeGate),
@@ -24607,6 +24634,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
this.completionInvariants.delete(tabId);
this.readCompletenessStates.delete(tabId);
this._captchaGateStates.delete(tabId);
+ this._nativeCaptchaSolutions?.delete(tabId);
if (preserveRunGuard) {
this._activeCloudflareManagedChallengeGate(tabId);
} else {
@@ -32757,11 +32785,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
try {
const result = await applyNativeCaptchaSolution(tabId, record, args, browser);
if (record && result.success === true) record.applicationSucceeded = true;
- return { ...result, injected: result.success === true, dispatched: record?.applied === true,
+ const outcome = { ...result, injected: result.success === true, dispatched: record?.applied === true,
applicationRetryable: result.success !== true && await applicationRetryable() };
+ if (record?.applied === true) {
+ this._captchaSolveGateAfterTool(tabId, name, outcome);
+ await this._persistNow(tabId);
+ }
+ return outcome;
} catch (error) {
- return { success: false, error: error.message, dispatched: record?.applied === true,
+ const outcome = { success: false, error: error.message, dispatched: record?.applied === true,
applicationRetryable: await applicationRetryable() };
+ if (record?.applied === true) {
+ this._captchaSolveGateAfterTool(tabId, name, outcome);
+ await this._persistNow(tabId);
+ }
+ return outcome;
}
}
if (name === 'solve_captcha') {
@@ -32817,6 +32855,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const result = await solveNativeCaptchaTasks(prepared);
record.solution = result.solution;
record.createdAt = Date.now();
+ this._captchaSolveGateAfterTool(tabId, 'solve_captcha', { success: true, applicationRequired: true });
+ await this._persistNow(tabId);
return { success: true, dispatched: true, type: result.family, provider: result.provider, method: result.method, taskId: result.taskId,
solution: result.solution, ...(result.userAgent ? { solverUserAgent: result.userAgent } : {}),
injected: false, applicationRequired: true,
diff --git a/src/firefox/src/agent/captcha-native-providers.js b/src/firefox/src/agent/captcha-native-providers.js
index 9a6760c46..70f598b3a 100644
--- a/src/firefox/src/agent/captcha-native-providers.js
+++ b/src/firefox/src/agent/captcha-native-providers.js
@@ -126,7 +126,8 @@ const FAMILY_IDENTIFIERS = {
lemin: [['captchaId', 'captcha_id'], ['divId', 'div_id']],
vk: [['redirectUri', 'redirect_uri']],
aws_waf: [['websiteKey', 'sitekey', 'awsKey'], ['iv', 'awsIv'], ['context', 'awsContext'],
- ['challengeScript', 'challenge_script', 'awsChallengeJS']],
+ ['challengeScript', 'challenge_script', 'awsChallengeJS'], ['captchaScript'],
+ ['jsapiScript', 'awsApiJs']],
alibaba: [['sceneId', 'metadata.sceneId'], ['prefix', 'metadata.prefix'],
['userId', 'metadata.userId'], ['userUserId', 'metadata.userUserId'],
['userCertifyId', 'metadata.UserCertifyId']],
diff --git a/test/captcha-native-providers.mjs b/test/captcha-native-providers.mjs
index 432a86be5..db8d8ef40 100644
--- a/test/captcha-native-providers.mjs
+++ b/test/captcha-native-providers.mjs
@@ -101,6 +101,26 @@ for (const browser of ['chrome','firefox']) {
assert.throws(() => native.prepareNativeCaptchaTasks(providers, [capsolver, token]), /same challenge mode/);
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: {
+ websiteURL: url, websiteKey: 'key', jsapiScript: 'https://a.test/jsapi.js',
+ challengeScript: 'https://a.test/challenge.js',
+ } };
+ const capsolver = { provider: 'capsolver', method: 'AntiAwsWafTaskProxyLess', parameters: {
+ websiteURL: url, awsKey: 'key', awsApiJs: 'https://a.test/jsapi.js',
+ awsChallengeJS: 'https://a.test/challenge.js',
+ } };
+ assert.equal(native.prepareNativeCaptchaTasks(providers, [twoCaptcha, capsolver]).length, 2);
+ for (const parameters of [
+ { ...capsolver.parameters, awsApiJs: 'https://b.test/jsapi.js' },
+ { ...capsolver.parameters, awsChallengeJS: 'https://b.test/challenge.js' },
+ ]) assert.throws(() => native.prepareNativeCaptchaTasks(providers, [twoCaptcha, { ...capsolver, parameters }]), /same observed challenge/);
+ assert.throws(() => native.prepareNativeCaptchaTasks(providers, [
+ { ...twoCaptcha, parameters: { ...twoCaptcha.parameters, captchaScript: 'https://a.test/captcha.js' } }, capsolver,
+ ]), /same observed challenge/);
+ assert.equal(calls.length, 0);
+ });
test(`${browser}: enumerated nested task fields reject undocumented data before paid dispatch`, t => {
const calls = mockApi(t, () => { throw new Error('Must not dispatch'); });
const entry = { provider: 'capmonster', method: 'CustomTask:alibaba', parameters: {
diff --git a/test/captcha-runtime-policy.mjs b/test/captcha-runtime-policy.mjs
index 6dca17120..c9280dcdb 100644
--- a/test/captcha-runtime-policy.mjs
+++ b/test/captcha-runtime-policy.mjs
@@ -175,6 +175,48 @@ for (const build of ['chrome', 'firefox']) {
assert.equal(record.dispatchedTimeOrigins.has(1000), true);
assert.equal(agent._captchaGateStates.has(1), false);
});
+ test(`${build}: an unexpired paid native answer survives background restart with its gate`, async () => {
+ const agent = agentFor('act', 'full');
+ const pageUrl = 'https://example.test/challenge';
+ agent.conversations.set(1, [{ role: 'system', content: 'test' }]);
+ agent._nativeCaptchaSolutions = new Map([[1, {
+ pageUrl, createdAt: Date.now(), applied: false, solution: { token: 'paid-answer' },
+ documents: [{ frameId: 0, url: pageUrl, timeOrigin: 1000 }],
+ dispatchedTimeOrigins: new Set([1000]),
+ }]]);
+ agent._captchaGateStates.set(1, { key: `${pageUrl}\nchallenge`, status: 'verification_pending',
+ publicGate: { status: 'verification_pending', nativeAnswerPending: true, solveAttempted: true } });
+ const entry = structuredClone(agent._conversationStorageEntry(1));
+ const savedAnswer = entry.nativeCaptchaAnswer;
+ assert.deepEqual(entry.nativeCaptchaAnswer.solution, { token: 'paid-answer' });
+ agent._nativeCaptchaSolutions.get(1).applied = true;
+ agent._captchaSolveGateAfterTool(1, 'apply_captcha_solution', { success: true, injected: true });
+ assert.equal(agent._conversationStorageEntry(1).nativeCaptchaAnswer, null);
+ assert.equal(agent._captchaGateStates.get(1).publicGate.nativeAnswerPending, false);
+ const restored = agentFor('act', 'full');
+ const apiName = build === 'chrome' ? 'chrome' : 'browser';
+ const previous = globalThis[apiName];
+ globalThis[apiName] = { storage: { session: { get: async key => key === restored._convKey(1)
+ ? { [key]: entry } : {} } } };
+ try {
+ await restored._hydrate(1);
+ assert.deepEqual(restored._nativeCaptchaSolutions.get(1).solution, { token: 'paid-answer' });
+ assert.equal(restored._nativeCaptchaSolutions.get(1).dispatchedTimeOrigins.has(1000), true);
+ assert.equal(restored._captchaGateStates.get(1).status, 'verification_pending');
+ assert.equal(restored._hasUnappliedNativeCaptchaSolution(1), true);
+
+ const missing = agentFor('act', 'full');
+ delete entry.nativeCaptchaAnswer;
+ await missing._hydrate(1);
+ assert.equal(missing._captchaGateStates.has(1), false);
+ const expired = agentFor('act', 'full');
+ entry.nativeCaptchaAnswer = { ...savedAnswer, createdAt: Date.now() - 180_001 };
+ await expired._hydrate(1);
+ assert.equal(expired._captchaGateStates.has(1), false);
+ } finally {
+ if (previous === undefined) delete globalThis[apiName]; else globalThis[apiName] = previous;
+ }
+ });
test(`${build}: a same-URL reload retires the answer and releases the previous gate`, async () => {
const agent = agentFor('act', 'full');
const pageUrl = 'https://example.test/challenge';