π OpenHands: AI-Driven Development
fix(utils): percent-encode branch names in provider branch URLs (#17107)
MarMar Labsf5faa71759f9
diff --git a/__tests__/utils/utils.test.ts b/__tests__/utils/utils.test.tsindex 7ac5a4540f..e8a9379a02 100644--- a/__tests__/utils/utils.test.ts+++ b/__tests__/utils/utils.test.ts@@ -1,6 +1,7 @@import { describe, it, expect } from "vitest";-import { getStatusText } from "#/utils/utils";+import { constructBranchUrl, getStatusText } from "#/utils/utils";import { AgentState } from "#/types/agent-state";+import { Provider } from "#/types/settings";import { I18nKey } from "#/i18n/declaration";const t = (key: string) => {@@ -168,3 +169,106 @@ describe("getStatusText", () => {expect(result).toBe(t(I18nKey.COMMON$RUNNING));});});++describe("constructBranchUrl", () => {+ type ProviderCase = {+ provider: Provider;+ repository: string;+ host: string | null;+ prefix: string;+ };++ // Providers that place the branch name in the URL path.+ const pathProviders: ProviderCase[] = [+ {+ provider: "github",+ repository: "owner/repo",+ host: null,+ prefix: "https://github.com/owner/repo/tree/",+ },+ {+ provider: "forgejo",+ repository: "owner/repo",+ host: null,+ prefix: "https://codeberg.org/owner/repo/src/branch/",+ },+ {+ provider: "gitlab",+ repository: "owner/repo",+ host: null,+ prefix: "https://gitlab.com/owner/repo/-/tree/",+ },+ {+ provider: "bitbucket",+ repository: "owner/repo",+ host: null,+ prefix: "https://bitbucket.org/owner/repo/src/",+ },+ ];++ // Providers that place the branch name in a query-string value.+ const queryProviders: ProviderCase[] = [+ {+ provider: "bitbucket_data_center",+ repository: "PROJECT/repo",+ host: "bitbucket.example.com",+ prefix:+ "https://bitbucket.example.com/projects/PROJECT/repos/repo/browse?at=refs/heads/",+ },+ {+ provider: "azure_devops",+ repository: "org/project/repo",+ host: null,+ prefix: "https://dev.azure.com/org/project/_git/repo?version=GB",+ },+ ];++ // `inPath`: encoded per `/`-separated segment, slashes preserved.+ // `inQuery`: encoded as one value, slashes included.+ const branches = [+ { branch: "main", inPath: "main", inQuery: "main" },+ {+ branch: "feature/ui#123",+ inPath: "feature/ui%23123",+ inQuery: "feature%2Fui%23123",+ },+ // `&` would open a new query parameter and `+` decodes as a space in a+ // query value; both are legal in a git ref.+ { branch: "feature&x", inPath: "feature%26x", inQuery: "feature%26x" },+ { branch: "feature+x", inPath: "feature%2Bx", inQuery: "feature%2Bx" },+ { branch: "100%done", inPath: "100%25done", inQuery: "100%25done" },+ // Unencoded, this href's path decodes server-side to the branch+ // `100/done`, so the link silently resolves somewhere else.+ {+ branch: "100%2Fdone",+ inPath: "100%252Fdone",+ inQuery: "100%252Fdone",+ },+ { branch: "release/1.0", inPath: "release/1.0", inQuery: "release%2F1.0" },+ ];++ describe.each(pathProviders)(+ "$provider (branch in URL path)",+ ({ provider, repository, host, prefix }) => {+ it.each(branches)(+ "encodes $branch per path segment",+ ({ branch, inPath }) => {+ expect(constructBranchUrl(provider, repository, branch, host)).toBe(+ `${prefix}${inPath}`,+ );+ },+ );+ },+ );++ describe.each(queryProviders)(+ "$provider (branch in query value)",+ ({ provider, repository, host, prefix }) => {+ it.each(branches)("encodes $branch as a whole", ({ branch, inQuery }) => {+ expect(constructBranchUrl(provider, repository, branch, host)).toBe(+ `${prefix}${inQuery}`,+ );+ });+ },+ );+});diff --git a/src/utils/utils.ts b/src/utils/utils.tsindex 4ba2087c36..4c7a50eea9 100644--- a/src/utils/utils.ts+++ b/src/utils/utils.ts@@ -332,6 +332,14 @@ export const constructRepositoryUrl = (return `${baseUrl}/${repositoryName}`;};+/**+ * Percent-encode a branch name for use as a URL path component. Each+ * `/`-separated segment is encoded on its own so nested branch names such as+ * `release/1.0` keep their slashes while `#`, `%` and `&` are escaped.+ */+const encodeBranchPath = (branchName: string): string =>+ branchName.split("/").map(encodeURIComponent).join("/");+/*** Construct the branch URL for different providers* @param provider The git provider@@ -356,19 +364,21 @@ export const constructBranchUrl = (switch (provider) {case "github":- return `${baseUrl}/${repositoryName}/tree/${branchName}`;+ return `${baseUrl}/${repositoryName}/tree/${encodeBranchPath(branchName)}`;case "forgejo":- return `${baseUrl}/${repositoryName}/src/branch/${branchName}`;+ return `${baseUrl}/${repositoryName}/src/branch/${encodeBranchPath(branchName)}`;case "gitlab":- return `${baseUrl}/${repositoryName}/-/tree/${branchName}`;+ return `${baseUrl}/${repositoryName}/-/tree/${encodeBranchPath(branchName)}`;case "bitbucket":- return `${baseUrl}/${repositoryName}/src/${branchName}`;+ return `${baseUrl}/${repositoryName}/src/${encodeBranchPath(branchName)}`;case "bitbucket_data_center": {// Bitbucket Server format: /projects/{PROJECT}/repos/{repo}/browse?at=refs/heads/{branch}const parts = repositoryName.split("/");if (parts.length >= 2) {const [project, repo] = parts;- return `${baseUrl}/projects/${project}/repos/${repo}/browse?at=refs/heads/${branchName}`;+ // The branch is one query value here, so encode it whole: an+ // unencoded `&` would start a new query parameter.+ return `${baseUrl}/projects/${project}/repos/${repo}/browse?at=refs/heads/${encodeURIComponent(branchName)}`;}return "";}@@ -377,7 +387,9 @@ export const constructBranchUrl = (const parts = repositoryName.split("/");if (parts.length === 3) {const [org, project, repo] = parts;- return `${baseUrl}/${org}/${project}/_git/${repo}?version=GB${branchName}`;+ // The branch is one query value here, so encode it whole: an+ // unencoded `&` would start a new query parameter.+ return `${baseUrl}/${org}/${project}/_git/${repo}?version=GB${encodeURIComponent(branchName)}`;}return "";}