Skip to content

Commit

Permalink
digest: allow separators in algorithm field
Browse files Browse the repository at this point in the history
In the past, we have had support for separators in the algorithm field
to allow parameterization of digest algorithms. A classic example is
`tarsum+sha256`. While this particular case is deprecated, support for
this case in the future must be allow in case we bring this back. This
will ensure that implementations these as valid digest, but correctly
report error when the algorithm is unsupported, rather than this being
treated as an invalid format.

We also widen the character set allowed in the hex encoded portion of
the digest to allow support for future encodings that are not hex-based.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
  • Loading branch information
stevvooe committed Apr 25, 2017
1 parent aa2ec05 commit 5ab10f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewDigestFromHex(alg, hex string) Digest {
}

// DigestRegexp matches valid digest types.
var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`)
var DigestRegexp = regexp.MustCompile(`[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9]+`)

// DigestRegexpAnchored matches valid digest types, anchored to the start and end of the match.
var DigestRegexpAnchored = regexp.MustCompile(`^` + DigestRegexp.String() + `$`)
Expand Down
20 changes: 19 additions & 1 deletion digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestParseDigest(t *testing.T) {
{
// not hex
input: "sha256:d41d8cd98f00b204e9800m98ecf8427e",
err: ErrDigestInvalidFormat,
err: ErrDigestInvalidLength,
},
{
// too short
Expand All @@ -69,6 +69,24 @@ func TestParseDigest(t *testing.T) {
input: "foo:d41d8cd98f00b204e9800998ecf8427e",
err: ErrDigestUnsupported,
},
{
// repeated separators
input: "sha384__foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
err: ErrDigestInvalidFormat,
},
{
// ensure that we parse, but we don't have support for the algorithm
input: "sha384.foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
algorithm: "sha384.foo+bar",
hex: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
err: ErrDigestUnsupported,
},
{
input: "sha384_foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
algorithm: "sha384_foo+bar",
hex: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
err: ErrDigestUnsupported,
},
} {
digest, err := Parse(testcase.input)
if err != testcase.err {
Expand Down

0 comments on commit 5ab10f5

Please sign in to comment.