Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v0.37] Transaction Validator recovers from script parse panic #6443

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions access/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,20 @@ func (v *TransactionValidator) checkExpiry(tx *flow.TransactionBody) error {
return nil
}

func (v *TransactionValidator) checkCanBeParsed(tx *flow.TransactionBody) error {
func (v *TransactionValidator) checkCanBeParsed(tx *flow.TransactionBody) (err error) {
defer func() {
if r := recover(); r != nil {
if panicErr, ok := r.(error); ok {
err = InvalidScriptError{ParserErr: panicErr}
} else {
err = InvalidScriptError{ParserErr: fmt.Errorf("non-error-typed panic: %v", r)}
}
}
}()
if v.options.CheckScriptsParse {
_, err := parser.ParseProgram(nil, tx.Script, parser.Config{})
if err != nil {
return InvalidScriptError{ParserErr: err}
_, parseErr := parser.ParseProgram(nil, tx.Script, parser.Config{})
if parseErr != nil {
return InvalidScriptError{ParserErr: parseErr}
}
}

Expand Down
19 changes: 19 additions & 0 deletions engine/collection/ingest/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ func (suite *Suite) TestInvalidTransaction() {
suite.Assert().True(errors.As(err, &access.InvalidScriptError{}))
})

// In some cases the Cadence parser will panic rather than return an error.
// If this happens, we should recover from the panic and return an InvalidScriptError.
// See: https://github.com/onflow/cadence/issues/3428, https://github.com/dapperlabs/flow-go/issues/6964
suite.Run("transaction script exceeds parse token limit (Cadence parser panic should be caught)", func() {
const tokenLimit = 1 << 19
script := "{};"
for len(script) < tokenLimit {
script += script
}

tx := unittest.TransactionBodyFixture()
tx.ReferenceBlockID = suite.root.ID()
tx.Script = []byte("transaction { execute {" + script + "}}")

err := suite.engine.ProcessTransaction(&tx)
suite.Assert().Error(err)
suite.Assert().True(errors.As(err, &access.InvalidScriptError{}))
})

suite.Run("invalid signature format", func() {
signer := flow.Testnet.Chain().ServiceAddress()
keyIndex := uint32(0)
Expand Down