Skip to content

Commit

Permalink
escape user provide string to git (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollao-hc authored Apr 15, 2024
1 parent 975961f commit 268c11c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions get_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (g *GitGetter) clone(ctx context.Context, dst, sshKeyFile string, u *url.UR
args = append(args, "--depth", strconv.Itoa(depth))
args = append(args, "--branch", ref)
}
args = append(args, u.String(), dst)
args = append(args, "--", u.String(), dst)

cmd := exec.CommandContext(ctx, "git", args...)
setupGitEnv(cmd, sshKeyFile)
Expand Down Expand Up @@ -289,7 +289,7 @@ func findDefaultBranch(ctx context.Context, dst string) string {
// default branch. "master" is returned if no HEAD symref exists.
func findRemoteDefaultBranch(ctx context.Context, u *url.URL) string {
var stdoutbuf bytes.Buffer
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--symref", u.String(), "HEAD")
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--symref", "--", u.String(), "HEAD")
cmd.Stdout = &stdoutbuf
err := cmd.Run()
matches := lsRemoteSymRefRegexp.FindStringSubmatch(stdoutbuf.String())
Expand Down
30 changes: 30 additions & 0 deletions get_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,36 @@ func TestGitGetter_subdirectory(t *testing.T) {
}
}

func TestGitGetter_BadRemoteUrl(t *testing.T) {

if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}

g := new(GitGetter)
dst := tempDir(t)

// try an option that exists
badUrl := "--no-refs"

u, err := url.Parse(badUrl)
if err != nil {
t.Fatal(err)
}

err = g.Get(dst, u)
if err == nil {
t.Fatalf("get succeeded; want error")
}

got := err.Error()
want := `repository '--no-refs' does not exist`
if !strings.Contains(got, want) {
t.Fatalf("wrong error\ngot: %s\nwant: %q", got, want)
}
}

// gitRepo is a helper struct which controls a single temp git repo.
type gitRepo struct {
t *testing.T
Expand Down

0 comments on commit 268c11c

Please sign in to comment.