Detecting Delegatecall Proxy Risks With a Static + Fuzz CI Workflow
The proxy class — controlled-delegatecall, delegatecall-in-loop, storage-slot collision, uninitialised UUPS…
Why proxies are the front line
Adversarial tooling has accelerated the rate at which static-analysis findings get weaponised. AI-driven scanners chain individually-known weaknesses into systemic exploits in seconds. The contribution to the security landscape is unsettling for one reason: the tools do not find new vulnerability classes, they find INSTANCES of known classes faster than humans.
Proxy contracts are the single highest-density target for this style of attack. delegatecall runs target bytecode in the caller's storage context. That sentence is the source of every delegatecall vulnerability ever published. Each instance is small. Adversaries that chain instances are the new normal. The defenders' answer has to be: ship the deterministic checks ourselves, before machine-speed scanners weaponise them.
The four classes worth bookmarking, all currently in production codebases, all detectable with tools that have been free for years:
- Controlled-selector delegatecall — proxy forwards an attacker-influenced
bytespayload viadelegatecall. Attacker chooses which function executes against proxy storage. SWC-112. - Delegatecall in a loop —
payablefunction delegate-calls a sub-handler inside a for-loop, accruingmsg.valuemore times than received. Drains balance via "free multiplier." - Storage-slot collision — proxy and implementation declare incompatible layouts; an upgrade or unprotected write corrupts
_admin,_implementation, or accounting. ERC-1967 fixes proxy slots but not implementation collisions across versions. - Uninitialised UUPS implementation — implementation contract not
_disableInitializers()-locked in its constructor; attacker initialises it directly, becomes its owner, callsupgradeToto selfdestruct or hijack.
Class 1: Controlled-selector delegatecall
The textbook case. From Crytic's published Slither corpus, the canonical bug shape:
function bad_delegate_call(bytes memory data) public {
addr_bad.delegatecall(data);
}
When Slither parses that, the controlled-delegatecall detector emits:
C.bad_delegate_call(bytes) uses delegatecall to a input-controlled function id
- addr_bad.delegatecall(data)
Severity high, default ruleset. Catches direct passthrough AND the abi.encode(func_id, data) shape.
The static finding triggers the Foundry test that proves impact. A fork test against the deployed proxy with an attacker-crafted payload:
contract ControlledDelegatecallExploit is Test {
Target target;
function setUp() public {
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 18_400_000);
target = Target(0xDEAD_DEAD_DEAD_DEAD_DEAD_DEAD_DEAD_DEAD_DEAD_DEAD);
}
function test_AttackerHijacksOwner() public {
bytes memory hijack = abi.encodeWithSignature(
"setOwner(address)", address(this)
);
target.bad_delegate_call(hijack);
// If the contract has the bug, ownership now lives in proxy storage.
assertEq(target.owner(), address(this));
}
}
vm.createSelectFork against a pinned mainnet block makes this reproducible across machines, per Foundry's documented cheatcode reference. If the test passes, the bug is real on-chain at that block height. That is the deliverable a reviewer hands the client — not "this looks bad," but "here is the block, here is the call, here is the state delta."
Class 2: Delegatecall in a loop
Less famous, more subtle. Slither's delegatecall-loop detector catches:
contract DelegatecallInLoop {
mapping(address => uint256) balances;
function bad(address[] memory receivers) public payable {
for (uint256 i = 0; i < receivers.length; i++) {
address(this).delegatecall(
abi.encodeWithSignature("addBalance(address)", receivers[i])
);
}
}
function addBalance(address a) public payable {
balances[a] += msg.value;
}
}
Each iteration delegate-calls addBalance with the same msg.value. The contract receives 1 ETH but credits N×1 ETH across receivers. The first attacker to drain wins. The Foundry kill:
function test_LoopMultipliesMsgValue() public {
address[] memory recv = new address[](5);
for (uint i = 0; i < 5; i++) recv[i] = address(uint160(0x1000 + i));
target.bad{value: 1 ether}(recv);
uint256 totalCredited = 0;
for (uint i = 0; i < 5; i++) {
totalCredited += target.balances(recv[i]);
}
assertGt(totalCredited, 1 ether);
}
assertGt(totalCredited, 1 ether) fails on a clean implementation, passes on the buggy one. That asymmetry is the right shape for a regression test you keep in CI for the lifetime of the contract.
Class 3: Storage-slot collision (the part ERC-1967 does not solve)
ERC-1967 fixes proxy-slot collisions by hashing names like eip1967.proxy.implementation. What it does NOT fix is the collision risk between the implementation's OWN variables across upgrades. If V1 declared address public owner; uint256 public counter; and V2 declared uint256 public counter; address public owner;, the upgrade silently swaps slot meanings. Existing data is corrupted.
OpenZeppelin's Upgrades plugin auto-checks via solc --storage-layout. For cross-version comparison, the toolchain is validateUpgrade in CI. Foundry-side, the Class 3 invariant:
function invariant_OwnerSlotStable() public view {
bytes32 slot = bytes32(uint256(0));
address loaded = address(uint160(uint256(vm.load(address(target), slot))));
require(loaded != address(0), "owner slot was overwritten by upgrade");
}
Static analysis (slot-layout diff) and fuzzing combine here: Slither tells you the collision is possible; Foundry's invariant runner proves the corruption when you actually exercise the upgrade path.
Class 4: Uninitialised UUPS implementation
The shortest class to describe and the deadliest. UUPS implementation contracts are themselves deployable and call initialize(). If the constructor does not call _disableInitializers(), anyone can:
- Find the implementation at its deployed address.
- Call
initialize(attacker)directly. - As "implementation owner," call
upgradeTo(attackerImpl)whose new code includesselfdestructor arbitrary logic. - Trigger the upgrade. The implementation self-destructs. The proxy points at empty bytecode. Every interaction reverts. Funds stuck.
OpenZeppelin's Initializable contract documents the fix — a constructor that calls _disableInitializers():
constructor() {
_disableInitializers();
}
The Foundry test "try to initialise the implementation directly":
function test_ImplementationCannotBeInitialised() public {
Implementation impl = new Implementation();
vm.expectRevert(); // _disableInitializers() should make this revert
impl.initialize(address(this));
}
If that revert does not happen, the contract is in front-run territory. Ship-blocking severity. The OpenZeppelin Contracts repo documents this exact pattern in the Initializable contract; running the OpenZeppelin Upgrades plugin in CI catches missing _disableInitializers calls automatically.
Comparison: tools and what they catch
| Tool | Class 1: Controlled selector | Class 2: Delegatecall in loop | Class 3: Storage collision | Class 4: Uninit UUPS |
| Slither (default rules) | Yes (controlled-delegatecall) | Yes (delegatecall-loop) | Partial (naming-convention, manual slot diff) | Partial (uninitialized-state) |
| Foundry fork test | Proves exploit | Proves multiplication | Proves corruption mid-upgrade | Proves init front-run |
| Foundry invariant fuzzer | Detects slow-leak forms | Detects multiplication | Detects post-upgrade drift | N/A — single-tx |
| OpenZeppelin Upgrades plugin | N/A | N/A | Yes (storage-layout diff) | Yes (_disableInitializers lint) |
The right move on a real review is to run all four columns. Slither is free and fast. Foundry tests are the proof artefact. The Upgrades plugin is the upgrade-path safety net. None substitutes the others.
A reproducible CI job (drop into .github/workflows/proxy-checks.yml)
name: proxy-checks
on: [pull_request]
jobs:
slither:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: crytic/slither-action@v0.4.0
with:
target: "contracts/"
fail-on: high
slither-args: "--detect controlled-delegatecall,delegatecall-loop,uninitialized-state"
foundry-fork:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: foundry-rs/foundry-toolchain@v1
- run: forge test --match-contract "Delegatecall" -vvv
env:
MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }}
oz-upgrades:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npx hardhat run scripts/validate-upgrade.ts
Two of three jobs cost zero outside CI minutes. The Foundry job needs an RPC; Alchemy / Infura free tiers cover the volume.
What this scope deliberately misses
Out of scope for this article: EIP-7702 delegation surface (authorisation replay, sponsor-funded delegation drain), Diamond proxy facet collisions, Beacon proxy update races, and the post-Cancun selfdestruct semantics change. Each deserves its own walk-through. Start with the four classes above; they are the routine ones a reviewer triages first.
The combined static-plus-fuzz workflow above is the routine on every proxy review. It is not glamorous. The bugs it catches are not glamorous either.
Posted by the Soken security team. We routinely review proxy upgradability across Solidity, Move, Cairo and FunC stacks. The Slither + Foundry harnesses above are roughly the same workflow we ship in CI for client engagements.
Comments
Join the discussionNo comments yet. Be the first to comment.
