Selaa lähdekoodia

Merge pull request #14664 from calavera/fix_load_tag_with_digest

Check if a tag name to load is a valid digest.
Stephen Day 10 vuotta sitten
vanhempi
commit
212525f951
3 muutettua tiedostoa jossa 25 lisäystä ja 18 poistoa
  1. 11 9
      graph/tags.go
  2. 12 7
      graph/tags/tags.go
  3. 2 2
      graph/tags_unit_test.go

+ 11 - 9
graph/tags.go

@@ -8,11 +8,11 @@ import (
 	"io/ioutil"
 	"io/ioutil"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
-	"regexp"
 	"sort"
 	"sort"
 	"strings"
 	"strings"
 	"sync"
 	"sync"
 
 
+	"github.com/docker/distribution/digest"
 	"github.com/docker/docker/daemon/events"
 	"github.com/docker/docker/daemon/events"
 	"github.com/docker/docker/graph/tags"
 	"github.com/docker/docker/graph/tags"
 	"github.com/docker/docker/pkg/parsers"
 	"github.com/docker/docker/pkg/parsers"
@@ -25,11 +25,6 @@ import (
 
 
 const DEFAULTTAG = "latest"
 const DEFAULTTAG = "latest"
 
 
-var (
-	//FIXME this regex also exists in registry/v2/regexp.go
-	validDigest = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`)
-)
-
 type TagStore struct {
 type TagStore struct {
 	path         string
 	path         string
 	graph        *Graph
 	graph        *Graph
@@ -253,7 +248,14 @@ func (store *TagStore) SetLoad(repoName, tag, imageName string, force bool, out
 		return err
 		return err
 	}
 	}
 	if err := tags.ValidateTagName(tag); err != nil {
 	if err := tags.ValidateTagName(tag); err != nil {
-		return err
+		if _, formatError := err.(tags.ErrTagInvalidFormat); !formatError {
+			return err
+		}
+		if _, dErr := digest.ParseDigest(tag); dErr != nil {
+			// Still return the tag validation error.
+			// It's more likely to be a user generated issue.
+			return err
+		}
 	}
 	}
 	if err := store.reload(); err != nil {
 	if err := store.reload(); err != nil {
 		return err
 		return err
@@ -387,8 +389,8 @@ func validateDigest(dgst string) error {
 	if dgst == "" {
 	if dgst == "" {
 		return errors.New("digest can't be empty")
 		return errors.New("digest can't be empty")
 	}
 	}
-	if !validDigest.MatchString(dgst) {
-		return fmt.Errorf("illegal digest (%s): must be of the form [a-zA-Z0-9-_+.]+:[a-fA-F0-9]+", dgst)
+	if _, err := digest.ParseDigest(dgst); err != nil {
+		return err
 	}
 	}
 	return nil
 	return nil
 }
 }

+ 12 - 7
graph/tags/tags.go

@@ -2,23 +2,28 @@ package tags
 
 
 import (
 import (
 	"fmt"
 	"fmt"
-	"regexp"
+
+	"github.com/docker/distribution/registry/api/v2"
 )
 )
 
 
 const DEFAULTTAG = "latest"
 const DEFAULTTAG = "latest"
 
 
-var (
-	//FIXME this regex also exists in registry/v2/regexp.go
-	validTagName = regexp.MustCompile(`^[\w][\w.-]{0,127}$`)
-)
+type ErrTagInvalidFormat struct {
+	name string
+}
+
+func (e ErrTagInvalidFormat) Error() string {
+	return fmt.Sprintf("Illegal tag name (%s): only [A-Za-z0-9_.-] are allowed ('.' and '-' are NOT allowed in the initial), minimum 1, maximum 128 in length", e.name)
+}
 
 
 // ValidateTagName validates the name of a tag
 // ValidateTagName validates the name of a tag
 func ValidateTagName(name string) error {
 func ValidateTagName(name string) error {
 	if name == "" {
 	if name == "" {
 		return fmt.Errorf("tag name can't be empty")
 		return fmt.Errorf("tag name can't be empty")
 	}
 	}
-	if !validTagName.MatchString(name) {
-		return fmt.Errorf("Illegal tag name (%s): only [A-Za-z0-9_.-] are allowed ('.' and '-' are NOT allowed in the initial), minimum 1, maximum 128 in length", name)
+
+	if !v2.TagNameAnchoredRegexp.MatchString(name) {
+		return ErrTagInvalidFormat{name}
 	}
 	}
 	return nil
 	return nil
 }
 }

+ 2 - 2
graph/tags_unit_test.go

@@ -195,8 +195,8 @@ func TestValidateDigest(t *testing.T) {
 	}{
 	}{
 		{"", true},
 		{"", true},
 		{"latest", true},
 		{"latest", true},
-		{"a:b", false},
-		{"aZ0124-.+:bY852-_.+=", false},
+		{"sha256:b", false},
+		{"tarsum+v1+sha256:bY852-_.+=", false},
 		{"#$%#$^:$%^#$%", true},
 		{"#$%#$^:$%^#$%", true},
 	}
 	}