|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package attestation_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/base64" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/chainloop-dev/chainloop/pkg/attestation" |
| 23 | + "github.com/secure-systems-lab/go-securesystemslib/dsse" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + testRawSig = []byte{0x30, 0x44, 0x02, 0x20, 0xAA, 0xBB, 0xCC, 0xDD} |
| 30 | + testRawPayload = []byte(`{"_type":"statement"}`) |
| 31 | +) |
| 32 | + |
| 33 | +func newTestEnvelope(t *testing.T) *dsse.Envelope { |
| 34 | + t.Helper() |
| 35 | + return &dsse.Envelope{ |
| 36 | + PayloadType: "application/vnd.in-toto+json", |
| 37 | + Payload: base64.StdEncoding.EncodeToString(testRawPayload), |
| 38 | + Signatures: []dsse.Signature{ |
| 39 | + {KeyID: "key-1", Sig: base64.StdEncoding.EncodeToString(testRawSig)}, |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Guards against the double-base64 bug in https://github.com/chainloop-dev/chainloop/issues/1832. |
| 45 | +func TestBundleFromDSSEEnvelopeDecodesSignature(t *testing.T) { |
| 46 | + bundle, err := attestation.BundleFromDSSEEnvelope(newTestEnvelope(t)) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + gotEnv := bundle.GetDsseEnvelope() |
| 50 | + assert.Equal(t, testRawPayload, gotEnv.GetPayload()) |
| 51 | + require.Len(t, gotEnv.GetSignatures(), 1) |
| 52 | + assert.Equal(t, testRawSig, gotEnv.GetSignatures()[0].GetSig()) |
| 53 | + assert.Equal(t, "key-1", gotEnv.GetSignatures()[0].GetKeyid()) |
| 54 | +} |
| 55 | + |
| 56 | +func TestBundleFromDSSEEnvelopeNoSignatures(t *testing.T) { |
| 57 | + env := newTestEnvelope(t) |
| 58 | + env.Signatures = nil |
| 59 | + _, err := attestation.BundleFromDSSEEnvelope(env) |
| 60 | + require.Error(t, err) |
| 61 | +} |
| 62 | + |
| 63 | +func TestFixSignatureInBundleIsNoOpOnFixedBundles(t *testing.T) { |
| 64 | + bundle, err := attestation.BundleFromDSSEEnvelope(newTestEnvelope(t)) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + before := bundle.GetDsseEnvelope().GetSignatures()[0].GetSig() |
| 68 | + attestation.FixSignatureInBundle(bundle) |
| 69 | + assert.Equal(t, before, bundle.GetDsseEnvelope().GetSignatures()[0].GetSig()) |
| 70 | +} |
| 71 | + |
| 72 | +func TestFixSignatureInBundleRepairsLegacyBundles(t *testing.T) { |
| 73 | + bundle, err := attestation.BundleFromDSSEEnvelope(newTestEnvelope(t)) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + // Simulate the legacy bug: signature is stored as the ASCII bytes of the base64 string. |
| 77 | + encodedSig := base64.StdEncoding.EncodeToString(testRawSig) |
| 78 | + bundle.GetDsseEnvelope().GetSignatures()[0].Sig = []byte(encodedSig) |
| 79 | + |
| 80 | + attestation.FixSignatureInBundle(bundle) |
| 81 | + assert.Equal(t, testRawSig, bundle.GetDsseEnvelope().GetSignatures()[0].GetSig()) |
| 82 | +} |
0 commit comments