Skip to content

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

fix: fail closed for malformed safe-first options

Emre Sokullu0581527763a4
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 6728a1757..5cbfc7902 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -16725,7 +16725,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const previous = this._clarificationAuthorizationGuards.get(tabId);
const priorTimeoutGuard = previous?.source === 'timeout'
&& previous?.authorized === false
- && previous?.conversationId === conversationId;
+ && (!previous?.conversationId || previous.conversationId === conversationId);
const timedOutAffirmative = normalizedSource === 'timeout' && safeFirst === true && !priorTimeoutGuard;
if (normalizedSource === 'timeout' && !timedOutAffirmative) {
const blockedAttempts = priorTimeoutGuard
@@ -33851,10 +33851,12 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
if (!question) {
return { success: false, error: 'clarify: `question` is required (a single sentence asking the user something specific).' };
}
- const rawOptions = Array.isArray(args?.options) ? args.options : [];
- const options = rawOptions
- .map(s => String(s).trim().slice(0, 200)).filter(option => /[\p{L}\p{N}\p{P}\p{S}]/u.test(option)).slice(0, 4);
- const safeFirst = args?.safe_first === true && /[\p{L}\p{N}\p{P}\p{S}]/u.test(String(rawOptions[0] || ''));
+ const rawOptions = Array.isArray(args?.options) ? args.options.slice(0, 4) : [];
+ const normalizedOptions = rawOptions.map(s => String(s).trim().slice(0, 200));
+ const options = normalizedOptions.every(option => /[\p{L}\p{N}\p{P}\p{S}]/u.test(option))
+ ? normalizedOptions
+ : [];
+ const safeFirst = args?.safe_first === true && options.length > 0;
const reason = args?.reason ? String(args.reason).slice(0, 300) : null;
const purpose = args?.purpose === 'research_escalation'
? 'research_escalation'
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 04324e6d3..c027d9749 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -14493,7 +14493,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const previous = this._clarificationAuthorizationGuards.get(tabId);
const priorTimeoutGuard = previous?.source === 'timeout'
&& previous?.authorized === false
- && previous?.conversationId === conversationId;
+ && (!previous?.conversationId || previous.conversationId === conversationId);
const timedOutAffirmative = normalizedSource === 'timeout' && safeFirst === true && !priorTimeoutGuard;
if (normalizedSource === 'timeout' && !timedOutAffirmative) {
const blockedAttempts = priorTimeoutGuard
@@ -30560,10 +30560,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (!question) {
return { success: false, error: 'clarify: `question` is required (a single sentence asking the user something specific).' };
}
- const rawOptions = Array.isArray(args?.options) ? args.options : [];
- const options = rawOptions
- .map(s => String(s).trim().slice(0, 200)).filter(option => /[\p{L}\p{N}\p{P}\p{S}]/u.test(option)).slice(0, 4);
- const safeFirst = args?.safe_first === true && /[\p{L}\p{N}\p{P}\p{S}]/u.test(String(rawOptions[0] || ''));
+ const rawOptions = Array.isArray(args?.options) ? args.options.slice(0, 4) : [];
+ const normalizedOptions = rawOptions.map(s => String(s).trim().slice(0, 200));
+ const options = normalizedOptions.every(option => /[\p{L}\p{N}\p{P}\p{S}]/u.test(option))
+ ? normalizedOptions
+ : [];
+ const safeFirst = args?.safe_first === true && options.length > 0;
const reason = args?.reason ? String(args.reason).slice(0, 300) : null;
const purpose = args?.purpose === 'research_escalation'
? 'research_escalation'
diff --git a/test/run.js b/test/run.js
index 1c44bd734..42187bdef 100644
--- a/test/run.js
+++ b/test/run.js
@@ -48831,6 +48831,17 @@ test('clarify result distinguishes waited timeout from user and Instant authoriz
);
assert.equal(promotedOptionTimeout.authorized, false, `${AgentClass.name}: safe_first authorized a later promoted option`);
+ await agent._recordClarificationAuthorization(tabId, 'user');
+ const truncatedInvisibleOptionTimeout = await agent.executeTool(
+ tabId,
+ 'clarify',
+ { question: 'Delete everything?', options: [`${'\u200B'.repeat(200)}Yes`, 'Yes, delete them'], safe_first: true },
+ (type, data) => {
+ if (type === 'clarify') agent.submitClarifyResponse(tabId, data.clarifyId, 'Yes, delete them', 'timeout');
+ },
+ );
+ assert.equal(truncatedInvisibleOptionTimeout.authorized, false, `${AgentClass.name}: truncated invisible option promoted a later safe_first choice`);
+
await agent._recordClarificationAuthorization(tabId, 'user');
const affirmativeTimeout = await agent.executeTool(
tabId,
@@ -48876,6 +48887,17 @@ test('clarify result distinguishes waited timeout from user and Instant authoriz
assert.equal(instant.source, 'auto', `${AgentClass.name}: Instant did not use source=auto`);
assert.equal(instant.authorized, true, `${AgentClass.name}: configured Instant mode was not authorized`);
assert.equal(agent._clarificationAuthorizationGuards.has(tabId), false, `${AgentClass.name}: Instant unexpectedly armed the timeout guard`);
+
+ const unscopedTabId = tabId + 100;
+ await agent._recordClarificationAuthorization(unscopedTabId, 'timeout');
+ agent.conversationIds.set(unscopedTabId, `conv_${unscopedTabId}`);
+ assert.equal(
+ await agent._recordClarificationAuthorization(unscopedTabId, 'timeout', true),
+ false,
+ `${AgentClass.name}: safe_first cleared an unscoped timeout guard after conversation creation`,
+ );
+ assert.equal(agent._clarificationAuthorizationGuards.get(unscopedTabId)?.authorized, false,
+ `${AgentClass.name}: unscoped timeout guard was removed after conversation creation`);
}
});