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
fix(agent): BOM-preserving blob hash, commit branch attribution
Emre Sokulluafbebfd3f34f
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 37a3bd531..470324986 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -2361,6 +2361,35 @@ export class Agent extends LoopDetector {}}+ // Confirm a fresh commit is the head of the requested branch: the commit+ // dialog can create a new branch + PR instead of committing to the URL+ // branch, while everything else here is SHA-addressed and branch-blind.+ // Returns true (confirmed), false (affirmatively elsewhere), or null+ // (inconclusive β auth, rate limit, empty or oversize list β keep today's+ // behavior). Only an affirmative mismatch ever rejects.+ async _githubCommitBranchHeadMatches(repository, branch, sha) {+ try {+ if (!repository || !branch || !sha) return null;+ const response = await fetch(+ `https://api.github.com/repos/${repository}/commits/${sha}/branches-where-head?per_page=100`,+ { headers: { Accept: 'application/vnd.github+json' }, cache: 'no-store' },+ );+ if (!response.ok) return null;+ const length = Number(response.headers?.get?.('content-length') || 0);+ if (length > 65536) return null;+ const text = await response.text();+ if (!text || text.length > 65536) return null;+ let entries = null;+ try { entries = JSON.parse(text); } catch { return null; }+ if (!Array.isArray(entries) || entries.length === 0 || entries.length >= 100) return null;+ const names = entries.map(entry => String(entry?.name || '')).filter(Boolean);+ if (names.length === 0) return null;+ return names.includes(branch);+ } catch {+ return null;+ }+ }+async _githubCommittedFileVerification(tabId, pageState, pageUrl, submissionEvidence) {const state = this._planExecutionGuards.get(tabId);if (state?.siteWorkflow?.adapterName !== 'github'@@ -2504,7 +2533,26 @@ export class Agent extends LoopDetector {}const contentLength = Number(response.headers?.get?.('content-length') || 0);if (contentLength > 2_000_000) return { verified: false, reason: 'raw_file_too_large' };- const text = await response.text();+ // Hash the raw response bytes: Response.text() strips a leading+ // UTF-8 BOM during decoding while the expected length/SHA were+ // computed from the exact editor string including it, so decoding+ // first would mismatch every BOM-led file after a good commit.+ let text = null;+ try {+ if (typeof response.arrayBuffer === 'function') {+ const buffer = await response.arrayBuffer();+ if (buffer.byteLength > 2_000_000) return { verified: false, reason: 'raw_file_too_large' };+ text = new TextDecoder('utf-8', { ignoreBOM: true }).decode(buffer);+ }+ } catch { text = null; }+ if (text == null) {+ try {+ text = await response.text();+ } catch {+ lastFetchReason = 'raw_file_read_failed';+ continue;+ }+ }if (text.length > 2_000_000) return { verified: false, reason: 'raw_file_too_large' };const actualSha256 = await this._sha256Text(text);const contentMatches = !!actualSha256@@ -2515,6 +2563,25 @@ export class Agent extends LoopDetector {if (attempt.branch !== expected.branch || attempt.path !== expected.path) {binding.githubFileCommit = { ...expected, branch: attempt.branch, path: attempt.path };}+ // Branch attribution: the dialog may have created a new branch ++ // PR instead of committing to the requested branch, while+ // everything above is SHA-addressed and branch-blind. Only an+ // affirmative mismatch rejects; inconclusive API outcomes keep the+ // content verdict. Skipped for fully-ambiguous scopes, where no+ // branch was requested to check against.+ if (!scopeAmbiguous && await this._githubCommitBranchHeadMatches(+ commit.repository, expected.branch, commit.sha,+ ) === false) {+ return {+ verified: false,+ reason: 'commit_wrong_branch',+ repository: commit.repository,+ commitSha: commit.sha,+ path: attempt.path,+ expectedLength: expected.expectedLength,+ expectedSha256: expected.expectedSha256,+ };+ }return {verified: true,repository: commit.repository,diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 894059d7d..0979391b2 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -2075,6 +2075,35 @@ export class Agent extends LoopDetector {}}+ // Confirm a fresh commit is the head of the requested branch: the commit+ // dialog can create a new branch + PR instead of committing to the URL+ // branch, while everything else here is SHA-addressed and branch-blind.+ // Returns true (confirmed), false (affirmatively elsewhere), or null+ // (inconclusive β auth, rate limit, empty or oversize list β keep today's+ // behavior). Only an affirmative mismatch ever rejects.+ async _githubCommitBranchHeadMatches(repository, branch, sha) {+ try {+ if (!repository || !branch || !sha) return null;+ const response = await fetch(+ `https://api.github.com/repos/${repository}/commits/${sha}/branches-where-head?per_page=100`,+ { headers: { Accept: 'application/vnd.github+json' }, cache: 'no-store' },+ );+ if (!response.ok) return null;+ const length = Number(response.headers?.get?.('content-length') || 0);+ if (length > 65536) return null;+ const text = await response.text();+ if (!text || text.length > 65536) return null;+ let entries = null;+ try { entries = JSON.parse(text); } catch { return null; }+ if (!Array.isArray(entries) || entries.length === 0 || entries.length >= 100) return null;+ const names = entries.map(entry => String(entry?.name || '')).filter(Boolean);+ if (names.length === 0) return null;+ return names.includes(branch);+ } catch {+ return null;+ }+ }+async _githubCommittedFileVerification(tabId, pageState, pageUrl, submissionEvidence) {const state = this._planExecutionGuards.get(tabId);if (state?.siteWorkflow?.adapterName !== 'github'@@ -2218,7 +2247,26 @@ export class Agent extends LoopDetector {}const contentLength = Number(response.headers?.get?.('content-length') || 0);if (contentLength > 2_000_000) return { verified: false, reason: 'raw_file_too_large' };- const text = await response.text();+ // Hash the raw response bytes: Response.text() strips a leading+ // UTF-8 BOM during decoding while the expected length/SHA were+ // computed from the exact editor string including it, so decoding+ // first would mismatch every BOM-led file after a good commit.+ let text = null;+ try {+ if (typeof response.arrayBuffer === 'function') {+ const buffer = await response.arrayBuffer();+ if (buffer.byteLength > 2_000_000) return { verified: false, reason: 'raw_file_too_large' };+ text = new TextDecoder('utf-8', { ignoreBOM: true }).decode(buffer);+ }+ } catch { text = null; }+ if (text == null) {+ try {+ text = await response.text();+ } catch {+ lastFetchReason = 'raw_file_read_failed';+ continue;+ }+ }if (text.length > 2_000_000) return { verified: false, reason: 'raw_file_too_large' };const actualSha256 = await this._sha256Text(text);const contentMatches = !!actualSha256@@ -2229,6 +2277,25 @@ export class Agent extends LoopDetector {if (attempt.branch !== expected.branch || attempt.path !== expected.path) {binding.githubFileCommit = { ...expected, branch: attempt.branch, path: attempt.path };}+ // Branch attribution: the dialog may have created a new branch ++ // PR instead of committing to the requested branch, while+ // everything above is SHA-addressed and branch-blind. Only an+ // affirmative mismatch rejects; inconclusive API outcomes keep the+ // content verdict. Skipped for fully-ambiguous scopes, where no+ // branch was requested to check against.+ if (!scopeAmbiguous && await this._githubCommitBranchHeadMatches(+ commit.repository, expected.branch, commit.sha,+ ) === false) {+ return {+ verified: false,+ reason: 'commit_wrong_branch',+ repository: commit.repository,+ commitSha: commit.sha,+ path: attempt.path,+ expectedLength: expected.expectedLength,+ expectedSha256: expected.expectedSha256,+ };+ }return {verified: true,repository: commit.repository,diff --git a/test/run.js b/test/run.jsindex 4f399b375..82bfe545f 100644--- a/test/run.js+++ b/test/run.js@@ -77984,6 +77984,141 @@ test('explicit scope verifies without file-listing the path', async () => {}});+test('committed blobs verify byte-exact through a leading BOM', async () => {+ const originalFetch = globalThis.fetch;+ // Real Response.text() strips a leading BOM during UTF-8 decoding; the+ // mock emulates that so only byte reads can match.+ const body = '\uFEFF# BOM document\n\nOne complete copy.\n';+ const commitSha = '0123456789abcdef0123456789abcdef01234567';+ const commitUrl = `https://github.com/Example/Repo/commit/${commitSha}`;+ const naiveUrl = `https://github.com/example/repo/raw/${commitSha}/docs/plan.md`;+ try {+ globalThis.fetch = async (url) => {+ if (url === naiveUrl) {+ return { ok: true, status: 200,+ headers: { get: () => String(new TextEncoder().encode(body).byteLength) },+ arrayBuffer: async () => new TextEncoder().encode(body).buffer,+ text: async () => body.slice(1) };+ }+ if (String(url).startsWith('https://api.github.com/')) {+ return { ok: true, status: 200, headers: { get: () => '16' },+ text: async () => JSON.stringify([{ name: 'main' }]) };+ }+ return { ok: false, status: 404, headers: { get: () => '0' }, text: async () => 'Not Found' };+ };+ for (const [label, AgentClass, tabId] of [+ ['chrome', AgentCh, 9536],+ ['firefox', AgentFx, 9537],+ ]) {+ const agent = new AgentClass({});+ agent._planExecutionGuards.set(tabId, {+ enabled: true,+ siteWorkflow: { adapterName: 'github', job: { id: 'edit-file-and-commit' } },+ });+ const binding = {+ githubFileCommit: {+ repository: 'example/repo',+ branch: 'main',+ path: 'docs/plan.md',+ expectedLength: body.length,+ expectedSha256: await agent._sha256Text(body),+ commitMessageVerified: true,+ },+ metadataRequirements: [+ { field: 'path', value: 'docs/plan.md' },+ { field: 'branch', value: 'main' },+ ],+ preDispatchPublishedResourceIdentities: [],+ };+ const pageState = { workflowResourceUrls: [commitUrl] };+ const proof = await agent._githubCommittedFileVerification(+ tabId, pageState, commitUrl, { verifiedFinalSubmit: true, submit: { workflowBinding: binding } },+ );+ assert.equal(proof.verified, true, `${label}: BOM-led blob was reported as a content mismatch`);+ assert.equal(proof.path, 'docs/plan.md');+ }+ } finally {+ globalThis.fetch = originalFetch;+ }+});++test('verification rejects commits unattributed to the requested branch', async () => {+ const originalFetch = globalThis.fetch;+ const body = '# Branch attribution document\n\nOne complete copy.\n';+ const commitSha = '0123456789abcdef0123456789abcdef01234567';+ const commitUrl = `https://github.com/Example/Repo/commit/${commitSha}`;+ const naiveUrl = `https://github.com/example/repo/raw/${commitSha}/docs/plan.md`;+ const apiUrl = `https://api.github.com/repos/example/repo/commits/${commitSha}/branches-where-head?per_page=100`;+ try {+ let apiResponse = { ok: true, status: 200, body: JSON.stringify([{ name: 'main' }]) };+ const seen = [];+ globalThis.fetch = async (url) => {+ seen.push(url);+ if (url === naiveUrl) {+ return { ok: true, status: 200, headers: { get: () => String(body.length) }, text: async () => body };+ }+ if (url === apiUrl) {+ return { ok: apiResponse.ok, status: apiResponse.status,+ headers: { get: () => String(apiResponse.body.length) },+ text: async () => apiResponse.body };+ }+ return { ok: false, status: 404, headers: { get: () => '0' }, text: async () => 'Not Found' };+ };+ for (const [label, AgentClass, tabId] of [+ ['chrome', AgentCh, 9538],+ ['firefox', AgentFx, 9539],+ ]) {+ const agent = new AgentClass({});+ agent._planExecutionGuards.set(tabId, {+ enabled: true,+ siteWorkflow: { adapterName: 'github', job: { id: 'edit-file-and-commit' } },+ });+ const sha = await agent._sha256Text(body);+ const bindingFor = () => ({+ githubFileCommit: {+ repository: 'example/repo',+ branch: 'main',+ path: 'docs/plan.md',+ expectedLength: body.length,+ expectedSha256: sha,+ commitMessageVerified: true,+ },+ metadataRequirements: [+ { field: 'path', value: 'docs/plan.md' },+ { field: 'branch', value: 'main' },+ ],+ preDispatchPublishedResourceIdentities: [],+ });+ const pageState = { workflowResourceUrls: [commitUrl] };+ const verify = (binding) => agent._githubCommittedFileVerification(+ tabId, pageState, commitUrl, { verifiedFinalSubmit: true, submit: { workflowBinding: binding } },+ );+ // Commit is the head of the requested branch: verified.+ apiResponse = { ok: true, status: 200, body: JSON.stringify([{ name: 'main' }]) };+ assert.equal((await verify(bindingFor())).verified, true,+ `${label}: branch-attributed commit was not verified`);+ // Commit landed on a new PR branch instead: rejected.+ apiResponse = { ok: true, status: 200, body: JSON.stringify([{ name: 'user-patch-1' }]) };+ const wrongBranch = await verify(bindingFor());+ assert.equal(wrongBranch.verified, false,+ `${label}: commit on another branch reported full success`);+ assert.equal(wrongBranch.reason, 'commit_wrong_branch',+ `${label}: wrong-branch commit misreported (${wrongBranch.reason})`);+ // Inconclusive API outcomes keep the content verdict.+ apiResponse = { ok: false, status: 403, body: 'rate limited' };+ assert.equal((await verify(bindingFor())).verified, true,+ `${label}: rate-limited branch check blocked verification`);+ apiResponse = { ok: true, status: 200, body: '[]' };+ assert.equal((await verify(bindingFor())).verified, true,+ `${label}: empty branch list blocked verification`);+ assert.ok(seen.includes(naiveUrl) && seen.includes(apiUrl),+ `${label}: raw blob and branch API were not both consulted`);+ }+ } finally {+ globalThis.fetch = originalFetch;+ }+});+test('Firefox selector debt recovers on exact digest readback', async () => {const originalChrome = globalThis.chrome;const originalBrowser = globalThis.browser;