Skip to content

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

fix(agent): decode branch entities once

Emre Sokullu61c5f1fd3ac2
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 21f941d53..674537d51 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -3599,9 +3599,14 @@ export class Agent extends LoopDetector {
let match = null;
// eslint-disable-next-line no-cond-assign
while ((match = branchItem.exec(html)) !== null) {
- const name = match[1]
- .replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>')
- .replace(/&quot;/g, '"').replace(/&#39;/g, "'").trim();
+ const entities = { amp: '&', lt: '<', gt: '>', quot: '"', '#39': "'" };
+ // Decode exactly one HTML layer. Chained replacements would turn an
+ // actual branch named "main&lt;x" (rendered as main&amp;lt;x) into
+ // "main<x" and could attribute a commit to the wrong branch.
+ const name = match[1].replace(
+ /&(amp|lt|gt|quot|#39);/gi,
+ (entity, key) => entities[String(key).toLowerCase()] || entity,
+ ).trim();
if (name) names.push(name);
}
if (names.length === 0) return null;
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 87f4a5949..a500baf0a 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -3314,9 +3314,14 @@ export class Agent extends LoopDetector {
let match = null;
// eslint-disable-next-line no-cond-assign
while ((match = branchItem.exec(html)) !== null) {
- const name = match[1]
- .replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>')
- .replace(/&quot;/g, '"').replace(/&#39;/g, "'").trim();
+ const entities = { amp: '&', lt: '<', gt: '>', quot: '"', '#39': "'" };
+ // Decode exactly one HTML layer. Chained replacements would turn an
+ // actual branch named "main&lt;x" (rendered as main&amp;lt;x) into
+ // "main<x" and could attribute a commit to the wrong branch.
+ const name = match[1].replace(
+ /&(amp|lt|gt|quot|#39);/gi,
+ (entity, key) => entities[String(key).toLowerCase()] || entity,
+ ).trim();
if (name) names.push(name);
}
if (names.length === 0) return null;
diff --git a/test/run.js b/test/run.js
index aaccab86e..7befa1e96 100644
--- a/test/run.js
+++ b/test/run.js
@@ -79635,10 +79635,10 @@ test('verification rejects commits unattributed to the requested branch', async
siteWorkflow: { adapterName: 'github', job: { id: 'edit-file-and-commit' } },
});
const sha = await agent._sha256Text(body);
- const bindingFor = () => ({
+ const bindingFor = (branch = 'main') => ({
githubFileCommit: {
repository: 'example/repo',
- branch: 'main',
+ branch,
path: 'docs/plan.md',
expectedLength: body.length,
expectedSha256: sha,
@@ -79646,7 +79646,7 @@ test('verification rejects commits unattributed to the requested branch', async
},
metadataRequirements: [
{ field: 'path', value: 'docs/plan.md' },
- { field: 'branch', value: 'main' },
+ { field: 'branch', value: branch },
],
preDispatchPublishedResourceIdentities: [],
});
@@ -79661,6 +79661,14 @@ test('verification rejects commits unattributed to the requested branch', async
`${label}: branch-attributed commit was not verified`);
assert.ok(!seen.includes(apiUrl),
`${label}: same-host attribution needlessly consulted the token API`);
+ // Decode one HTML entity layer, not the newly produced entity text.
+ // The actual branch here is literally "main&lt;escape".
+ sameHost = { ok: true, status: 200, body: branchHtml('main&amp;lt;escape') };
+ assert.equal((await verify(bindingFor('main&lt;escape'))).verified, true,
+ `${label}: a singly decoded branch name was not attributed`);
+ const doubleDecodedBranch = await verify(bindingFor('main<escape'));
+ assert.equal(doubleDecodedBranch.reason, 'commit_wrong_branch',
+ `${label}: nested entity text was decoded twice into another branch name`);
// Same-host list names only the PR branch: rejected.
sameHost = { ok: true, status: 200, body: branchHtml('user-patch-1') };
const wrongBranch = await verify(bindingFor());