Why tx.origin authentication is a vulnerability

Tx origin authentication is deciding who may act by checking tx.origin, the account that started the transaction, instead of msg.sender, the account that is calling you. Any contract the user calls can call yours in the same transaction and pass that check, so a phishing contract can spend the user's funds. Authorize with msg.sender, and don't use tx.origin == msg.sender to keep contracts out, because EIP-7702 lets externally owned accounts run code.

Last updated . 5 minute read. By the QuillAudits Team.

How does a tx.origin phishing attack work?

The Solidity docs define msg.sender as the 'sender of the message (current call)' and tx.origin as the 'sender of the transaction (full call chain)'. When a user calls contract A and A calls contract B, B sees msg.sender = A and tx.origin = the user. SWC-115 spells out the consequence: using tx.origin for authorization 'could make a contract vulnerable if an authorized account calls into a malicious contract'.

  1. A wallet contract checks tx.origin == owner before it sends funds.
  2. The attacker gets the owner to call a contract they control: a free mint, an airdrop claim, a token whose transfer runs a hook.
  3. That contract calls the wallet's transfer function in the same transaction.
  4. The wallet sees tx.origin = owner, passes the check and pays the attacker.

The Solidity security docs show the same attack against a wallet called TxUserWallet, conclude that 'the attack wallet instantly drains all your funds', and state the rule: 'Never use tx.origin for authorization.'

Is require(tx.origin == msg.sender) still a safe way to block contracts?

No. The check was used to allow only externally owned accounts, as a cheap guard against reentrancy, flash loans and bots. EIP-7702, which lets an externally owned account set code, lists among its security considerations that it can 'Break reentrancy guards of the style require(tx.origin == msg.sender)' and 'Break atomic sandwich protections which rely on tx.origin', and its authors judged those risks acceptable.

Even before that, the check locked out every smart contract wallet and multisig, because a contract never starts a transaction. Protect the property you need directly: a reentrancy guard against reentrancy, time-weighted measurements against flash loan attacks, a minimum output against sandwich attacks.

What does vulnerable code look like?

Two misuses: a wallet that authorizes the transaction's origin, and a withdrawal contract that relies on 'no contracts' instead of a reentrancy guard. FakeMint is the phishing side.

Vulnerable: tx.origin for access, and an EOA-only guard (illustrative, not audited code) OriginWallet.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract OriginWallet {
    address public owner;

    constructor() payable {
        owner = msg.sender;
    }

    // Checks who started the transaction, not who is calling.
    function transfer(address payable to, uint256 amount) external {
        require(tx.origin == owner, "not owner");
        (bool ok, ) = to.call{value: amount}("");
        require(ok, "send failed");
    }

    receive() external payable {}
}

interface IOriginWallet {
    function transfer(address payable to, uint256 amount) external;
}

// A "free mint" the owner is tricked into calling. It empties the wallet in the same transaction.
contract FakeMint {
    IOriginWallet public immutable wallet;
    address payable public immutable attacker;

    constructor(IOriginWallet wallet_) {
        wallet = wallet_;
        attacker = payable(msg.sender);
    }

    function mint() external {
        wallet.transfer(attacker, address(wallet).balance);
    }
}

contract EoaOnlyWithdrawals {
    mapping(address => uint256) public deposits;

    function deposit() external payable {
        deposits[msg.sender] += msg.value;
    }

    // "No contracts, so no reentrancy." Since EIP-7702 an account that passes
    // this check can run code when it receives the ETH, and call back in.
    function withdraw() external {
        require(tx.origin == msg.sender, "no contracts");
        uint256 amount = deposits[msg.sender];
        (bool ok, ) = msg.sender.call{value: amount}("");
        require(ok, "send failed");
        deposits[msg.sender] = 0;
    }
}

The owner calls FakeMint.mint(). The wallet sees tx.origin = owner and sends everything to the attacker. In the second contract, an account with delegated code passes tx.origin == msg.sender, receives the ETH in its own code and calls withdraw again before its deposit is cleared.

How do you fix it?

Fixed: msg.sender for access, a real reentrancy guard (illustrative, not audited code) SenderWallet.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract SenderWallet {
    address public owner;

    constructor() payable {
        owner = msg.sender;
    }

    // Authorizes the account that is calling, whatever started the transaction.
    // A contract the owner was tricked into calling is msg.sender here, not the owner.
    function transfer(address payable to, uint256 amount) external {
        require(msg.sender == owner, "not owner");
        (bool ok, ) = to.call{value: amount}("");
        require(ok, "send failed");
    }

    receive() external payable {}
}

