From ffde45d164f5cbdc4c1472dc2c2453c94d886a9d Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 9 Sep 2021 14:41:04 +0300 Subject: [PATCH] [#807] morph/container: Add opcode check Parsers return error if unexpected opcode returns. Signed-off-by: Pavel Karpy --- pkg/morph/event/container/put_notary.go | 14 +++++++++++--- pkg/morph/event/notary.go | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/morph/event/container/put_notary.go b/pkg/morph/event/container/put_notary.go index 0fa4cb5f..82eee70e 100644 --- a/pkg/morph/event/container/put_notary.go +++ b/pkg/morph/event/container/put_notary.go @@ -50,20 +50,28 @@ var errUnexpectedArgumentAmount = fmt.Errorf("unexpected arguments amount in %s // ParsePutNotary from NotaryEvent into container event structure. func ParsePutNotary(ne event.NotaryEvent) (event.Event, error) { - var ev Put + var ( + ev Put + currentOp opcode.Opcode + ) fieldNum := 0 for _, op := range ne.Params() { - switch op.Code() { - case opcode.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4: + currentOp = op.Code() + + switch { + case opcode.PUSHDATA1 <= currentOp && currentOp <= opcode.PUSHDATA4: if fieldNum == expectedItemNumPut { return nil, errUnexpectedArgumentAmount } fieldSetters[fieldNum](&ev, op.Param()) fieldNum++ + case opcode.PUSH0 <= currentOp && currentOp <= opcode.PUSH16 || currentOp == opcode.PACK: + // array packing opcodes. do nothing with it default: + return nil, event.UnexpectedOpcode(PutNotaryEvent, op.Code()) } } diff --git a/pkg/morph/event/notary.go b/pkg/morph/event/notary.go index cbb2c5f0..0319158c 100644 --- a/pkg/morph/event/notary.go +++ b/pkg/morph/event/notary.go @@ -1,8 +1,11 @@ package event import ( + "fmt" + "github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/vm/opcode" ) // NotaryType is a notary event enumeration type. @@ -39,3 +42,9 @@ func NotaryTypeFromBytes(data []byte) NotaryType { func NotaryTypeFromString(str string) NotaryType { return NotaryType(str) } + +// UnexpectedOpcode returns error when notary parsers +// get unexpected opcode in contract call. +func UnexpectedOpcode(method string, op opcode.Opcode) error { + return fmt.Errorf("unexpected opcode in %s call: %s", method, op) +}