gitutils: add validation for ref

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit 723b107ca4fba14580a6cd971e63d8af2e7d2bbe)
Signed-off-by: Andrew Hsu <andrewhsu@docker.com>
(cherry picked from commit a588898f99)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Tonis Tiigi 2019-02-06 11:58:40 -08:00 committed by Sebastiaan van Stijn
parent 6e2e248bdf
commit 37ec11c8e3
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 24 additions and 4 deletions

View file

@ -102,6 +102,11 @@ func parseRemoteURL(remoteURL string) (gitRepo, error) {
u.Fragment = "" u.Fragment = ""
repo.remote = u.String() repo.remote = u.String()
} }
if strings.HasPrefix(repo.ref, "-") {
return gitRepo{}, errors.Errorf("invalid refspec: %s", repo.ref)
}
return repo, nil return repo, nil
} }
@ -124,7 +129,7 @@ func fetchArgs(remoteURL string, ref string) []string {
args = append(args, "--depth", "1") args = append(args, "--depth", "1")
} }
return append(args, "origin", ref) return append(args, "origin", "--", ref)
} }
// Check if a given git URL supports a shallow git clone, // Check if a given git URL supports a shallow git clone,

View file

@ -59,7 +59,7 @@ func TestCloneArgsSmartHttp(t *testing.T) {
}) })
args := fetchArgs(serverURL.String(), "master") args := fetchArgs(serverURL.String(), "master")
exp := []string{"fetch", "--depth", "1", "origin", "master"} exp := []string{"fetch", "--depth", "1", "origin", "--", "master"}
assert.Check(t, is.DeepEqual(exp, args)) assert.Check(t, is.DeepEqual(exp, args))
} }
@ -75,13 +75,13 @@ func TestCloneArgsDumbHttp(t *testing.T) {
}) })
args := fetchArgs(serverURL.String(), "master") args := fetchArgs(serverURL.String(), "master")
exp := []string{"fetch", "origin", "master"} exp := []string{"fetch", "origin", "--", "master"}
assert.Check(t, is.DeepEqual(exp, args)) assert.Check(t, is.DeepEqual(exp, args))
} }
func TestCloneArgsGit(t *testing.T) { func TestCloneArgsGit(t *testing.T) {
args := fetchArgs("git://github.com/docker/docker", "master") args := fetchArgs("git://github.com/docker/docker", "master")
exp := []string{"fetch", "--depth", "1", "origin", "master"} exp := []string{"fetch", "--depth", "1", "origin", "--", "master"}
assert.Check(t, is.DeepEqual(exp, args)) assert.Check(t, is.DeepEqual(exp, args))
} }
@ -276,3 +276,18 @@ func TestValidGitTransport(t *testing.T) {
} }
} }
} }
func TestGitInvalidRef(t *testing.T) {
gitUrls := []string{
"git://github.com/moby/moby#--foo bar",
"git@github.com/moby/moby#--upload-pack=sleep;:",
"git@g.com:a/b.git#-B",
"git@g.com:a/b.git#with space",
}
for _, url := range gitUrls {
_, err := Clone(url)
assert.Assert(t, err != nil)
assert.Check(t, is.Contains(strings.ToLower(err.Error()), "invalid refspec"))
}
}