DEV Community

Kestrova
Kestrova

Posted on AI-assisted

Your agent's vault checks tx.origin. A malicious contract can pay itself.

Agents are starting to hold money. A common pattern is a small vault: the owner funds it, an AI agent calls pay() when it needs an API or a service, and an allowlist keeps the agent from paying just anyone.

It is also the kind of contract a coding agent writes in a few seconds when you ask for one. Here is a version that compiles, passes a happy-path test and looks reasonable at a glance:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/// A small vault an AI agent uses to pay for API calls on its owner's behalf.
contract AgentVault {
    address public owner;
    mapping(address => bool) public allowedPayee;

    constructor() {
        owner = msg.sender;
    }

    function allow(address payee) external {
        require(tx.origin == owner, "not owner");
        allowedPayee[payee] = true;
    }

    function pay(address payee, uint256 amount) external {
        require(tx.origin == owner, "not owner");
        require(allowedPayee[payee], "payee not allowed");
        payee.call{value: amount}("");
    }

    receive() external payable {}
}
Enter fullscreen mode Exit fullscreen mode

There are two problems in it, and both are the kind that survive a quick review because the code "has a require".

What the free preview reports

I ran this file through the free Kestrova preview (automated Slither analysis, no account and no wallet). The result, unedited:

  • High: 1, Medium: 3, Informational: 1, Optimization: 1
  • arbitrary-send-eth (High): "A function sends the contract's ETH to an address chosen by the caller. If access control is missing or weak, anyone may be able to drain funds."
  • tx-origin (Medium, 2 places): "Checking tx.origin lets a malicious contract act on behalf of a user who calls it, for example by tricking the owner into a transaction."
  • unchecked-lowlevel (Medium): "A low-level call can fail silently. If its success flag is ignored, failures go unnoticed."

The preview names each issue and explains why it matters. It doesn't give the file, line or fix, but in a 25-line contract you don't need them.

Why tx.origin is worse when an agent is involved

tx.origin is the externally owned account that started the transaction, not the contract or account that called you directly. msg.sender is the direct caller.

So require(tx.origin == owner) really says: "allow this if the owner started the transaction, whatever path it took to get here." Picture the owner's wallet, or an agent signing with the owner's key, calling some other contract: a token claim, a swap router, an "airdrop checker" a tool suggested. If that contract is malicious, it can call AgentVault.allow(attacker) and then pay(attacker, balance) in the same transaction. Both checks pass, because tx.origin is still the owner.

Agents make this easier to trigger, not harder. They interact with many contracts, often ones they found a minute ago, and they sign more transactions than a careful human would. Any of those transactions can carry a hidden call back into your vault.

That is also why the scanner raises arbitrary-send-eth to High. pay() sends ETH to an address the caller chooses, and the only thing protecting it is a weak check. The allowlist doesn't help, because the attacker can add itself through allow() first.

Why the unchecked call matters for agents

payee.call{value: amount}("") returns a success flag. The contract ignores it. If the payee reverts (a contract with no receive, an out-of-gas callback), pay() still succeeds.

For a person that's an annoyance. For an agent it's a logic bug: the agent sees a successful transaction, marks the API call as paid and moves on, while the money never left. Some agents will then retry with a fresh payment, and the books stop matching reality.

The fix

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract AgentVault {
    address public immutable owner;
    mapping(address => bool) public allowedPayee;

    error NotOwner();
    error PayeeNotAllowed();
    error PaymentFailed();

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        if (msg.sender != owner) revert NotOwner();
        _;
    }

    function allow(address payee) external onlyOwner {
        allowedPayee[payee] = true;
    }

    function pay(address payable payee, uint256 amount) external onlyOwner {
        if (!allowedPayee[payee]) revert PayeeNotAllowed();
        (bool ok, ) = payee.call{value: amount}("");
        if (!ok) revert PaymentFailed();
    }

    receive() external payable {}
}
Enter fullscreen mode Exit fullscreen mode

Three changes:

  1. msg.sender instead of tx.origin, in one onlyOwner modifier. A contract in the middle of the call chain is now the caller, so it fails the check.
  2. The call's success flag is checked, and a failed payment reverts, so the agent sees a failed transaction instead of a fake success.
  3. owner is immutable, which removes a storage read and makes it clear it never changes.

Running the fixed file through the same preview gives: High 0, Medium 0, one Informational note (low-level-calls), which just records that the contract uses .call. That's expected here, since .call is the recommended way to send ETH.

If the agent itself should be able to pay, give the agent its own key and check msg.sender == agent for pay() only, keeping allow() owner-only. Never solve it by going back to tx.origin.

Putting the check where the agent writes code

The useful part isn't this one contract. It's making the check happen every time an agent writes or changes Solidity, before anyone deploys.

  • In your editor or agent: add the MCP server https://kestrova-api.onrender.com/mcp to Claude Code, Cursor, VS Code, Claude.ai or ChatGPT (no sign-in). The agent can call scan_solidity_preview after editing a contract and fix what it finds before showing you the diff.
  • In pull requests: the free GitHub Action runs the same preview on the changed Solidity files and reports only new findings, so a tx.origin check added in a PR shows up in the job summary.
  • For deployed contracts: before an agent approves or pays a contract it didn't write, it can check the contract by address with scan_contract_address_preview.

What this doesn't do

This is automated Slither analysis, not an audit. It catches known patterns like the ones above. It won't prove the vault is correct, and it says nothing about business logic, key management or whether the payees are honest. A clean result means "no known pattern was found", nothing more.

Try it

Paste the first contract above into the scanner at https://kestrova.dev and compare the result with the fixed version. If you're building agents that hold or move funds, I'd like to hear which checks you run before an agent touches a contract.

Top comments (0)