Please note, this content is no longer actively maintained.
The content of the SWC registry has not been thoroughly updated since 2020. It is known to be incomplete and may contain errors as well as crucial omissions.
For currently maintained guidance on known Smart Contract vulnerabilities written primarily as guidance for security reviewers, please see the EEA EthTrust Security Levels specification. As well as the latest release version, an Editor's draft is available, that represents the latest work of the group developing the specification.
General guidance for developers on what to consider to ensure security, that is currently maintained, is also available through the Smart Contract Security Verification Standard (SCSVS).
Title
Requirement Violation
Relationships
CWE-573: Improper Following of Specification by Caller
Description
The Solidity require()
construct is meant to validate external inputs of a function. In most cases, such external inputs are provided by callers, but they may also be returned by callees. In the former case, we refer to them as precondition violations. Violations of a requirement can indicate one of two possible issues:
- A bug exists in the contract that provided the external input.
- The condition used to express the requirement is too strong.
Remediation
If the required logical condition is too strong, it should be weakened to allow all valid external inputs.
Otherwise, the bug must be in the contract that provided the external input and one should consider fixing its code by making sure no invalid inputs are provided.
References
Samples
requirement_simple.sol
pragma solidity ^0.4.25;
contract Bar {
Foo private f = new Foo();
function doubleBaz() public view returns (int256) {
return 2 * f.baz(0);
}
}
contract Foo {
function baz(int256 x) public pure returns (int256) {
require(0 < x);
return 42;
}
}
requirement_simple_fixed.sol
pragma solidity ^0.4.25;
contract Bar {
Foo private f = new Foo();
function doubleBaz() public view returns (int256) {
return 2 * f.baz(1); //Changes the external contract to not hit the overly strong requirement.
}
}
contract Foo {
function baz(int256 x) public pure returns (int256) {
require(0 < x); //You can also fix the contract by changing the input to the uint type and removing the require
return 42;
}
}