Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(gitwork): split ParseSSHRemote into splitSSHRemote and validSSHRepoPath (#595) (#596)

fx-kurumi57ae4738ce71
diff --git a/internal/gitwork/ssh_remote.go b/internal/gitwork/ssh_remote.go
index e51736ed..c299d12a 100644
--- a/internal/gitwork/ssh_remote.go
+++ b/internal/gitwork/ssh_remote.go
@@ -76,27 +76,8 @@ func ParseSSHRemote(raw string) (SSHRemote, error) {
if len(s) > 2048 || strings.ContainsAny(s, " \t\r\n%?#\\") {
return SSHRemote{}, invalid
}
- var user, host, path string
- port := "22"
- if strings.HasPrefix(s, "ssh://") {
- u, err := url.Parse(s)
- if err != nil || u.User == nil || strings.HasSuffix(u.Host, ":") {
- return SSHRemote{}, invalid
- }
- if _, hasPassword := u.User.Password(); hasPassword {
- return SSHRemote{}, invalid
- }
- user, host, path = u.User.Username(), u.Hostname(), u.Path
- if u.Port() != "" {
- p, err := strconv.Atoi(u.Port())
- if err != nil || p < 1 || p > 65535 {
- return SSHRemote{}, invalid
- }
- port = strconv.Itoa(p)
- }
- } else if m := sshSCP.FindStringSubmatch(s); m != nil {
- user, host, path = m[1], strings.Trim(m[2], "[]"), m[3]
- } else {
+ user, host, path, port, ok := splitSSHRemote(s)
+ if !ok {
return SSHRemote{}, invalid
}
host = strings.ToLower(host)
@@ -105,15 +86,7 @@ func ParseSSHRemote(raw string) (SSHRemote, error) {
} else if !sshHost.MatchString(host) || strings.Contains(host, "..") {
return SSHRemote{}, invalid
}
- if !sshUser.MatchString(user) || !sshPath.MatchString(path) {
- return SSHRemote{}, invalid
- }
- for _, part := range strings.Split(strings.TrimPrefix(path, "/"), "/") {
- if part == "" || part == "." || part == ".." {
- return SSHRemote{}, invalid
- }
- }
- if strings.TrimSuffix(path[strings.LastIndex(path, "/")+1:], ".git") == "" {
+ if !sshUser.MatchString(user) || !validSSHRepoPath(path) {
return SSHRemote{}, invalid
}
// Preserve the distinction between an absolute SSH URL and a path
@@ -129,3 +102,45 @@ func ParseSSHRemote(raw string) (SSHRemote, error) {
}
return SSHRemote{URL: s, User: user, Host: host, Path: path, Port: port}, nil
}
+
+// splitSSHRemote extracts user, host, path and port from an ssh:// URL or an
+// scp-style address. It rejects passwords and out-of-range ports; the caller
+// validates and normalizes the returned parts.
+func splitSSHRemote(s string) (user, host, path, port string, ok bool) {
+ port = "22"
+ if strings.HasPrefix(s, "ssh://") {
+ u, err := url.Parse(s)
+ if err != nil || u.User == nil || strings.HasSuffix(u.Host, ":") {
+ return "", "", "", "", false
+ }
+ if _, hasPassword := u.User.Password(); hasPassword {
+ return "", "", "", "", false
+ }
+ if u.Port() != "" {
+ p, err := strconv.Atoi(u.Port())
+ if err != nil || p < 1 || p > 65535 {
+ return "", "", "", "", false
+ }
+ port = strconv.Itoa(p)
+ }
+ return u.User.Username(), u.Hostname(), u.Path, port, true
+ }
+ if m := sshSCP.FindStringSubmatch(s); m != nil {
+ return m[1], strings.Trim(m[2], "[]"), m[3], port, true
+ }
+ return "", "", "", "", false
+}
+
+// validSSHRepoPath reports whether path only uses safe characters, has no
+// empty, "." or ".." segments and names a repository.
+func validSSHRepoPath(path string) bool {
+ if !sshPath.MatchString(path) {
+ return false
+ }
+ for _, part := range strings.Split(strings.TrimPrefix(path, "/"), "/") {
+ if part == "" || part == "." || part == ".." {
+ return false
+ }
+ }
+ return strings.TrimSuffix(path[strings.LastIndex(path, "/")+1:], ".git") != ""
+}
diff --git a/internal/gitwork/ssh_remote_parse_test.go b/internal/gitwork/ssh_remote_parse_test.go
new file mode 100644
index 00000000..00a4425e
--- /dev/null
+++ b/internal/gitwork/ssh_remote_parse_test.go
@@ -0,0 +1,87 @@
+package gitwork
+
+import "testing"
+
+func TestParseSSHRemoteNormalizesAcceptedAddresses(t *testing.T) {
+ cases := []struct {
+ raw string
+ want SSHRemote
+ }{
+ {"[email protected]:group/repo.git", SSHRemote{URL: "[email protected]:group/repo.git", User: "git", Host: "gitlab.com", Path: "group/repo.git", Port: "22"}},
+ {` git clone "[email protected]:group/repo" `, SSHRemote{URL: "[email protected]:group/repo", User: "git", Host: "gitlab.com", Path: "group/repo", Port: "22"}},
+ {"ssh://[email protected]:2222/group/repo.git", SSHRemote{URL: "ssh://[email protected]:2222/group/repo.git", User: "git", Host: "git.example.org", Path: "/group/repo.git", Port: "2222"}},
+ {"ssh://[email protected]:02222/repo", SSHRemote{URL: "ssh://[email protected]:2222/repo", User: "git", Host: "git.example.org", Path: "/repo", Port: "2222"}},
+ {"ssh://git@[::1]/repo.git", SSHRemote{URL: "ssh://git@[::1]:22/repo.git", User: "git", Host: "::1", Path: "/repo.git", Port: "22"}},
+ {"git@[0:0::1]:repo.git", SSHRemote{URL: "git@[::1]:repo.git", User: "git", Host: "::1", Path: "repo.git", Port: "22"}},
+ }
+ for _, tc := range cases {
+ got, err := ParseSSHRemote(tc.raw)
+ if err != nil {
+ t.Fatalf("ParseSSHRemote(%q) error = %v", tc.raw, err)
+ }
+ if got != tc.want {
+ t.Fatalf("ParseSSHRemote(%q) = %+v, want %+v", tc.raw, got, tc.want)
+ }
+ }
+}
+
+func TestParseSSHRemoteRejectsInvalidAddresses(t *testing.T) {
+ for _, raw := range []string{
+ "",
+ "https://gitlab.com/group/repo.git",
+ "gitlab.com:group/repo.git",
+ "[email protected]:group/repo.git?x=1",
+ "[email protected]:group/re po.git",
+ "ssh://gitlab.com/group/repo.git",
+ "ssh://git:[email protected]/group/repo.git",
+ "ssh://[email protected]:/group/repo.git",
+ "ssh://[email protected]:0/group/repo.git",
+ "ssh://[email protected]:65536/group/repo.git",
+ "[email protected]:group/repo.git",
+ "[email protected]:group/repo.git",
+ "[email protected]:group/repo.git",
+ "[email protected]:group/../repo.git",
+ "[email protected]:group//repo.git",
+ "[email protected]:group/.git",
+ "[email protected]:group/repo$.git",
+ } {
+ if got, err := ParseSSHRemote(raw); err == nil {
+ t.Fatalf("ParseSSHRemote(%q) = %+v, want error", raw, got)
+ }
+ }
+}
+
+func TestSplitSSHRemote(t *testing.T) {
+ user, host, path, port, ok := splitSSHRemote("ssh://git@Host:2200/a/b.git")
+ if !ok || user != "git" || host != "Host" || path != "/a/b.git" || port != "2200" {
+ t.Fatalf("ssh url split = %q %q %q %q %v", user, host, path, port, ok)
+ }
+ user, host, path, port, ok = splitSSHRemote("git@[::1]:a/b")
+ if !ok || user != "git" || host != "::1" || path != "a/b" || port != "22" {
+ t.Fatalf("scp split = %q %q %q %q %v", user, host, path, port, ok)
+ }
+ for _, s := range []string{"ssh://host/a", "ssh://u:p@host/a", "ssh://u@host:99999/a", "host:a/b"} {
+ if _, _, _, _, ok := splitSSHRemote(s); ok {
+ t.Fatalf("splitSSHRemote(%q) ok, want rejection", s)
+ }
+ }
+}
+
+func TestValidSSHRepoPath(t *testing.T) {
+ for path, want := range map[string]bool{
+ "group/repo.git": true,
+ "/group/repo": true,
+ "~user/repo.git": true,
+ "": false,
+ "/": false,
+ "group/": false,
+ "group/.git": false,
+ "group/./repo": false,
+ "group/../repo": false,
+ "group/re:po.git": false,
+ } {
+ if got := validSSHRepoPath(path); got != want {
+ t.Fatalf("validSSHRepoPath(%q) = %v, want %v", path, got, want)
+ }
+ }
+}