Skip to content

Unrestricted transferOwnership

Description

The transferOwnership function is not restricted to the owner, allowing any user to transfer ownership of the contract.

Examples

Insecure Code

solidity
function transferOwnership(address newOwner) public {}

Secure Code

solidity
function transferOwnership(address newOwner) public { require(msg.sender == owner, 'Only the owner can transfer ownership'); owner = newOwner; }

Remediation

Add a require statement to restrict the transferOwnership function to the owner, such as require(msg.sender == owner, 'Only the owner can transfer ownership');

Rule Details

FieldValue
IDCODE-0202
CategoryAccessControl
SeverityCRITICAL
CWECWE-284
ConfidenceLOW
ImpactHIGH
LikelihoodHIGH
ExploitabilityEASY
Tagssmart contract, access control
OWASPN/A

References