[Bridge] [Solidity] Use require
for input validation rather than if-revert
pattern
#827
Labels
require
for input validation rather than if-revert
pattern
#827
Problem
Using
require
to validate inputsIn our (bridge) Solidity contracts, we are using the pattern
if (!cond) revert Error
to validate inputs. This is not the best practice and there were several instances of bugs in the past where the contract hadif !C someCode; revert()
, ommitting the{}
around theif-body
, which can have unintended consequences. You can read more about the risks (and a bug in APPLE-SSL) here.Warning
In short, if written by hand, there is no guarantee that the
if-revert
pattern is used properly.Using
require(cond, error)
has several advantages:require
clearly identify the conditioncond
that should be checked rather than the negation of the condition that appears in the equivalentif-revert
pattern;require(cond, error)
is automatically translated (by the Solidity compiler) inif (!cond) revert error
thereby enforcing that the patternif-revert
is used correctly.Important
The use of
require
is recommended to validate inputs and has better readability and security.CustomError
to minimise gas costThere is a caveat though: until recently,
require(cond, error)
only supportederror
of type int or strings, and these two types are more expensive gas-wise than custom errors.This is why developers would deliberately use
if (!C) revert CustomError
(instead of therequire
) in the code to optimise gas costs, while compromising security.As of version 0.8.27 (release notes) , the Solidity compiler supports
require(conditional, customError)
.Tip
We can now use a safe and gas-efficient pattern
require(cond, customError)
.Proposed changes
There are two Solidity contracts that are currently using the unsafe and hard-to-read
if-revert
pattern to validate inputs:The proposal is to use
require
for input validation.For example, the validation of inputs
amount > 0
in the functioninitiateBridgeTransfer
:would be re-written into (note that the
revert
related to the transfer of Move tokens is not an input validation, but a more complex error and best practice is to use anif ... revert
in that case):Implementation
To implement the changes we have to:
require
Important
There does not seem to be any test in AtomicBridgeInitiatorMOVE.t.sol to check that the functions revert under certain conditions. If is absolutely necessary to add some tests to ensure that we don't disable the checks (e.g.
amount > 0
) later and introduce some bugs.Validation
Here are the steps to validate the changes:
The text was updated successfully, but these errors were encountered: