Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ func (r *Repository) RevParse(ctx context.Context, rev string, opts ...RevParseO
opt = opts[0]
}

// A revision that starts with a dash is never a valid object and would
// instead be interpreted by "git rev-parse" as a command-line option,
// allowing argument injection. The "--end-of-options" separator does not
// help here because "git rev-parse" does not honor it for revisions, so
// reject such input outright.
if strings.HasPrefix(rev, "-") {
return "", ErrRevisionNotExist
}

args := []string{"rev-parse", rev}

commitID, err := exec(ctx, r.path, args, opt.Envs)
Expand Down
13 changes: 13 additions & 0 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,19 @@ func TestRepository_RevParse(t *testing.T) {
expID: "",
expErr: ErrRevisionNotExist,
},

// A revision starting with a dash must be rejected to prevent argument
// injection into "git rev-parse".
{
rev: "--absolute-git-dir",
expID: "",
expErr: ErrRevisionNotExist,
},
{
rev: "-h",
expID: "",
expErr: ErrRevisionNotExist,
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
Expand Down
Loading