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 sha256:... as alternative way to specify a blob #4016

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
55 changes: 35 additions & 20 deletions cmd/cosign/cli/verify/verify_blob_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"io"
"os"
"path/filepath"
"strings"
"regexp"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
Expand Down Expand Up @@ -76,6 +78,9 @@ type VerifyBlobAttestationCommand struct {
UseSignedTimestamps bool
}

// Add validation helper
var sha256RegExp = regexp.MustCompile("^[a-f0-9]{64}$")

// Exec runs the verification command
func (c *VerifyBlobAttestationCommand) Exec(ctx context.Context, artifactPath string) (err error) {
if options.NOf(c.SignaturePath, c.BundlePath) == 0 {
Expand Down Expand Up @@ -127,29 +132,39 @@ func (c *VerifyBlobAttestationCommand) Exec(ctx context.Context, artifactPath st
}
var h v1.Hash
if c.CheckClaims {
// Get the actual digest of the blob
var payload internal.HashReader
f, err := os.Open(filepath.Clean(artifactPath))
if err != nil {
return err
}
defer f.Close()
fileInfo, err := f.Stat()
if err != nil {
return err
}
err = payloadsize.CheckSize(uint64(fileInfo.Size()))
if err != nil {
return err
}
var hexDigest string
if strings.HasPrefix(artifactPath, "sha256:") {
// log that we are using a sha256 hash
fmt.Fprintf(os.Stderr, "Using sha256 hash %s\n", artifactPath)
hexDigest = strings.TrimPrefix(artifactPath, "sha256:")
if !sha256RegExp.MatchString(hexDigest) {
return fmt.Errorf("invalid sha256 hash format: must be 64 hex characters")
}
} else {
// Get the actual digest of the blob
var payload internal.HashReader
f, err := os.Open(filepath.Clean(artifactPath))
if err != nil {
return err
}
defer f.Close()
fileInfo, err := f.Stat()
if err != nil {
return err
}
err = payloadsize.CheckSize(uint64(fileInfo.Size()))
if err != nil {
return err
}

payload = internal.NewHashReader(f, sha256.New())
if _, err := io.ReadAll(&payload); err != nil {
return err
payload = internal.NewHashReader(f, sha256.New())
if _, err := io.ReadAll(&payload); err != nil {
return err
}
hexDigest = hex.EncodeToString(payload.Sum(nil))
}
digest := payload.Sum(nil)
h = v1.Hash{
Hex: hex.EncodeToString(digest),
Hex: hexDigest,
Algorithm: "sha256",
}
co.ClaimVerifier = cosign.IntotoSubjectClaimVerifier
Expand Down