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.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
fix: harden git remote URL handling (#78)
Robert062275b40809013a
diff --git a/internal/engine/clarify_workspace.go b/internal/engine/clarify_workspace.goindex 6c2f35b2..a4f11438 100644--- a/internal/engine/clarify_workspace.go+++ b/internal/engine/clarify_workspace.go@@ -36,6 +36,9 @@ func PrepareClarifyWorkspaceAtBranch(ctx context.Context, cacheDir string, repoif cloneURL == "" {return "", fmt.Errorf("repository %q has no cloneUrl", repo.ID)}+ if err := gitwork.ValidateRemoteURL(cloneURL, true); err != nil {+ return "", fmt.Errorf("repository %q: %w", repo.ID, err)+ }baseBranch = strings.TrimSpace(baseBranch)if baseBranch == "" {baseBranch = strings.TrimSpace(repo.DefaultBranch)@@ -71,7 +74,7 @@ func PrepareClarifyWorkspaceAtBranch(ctx context.Context, cacheDir string, repoargs := []string{"clone", "--no-hardlinks", "--single-branch", "--depth", "1","--branch", baseBranch,- gitwork.RewriteRemoteForToken(cloneURL, token), cacheDir,+ "--", gitwork.RewriteRemoteForToken(cloneURL, token), cacheDir,}if _, err := runGit(ctx, cacheDir, args...); err != nil {_ = os.RemoveAll(cacheDir)@@ -80,7 +83,7 @@ func PrepareClarifyWorkspaceAtBranch(ctx context.Context, cacheDir string, repoif err := applyWorkspaceGitAuth(ctx, cacheDir); err != nil {return "", err}- if _, err := gitC(ctx, cacheDir, "remote", "set-url", "origin", cloneURL); err != nil {+ if _, err := gitC(ctx, cacheDir, "remote", "set-url", "--", "origin", cloneURL); err != nil {return "", fmt.Errorf("set origin url: %w", err)}// git clone --branch also accepts tags. Requirements must start from an@@ -98,7 +101,7 @@ func refreshClarifyWorkspace(ctx context.Context, cacheDir, cloneURL, baseBranchif err := applyWorkspaceGitAuth(ctx, cacheDir); err != nil {return err}- if _, err := gitC(ctx, cacheDir, "remote", "set-url", "origin", cloneURL); err != nil {+ if _, err := gitC(ctx, cacheDir, "remote", "set-url", "--", "origin", cloneURL); err != nil {return fmt.Errorf("set origin url: %w", err)}steps := [][]string{diff --git a/internal/engine/repository_workspace.go b/internal/engine/repository_workspace.goindex eb5c61e1..e18ca254 100644--- a/internal/engine/repository_workspace.go+++ b/internal/engine/repository_workspace.go@@ -155,6 +155,9 @@ func prepareRepository(ctx context.Context, workspace string, repository domain.if remote == "" {return fmt.Errorf("repository has neither localPath nor cloneUrl")}+ if err := gitwork.ValidateRemoteURL(remote, true); err != nil {+ return fmt.Errorf("git clone: %w", err)+ }baseBranch := repositoryBaseBranch(repository)clone := func(branch string, useReference bool) error {args := []string{"clone", "--no-hardlinks", "--single-branch", "--branch", branch}@@ -163,7 +166,7 @@ func prepareRepository(ctx context.Context, workspace string, repository domain.}// Clone into the existing directory (`.`) so git cannot unlink and// recreate `workspace`. That inode is the Docker bind-mount source.- args = append(args, gitwork.RewriteRemoteForToken(remote, gitTokenFrom(ctx)), ".")+ args = append(args, "--", gitwork.RewriteRemoteForToken(remote, gitTokenFrom(ctx)), ".")if _, err := runGit(ctx, workspace, args...); err != nil {return fmt.Errorf("git clone: %w", err)}@@ -218,7 +221,7 @@ func prepareRepository(ctx context.Context, workspace string, repository domain.return err}if origin := strings.TrimSpace(cloneURL); origin != "" {- if _, err := gitC(ctx, workspace, "remote", "set-url", "origin", origin); err != nil {+ if _, err := gitC(ctx, workspace, "remote", "set-url", "--", "origin", origin); err != nil {return fmt.Errorf("set origin url: %w", err)}}@@ -255,7 +258,7 @@ func prepareRepository(ctx context.Context, workspace string, repository domain.// one empty commit and leaves workspace on it. seeded is false when the// remote is unreachable or has branches; the clone error then stands.func seedEmptyRepository(ctx context.Context, workspace, remote, defaultBranch, baseBranch string, reset func(string) error) (seeded bool, err error) {- heads, lsErr := runGit(ctx, workspace, "ls-remote", "--heads", remote)+ heads, lsErr := runGit(ctx, workspace, "ls-remote", "--heads", "--", remote)if lsErr != nil || strings.TrimSpace(heads) != "" {return false, nil}@@ -269,7 +272,7 @@ func seedEmptyRepository(ctx context.Context, workspace, remote, defaultBranch,for _, args := range [][]string{{"init", "-q", "."},{"symbolic-ref", "HEAD", "refs/heads/" + first},- {"remote", "add", "origin", remote},+ {"remote", "add", "--", "origin", remote},} {if _, err := runGit(ctx, workspace, args...); err != nil {return true, fmt.Errorf("initialize empty repository: %w", err)@@ -341,7 +344,10 @@ func resumeRepository(ctx context.Context, workspace string, repository domain.Rreturn err}if origin := strings.TrimSpace(repository.CloneURL); origin != "" {- _, _ = gitC(ctx, workspace, "remote", "set-url", "origin", origin)+ if err := gitwork.ValidateRemoteURL(origin, true); err != nil {+ return fmt.Errorf("set origin url: %w", err)+ }+ _, _ = gitC(ctx, workspace, "remote", "set-url", "--", "origin", origin)}baseBranch := repositoryBaseBranch(repository)if repository.IntegrateBranch != "" && repository.WorkBranch != "" {diff --git a/internal/gitwork/auth.go b/internal/gitwork/auth.goindex 11124696..6c6065bc 100644--- a/internal/gitwork/auth.go+++ b/internal/gitwork/auth.go@@ -90,9 +90,10 @@ func authedBase(token string) string {}// SandboxEnv is the git environment for a child process: no interactive-// prompts, plus Gongfeng URL rewrites carrying the token when one is set.+// prompts, no command-running remote helpers, plus Gongfeng URL rewrites+// carrying the token when one is set.func SandboxEnv(token string) map[string]string {- extra := map[string]string{"GIT_TERMINAL_PROMPT": "0"}+ extra := map[string]string{"GIT_TERMINAL_PROMPT": "0", "GIT_ALLOW_PROTOCOL": AllowedProtocols}user, secret := SplitToken(token)if secret == "" {return extra@@ -121,6 +122,8 @@ func Env(token string) []string {strings.HasPrefix(kv, "GIT_CONFIG_VALUE_"),strings.HasPrefix(kv, "GIT_ASKPASS="),strings.HasPrefix(kv, "SSH_ASKPASS="),+ strings.HasPrefix(kv, "GIT_ALLOW_PROTOCOL="),+ strings.HasPrefix(kv, "GIT_PROTOCOL_FROM_USER="),strings.HasPrefix(kv, gitHTTPUserEnv+"="),strings.HasPrefix(kv, gitHTTPPassEnv+"="):continuediff --git a/internal/gitwork/remote.go b/internal/gitwork/remote.gonew file mode 100644index 00000000..3e2c1806--- /dev/null+++ b/internal/gitwork/remote.go@@ -0,0 +1,104 @@+package gitwork++import (+ "fmt"+ "net/url"+ "strings"+)++// AllowedProtocols is the GIT_ALLOW_PROTOCOL list for every git process the+// platform starts. Remote helpers such as ext:: and fd:: run arbitrary+// commands, so they stay disabled even when a git config enables them.+const AllowedProtocols = "https:http:ssh:git:file"++var networkSchemes = map[string]bool{"https": true, "http": true, "ssh": true, "git": true, "git+ssh": true, "ssh+git": true}++// ValidateRemoteURL rejects repository URLs git could treat as an option, a+// remote helper (`<transport>::<address>`) or an unsupported scheme. Local+// paths and file:// URLs are accepted only with allowLocal: they read from the+// executing host, which only platform administrators may choose.+func ValidateRemoteURL(raw string, allowLocal bool) error {+ s := strings.TrimSpace(raw)+ if s == "" {+ return fmt.Errorf("仓库地址为空")+ }+ if strings.HasPrefix(s, "-") {+ return fmt.Errorf("仓库地址不能以 - 开头")+ }+ for _, r := range s {+ if r < 0x20 || r == 0x7f {+ return fmt.Errorf("仓库地址包含控制字符")+ }+ }+ if i := strings.Index(s, "::"); i >= 0 && isTransportName(s[:i]) {+ return fmt.Errorf("不支持的仓库地址协议 %q", s[:i])+ }+ if i := strings.Index(s, "://"); i >= 0 {+ scheme := strings.ToLower(s[:i])+ if scheme == "file" {+ if !allowLocal {+ return fmt.Errorf("仅平台管理员可以使用本地仓库路径")+ }+ return nil+ }+ if !networkSchemes[scheme] {+ return fmt.Errorf("不支持的仓库地址协议 %q,请使用 https 或 ssh", scheme)+ }+ u, err := url.Parse(s)+ if err != nil || u.Hostname() == "" {+ return fmt.Errorf("仓库地址无法解析")+ }+ if strings.HasPrefix(u.Hostname(), "-") {+ return fmt.Errorf("仓库地址主机名不能以 - 开头")+ }+ return nil+ }+ if host, ok := scpHost(s); ok {+ if host == "" || strings.HasPrefix(host, "-") {+ return fmt.Errorf("仓库地址主机名无效")+ }+ return nil+ }+ if !allowLocal {+ return fmt.Errorf("仅平台管理员可以使用本地仓库路径")+ }+ return nil+}++// isTransportName mirrors git's check for `<transport>::<address>`: the+// prefix consists only of RFC 3986 unreserved characters.+func isTransportName(s string) bool {+ if s == "" {+ return false+ }+ for _, r := range s {+ if !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '-' || r == '.' || r == '_' || r == '~' || r == '+') {+ return false+ }+ }+ return true+}++// scpHost returns the host of an scp-like `[user@]host:path` remote. Git+// treats such a string as ssh when a colon comes before the first slash.+func scpHost(s string) (string, bool) {+ colon := strings.Index(s, ":")+ if colon < 0 {+ return "", false+ }+ if slash := strings.Index(s, "/"); slash >= 0 && slash < colon {+ return "", false+ }+ hostPart := s[:colon]+ if strings.HasPrefix(hostPart, "[") {+ end := strings.Index(s, "]:")+ if end < 0 {+ return "", false+ }+ hostPart = s[1:end]+ }+ if at := strings.LastIndex(hostPart, "@"); at >= 0 {+ hostPart = hostPart[at+1:]+ }+ return hostPart, true+}diff --git a/internal/gitwork/remote_test.go b/internal/gitwork/remote_test.gonew file mode 100644index 00000000..1fdeaca2--- /dev/null+++ b/internal/gitwork/remote_test.go@@ -0,0 +1,74 @@+package gitwork++import (+ "os"+ "os/exec"+ "path/filepath"+ "strings"+ "testing"+)++func TestValidateRemoteURL(t *testing.T) {+ for _, raw := range []string{+ "https://github.com/Tencent/LoopForge.git",+ "http://git.example.com/a/b.git",+ "ssh://[email protected]:22/a/b.git",+ "git://git.example.com/a/b.git",+ "[email protected]:a/b.git",+ "git@[::1]:a/b.git",+ } {+ if err := ValidateRemoteURL(raw, false); err != nil {+ t.Errorf("ValidateRemoteURL(%q) = %v", raw, err)+ }+ }+ for _, raw := range []string{+ "",+ "ext::bash -c id>/tmp/pwn_rce",+ "ext::sh -c touch% /tmp/pwn",+ "fd::17",+ "EXT::bash",+ "-u touch /tmp/pwn_ok",+ "--upload-pack=touch /tmp/pwn",+ "ssh://-oProxyCommand=touch%20/tmp/pwn/a",+ "-oProxyCommand=id:a/b",+ "ftp://example.com/a.git",+ "https://git.example.com/a\n.git",+ "/srv/other-project",+ "file:///srv/other-project",+ } {+ if err := ValidateRemoteURL(raw, false); err == nil {+ t.Errorf("ValidateRemoteURL(%q) accepted", raw)+ }+ }+ for _, raw := range []string{"/srv/repo", "./repo", "file:///srv/repo"} {+ if err := ValidateRemoteURL(raw, true); err != nil {+ t.Errorf("local ValidateRemoteURL(%q) = %v", raw, err)+ }+ }+ if err := ValidateRemoteURL("ext::bash -c id", true); err == nil {+ t.Error("allowLocal must not enable remote helpers")+ }+}++func TestEnvPinsAllowedProtocols(t *testing.T) {+ t.Setenv("GIT_ALLOW_PROTOCOL", "ext")+ t.Setenv("GIT_PROTOCOL_FROM_USER", "1")+ env := strings.Join(Env(""), "\n")+ if strings.Contains(env, "GIT_ALLOW_PROTOCOL=ext") || strings.Contains(env, "GIT_PROTOCOL_FROM_USER=") {+ t.Fatalf("inherited protocol policy kept: %s", env)+ }+ if !strings.Contains(env, "GIT_ALLOW_PROTOCOL="+AllowedProtocols) {+ t.Fatal("GIT_ALLOW_PROTOCOL not pinned")+ }+}++func TestEnvBlocksExtTransportEvenWhenConfigAllowsIt(t *testing.T) {+ dir := t.TempDir()+ marker := filepath.Join(dir, "pwned")+ cmd := exec.Command("git", "-c", "protocol.ext.allow=always", "clone", "--quiet", "--", "ext::sh -c touch% "+marker, filepath.Join(dir, "ws"))+ cmd.Env = Env("")+ _ = cmd.Run()+ if _, err := os.Stat(marker); err == nil {+ t.Fatal("ext:: transport executed a command")+ }+}diff --git a/internal/httpapi/catalog.go b/internal/httpapi/catalog.goindex c69c4615..596f16ce 100644--- a/internal/httpapi/catalog.go+++ b/internal/httpapi/catalog.go@@ -10,6 +10,7 @@ import ("time""agentflow/internal/domain"+ "agentflow/internal/gitwork""agentflow/internal/repo""github.com/gin-gonic/gin"@@ -200,6 +201,12 @@ func (h *catalogHandlers) repositories(c *gin.Context) {if v.DefaultBranch == "" {v.DefaultBranch = "master"}+ if v.CloneURL != "" {+ if err := gitwork.ValidateRemoteURL(v.CloneURL, !authRequired() || h.isPlatformAdmin(c.Request)); err != nil {+ c.JSON(400, map[string]string{"error": "cloneUrl 无效:" + err.Error()})+ return+ }+ }if err := h.repo.PutRepository(v); err != nil {respond(c, err)returndiff --git a/internal/httpapi/repository_clone_url_test.go b/internal/httpapi/repository_clone_url_test.gonew file mode 100644index 00000000..82a70dfb--- /dev/null+++ b/internal/httpapi/repository_clone_url_test.go@@ -0,0 +1,32 @@+package httpapi++import (+ "net/http"+ "testing"+)++func TestRepositorySaveRejectsUnsafeCloneURL(t *testing.T) {+ f := cvBNewHTTPFixture(t)+ for _, cloneURL := range []string{"ext::bash -c id>/tmp/pwn_rce", "fd::7", "--upload-pack=id", "ssh://-oProxyCommand=id/a"} {+ rec := cvBRequest(t, f, http.MethodPost, "/api/repositories", map[string]any{+ "projectId": "platform", "name": "pwn", "gongfengProjectId": "example/pwn", "cloneUrl": cloneURL,+ }, true)+ if rec.Code != http.StatusBadRequest {+ t.Fatalf("cloneUrl %q = %d: %s", cloneURL, rec.Code, rec.Body.String())+ }+ }++ t.Setenv("AGENTFLOW_AUTH_REQUIRED", "true")+ rec := cvBRequest(t, f, http.MethodPost, "/api/repositories", map[string]any{+ "projectId": "platform", "name": "local", "gongfengProjectId": "example/local", "cloneUrl": "/srv/other-project",+ }, true)+ if rec.Code != http.StatusBadRequest {+ t.Fatalf("non-admin local cloneUrl = %d: %s", rec.Code, rec.Body.String())+ }+ rec = cvBRequest(t, f, http.MethodPost, "/api/repositories", map[string]any{+ "projectId": "platform", "name": "ok", "gongfengProjectId": "example/ok", "cloneUrl": "[email protected]:example/ok.git",+ }, true)+ if rec.Code != http.StatusOK {+ t.Fatalf("ssh cloneUrl = %d: %s", rec.Code, rec.Body.String())+ }+}diff --git a/internal/runner/daemon.go b/internal/runner/daemon.goindex 9d1a0937..85395a19 100644--- a/internal/runner/daemon.go+++ b/internal/runner/daemon.go@@ -1174,13 +1174,16 @@ func cloneRepository(ctx context.Context, workspace string, repo *domain.RunnerRcloneCtx, cancel := context.WithTimeout(ctx, 10*time.Minute)defer cancel()url := strings.TrimSpace(repo.CloneURL)+ if err := gitwork.ValidateRemoteURL(url, true); err != nil {+ return fmt.Errorf("git clone: %w", err)+ }// `-c` auth is scoped to this command, so origin stays the clean URL and// the token never lands in .git/config.args := append(gitwork.AuthConfigArgs(repo.GitToken), "clone", "--quiet")if ref := strings.TrimSpace(repo.Ref); ref != "" {args = append(args, "--branch", ref)}- args = append(args, url, ".")+ args = append(args, "--", url, ".")cmd := exec.CommandContext(cloneCtx, "git", args...)cmd.Dir = workspacecmd.Env = gitwork.Env(repo.GitToken)diff --git a/internal/runner/repository_checkout_test.go b/internal/runner/repository_checkout_test.goindex 62120509..f0bb5d00 100644--- a/internal/runner/repository_checkout_test.go+++ b/internal/runner/repository_checkout_test.go@@ -261,3 +261,16 @@ func TestCanceledCloneStopsGitChildren(t *testing.T) {t.Fatalf("canceled clone left a Git child running: %v", err)}}++func TestCheckoutRepositoryRejectsCommandRunningCloneURLs(t *testing.T) {+ marker := filepath.Join(t.TempDir(), "pwned")+ for _, cloneURL := range []string{"ext::sh -c touch% " + marker, "--upload-pack=touch " + marker} {+ err := checkoutRepository(context.Background(), t.TempDir(), &domain.RunnerRepository{CloneURL: cloneURL, Ref: "master"})+ if err == nil {+ t.Fatalf("checkoutRepository(%q) succeeded", cloneURL)+ }+ if _, statErr := os.Stat(marker); statErr == nil {+ t.Fatalf("checkoutRepository(%q) executed a command", cloneURL)+ }+ }+}