Skip to content

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

fix(recipient-guard): authorize recipient role changes strictly from user answer

Emre Sokullu8bdf391d95a6
diff --git a/src/chrome/src/agent/message-recipient-guard.js b/src/chrome/src/agent/message-recipient-guard.js
index 04a9e105b..93851a62d 100644
--- a/src/chrome/src/agent/message-recipient-guard.js
+++ b/src/chrome/src/agent/message-recipient-guard.js
@@ -229,27 +229,24 @@ export function messageTargetMatchesObservedIdentities(target, candidates) {
}
/**
- * Does the clarification context or user answer explicitly authorize targetRole
- * (to, cc, or bcc) for candidateIdentity?
+ * Does the user answer explicitly authorize targetRole (to, cc, or bcc) for
+ * candidateIdentity?
*
- * For 'bcc' and 'cc', explicit mentions of BCC or CC in the text surrounding
- * the recipient identity or in the answer authorize the role.
+ * Role authorization must come strictly from the user's answer, never from
+ * the clarification context (question, reason, or options) which are
+ * model-authored and cannot supply recipient authorization.
+ *
+ * For 'bcc' and 'cc', explicit mentions of BCC or CC in the answer authorize the role.
* For 'to', because "to" is also a ubiquitous preposition ("send to Alice"),
- * an explicit role label ("To: Alice", "Alice (To)", "as To", "in To", "To field")
- * or localized role indicator ("收件人", "主送") is required.
+ * an explicit role label ("To: Alice", "Alice (To)", "as To", "in To", "to To", "To field")
+ * or localized role indicator ("收件人", "主送") is required in the user's answer.
*/
export function clarificationAuthorizesRecipientRole(clarifyContext, answer, candidateIdentity, targetRole) {
const role = String(targetRole || '').trim().toLowerCase();
if (!MESSAGE_RECIPIENT_ROLES.has(role)) return false;
const answerText = String(answer ?? '').trim();
- const contextTexts = clarifyContext && typeof clarifyContext === 'object'
- ? [
- clarifyContext.question,
- clarifyContext.reason,
- ...(Array.isArray(clarifyContext.options) ? clarifyContext.options : []),
- ].filter(Boolean).map(s => String(s).trim())
- : [];
+ if (!answerText) return false;
const candidate = typeof candidateIdentity === 'object' && candidateIdentity !== null
? candidateIdentity
@@ -272,7 +269,7 @@ export function clarificationAuthorizesRecipientRole(clarifyContext, answer, can
if (roleToCheck === 'to') {
if (/\bto\s*[::]/i.test(snippet)) return true;
if (/[(\[【]\s*to\s*[)\]】]/i.test(snippet)) return true;
- if (/\b(?:as|in|into|role|field)\s+to\b/i.test(snippet)) return true;
+ if (/\b(?:as|in|into|role|field|to)\s+to\b/i.test(snippet)) return true;
if (/\bto\s+(?:field|role|recipient)\b/i.test(snippet)) return true;
if (/\bfrom\s+(?:bcc|cc)\s+to\s+to\b/i.test(snippet)) return true;
if (/\b(?:bcc|cc)\s*(?:->|=>|→)\s*to\b/i.test(snippet)) return true;
@@ -313,17 +310,7 @@ export function clarificationAuthorizesRecipientRole(clarifyContext, answer, can
return false;
}
- if (answerText && textAuthorizesRoleForIdentity(answerText)) {
- return true;
- }
-
- for (const contextText of contextTexts) {
- if (contextText && textAuthorizesRoleForIdentity(contextText)) {
- return true;
- }
- }
-
- return false;
+ return textAuthorizesRoleForIdentity(answerText);
}
/**
diff --git a/src/firefox/src/agent/message-recipient-guard.js b/src/firefox/src/agent/message-recipient-guard.js
index 04a9e105b..93851a62d 100644
--- a/src/firefox/src/agent/message-recipient-guard.js
+++ b/src/firefox/src/agent/message-recipient-guard.js
@@ -229,27 +229,24 @@ export function messageTargetMatchesObservedIdentities(target, candidates) {
}
/**
- * Does the clarification context or user answer explicitly authorize targetRole
- * (to, cc, or bcc) for candidateIdentity?
+ * Does the user answer explicitly authorize targetRole (to, cc, or bcc) for
+ * candidateIdentity?
*
- * For 'bcc' and 'cc', explicit mentions of BCC or CC in the text surrounding
- * the recipient identity or in the answer authorize the role.
+ * Role authorization must come strictly from the user's answer, never from
+ * the clarification context (question, reason, or options) which are
+ * model-authored and cannot supply recipient authorization.
+ *
+ * For 'bcc' and 'cc', explicit mentions of BCC or CC in the answer authorize the role.
* For 'to', because "to" is also a ubiquitous preposition ("send to Alice"),
- * an explicit role label ("To: Alice", "Alice (To)", "as To", "in To", "To field")
- * or localized role indicator ("收件人", "主送") is required.
+ * an explicit role label ("To: Alice", "Alice (To)", "as To", "in To", "to To", "To field")
+ * or localized role indicator ("收件人", "主送") is required in the user's answer.
*/
export function clarificationAuthorizesRecipientRole(clarifyContext, answer, candidateIdentity, targetRole) {
const role = String(targetRole || '').trim().toLowerCase();
if (!MESSAGE_RECIPIENT_ROLES.has(role)) return false;
const answerText = String(answer ?? '').trim();
- const contextTexts = clarifyContext && typeof clarifyContext === 'object'
- ? [
- clarifyContext.question,
- clarifyContext.reason,
- ...(Array.isArray(clarifyContext.options) ? clarifyContext.options : []),
- ].filter(Boolean).map(s => String(s).trim())
- : [];
+ if (!answerText) return false;
const candidate = typeof candidateIdentity === 'object' && candidateIdentity !== null
? candidateIdentity
@@ -272,7 +269,7 @@ export function clarificationAuthorizesRecipientRole(clarifyContext, answer, can
if (roleToCheck === 'to') {
if (/\bto\s*[::]/i.test(snippet)) return true;
if (/[(\[【]\s*to\s*[)\]】]/i.test(snippet)) return true;
- if (/\b(?:as|in|into|role|field)\s+to\b/i.test(snippet)) return true;
+ if (/\b(?:as|in|into|role|field|to)\s+to\b/i.test(snippet)) return true;
if (/\bto\s+(?:field|role|recipient)\b/i.test(snippet)) return true;
if (/\bfrom\s+(?:bcc|cc)\s+to\s+to\b/i.test(snippet)) return true;
if (/\b(?:bcc|cc)\s*(?:->|=>|→)\s*to\b/i.test(snippet)) return true;
@@ -313,17 +310,7 @@ export function clarificationAuthorizesRecipientRole(clarifyContext, answer, can
return false;
}
- if (answerText && textAuthorizesRoleForIdentity(answerText)) {
- return true;
- }
-
- for (const contextText of contextTexts) {
- if (contextText && textAuthorizesRoleForIdentity(contextText)) {
- return true;
- }
- }
-
- return false;
+ return textAuthorizesRoleForIdentity(answerText);
}
/**
diff --git a/test/run.js b/test/run.js
index 829e7dddc..031b23079 100644
--- a/test/run.js
+++ b/test/run.js
@@ -4774,6 +4774,43 @@ test('direct-message recipient guard uses structured intent and exact active ide
'explicit "To:" answer must update to the authorized To role',
);
+ const modelAuthoredRoleContext = {
+ question: 'Should Alice move from BCC to To?',
+ reason: 'Recipient is in To in composer',
+ options: ['Move Alice from BCC to To', 'Keep Alice as BCC'],
+ purpose: 'recipient_change',
+ };
+ assert.equal(
+ helper.clarificationAuthorizesRecipientRole(modelAuthoredRoleContext, 'No, keep Alice as BCC', 'Alice', 'to'),
+ false,
+ 'model-authored question mentioning "from BCC to To" must not authorize role change when user rejects or does not authorize',
+ );
+ assert.equal(
+ helper.clarificationAuthorizesRecipientRole(modelAuthoredRoleContext, 'Alice', 'Alice', 'to'),
+ false,
+ 'bare identity answer under model-authored role question must not authorize role change',
+ );
+ assert.deepEqual(
+ helper.resolveClarifiedRecipients(observedToCandidates, prevBccTarget, modelAuthoredRoleContext, 'No, keep Alice as BCC'),
+ [{ identity: '[email protected]', role: 'bcc' }],
+ 'user answer rejecting role change under model-authored context must retain previously authorized bcc role',
+ );
+ assert.deepEqual(
+ helper.resolveClarifiedRecipients(observedToCandidates, prevBccTarget, modelAuthoredRoleContext, '[email protected]'),
+ [{ identity: '[email protected]', role: 'bcc' }],
+ 'bare identity answer under model-authored role question must retain previously authorized bcc role',
+ );
+ assert.deepEqual(
+ helper.resolveClarifiedRecipients(observedToCandidates, prevBccTarget, modelAuthoredRoleContext, 'Move Alice to To'),
+ [{ identity: '[email protected]', role: 'to' }],
+ 'user answer explicitly authorizing To role must update to To role',
+ );
+ assert.deepEqual(
+ helper.resolveClarifiedRecipients(observedToCandidates, prevBccTarget, modelAuthoredRoleContext, 'Move Alice from BCC to To'),
+ [{ identity: '[email protected]', role: 'to' }],
+ 'user answer selecting option to move from BCC to To must update to To role',
+ );
+
assert.equal(helper.messageTargetMatchesObservedIdentities(
{ target_kind: 'named', recipients: ['Alice', '[email protected]'] },
['Alice'],