Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit eed4d4b

Browse files
authored
Merge pull request #1380 from Civil/improve_git_ls_validation
Add a sanity check for git ls-remote output
2 parents 2b7a080 + b7882f0 commit eed4d4b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ BUG FIXES:
55
* Releases targeting Windows now have a `.exe` suffix (#1291).
66
* Adaptively recover from dirty and corrupted git repositories in cache (#1279).
77
* Suppress git password prompts in more places (#1357).
8+
* Validate `git ls-remote` output and ignore all malformed lines (#1379)
89

910
IMPROVEMENTS:
1011

gps/vcs_source.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"os"
1212
"path/filepath"
13+
"regexp"
1314
"strings"
1415

1516
"github.com/Masterminds/semver"
@@ -117,6 +118,10 @@ func (bs *baseVCSSource) exportRevisionTo(ctx context.Context, r Revision, to st
117118
return fs.CopyDir(bs.repo.LocalPath(), to)
118119
}
119120

121+
var (
122+
gitHashRE = regexp.MustCompile(`^[a-f0-9]{40}$`)
123+
)
124+
120125
// gitSource is a generic git repository implementation that should work with
121126
// all standard git remotes.
122127
type gitSource struct {
@@ -238,6 +243,10 @@ func (s *gitSource) exportRevisionTo(ctx context.Context, rev Revision, to strin
238243
return nil
239244
}
240245

246+
func (s *gitSource) isValidHash(hash []byte) bool {
247+
return gitHashRE.Match(hash)
248+
}
249+
241250
func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, err error) {
242251
r := s.repo
243252

@@ -298,6 +307,13 @@ func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, er
298307
vlist = make([]PairedVersion, len(all))
299308
for _, pair := range all {
300309
var v PairedVersion
310+
// Valid `git ls-remote` output should start with hash, be at least
311+
// 45 chars long and 40th character should be '\t'
312+
//
313+
// See: https://github.com/golang/dep/pull/1160#issuecomment-328843519
314+
if len(pair) < 45 || pair[40] != '\t' || !s.isValidHash(pair[:40]) {
315+
continue
316+
}
301317
if string(pair[41:]) == "HEAD" {
302318
// If HEAD is present, it's always first
303319
headrev = Revision(pair[:40])

0 commit comments

Comments
 (0)