Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Guard social API publication writes
Emre Sokullu44be73e5fb2d
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex fd1b64e13..940392058 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -1,4 +1,4 @@-import { SOCIAL_PLATFORMS, normalizePublicationContract, publicationProgress, exactPublicationText, publicationMediaMatches, publicationContractMessages, publicationAuditMessages, publicationAuditAccepted } from './social-publish-contract.js';+import { SOCIAL_PLATFORMS, socialPublicationApiPlatform, normalizePublicationContract, publicationProgress, exactPublicationText, publicationMediaMatches, publicationContractMessages, publicationAuditMessages, publicationAuditAccepted } from './social-publish-contract.js';import { AGENT_TOOLS, AGENT_TOOL_NAMES, RESERVED_AGENT_TOOL_NAMES, getToolsForMode, SYSTEM_PROMPT_ASK, SYSTEM_PROMPT_ACT, SYSTEM_PROMPT_ACT_COMPACT, SYSTEM_PROMPT_ACT_MID, SYSTEM_PROMPT_DEV_APPENDIX, SYSTEM_PROMPT_WEBMCP_ASK, SYSTEM_PROMPT_WEBMCP_ACT } from './tools.js';import { validateToolArguments } from './tool-arguments.js';import { isSessionQuotaError, serializeConversationForSession, SESSION_CONVERSATION_BUDGET_BYTES, SESSION_CONVERSATION_RETRY_BUDGET_BYTES } from './conversation-persistence.js';@@ -18462,11 +18462,30 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dasync _socialPublicationPreSubmitBlock(tabId, name, args, detected, provider) {const guard = this._planExecutionGuards.get(tabId);+ const networkMutation = isNetworkMutation(name, args);const rawKeys = args?.key ?? args?.keys ?? '';const activationKey = name === 'press_keys' && (Array.isArray(rawKeys) ? rawKeys : [rawKeys]).some(key => typeof key === 'string' && /^(?:enter|return|space|spacebar| )$/i.test(key));if (!guard?.enabled || (!this._isFormValidationCandidate(name, args) && !activationKey- && name !== 'execute_webmcp_tool' && !isNetworkMutation(name, args))) return null;+ && name !== 'execute_webmcp_tool' && !networkMutation)) return null;+ const blocked = error => ({ success: false, dispatched: false, noDispatch: true, repeatBlocked: true,+ workflowJob: 'publish-post', error,+ publicationContract: guard.socialPublication?.contract || null,+ publicationProgress: publicationProgress(guard.socialPublication?.contract, guard.socialPublication?.outcomes),+ ...(guard.socialPublication?.deniedAudit ? { publicationAudit: guard.socialPublication.deniedAudit } : {}),+ });+ if (networkMutation) {+ let platform;+ try { platform = socialPublicationApiPlatform(args?.url); }+ catch {+ return { success: false, dispatched: false, noDispatch: true,+ error: 'The API destination could not be resolved. Use an explicit HTTP(S) URL without embedded credentials.' };+ }+ if (!platform) return null;+ // A network write cannot supply a verified composer/publish control.+ // Changing tabs must not let social API writes bypass that requirement.+ return blocked('Social-site API mutations require a verified publish control. Prepare and verify the draft, then use the site’s publish button.');+ }const pageUrl = await this._currentUrl(tabId);const live = resolveAdapterWorkflowJob(pageUrl, 'publish-post');if (!SOCIAL_PLATFORMS.includes(live?.adapterName)) return null;@@ -18476,12 +18495,6 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// probes, and unresolved controls cannot claim this exemption.if (detected?.isSubmit === true && detected.publicationControl === false&& ['click', 'click_ax', 'iframe_click', 'set_field', 'press_keys'].includes(name)) return null;- const blocked = error => ({ success: false, dispatched: false, noDispatch: true, repeatBlocked: true,- workflowJob: 'publish-post', error,- publicationContract: guard.socialPublication?.contract || null,- publicationProgress: publicationProgress(guard.socialPublication?.contract, guard.socialPublication?.outcomes),- ...(guard.socialPublication?.deniedAudit ? { publicationAudit: guard.socialPublication.deniedAudit } : {}),- });if (detected?.isSubmit !== true) return blocked('Publication-capable action could not be identified. Use a resolved page control; do not run arbitrary JavaScript or bundle editing and submission.');if (!['click', 'click_ax', 'iframe_click'].includes(name)) return blocked('Write and verify the draft first, then activate its publish control in a separate click.');if (detected.publicationControl !== true) return blocked('Publication composer ownership could not be observed. Read the current page and use its resolved publish control.');diff --git a/src/chrome/src/agent/social-publish-contract.js b/src/chrome/src/agent/social-publish-contract.jsindex 10f4b44e9..1aaf95511 100644--- a/src/chrome/src/agent/social-publish-contract.js+++ b/src/chrome/src/agent/social-publish-contract.js@@ -1,6 +1,25 @@// Language belongs in the selected provider. This module only validates the// resulting contract and compares app-observed values; it never parses prose.export const SOCIAL_PLATFORMS = Object.freeze(['twitter', 'bluesky']);++// Network tools act on their explicit destination, independently of the open+// tab. Include API hosts and AT Protocol repository writes on self-hosted PDSes.+// This only selects the publication guard; API permission, SSRF, and redirect+// checks still belong to the network dispatch path.+export function socialPublicationApiPlatform(rawUrl) {+ if (typeof rawUrl !== 'string') throw new Error('Missing network destination');+ const url = new URL(rawUrl);+ if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password) {+ throw new Error('Invalid network destination');+ }+ const host = url.hostname.toLowerCase().replace(/\.$/, '');+ const within = domain => host === domain || host.endsWith('.' + domain);+ if (['x.com', 'twitter.com'].some(within)) return 'twitter';+ if (['bsky.app', 'bsky.social', 'bsky.network'].some(within)+ || decodeURIComponent(url.pathname).startsWith('/xrpc/com.atproto.repo.')) return 'bluesky';+ return null;+}+const TYPES = ['any', 'image', 'video', 'gif'];const FORMATS = ['png', 'jpeg', 'webp', 'avif', 'heic', 'bmp', 'svg', 'gif', 'mp4', 'mov', 'webm', 'mkv'];const object = x => x !== null && typeof x === 'object' && !Array.isArray(x);diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex e89a02e32..a9a3dbe59 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -1,5 +1,5 @@import { firefoxBidi } from '../bidi/client.js';-import { SOCIAL_PLATFORMS, normalizePublicationContract, publicationProgress, exactPublicationText, publicationMediaMatches, publicationContractMessages, publicationAuditMessages, publicationAuditAccepted } from './social-publish-contract.js';+import { SOCIAL_PLATFORMS, socialPublicationApiPlatform, normalizePublicationContract, publicationProgress, exactPublicationText, publicationMediaMatches, publicationContractMessages, publicationAuditMessages, publicationAuditAccepted } from './social-publish-contract.js';import { AGENT_TOOLS, AGENT_TOOL_NAMES, RESERVED_AGENT_TOOL_NAMES, getToolsForMode, SYSTEM_PROMPT_ASK, SYSTEM_PROMPT_ACT, SYSTEM_PROMPT_ACT_COMPACT, SYSTEM_PROMPT_ACT_MID, SYSTEM_PROMPT_DEV_APPENDIX } from './tools.js';import { validateToolArguments } from './tool-arguments.js';import { isSessionQuotaError, serializeConversationForSession, SESSION_CONVERSATION_BUDGET_BYTES, SESSION_CONVERSATION_RETRY_BUDGET_BYTES } from './conversation-persistence.js';@@ -9928,6 +9928,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dasync _executeToolBatchInner(tabId, toolCalls, messages, onUpdate, provider, partialAssistantText, allowedToolNames, step, runOptions, toolSchemas, cancellationState) {let didStateChange = false;+ const apiMutationsDeniedForRun = runOptions.apiMutationsDenied === true;+ const apiMutationsAllowedForRun = () =>+ !apiMutationsDeniedForRun && this.isApiMutationsAllowed(tabId);const promptTier = this._resolvePromptTier();const readLimits = this._readWindowLimits();const completionBatchStartState = this.completionInvariants.get(tabId) || null;@@ -10296,18 +10299,25 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout donUpdate('warning', { message });continue;}- if (isNetworkMutation(fnName, fnArgs) && !this.isApiMutationsAllowed(tabId)) {+ if (isNetworkMutation(fnName, fnArgs) && !apiMutationsAllowedForRun()) {+ const askModeError = apiMutationsDeniedForRun+ ? `API mutations via ${fnName} are not allowed in Ask mode. Do not retry this mutating API call in this run; continue with read-only tools or return what you can observe.`+ : `API mutations via ${fnName} require the user to enable /allow-api for this conversation. Do not retry this mutating API call unless the user enables /allow-api; continue through the visible UI or ask the user to type /allow-api.`;messages.push({role: 'tool',tool_call_id: tc.id,content: JSON.stringify({success: false,denied: true,- requiresApiAllow: true,- error: `API mutations via ${fnName} require the user to enable /allow-api for this conversation. Do not retry this mutating API call unless the user enables /allow-api; continue through the visible UI or ask the user to type /allow-api.`,+ requiresApiAllow: !apiMutationsDeniedForRun,+ error: askModeError,}),});- onUpdate('warning', { message: 'API mutation blocked until /allow-api is enabled.' });+ onUpdate('warning', {+ message: apiMutationsDeniedForRun+ ? 'API mutation blocked because Ask mode is read-only.'+ : 'API mutation blocked until /allow-api is enabled.',+ });continue;}const formValidationCandidate = this._isFormValidationCandidate(fnName, fnArgs);@@ -10491,7 +10501,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dfor (const capability of capabilities) {if (this._skipPermissionGate) { gateDisabled = true; break; }// /allow-api waives ONLY write-method network egress.- if (capability === Capability.NETWORK && isNetworkMutation(fnName, fnArgs) && this.isApiMutationsAllowed(tabId)) continue;+ if (capability === Capability.NETWORK && isNetworkMutation(fnName, fnArgs) && apiMutationsAllowedForRun()) continue;// Every distinct host the call touches must be granted. Usually one,// but download_files takes a urls[] array that can span many hosts.const gateArgs = fnName === OTP_EMAIL_TOOL_NAME && otpEmailPermissionArgs@@ -16263,11 +16273,30 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dasync _socialPublicationPreSubmitBlock(tabId, name, args, detected, provider) {const guard = this._planExecutionGuards.get(tabId);+ const networkMutation = isNetworkMutation(name, args);const rawKeys = args?.key ?? args?.keys ?? '';const activationKey = name === 'press_keys' && (Array.isArray(rawKeys) ? rawKeys : [rawKeys]).some(key => typeof key === 'string' && /^(?:enter|return|space|spacebar| )$/i.test(key));if (!guard?.enabled || (!this._isFormValidationCandidate(name, args) && !activationKey- && name !== 'execute_webmcp_tool' && !isNetworkMutation(name, args))) return null;+ && name !== 'execute_webmcp_tool' && !networkMutation)) return null;+ const blocked = error => ({ success: false, dispatched: false, noDispatch: true, repeatBlocked: true,+ workflowJob: 'publish-post', error,+ publicationContract: guard.socialPublication?.contract || null,+ publicationProgress: publicationProgress(guard.socialPublication?.contract, guard.socialPublication?.outcomes),+ ...(guard.socialPublication?.deniedAudit ? { publicationAudit: guard.socialPublication.deniedAudit } : {}),+ });+ if (networkMutation) {+ let platform;+ try { platform = socialPublicationApiPlatform(args?.url); }+ catch {+ return { success: false, dispatched: false, noDispatch: true,+ error: 'The API destination could not be resolved. Use an explicit HTTP(S) URL without embedded credentials.' };+ }+ if (!platform) return null;+ // A network write cannot supply a verified composer/publish control.+ // Changing tabs must not let social API writes bypass that requirement.+ return blocked('Social-site API mutations require a verified publish control. Prepare and verify the draft, then use the site’s publish button.');+ }const pageUrl = await this._currentUrl(tabId);const live = resolveAdapterWorkflowJob(pageUrl, 'publish-post');if (!SOCIAL_PLATFORMS.includes(live?.adapterName)) return null;@@ -16277,12 +16306,6 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// probes, and unresolved controls cannot claim this exemption.if (detected?.isSubmit === true && detected.publicationControl === false&& ['click', 'click_ax', 'iframe_click', 'set_field', 'press_keys'].includes(name)) return null;- const blocked = error => ({ success: false, dispatched: false, noDispatch: true, repeatBlocked: true,- workflowJob: 'publish-post', error,- publicationContract: guard.socialPublication?.contract || null,- publicationProgress: publicationProgress(guard.socialPublication?.contract, guard.socialPublication?.outcomes),- ...(guard.socialPublication?.deniedAudit ? { publicationAudit: guard.socialPublication.deniedAudit } : {}),- });if (detected?.isSubmit !== true) return blocked('Publication-capable action could not be identified. Use a resolved page control; do not run arbitrary JavaScript or bundle editing and submission.');if (!['click', 'click_ax', 'iframe_click'].includes(name)) return blocked('Write and verify the draft first, then activate its publish control in a separate click.');if (detected.publicationControl !== true) return blocked('Publication composer ownership could not be observed. Read the current page and use its resolved publish control.');diff --git a/src/firefox/src/agent/social-publish-contract.js b/src/firefox/src/agent/social-publish-contract.jsindex 10f4b44e9..1aaf95511 100644--- a/src/firefox/src/agent/social-publish-contract.js+++ b/src/firefox/src/agent/social-publish-contract.js@@ -1,6 +1,25 @@// Language belongs in the selected provider. This module only validates the// resulting contract and compares app-observed values; it never parses prose.export const SOCIAL_PLATFORMS = Object.freeze(['twitter', 'bluesky']);++// Network tools act on their explicit destination, independently of the open+// tab. Include API hosts and AT Protocol repository writes on self-hosted PDSes.+// This only selects the publication guard; API permission, SSRF, and redirect+// checks still belong to the network dispatch path.+export function socialPublicationApiPlatform(rawUrl) {+ if (typeof rawUrl !== 'string') throw new Error('Missing network destination');+ const url = new URL(rawUrl);+ if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password) {+ throw new Error('Invalid network destination');+ }+ const host = url.hostname.toLowerCase().replace(/\.$/, '');+ const within = domain => host === domain || host.endsWith('.' + domain);+ if (['x.com', 'twitter.com'].some(within)) return 'twitter';+ if (['bsky.app', 'bsky.social', 'bsky.network'].some(within)+ || decodeURIComponent(url.pathname).startsWith('/xrpc/com.atproto.repo.')) return 'bluesky';+ return null;+}+const TYPES = ['any', 'image', 'video', 'gif'];const FORMATS = ['png', 'jpeg', 'webp', 'avif', 'heic', 'bmp', 'svg', 'gif', 'mp4', 'mov', 'webm', 'mkv'];const object = x => x !== null && typeof x === 'object' && !Array.isArray(x);diff --git a/test/social-publish-contract.mjs b/test/social-publish-contract.mjsindex 6ba09ea8b..37125aec9 100644--- a/test/social-publish-contract.mjs+++ b/test/social-publish-contract.mjs@@ -721,6 +721,111 @@ for (const browser of ['chrome', 'firefox']) {}});+ test(`${browser}: unrelated API writes do not inherit the active social page's publication contract`, async () => {+ const f = setup('Call the business in Turkish');+ const destinations = [+ 'https://phonr.xyz/v1/calls',+ 'https://service.example/v1/bookings',+ 'https://x.com.example/v1/calls',+ 'https://service.example/v1/calls?next=https://x.com/api/post',+ ];+ for (const page of ['https://x.com/user/status/123/photo/1', 'https://bsky.app/', 'https://phonr.xyz/docs']) {+ f.agent._currentUrl = async () => page;+ for (const name of ['fetch_url', 'research_url']) {+ for (const method of ['POST', 'PUT', 'PATCH', 'DELETE']) {+ for (const url of destinations) {+ const result = await f.agent._workflowPreSubmitDispatchBlock(f.tabId, name, { url, method }, null, f.provider);+ assert.equal(result, null, `${page}: ${name} ${method} ${url}`);+ }+ }+ }+ }+ assert.equal(f.guard.socialPublication, null);+ assert.equal(f.calls.length, 0, 'unrelated APIs must not ask a model for social-post authorization');+ });++ test(`${browser}: social API writes stay guarded independently of the active page`, async () => {+ const f = setup('Call the business in Turkish');+ const destinations = [+ 'https://x.com/i/api/graphql/CreateTweet',+ 'https://api.x.com/2/tweets',+ 'https://api.twitter.com/1.1/statuses/update.json',+ 'https://API.X.COM.:443/2/tweets',+ 'https://bsky.app/api/post',+ 'https://bsky.social/xrpc/com.atproto.repo.createRecord',+ 'https://pds.host.bsky.network/xrpc/com.atproto.repo.putRecord',+ 'https://pds.example/xrpc/com.atproto.repo.applyWrites',+ 'https://pds.example/xrpc/%63om.atproto.repo.createRecord',+ ];+ for (const page of ['https://x.com/home', 'https://bsky.app/', 'https://phonr.xyz/docs']) {+ f.agent._currentUrl = async () => page;+ for (const name of ['fetch_url', 'research_url']) {+ for (const url of destinations) {+ const result = await f.agent._workflowPreSubmitDispatchBlock(f.tabId, name, { url, method: 'POST' }, null, f.provider);+ assert.equal(result?.noDispatch, true, `${page}: ${name} ${url}`);+ assert.equal(result.dispatched, false);+ assert.equal(result.workflowJob, 'publish-post');+ }+ }+ assert.equal(await f.agent._workflowPreSubmitDispatchBlock(f.tabId, 'fetch_url', {+ url: 'https://api.x.com/2/tweets/123', method: 'GET',+ }, null, f.provider), null, 'read-only API access remains available');+ }+ assert.equal(f.calls.length, 0);+ });++ test(`${browser}: unresolved network destinations cannot claim an unrelated-API exemption`, async () => {+ const f = setup();+ for (const page of ['https://x.com/home', 'https://phonr.xyz/docs']) {+ f.agent._currentUrl = async () => page;+ for (const url of [undefined, '', '/v1/calls', '//phonr.xyz/v1/calls', 'https://', 'javascript:publish()', 'data:text/plain,post', 'https://user:[email protected]/v1/calls']) {+ const result = await f.agent._workflowPreSubmitDispatchBlock(f.tabId, 'fetch_url', { url, method: 'POST' }, null, f.provider);+ assert.equal(result?.noDispatch, true, `${page}: ${url}`);+ assert.equal(result.dispatched, false);+ assert.match(result.error, /destination/i);+ }+ }+ });++ test(`${browser}: phone API dispatch retains API permission and Ask-mode gates`, async () => {+ for (const scenario of ['allowed', 'no-api-permission', 'ask', 'retargeted-to-social']) {+ const f = setup('Call the business in Turkish');+ f.agent._skipPermissionGate = true;+ f.agent._ensureGateSetting = async () => true;+ f.agent.setApiMutationsAllowed(f.tabId, scenario !== 'no-api-permission');+ f.agent._recordProgressObservation = async () => null;+ f.agent._autoRecordProgressAction = () => null;+ f.agent._progressWarningForAction = () => '';+ const dispatches = [];+ f.agent.executeTool = async (_tabId, name, args) => {+ dispatches.push({ name, args });+ return { success: true, status: 201, json: '{"call":{"id":"mock-call","status":"dialing"}}' };+ };+ if (scenario === 'retargeted-to-social') {+ f.agent._preflightRichTextToolbarTarget = async (_tabId, _name, args) => {+ args.url = 'https://api.x.com/2/tweets';+ return { block: null };+ };+ }+ const args = { url: 'https://phonr.xyz/v1/calls', method: 'POST', body: '{"to":"+14155550123","purpose":"Ask when the shop closes.","language":"English"}' };+ const messages = [];+ await f.agent._executeToolBatch(f.tabId, [{ id: 'phone-call', function: {+ name: 'fetch_url', arguments: JSON.stringify(args),+ } }], messages, () => {}, f.provider, '', new Set(['fetch_url']), 1, { apiMutationsDenied: scenario === 'ask' });+ const result = JSON.parse(f.agent._unwrapUntrusted(messages.find(m => m.tool_call_id === 'phone-call').content));+ assert.equal(dispatches.length, scenario === 'allowed' ? 1 : 0, scenario);+ if (scenario === 'allowed') {+ assert.deepEqual(dispatches[0], { name: 'fetch_url', args });+ assert.equal(result.status, 201);+ } else if (scenario === 'retargeted-to-social') assert.equal(result.noDispatch, true);+ else {+ assert.equal(result.denied, true);+ assert.equal(result.requiresApiAllow, scenario === 'no-api-permission');+ }+ assert.equal(f.calls.length, 0);+ }+ });+test(`${browser}: publication activation requires a complete key name`,async()=>{const f=setup();for(const key of ['Backspace','Workspace','ReturnToSender','Entertain','Escape','Tab','ArrowLeft',';','']) {