6 min read

Authorization

Access Control

Missing or incorrect access control is one of the most common smart contract vulnerabilities. Functions meant to be restricted can be called by anyone.

Authorised Targets Only

The attacker contract below is for a victim you deployed on Foundry/Anvil, Hardhat, or a testnet you control. Phishing a real key-holder into draining a live contract is theft — unauthorised exploitation is a criminal offence in most jurisdictions and may also be a securities or commodities violation.

Common Access Control Issues

The bug is usually an omission rather than a mistake in logic: a function that was written as an admin action and never decorated. Read the modifier list of every state-changing function and ask who can reach it.

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

// VULNERABLE: missing access control
contract Vulnerable {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    // Anyone can call this! Whoever calls it becomes the owner.
    function setOwner(address newOwner) public {
        owner = newOwner;
    }

    // Missing modifier - and it pays the new owner, whoever that is.
    function withdrawAll() public {
        (bool ok, ) = payable(owner).call{value: address(this).balance}("");
        require(ok, "Transfer failed");
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

// VULNERABLE: missing access control
contract Vulnerable {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    // Anyone can call this! Whoever calls it becomes the owner.
    function setOwner(address newOwner) public {
        owner = newOwner;
    }

    // Missing modifier - and it pays the new owner, whoever that is.
    function withdrawAll() public {
        (bool ok, ) = payable(owner).call{value: address(this).balance}("");
        require(ok, "Transfer failed");
    }
}

The ordering of those two calls is what makes the pair dangerous: setOwner hands the caller ownership, and withdrawAll then pays it out in the next transaction. One missing modifier on either function is enough.

tx.origin Phishing

This is the case where the check looks correct. The victim genuinely tests for the owner — but against tx.origin, which is the EOA that signed the transaction and is unchanged by any intermediate contract call. The attacker only needs the owner to interact with their contract once; the victim's own balance is then transferred to the attacker's caller.

txorigin-victim.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

// VULNERABLE: using tx.origin for authorisation
contract TxOriginVictim {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    function withdrawAll() public {
        require(tx.origin == owner, "Not owner");  // BAD!
        payable(msg.sender).transfer(address(this).balance);
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

// VULNERABLE: using tx.origin for authorisation
contract TxOriginVictim {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    function withdrawAll() public {
        require(tx.origin == owner, "Not owner");  // BAD!
        payable(msg.sender).transfer(address(this).balance);
    }
}

The fix is one identifier: msg.sender, which is the caller of this function and therefore changes as soon as a contract is in the middle. The same substitution appears in Solidity Security.

EIP-7702, live since Pectra in May 2025, gives the rule a second reason. An account can now set its own code by signing an authorisation tuple, so tx.origin is no longer guaranteed to be code-free, and msg.sender == tx.origin no longer means "topmost frame" because a delegated account can make further calls inside the same transaction. The lure is cheaper as well: one signed authorisation points the victim's account at the attacker's delegate, which then has unrestricted access until it is overwritten or cleared by delegating to the zero address.

Always Use msg.sender

Never use tx.origin for authentication. It can be manipulated through intermediate contract calls.