Foundry scripts use incorrect pre-simulated values
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:
// 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:
// 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
- At minimum, the docs for
--slowshould 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 - 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 txn-1rather than from the original one-shot simulation).- I believe this should be the default behavior of
forge scriptcommand 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
- I believe this should be the default behavior of
Source: foundry-rs/foundry