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

Add a sanity check for git ls-remote output #1380

Merged
merged 1 commit into from
Nov 16, 2017
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ BUG FIXES:
* Releases targeting Windows now have a `.exe` suffix (#1291).
* Adaptively recover from dirty and corrupted git repositories in cache (#1279).
* Suppress git password prompts in more places (#1357).
* Validate `git ls-remote` output and ignore all malformed lines (#1379)

IMPROVEMENTS:

Expand Down
16 changes: 16 additions & 0 deletions gps/vcs_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

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

var (
gitHashRE = regexp.MustCompile(`^[a-f0-9]{40}$`)
)

// gitSource is a generic git repository implementation that should work with
// all standard git remotes.
type gitSource struct {
Expand Down Expand Up @@ -238,6 +243,10 @@ func (s *gitSource) exportRevisionTo(ctx context.Context, rev Revision, to strin
return nil
}

func (s *gitSource) isValidHash(hash []byte) bool {
return gitHashRE.Match(hash)
}

func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, err error) {
r := s.repo

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