mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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:
parent
48a01a317c
commit
1ec25653d8
3 changed files with 25 additions and 18 deletions
|
@ -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) {
|
if _, err := digest.ParseDigest(dgst); err != nil {
|
||||||
return fmt.Errorf("illegal digest (%s): must be of the form [a-zA-Z0-9-_+.]+:[a-fA-F0-9]+", dgst)
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 (
|
type ErrTagInvalidFormat struct {
|
||||||
//FIXME this regex also exists in registry/v2/regexp.go
|
name string
|
||||||
validTagName = regexp.MustCompile(`^[\w][\w.-]{0,127}$`)
|
}
|
||||||
)
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -195,8 +195,8 @@ func TestValidateDigest(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{"", true},
|
{"", true},
|
||||||
{"latest", true},
|
{"latest", true},
|
||||||
{"a:b", false},
|
{"sha256:b", false},
|
||||||
{"aZ0124-.+:bY852-_.+=", false},
|
{"tarsum+v1+sha256:bY852-_.+=", false},
|
||||||
{"#$%#$^:$%^#$%", true},
|
{"#$%#$^:$%^#$%", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue