Skip to content

Commit 39e1ce5

Browse files
authored
fix(core): reject relative path segments in repository hosts (#51577)
1 parent d9f5439 commit 39e1ce5

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎packages/core/src/repository.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ function parts(input: string) {
145145
.filter(Boolean)
146146
}
147147

148+
// cachePath makes each `:`-separated host part a directory.
148149
function safeHost(input: string) {
149-
return Boolean(input) && !input.startsWith("-") && !/[\s/\\]/.test(input)
150+
return Boolean(input) && !input.startsWith("-") && input.split(":").every(safeSegment)
150151
}
151152

152153
function safeSegment(input: string) {

‎packages/core/test/repository.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ describe("Repository", () => {
4141
})
4242
})
4343

44+
test("caches a host with a port under a directory per host part", () => {
45+
expect(Repository.cachePath("/cache", Repository.parseRemote("ssh://[email protected]:2222/owner/repo"))).toBe(
46+
path.join("/cache", "example.com", "2222", "owner", "repo"),
47+
)
48+
})
49+
4450
test("keeps local file repositories distinct from remote repositories", () => {
4551
const localPath = path.resolve("repo.git")
4652
const reference = Repository.parse(pathToFileURL(localPath).href)
@@ -62,6 +68,18 @@ describe("Repository", () => {
6268
expect(() => Repository.validateBranch("bad branch")).toThrow(Repository.InvalidBranchError)
6369
})
6470

71+
test.each([
72+
"..:repo",
73+
"git@..:repo",
74+
"../owner/repo",
75+
"ssh://../repo",
76+
"ssh://..:22/repo",
77+
"https://%2e%2e/owner/repo",
78+
".:repo",
79+
])("rejects %s because its host contains a relative path segment", (input) => {
80+
expect(() => Repository.parseRemote(input)).toThrow(Repository.InvalidReferenceError)
81+
})
82+
6583
test("compares cache identity independent of input spelling", () => {
6684
const shorthand = Repository.parseRemote("owner/repo")
6785

0 commit comments

Comments
 (0)