Skip to content

Commit 39bae3d

Browse files
authored
Merge commit from fork (#2729)
This adds graceful handling of dereferencing a number of pointers where the content may be user-controlled. Also took care of a few spelling errors too. Fixes GHSA-273p-m2cw-6833 Signed-off-by: Hayden <8418760+Hayden-IO@users.noreply.github.com>
1 parent 812e699 commit 39bae3d

File tree

8 files changed

+88
-7
lines changed

8 files changed

+88
-7
lines changed

cmd/cleanup-index/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
/*
16-
cleanup-index checks what index entries are in the MySQL table and deletes those entries from the Redis databse.
16+
cleanup-index checks what index entries are in the MySQL table and deletes those entries from the Redis database.
1717
It does not go the other way
1818
1919
To run:

pkg/pki/minisign/minisign_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func TestVerifySignature(t *testing.T) {
391391
}
392392

393393
if err := s.Verify(dataFile, k); (err == nil) != tc.verified {
394-
t.Errorf("%v: unexpected result in verifying sigature: %v", tc.caseDesc, err)
394+
t.Errorf("%v: unexpected result in verifying signature: %v", tc.caseDesc, err)
395395
}
396396
}
397397

pkg/pki/pgp/pgp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func TestEmailAddresses(t *testing.T) {
354354

355355
var k PublicKey
356356
if len(k.Subjects()) != 0 {
357-
t.Errorf("Subjects for unitialized key should give empty slice")
357+
t.Errorf("Subjects for uninitialized key should give empty slice")
358358
}
359359
tests := []test{
360360
{caseDesc: "Valid armored public key", inputFile: "testdata/valid_armored_public.pgp", subjects: []string{}, keys: 2},
@@ -447,7 +447,7 @@ func TestVerifySignature(t *testing.T) {
447447
}
448448

449449
if err := s.Verify(dataFile, k); (err == nil) != tc.verified {
450-
t.Errorf("%v: unexpected result in verifying sigature: %v", tc.caseDesc, err)
450+
t.Errorf("%v: unexpected result in verifying signature: %v", tc.caseDesc, err)
451451
}
452452
}
453453

pkg/pki/tuf/tuf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func TestVerifySignature(t *testing.T) {
229229
}
230230

231231
if err := s.Verify(nil, k); (err == nil) != tc.verified {
232-
t.Errorf("%v: unexpected result in verifying sigature: %v", tc.caseDesc, err)
232+
t.Errorf("%v: unexpected result in verifying signature: %v", tc.caseDesc, err)
233233
}
234234
}
235235

pkg/types/cose/v0.0.1/entry.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func (v V001Entry) IndexKeys() ([]string, error) {
8383
var result []string
8484

8585
// We add the key, the hash of the overall cose envelope, and the hash of the payload itself as keys.
86+
if v.CoseObj.PublicKey == nil {
87+
return nil, errors.New("missing public key")
88+
}
8689
keyObj, err := x509.NewPublicKey(bytes.NewReader(*v.CoseObj.PublicKey))
8790
if err != nil {
8891
return nil, err
@@ -169,6 +172,9 @@ func (v *V001Entry) Unmarshal(pe models.ProposedEntry) error {
169172
return err
170173
}
171174

175+
if v.CoseObj.PublicKey == nil {
176+
return errors.New("missing public key")
177+
}
172178
v.keyObj, err = x509.NewPublicKey(bytes.NewReader(*v.CoseObj.PublicKey))
173179
if err != nil {
174180
return err
@@ -199,8 +205,15 @@ func (v *V001Entry) Unmarshal(pe models.ProposedEntry) error {
199205

200206
func (v *V001Entry) Canonicalize(_ context.Context) ([]byte, error) {
201207
if v.keyObj == nil {
202-
return nil, errors.New("cannot canonicalze empty key")
208+
return nil, errors.New("cannot canonicalize empty key")
203209
}
210+
if v.sign1Msg == nil {
211+
return nil, errors.New("signed message uninitialized")
212+
}
213+
if v.sign1Msg.Payload == nil {
214+
return nil, errors.New("payload empty")
215+
}
216+
204217
pk, err := v.keyObj.CanonicalValue()
205218
if err != nil {
206219
return nil, err

pkg/types/cose/v0.0.1/entry_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,3 +923,53 @@ func TestInsertable(t *testing.T) {
923923
})
924924
}
925925
}
926+
927+
func TestV001Entry_IndexKeys_MissingPublicKey(t *testing.T) {
928+
v := V001Entry{
929+
CoseObj: models.CoseV001Schema{
930+
Data: &models.CoseV001SchemaData{},
931+
PublicKey: nil,
932+
},
933+
}
934+
_, err := v.IndexKeys()
935+
if err == nil {
936+
t.Fatal("expected error")
937+
}
938+
if err.Error() != "missing public key" {
939+
t.Errorf("expected 'missing public key' error, got %v", err)
940+
}
941+
}
942+
943+
func TestCanonicalizeHandlesInvalidInput(t *testing.T) {
944+
v := &V001Entry{}
945+
946+
// 1. Missing keyObj
947+
_, err := v.Canonicalize(context.TODO())
948+
if err == nil || err.Error() != "cannot canonicalize empty key" {
949+
t.Fatalf("expected error 'cannot canonicalize empty key', got %v", err)
950+
}
951+
952+
// Setup valid keyObj for subsequent tests
953+
priv, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
954+
der, _ := x509.MarshalPKIXPublicKey(&priv.PublicKey)
955+
pub := pem.EncodeToMemory(&pem.Block{
956+
Bytes: der,
957+
Type: "PUBLIC KEY",
958+
})
959+
keyObj, _ := sigx509.NewPublicKey(bytes.NewReader(pub))
960+
v.keyObj = keyObj
961+
962+
// 2. Missing sign1Msg
963+
_, err = v.Canonicalize(context.TODO())
964+
if err == nil || err.Error() != "signed message uninitialized" {
965+
t.Fatalf("expected error 'signed message uninitialized', got %v", err)
966+
}
967+
968+
// 3. Missing Payload in sign1Msg
969+
v.sign1Msg = gocose.NewSign1Message()
970+
v.sign1Msg.Payload = nil
971+
_, err = v.Canonicalize(context.TODO())
972+
if err == nil || err.Error() != "payload empty" {
973+
t.Fatalf("expected error 'payload empty', got %v", err)
974+
}
975+
}

pkg/types/dsse/v0.0.1/entry.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ func (v *V001Entry) Unmarshal(pe models.ProposedEntry) error {
292292
}
293293

294294
env := &dsse.Envelope{}
295+
if dsseObj.ProposedContent.Envelope == nil {
296+
return errors.New("proposed content envelope is missing")
297+
}
295298
if err := json.Unmarshal([]byte(*dsseObj.ProposedContent.Envelope), env); err != nil {
296299
return err
297300
}
@@ -374,7 +377,7 @@ func (v *V001Entry) Canonicalize(_ context.Context) ([]byte, error) {
374377
}
375378

376379
for _, s := range canonicalEntry.Signatures {
377-
if s.Signature == nil {
380+
if s == nil || s.Signature == nil {
378381
return nil, errors.New("canonical entry missing required signature")
379382
}
380383
}

pkg/types/dsse/v0.0.1/entry_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ func TestV001Entry_Unmarshal(t *testing.T) {
252252
},
253253
wantErr: true,
254254
},
255+
{
256+
name: "missing envelope with verifiers",
257+
it: &models.DSSEV001Schema{
258+
ProposedContent: &models.DSSEV001SchemaProposedContent{
259+
Verifiers: []strfmt.Base64{[]byte("verifier")},
260+
},
261+
},
262+
wantErr: true,
263+
},
255264
{
256265
env: envelope(t, key, []byte(validPayload)),
257266
name: "valid",
@@ -624,4 +633,10 @@ func TestCanonicalizeHandlesInvalidInput(t *testing.T) {
624633
if err == nil {
625634
t.Fatalf("expected error canonicalizing invalid input")
626635
}
636+
637+
v.DSSEObj.Signatures = []*models.DSSEV001SchemaSignaturesItems0{nil}
638+
_, err = v.Canonicalize(context.TODO())
639+
if err == nil {
640+
t.Fatalf("expected error canonicalizing nil signature")
641+
}
627642
}

0 commit comments

Comments
 (0)