Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
fix: MaxRecordSize of 10 KiB
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Sep 20, 2022
1 parent 237111a commit e5d96b3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ var ErrPublicKeyMismatch = errors.New("public key in record did not match expect

// ErrBadRecord should be returned when an ipns record cannot be unmarshalled
var ErrBadRecord = errors.New("record could not be unmarshalled")

// 10 KiB limit defined in https://github.com/ipfs/specs/pull/319
const MaxRecordSize int = 10 << (10 * 1)

// ErrRecordSize should be returned when an ipns record is
// invalid due to being too big
var ErrRecordSize = errors.New("record exceeds allowed size limit")
5 changes: 5 additions & 0 deletions ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func createCborDataForIpnsEntry(e *pb.IpnsEntry) ([]byte, error) {

// Validates validates the given IPNS entry against the given public key.
func Validate(pk ic.PubKey, entry *pb.IpnsEntry) error {
// Make sure max size is respected
if entry.Size() > MaxRecordSize {
return ErrRecordSize
}

// Check the ipns record signature with the public key
if entry.GetSignatureV2() == nil {
// always error if no valid signature could be found
Expand Down
20 changes: 20 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,26 @@ func TestSignatureV1Ignored(t *testing.T) {
}
}

func TestMaxSizeValidate(t *testing.T) {
goodeol := time.Now().Add(time.Hour)

sk, pk, err := crypto.GenerateEd25519Key(rand.New(rand.NewSource(42)))
if err != nil {
t.Fatal(err)
}

// Create record over the max size (value+other fields)
value := make([]byte, MaxRecordSize)
entry, err := Create(sk, value, 1, goodeol, 0)
if err != nil {
t.Fatal(err)
}
// Must fail with ErrRecordSize
if err := Validate(pk, entry); !errors.Is(err, ErrRecordSize) {
t.Fatal(err)
}
}

func TestCborDataCanonicalization(t *testing.T) {
goodeol := time.Now().Add(time.Hour)

Expand Down

0 comments on commit e5d96b3

Please sign in to comment.