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

fix: only allow hashes of 256 bits or more #633

Merged
merged 2 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ var (
ErrorRekorPubKey = errors.New("error retrieving Rekor public keys")
ErrorInvalidPackageName = errors.New("invalid package name")
ErrorInvalidSubject = errors.New("invalid subject")
ErrorInvalidHash = errors.New("invalid hash")
ErrorNotPresent = errors.New("not present")
)
6 changes: 2 additions & 4 deletions verifiers/internal/gha/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,8 @@ func verifyDigest(prov slsaprovenance.Provenance, expectedHash string) error {
// 8 bit represented in hex, so 8/2=4.
bitLength := len(expectedHash) * 4
expectedAlgo := fmt.Sprintf("sha%v", bitLength)
// TODO(#630): Add subject digest minimum bit length check.
// sha1 is 160 bit (FWIW).
if bitLength == 160 {
expectedAlgo = "sha1"
if bitLength < 256 {
return fmt.Errorf("%w: expected minimum 256-bit. Got %d", serrors.ErrorInvalidHash, bitLength)
}

for _, subject := range subjects {
Expand Down
15 changes: 15 additions & 0 deletions verifiers/internal/gha/provenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ func Test_VerifyDigest(t *testing.T) {
artifactHash string
expected error
}{
{
name: "invalid short hash",
prov: &testProvenance{
subjects: []intoto.Subject{
{
Digest: common.DigestSet{
"sha1": "4506290e2e8feb1f34b27a044f7cc863c830ef6b",
},
},
},
},
// NOTE: the hash is one character short of sha256 hash.
artifactHash: "0ae7e4fa71686538440012ee36a2634dbaa19df2dd16a466f52411fb348bbc4",
expected: serrors.ErrorInvalidHash,
},
{
name: "invalid dsse: no sha256 subject digest",
prov: &testProvenance{
Expand Down
Loading