Check if a tag name to load is a valid digest.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-07-15 17:33:27 -07:00
parent 48a01a317c
commit 1ec25653d8
3 changed files with 25 additions and 18 deletions

View file

@ -8,11 +8,11 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"github.com/docker/distribution/digest"
"github.com/docker/docker/daemon/events"
"github.com/docker/docker/graph/tags"
"github.com/docker/docker/pkg/parsers"
@ -25,11 +25,6 @@ import (
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 {
path string
graph *Graph
@ -253,7 +248,14 @@ func (store *TagStore) SetLoad(repoName, tag, imageName string, force bool, out
return err
}
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 {
return err
@ -387,8 +389,8 @@ func validateDigest(dgst string) error {
if dgst == "" {
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
}

View file

@ -2,23 +2,28 @@ package tags
import (
"fmt"
"regexp"
"github.com/docker/distribution/registry/api/v2"
)
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
func ValidateTagName(name string) error {
if name == "" {
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
}

View file

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