From 6824bd9f408f532e469c6f4a0e087410cd2748f8 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 22 Nov 2023 14:07:15 +0300 Subject: [PATCH] smartcontract: require manifest serialization in (*Manifest).IsValid Port the restrictive part of https://github.com/neo-project/neo/pull/2948. Signed-off-by: Anna Shaleva --- pkg/smartcontract/manifest/manifest.go | 14 +++++++++++++- pkg/smartcontract/manifest/manifest_test.go | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/smartcontract/manifest/manifest.go b/pkg/smartcontract/manifest/manifest.go index 5c002381e..900701cef 100644 --- a/pkg/smartcontract/manifest/manifest.go +++ b/pkg/smartcontract/manifest/manifest.go @@ -118,7 +118,19 @@ func (m *Manifest) IsValid(hash util.Uint160) error { return errors.New("duplicate trusted contracts") } } - return Permissions(m.Permissions).AreValid() + err = Permissions(m.Permissions).AreValid() + if err != nil { + return err + } + si, err := m.ToStackItem() + if err != nil { + return fmt.Errorf("failed to check manifest serialisation: %w", err) + } + _, err = stackitem.Serialize(si) + if err != nil { + return fmt.Errorf("manifest is not serializable: %w", err) + } + return nil } // IsStandardSupported denotes whether the specified standard is supported by the contract. diff --git a/pkg/smartcontract/manifest/manifest_test.go b/pkg/smartcontract/manifest/manifest_test.go index 2b9b20bae..47ad9805e 100644 --- a/pkg/smartcontract/manifest/manifest_test.go +++ b/pkg/smartcontract/manifest/manifest_test.go @@ -2,6 +2,7 @@ package manifest import ( "encoding/json" + "fmt" "math/big" "testing" @@ -243,6 +244,18 @@ func TestIsValid(t *testing.T) { require.Error(t, m.IsValid(contractHash)) }) }) + m.Groups = m.Groups[:0] + + t.Run("invalid, unserializable", func(t *testing.T) { + for i := 0; i < stackitem.MaxSerialized; i++ { + m.ABI.Events = append(m.ABI.Events, Event{ + Name: fmt.Sprintf("Event%d", i), + Parameters: []Parameter{}, + }) + } + err := m.IsValid(contractHash) + require.ErrorIs(t, err, stackitem.ErrTooBig) + }) } func TestManifestToStackItem(t *testing.T) {