#17012·foundry

Foundry scripts use incorrect pre-simulated values

Author: SidestreamColdMelonCreated Sep 23, 2026Updated Sep 24, 2026
LabelsT-bugT-needs-triage

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge Version: 1.8.1 Commit SHA: 982849d3140c01fd3b72905759581a132df7aa98 Build Timestamp: 2026-08-28T17:50:40.383510000Z (1787939440) Build Profile: dist

What version of Foundryup are you on?

N/A

What command(s) is the bug in?

forge script --broadcast

Operating System

macOS (Apple Silicon)

Describe the bug

Foundry scripts simulate a value returned by write call (e.g., an address returned by a factory) and never verifies it. If this value is used inside the same script, the value might be incorrect. The there is no mode or flag in forge that would re-simulate a transaction or correctly derive output from the real already mined transaction.

The forge script --slow command that can be understood as the safe and sequential broadcast mode, does not protect against it. All transactions in a script are always built from a single local simulation that runs before any broadcasting starts and --slow only serializes sending of those already-built transactions (waiting for each receipt before sending the next).

Specific attack scenario: if the value returned by an earlier call depends on mutable on-chain state (e.g. the deploying factory's nonce for a plain CREATE), and that state changes between local simulation and the real mining of the earlier transaction (a front-run, or simply another unrelated transaction touching the same state), every later transaction in the script keeps using the stale, simulated value. Concretely, funds/permissions can be sent to an address the attacker fully controls.

Minimal PoC

src/Factory.sol:

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

contract Vault {
    address public owner;
    constructor() { owner = msg.sender; }
    receive() external payable {}
}

contract Factory {
    // Plain CREATE: resulting address depends on (factory address, factory nonce)
    // at the time this actually executes on-chain.
    function deploy() external returns (address) {
        return address(new Vault());
    }
}

script/DeployAndFund.s.sol:

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

import "forge-std/Script.sol";
import {Factory} from "../src/Factory.sol";

contract DeployAndFund is Script {
    function run() external {
        Factory factory = Factory(vm.envAddress("FACTORY"));

        vm.startBroadcast();

        // tx #1: vault address is simulated here once, never verified after the transaction is mined
        address vault = factory.deploy();

        // tx #2: simulated vault address is used to send ETH, instead of the actual one
        (bool ok,) = vault.call{value: 1 ether}("");
        require(ok, "transfer failed");

        vm.stopBroadcast();
    }
}

Improvement proposals

  1. At minimum, the docs for --slow should be explicit that it only serializes sending, not correctness of state derived from earlier calls, and does not protect against this class of front-running
  2. A stronger fix would re-simulate remaining transactions against the post-receipt on-chain state before building the next transaction (essentially: derive tx n's inputs from the receipt of tx n-1 rather than from the original one-shot simulation).
    • I believe this should be the default behavior of forge script command without any flags (even if this would be a breaking change) and the parallel one-shot simulated approach should require a separate flag (e.g., --unsafe-parallel) to explicitly indicate a potential issue