Skip to content

Commit f748bd4

Browse files
committed
fix: MaxRecordSize of 10 KiB
See rationale: ipfs/specs#319 (comment) This commit was moved from ipfs/go-ipns@e5d96b3
1 parent fc4155a commit f748bd4

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

ipns/errors.go

+7
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ var ErrPublicKeyMismatch = errors.New("public key in record did not match expect
3535

3636
// ErrBadRecord should be returned when an ipns record cannot be unmarshalled
3737
var ErrBadRecord = errors.New("record could not be unmarshalled")
38+
39+
// 10 KiB limit defined in https://github.com/ipfs/specs/pull/319
40+
const MaxRecordSize int = 10 << (10 * 1)
41+
42+
// ErrRecordSize should be returned when an ipns record is
43+
// invalid due to being too big
44+
var ErrRecordSize = errors.New("record exceeds allowed size limit")

ipns/ipns.go

+5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func createCborDataForIpnsEntry(e *pb.IpnsEntry) ([]byte, error) {
132132

133133
// Validates validates the given IPNS entry against the given public key.
134134
func Validate(pk ic.PubKey, entry *pb.IpnsEntry) error {
135+
// Make sure max size is respected
136+
if entry.Size() > MaxRecordSize {
137+
return ErrRecordSize
138+
}
139+
135140
// Check the ipns record signature with the public key
136141
if entry.GetSignatureV2() == nil {
137142
// always error if no valid signature could be found

ipns/validate_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,26 @@ func TestSignatureV1Ignored(t *testing.T) {
274274
}
275275
}
276276

277+
func TestMaxSizeValidate(t *testing.T) {
278+
goodeol := time.Now().Add(time.Hour)
279+
280+
sk, pk, err := crypto.GenerateEd25519Key(rand.New(rand.NewSource(42)))
281+
if err != nil {
282+
t.Fatal(err)
283+
}
284+
285+
// Create record over the max size (value+other fields)
286+
value := make([]byte, MaxRecordSize)
287+
entry, err := Create(sk, value, 1, goodeol, 0)
288+
if err != nil {
289+
t.Fatal(err)
290+
}
291+
// Must fail with ErrRecordSize
292+
if err := Validate(pk, entry); !errors.Is(err, ErrRecordSize) {
293+
t.Fatal(err)
294+
}
295+
}
296+
277297
func TestCborDataCanonicalization(t *testing.T) {
278298
goodeol := time.Now().Add(time.Hour)
279299

0 commit comments

Comments
 (0)