diff options
author | Sebastiaan van Stijn <thaJeztah@users.noreply.github.com> | 2019-03-27 09:34:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-27 09:34:06 +0100 |
commit | 200b524eff60a9c95a22bc2518042ac2ff617d07 (patch) | |
tree | 0da8339840c60a6b0aabf22ddc7a05a6b08101e1 | |
parent | 6e2e248bdf537a142eba8a5ef5f5af48d0f11742 (diff) | |
parent | 37ec11c8e3a6d1405680b7a309e72087904bcbd4 (diff) | |
download | docker-18.09.4.tar.gz |
Merge pull request #183 from thaJeztah/18.09_backport_gitutilsv18.09.4
[18.09 backport] gitutils: add validation for ref
-rw-r--r-- | builder/remotecontext/git/gitutils.go | 7 | ||||
-rw-r--r-- | builder/remotecontext/git/gitutils_test.go | 21 |
2 files changed, 24 insertions, 4 deletions
diff --git a/builder/remotecontext/git/gitutils.go b/builder/remotecontext/git/gitutils.go index 77a45beff3..6213963db2 100644 --- a/builder/remotecontext/git/gitutils.go +++ b/builder/remotecontext/git/gitutils.go @@ -102,6 +102,11 @@ func parseRemoteURL(remoteURL string) (gitRepo, error) { u.Fragment = "" repo.remote = u.String() } + + if strings.HasPrefix(repo.ref, "-") { + return gitRepo{}, errors.Errorf("invalid refspec: %s", repo.ref) + } + return repo, nil } @@ -124,7 +129,7 @@ func fetchArgs(remoteURL string, ref string) []string { 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, diff --git a/builder/remotecontext/git/gitutils_test.go b/builder/remotecontext/git/gitutils_test.go index 8c39679081..34dd495b5c 100644 --- a/builder/remotecontext/git/gitutils_test.go +++ b/builder/remotecontext/git/gitutils_test.go @@ -59,7 +59,7 @@ func TestCloneArgsSmartHttp(t *testing.T) { }) 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)) } @@ -75,13 +75,13 @@ func TestCloneArgsDumbHttp(t *testing.T) { }) args := fetchArgs(serverURL.String(), "master") - exp := []string{"fetch", "origin", "master"} + exp := []string{"fetch", "origin", "--", "master"} assert.Check(t, is.DeepEqual(exp, args)) } func TestCloneArgsGit(t *testing.T) { 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)) } @@ -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")) + } +} |