Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize and Escape refs in git backend (#21464) #21463

Merged
merged 14 commits into from
Oct 15, 2022
22 changes: 21 additions & 1 deletion modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Command struct {
parentContext context.Context
desc string
globalArgsLength int
broken bool
}

func (c *Command) String() string {
Expand Down Expand Up @@ -89,12 +90,25 @@ func (c *Command) SetDescription(desc string) *Command {
return c
}

// AddArguments adds new argument(s) to the command.
// AddArguments adds new argument(s) to the command. Each argument must be safe to be trusted.
func (c *Command) AddArguments(args ...string) *Command {
c.args = append(c.args, args...)
return c
}

// AddDynamicArguments adds new dynamic argument(s) to the command.
// The arguments may come from user input and can not be trusted, so no leading '-' is allowed to avoid passing options
func (c *Command) AddDynamicArguments(args ...string) *Command {
for _, arg := range args {
if arg != "" && arg[0] == '-' {
c.broken = true
return c
}
}
c.args = append(c.args, args...)
return c
}

// RunOpts represents parameters to run the command. If UseContextTimeout is specified, then Timeout is ignored.
type RunOpts struct {
Env []string
Expand Down Expand Up @@ -138,8 +152,14 @@ func CommonCmdServEnvs() []string {
return commonBaseEnvs()
}

var ErrBrokenCommand = errors.New("git command is command")

// Run runs the command with the RunOpts
func (c *Command) Run(opts *RunOpts) error {
if c.broken {
log.Error("git command is broken: %s", c.String())
return ErrBrokenCommand
}
if opts == nil {
opts = &RunOpts{}
}
Expand Down
15 changes: 15 additions & 0 deletions modules/git/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,19 @@ func TestRunWithContextStd(t *testing.T) {
assert.Contains(t, err.Error(), "exit status 129 - unknown option:")
assert.Empty(t, stdout)
}

cmd = NewCommand(context.Background())
cmd.AddDynamicArguments("-test")
assert.ErrorIs(t, cmd.Run(&RunOpts{}), ErrBrokenCommand)

cmd = NewCommand(context.Background())
cmd.AddDynamicArguments("--test")
assert.ErrorIs(t, cmd.Run(&RunOpts{}), ErrBrokenCommand)

subCmd := "version"
cmd = NewCommand(context.Background()).AddDynamicArguments(subCmd) // for test purpose only, the sub-command should never be dynamic for production
stdout, stderr, err = cmd.RunStdString(&RunOpts{})
assert.NoError(t, err)
assert.Empty(t, stderr)
assert.Contains(t, stdout, "git version")
}
2 changes: 1 addition & 1 deletion modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, file
// CommitsCountFiles returns number of total commits of until given revision.
func CommitsCountFiles(ctx context.Context, repoPath string, revision, relpath []string) (int64, error) {
cmd := NewCommand(ctx, "rev-list", "--count")
cmd.AddArguments(revision...)
cmd.AddDynamicArguments(revision...)
if len(relpath) > 0 {
cmd.AddArguments("--")
cmd.AddArguments(relpath...)
Expand Down
2 changes: 1 addition & 1 deletion modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// GitVersionRequired is the minimum Git version required
const GitVersionRequired = "2.0.0"
const GitVersionRequired = "2.20.0"
6543 marked this conversation as resolved.
Show resolved Hide resolved

var (
// GitExecutable is the command name of git
Expand Down
19 changes: 10 additions & 9 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co
// add previous arguments except for --grep and --all
hashCmd.AddArguments(args...)
// add keyword as <commit>
hashCmd.AddArguments(v)
hashCmd.AddDynamicArguments(v)

// search with given constraints for commit matching sha hash of v
hashMatching, _, err := hashCmd.RunStdBytes(&RunOpts{Dir: repo.Path})
Expand Down Expand Up @@ -208,14 +208,15 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
}()
go func() {
stderr := strings.Builder{}
err := NewCommand(repo.Ctx, "log", revision, "--follow",
"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize*page),
prettyLogFormat, "--", file).
Run(&RunOpts{
Dir: repo.Path,
Stdout: stdoutWriter,
Stderr: &stderr,
})
gitCmd := NewCommand(repo.Ctx, "log", prettyLogFormat, "--follow",
"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize*page))
gitCmd.AddDynamicArguments(revision)
gitCmd.AddArguments("--", file)
err := gitCmd.Run(&RunOpts{
Dir: repo.Path,
Stdout: stdoutWriter,
Stderr: &stderr,
})
if err != nil {
_ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
} else {
Expand Down
8 changes: 4 additions & 4 deletions modules/git/repo_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
_ = stdoutWriter.Close()
}()

args := []string{"log", "--numstat", "--no-merges", "--pretty=format:---%n%h%n%aN%n%aE%n", "--date=iso", fmt.Sprintf("--since='%s'", since)}
gitCmd := NewCommand(repo.Ctx, "log", "--numstat", "--no-merges", "--pretty=format:---%n%h%n%aN%n%aE%n", "--date=iso", fmt.Sprintf("--since='%s'", since))
if len(branch) == 0 {
args = append(args, "--branches=*")
gitCmd.AddArguments("--branches=*")
} else {
args = append(args, "--first-parent", branch)
gitCmd.AddArguments("--first-parent").AddDynamicArguments(branch)
}

stderr := new(strings.Builder)
err = NewCommand(repo.Ctx, args...).Run(&RunOpts{
err = gitCmd.Run(&RunOpts{
Env: []string{},
Dir: repo.Path,
Stdout: stdoutWriter,
Expand Down
19 changes: 7 additions & 12 deletions modules/gitgraph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,30 @@ func GetCommitGraph(r *git.Repository, page, maxAllowedColors int, hidePRRefs bo
page = 1
}

args := make([]string, 0, 12+len(branches)+len(files))

args = append(args, "--graph", "--date-order", "--decorate=full")
graphCmd := git.NewCommand(r.Ctx, "log", "--graph", "--date-order", "--decorate=full")

if hidePRRefs {
args = append(args, "--exclude="+git.PullPrefix+"*")
graphCmd.AddArguments("--exclude=" + git.PullPrefix + "*")
6543 marked this conversation as resolved.
Show resolved Hide resolved
}

if len(branches) == 0 {
args = append(args, "--all")
graphCmd.AddArguments("--all")
}

args = append(args,
graphCmd.AddArguments(
"-C",
"-M",
fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum*page),
"--date=iso",
fmt.Sprintf("--pretty=format:%s", format))

if len(branches) > 0 {
args = append(args, branches...)
graphCmd.AddDynamicArguments(branches...)
}
args = append(args, "--")
if len(files) > 0 {
args = append(args, files...)
graphCmd.AddArguments("--")
graphCmd.AddArguments(files...)
}

graphCmd := git.NewCommand(r.Ctx, "log")
graphCmd.AddArguments(args...)
graph := NewGraph()

stderr := new(strings.Builder)
Expand Down