Skip to content

Commit fa283eb

Browse files
authored
Protect against over-length origins (#260)
1 parent 77b77de commit fa283eb

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

note/note_cosigv1.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ func formatMLDSACosignatureV1(cosignerName string, timestamp uint64, logOrigin s
391391
if start > 0 && timestamp > 0 {
392392
return nil, errInvalidTimestamp
393393
}
394+
if len(logOrigin) > 255 || len(cosignerName) > 255 {
395+
return nil, errSignerID
396+
}
394397

395398
// The signed message is a binary TLS presentation encoding of the
396399
// following structure:
@@ -499,6 +502,9 @@ func (v *SubtreeVerifier) VerifySubtree(timestamp uint64, logOrigin string, star
499502
// isValidName reports whether name is valid.
500503
// It must be non-empty and not have any Unicode spaces or pluses.
501504
func isValidName(name string) bool {
505+
if len(name) > 255 {
506+
return false
507+
}
502508
return name != "" && utf8.ValidString(name) && strings.IndexFunc(name, unicode.IsSpace) < 0 && !strings.Contains(name, "+")
503509
}
504510

note/note_cosigv1_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package note
66

77
import (
88
"crypto/rand"
9+
"strings"
910
"testing"
1011
"time"
1112

@@ -48,7 +49,41 @@ func TestSignerRoundtrip(t *testing.T) {
4849
}
4950
}
5051

51-
func TestCosignnatureV1RoundTrip(t *testing.T) {
52+
func TestFormatMLDSASignatureV1(t *testing.T) {
53+
for _, test := range []struct {
54+
name string
55+
cosignerName string
56+
logOrigin string
57+
wantErr bool
58+
}{
59+
{
60+
name: "ok",
61+
cosignerName: "mldsa",
62+
logOrigin: "test",
63+
},
64+
{
65+
name: "origin name too long",
66+
cosignerName: "mldsa",
67+
logOrigin: strings.Repeat("t", 256),
68+
wantErr: true,
69+
},
70+
{
71+
name: "cosigner name too long",
72+
cosignerName: "mldsa"+strings.Repeat("a", 255),
73+
logOrigin: "test",
74+
wantErr: true,
75+
},
76+
} {
77+
t.Run(test.name, func(t *testing.T) {
78+
_, err := formatMLDSACosignatureV1(test.cosignerName, 0, test.logOrigin, 0, 0, []byte{})
79+
if gotErr := err != nil; gotErr != test.wantErr {
80+
t.Fatalf("formatMLDSACosignatureV1: got %v", err)
81+
}
82+
})
83+
}
84+
}
85+
86+
func TestCosignatureV1RoundTrip(t *testing.T) {
5287
edSk, edPk := mustGenerateEd25519Key(t, "ed25519")
5388
mlSk, mlPk := mustGenerateMLDSAKey(t, "mldsa")
5489
for _, test := range []struct {
@@ -340,6 +375,10 @@ func TestGenerateMLDSAKey(t *testing.T) {
340375
name: "invalid name",
341376
wantErr: true,
342377
},
378+
{
379+
name: "name-too-long"+strings.Repeat("g", 255),
380+
wantErr: true,
381+
},
343382
} {
344383
t.Run(test.name, func(t *testing.T) {
345384
skey, vkey, err := GenerateMLDSAKey(test.name)

0 commit comments

Comments
 (0)