Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DigestSet hex encoding validation #330

Merged
merged 5 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions go/v1/resource_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,55 @@ Wrapper APIs for in-toto attestation ResourceDescriptor protos.

package v1

import "errors"
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha512"
"encoding/hex"
"errors"
)

var ErrRDRequiredField = errors.New("at least one of name, URI, or digest are required")
var (
ErrIncorrectDigestLength = errors.New("digest is not correct length")
marcelamelara marked this conversation as resolved.
Show resolved Hide resolved
ErrInvalidDigestEncoding = errors.New("digest is not valid hex-encoded string")
ErrRDRequiredField = errors.New("at least one of name, URI, or digest are required")
)

// Supported standard hash algorithms
marcelamelara marked this conversation as resolved.
Show resolved Hide resolved
func isSupportedAlgorithm(alg string) (bool, int) {
algos := map[string]int{"md5": md5.Size, "sha1": sha1.Size, "shake128": md5.Size, "sha224": sha512.Size224, "sha3_224": sha512.Size224, "sha512_224": sha512.Size224, "sha256": sha512.Size256, "sha3_256": sha512.Size256, "sha512_256": sha512.Size256, "shake256": sha512.Size256, "sha384": sha512.Size384, "sha3_384": sha512.Size384, "sha512_384": sha512.Size384, "sha512": sha512.Size, "sha3_512": sha512.Size, "dirHash": sha512.Size256, "gitCommit": sha1.Size}
marcelamelara marked this conversation as resolved.
Show resolved Hide resolved

size, ok := algos[alg]
return ok, size
}

func (d *ResourceDescriptor) Validate() error {
// at least one of name, URI or digest are required
if d.GetName() == "" && d.GetUri() == "" && len(d.GetDigest()) == 0 {
return ErrRDRequiredField
}

if len(d.GetDigest()) > 0 {
for alg, digest := range d.GetDigest() {

// check encoding and length for supported algorithms
supported, size := isSupportedAlgorithm(alg)
marcelamelara marked this conversation as resolved.
Show resolved Hide resolved
if supported {
// the in-toto spec expects a hex-encoded string in DigestSets for supported algorithms
// https:/in-toto/attestation/blob/main/spec/v1/digest_set.md
hashBytes, err := hex.DecodeString(digest)

if err != nil {
return ErrInvalidDigestEncoding
}

// check the length of the digest
if len(hashBytes) != size {
return ErrIncorrectDigestLength
}
}
}
}

return nil
}
24 changes: 24 additions & 0 deletions go/v1/resource_descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const wantFullRd = `{"name":"theName","uri":"https://example.com","digest":{"alg

const badRd = `{"downloadLocation":"https://example.com/test.zip","mediaType":"theMediaType"}`

const badRdDigestEncoding = `{"digest":{"sha256":"badDigest"},"downloadLocation":"https://example.com/test.zip","mediaType":"theMediaType"}`

const badRdDigestLength = `{"digest":{"sha256":"abc123"},"downloadLocation":"https://example.com/test.zip","mediaType":"theMediaType"}`

func createTestResourceDescriptor() (*ResourceDescriptor, error) {
// Create a ResourceDescriptor
a, err := structpb.NewStruct(map[string]interface{}{
Expand Down Expand Up @@ -65,3 +69,23 @@ func TestBadResourceDescriptor(t *testing.T) {
err = got.Validate()
assert.ErrorIs(t, err, ErrRDRequiredField, "created malformed ResourceDescriptor")
}

func TestBadResourceDescriptorDigestEncoding(t *testing.T) {
marcelamelara marked this conversation as resolved.
Show resolved Hide resolved
got := &ResourceDescriptor{}
err := protojson.Unmarshal([]byte(badRdDigestEncoding), got)

assert.NoError(t, err, "Error during JSON unmarshalling")

err = got.Validate()
assert.ErrorIs(t, err, ErrInvalidDigestEncoding, "created ResourceDescriptor with invalid digest encoding")
marcelamelara marked this conversation as resolved.
Show resolved Hide resolved
}

func TestBadResourceDescriptorDigestLength(t *testing.T) {
got := &ResourceDescriptor{}
err := protojson.Unmarshal([]byte(badRdDigestLength), got)

assert.NoError(t, err, "Error during JSON unmarshalling")

err = got.Validate()
assert.ErrorIs(t, err, ErrIncorrectDigestLength, "created ResourceDescriptor with incorrect digest length")
}