contract GuardedWithdrawals {
    mapping(address => uint256) public deposits;
    uint256 private locked = 1;

    modifier nonReentrant() {
        require(locked == 1, "reentrant call");
        locked = 2;
        _;
        locked = 1;
    }

    function deposit() external payable nonReentrant {
        deposits[msg.sender] += msg.value;
    }

    // Contracts and smart accounts are welcome: the guard and the order of
    // updates stop reentrancy, whoever the caller is.
    function withdraw() external nonReentrant {
        uint256 amount = deposits[msg.sender];
        require(amount > 0, "nothing to withdraw");
        deposits[msg.sender] = 0;
        (bool ok, ) = msg.sender.call{value: amount}("");
        require(ok, "send failed");
    }
}
  • Use `msg.sender` for every authorization decision. SWC-115: 'tx.origin should not be used for authorization. Use msg.sender instead.'
  • Replace EOA-only checks with the protection you meant. A reentrancy guard and checks-effects-interactions, as in GuardedWithdrawals; see reentrancy.
  • If you need a user's intent across contracts, ask for a signature. A signed message checked with replay protection says exactly what the user agreed to; see signature replay.
  • Reading `tx.origin` for logs or analytics is harmless. Deciding access or limits with it is not.

How has a tx.origin check been bypassed in a real attack?

Protocol POT staking contract
Date 24 August 2025
Chain BNB Chain
Loss about $85K, per OpenZeppelin
What happened An unverified staking contract that paid rewards in POT allowed only externally owned accounts to stake, using a msg.sender == tx.origin check, to keep flash-loan price manipulation out of staking. The attacker delegated an EOA to a malicious contract under EIP-7702 and triggered its code with a native BNB transfer to itself, so the check passed while contract code was running. With a flash loan the attacker pushed up the POT price, staked at the inflated value, let the price fall back and unstaked for about 3.3 million POT. OpenZeppelin's Security team puts the profit at $85K.
Sources OpenZeppelin

Nobody was phished here. This is the second misuse on this page: the check was meant to prove that the caller couldn't run code, and once EIP-7702 was live it no longer did. OpenZeppelin's digest of 17 September 2025 walks through the transactions.

What should a reviewer check?

  • Search for every tx.origin, including in modifiers, libraries and inherited contracts.
  • Is it used to decide access, limits, fees or rewards? Replace it with msg.sender.
  • Is it compared with msg.sender to keep contracts out? Write down what that check was protecting, and protect it directly.
  • Does the protocol rely on another protocol's EOA-only check for its own safety?
  • Do tests call each privileged function through an intermediate contract, in a transaction the owner starts, and expect a revert?
  • Do tests run the main flows from a contract account, so a smart wallet user isn't locked out?

How does QuillShield check for tx.origin authentication?

A tx.origin check is easy to find; deciding whether it is exploitable means knowing which functions it guards and what an intermediary contract could do with them. A QuillShield full audit covers the Solidity codebase in your Foundry project, builds a security graph of the code and lists the invariants it must keep, and a separate reviewer agent tries to disprove every finding before you see it. Each finding that holds up comes with the exact lines, the impact, a recommended fix and, on full audits, a proof of concept.

Old patterns come back through copied code, so the checks after the audit matter: QuillShield checks every pull request on GitHub or GitLab.com against the security graph, and on GitHub, with branch protection or rulesets, a required QuillShield check blocks the merge while a High or Critical finding is open.

QuillShield is made by QuillAudits, which has audited 1,500+ protocols since 2018. Run a full audit yourself, or talk to an auditor about having QuillAudits run it on your code.

FAQ

What is the difference between tx.origin and msg.sender?

tx.origin is the account that started the whole transaction. msg.sender is the account or contract that made the current call. They are the same only when a user calls your contract directly.

Is tx.origin ever safe to use?

For logging or analytics, yes. For deciding who may do something, no, and since EIP-7702 it no longer tells you whether the caller can run code either.

Does tx.origin == msg.sender stop flash loans and reentrancy?

It used to block contracts from calling directly. EIP-7702 lists breaking guards of that style among its security considerations, so treat the check as no protection and use a reentrancy guard and sound measurement instead.

How do I test for tx.origin misuse in Foundry?

Deploy a small intermediary contract, have the owner's account call it, let it call your privileged function, and expect a revert. Then run the same flow from a contract account to make sure legitimate smart wallets still work.

Which SWC and OWASP entries cover tx.origin authentication?

SWC-115, Authorization through tx.origin. The OWASP Smart Contract Top 10 (2025) has no separate entry; the closest is SC01, Access Control Vulnerabilities, which covers flaws that let unauthorized users reach a contract's functions.

Sources

QuillShield is building the security layer between AI and money, so you can keep shipping between audits.

Sign-up is free, and new accounts get $50 in credit, good for up to half of an order.

  1. 1Sign up and import a repo
  2. 2See the exact price before anything is charged
  3. 3Approve it, and the audit starts