7 min read

Development

Solidity Security

Solidity is the primary language for Ethereum smart contracts. Understanding its quirks and common vulnerabilities is essential for security auditing.

Authorised Targets Only

Deploy these contracts on a local chain (Foundry/Anvil, Hardhat) or a testnet you control. Attacking a deployed contract that holds other people's funds is a criminal offence in most jurisdictions and may also be a securities or commodities violation.

Common Vulnerability Patterns

The three pairs below are the ones that keep reappearing in audit findings. All samples target solc 0.8.34, so the arithmetic checks discussed on Integer Bugs are already active and are not what these examples are about.

1. tx.origin as an Authentication Check

tx.origin is the EOA that signed the transaction, and it stays the same through every intermediate call. Any contract the owner is tricked into calling can therefore pass this check on the owner's behalf — the phishing contract in Access Control is the working version. Authorise msg.sender instead.

wallet-vulnerable.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

contract Wallet {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    // VULNERABLE: tx.origin is the account that started the whole
    // transaction, not the contract that is calling right now.
    function withdraw() public {
        require(tx.origin == owner, "not owner");
        payable(msg.sender).transfer(address(this).balance);
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

contract Wallet {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    // VULNERABLE: tx.origin is the account that started the whole
    // transaction, not the contract that is calling right now.
    function withdraw() public {
        require(tx.origin == owner, "not owner");
        payable(msg.sender).transfer(address(this).balance);
    }
}

2. Unchecked ERC-20 Transfer

transfer returns a bool, and plenty of live tokens return false instead of reverting while others return nothing at all. Ignoring the result turns both cases into a silent no-op that your accounting already treats as paid.

payout-vulnerable.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
}

contract Payout {
    IERC20 public immutable token;

    constructor(address token_) {
        token = IERC20(token_);
    }

    // VULNERABLE: the return value is discarded, so a token that
    // returns false fails silently and the caller sees success.
    function pay(address to, uint256 amount) public {
        token.transfer(to, amount);
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
}

contract Payout {
    IERC20 public immutable token;

    constructor(address token_) {
        token = IERC20(token_);
    }

    // VULNERABLE: the return value is discarded, so a token that
    // returns false fails silently and the caller sees success.
    function pay(address to, uint256 amount) public {
        token.transfer(to, amount);
    }
}

3. delegatecall to a Caller-Supplied Target

delegatecall runs the target's bytecode against the calling contract's storage, so a caller who controls the target also controls your storage layout. The original page shipped only the vulnerable half; the mitigation is not a language feature you can switch on, it is a design constraint — pin the implementation address at deployment, restrict who may invoke it, and never pass a target chosen by the caller.

proxy-vulnerable.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

contract Proxy {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    // VULNERABLE: the caller chooses the target AND the calldata.
    // delegatecall executes in THIS contract's storage, so the target
    // can run "owner = attacker" and take the proxy over.
    function execute(address target, bytes calldata data) public {
        (bool ok, ) = target.delegatecall(data);
        require(ok, "delegatecall failed");
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

contract Proxy {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    // VULNERABLE: the caller chooses the target AND the calldata.
    // delegatecall executes in THIS contract's storage, so the target
    // can run "owner = attacker" and take the proxy over.
    function execute(address target, bytes calldata data) public {
        (bool ok, ) = target.delegatecall(data);
        require(ok, "delegatecall failed");
    }
}

Why the Fixed Proxy Is Only Half the Story

Pinning the implementation removes the takeover path, but the implementation still writes your proxy's storage. Storage-layout collisions between proxy and implementation are a separate bug class — check them explicitly in an upgradeable design, and prefer the audited proxy libraries over a hand-rolled one.

Static Analysis with Slither

slither-analysis.sh
# Run Slither on contract
slither contracts/MyContract.sol

# Output to JSON for further processing
slither contracts/ --json output.json

# Check specific detector
slither . --detect reentrancy-eth

# Common findings:
# - reentrancy-eth (high severity)
# - uninitialized-state
# - arbitrary-send
# - controlled-delegatecall
# - unchecked-transfer

# Integrate with CI/CD
slither . --fail-pedantic