From 34d7ef7a0efb07b5be163d971c97df0c64655abd Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Wed, 4 Sep 2024 10:31:41 -0600 Subject: [PATCH] `gh attestation verify` handles empty JSONL files (#9541) * handle empty jsonl files Signed-off-by: Meredith Lancaster * check processed attestations slice length Signed-off-by: Meredith Lancaster * update err name and message Signed-off-by: Meredith Lancaster --------- Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/verification/attestation.go | 5 +++++ .../attestation/verification/attestation_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/cmd/attestation/verification/attestation.go b/pkg/cmd/attestation/verification/attestation.go index 4d96196da01..3a2d7456ff8 100644 --- a/pkg/cmd/attestation/verification/attestation.go +++ b/pkg/cmd/attestation/verification/attestation.go @@ -16,6 +16,7 @@ import ( ) var ErrUnrecognisedBundleExtension = errors.New("bundle file extension not supported, must be json or jsonl") +var ErrEmptyBundleFile = errors.New("provided bundle file is empty") type FetchAttestationsConfig struct { APIClient api.Client @@ -94,6 +95,10 @@ func loadBundlesFromJSONLinesFile(path string) ([]*api.Attestation, error) { attestations = append(attestations, &a) } + if len(attestations) == 0 { + return nil, ErrEmptyBundleFile + } + return attestations, nil } diff --git a/pkg/cmd/attestation/verification/attestation_test.go b/pkg/cmd/attestation/verification/attestation_test.go index ba530e55d32..66b337ad700 100644 --- a/pkg/cmd/attestation/verification/attestation_test.go +++ b/pkg/cmd/attestation/verification/attestation_test.go @@ -42,6 +42,19 @@ func TestLoadBundlesFromJSONLinesFile(t *testing.T) { }) } +func TestLoadBundlesFromJSONLinesFile_RejectEmptyJSONLFile(t *testing.T) { + // Create a temporary file + emptyJSONL, err := os.CreateTemp("", "empty.jsonl") + require.NoError(t, err) + err = emptyJSONL.Close() + require.NoError(t, err) + + attestations, err := loadBundlesFromJSONLinesFile(emptyJSONL.Name()) + + require.ErrorIs(t, err, ErrEmptyBundleFile) + require.Nil(t, attestations) +} + func TestLoadBundleFromJSONFile(t *testing.T) { path := "../test/data/sigstore-js-2.1.0-bundle.json" attestations, err := loadBundleFromJSONFile(path)