Drop-in Foundry checks for the Uniswap v4 hook footguns that v4 core does not catch — the class of bug where every on-chain guardrail agrees the hook is valid, yet it is exploitable or silently inert.
Uniswap v4 moves custom logic into hooks, and with it a new attack surface. v4 core validates
one thing about a hook: that its declared permissions match its address bitmap
(Hooks.validateHookPermissions). It says nothing about whether the hook's implementation is
safe. v4-hookguard targets that gap: point a check at your hook and get a failing test — not a
vague warning — when a real hook-specific mistake is present.
Each check ships with a correct reference hook and a deliberately broken one, so the check is
proven to bite (no hollow assertions). Depends only on forge-std and v4-core.
forge install dngr2/v4-hookguardThe #1 hook footgun. A callback (beforeSwap, beforeAddLiquidity, …) is active in the
hook's address bitmap but callable by anyone, because the implementation forgot to gate the
caller to the PoolManager. An attacker then drives the hook's logic outside a real pool
operation: poison an oracle/TWAP the hook maintains, move its accounting, or advance its state
right before their own swap. v4 core accepts such a hook — the address and declared permissions
agree; only the missing access control is the bug.
import {OnlyPoolManagerCheck} from "v4-hookguard/src/checks/OnlyPoolManagerCheck.sol";
contract MyHookSecurity is OnlyPoolManagerCheck {
function test_hookCallbacksAreGated() public {
assertCallbacksGuarded(IHooks(address(myHook)), address(poolManager));
}
}assertCallbacksGuarded is caller-differential and needs no knowledge of your revert: it
sends each active callback once as a stranger and once as the PoolManager with identical
calldata. If the outcome is identical for both, the callback does not distinguish the PoolManager
from anyone else — it is unguarded. The reference pair proves it: the guarded hook rejects the
stranger and accepts the PoolManager; the unguarded hook lets a stranger advance its oracle state.
Pure, pre-deploy sanity on a hook address's low 14 permission bits — catches, before you mine a salt or deploy:
- a
*ReturnDeltaflag set without its base callback flag, and - a dead hook: a non-zero address with a zero bitmap, so the PoolManager never calls it and all of the hook's logic is silently inert.
import {HookAddressLint} from "v4-hookguard/src/checks/HookAddressLint.sol";
using HookAddressLint for address;
assertTrue(address(myHook).isWellFormed(), address(myHook).lint());A stateful fuzz campaign, not a single call. It stands up a real PoolManager + your hook,
seeds a live pool, and fuzzes swaps and liquidity through the actual v4 routers — then, after
every operation, asserts the property every hook must keep: it retains no value it is not owed
— no ERC-6909 claim on either pool currency inside the manager, and no direct token balance of
one. A hook that skims a return-delta, over-charges a fee into itself, or accumulates claims is
caught by a shrunk counterexample, not left for an auditor to find.
import {HookInvariantHarness} from "v4-hookguard/src/harness/HookInvariantHarness.sol";
contract MyHookInvariant is HookInvariantHarness {
function _setUpHook() internal override returns (IHooks) {
// deploy your hook at its permission-flagged address and return it
deployCodeTo("MyHook.sol:MyHook", HOOK_ADDR);
return IHooks(HOOK_ADDR);
}
}forge test then fuzzes the pool against your hook and reports invariant_hookRetainsNoValue.
If your hook is designed to custody value, override _setUpHook accordingly or assert on its
own accounting. The reference pair proves the harness bites: a benign no-op hook holds the
invariant across thousands of operations, while a skimming hook that takes an afterSwap
return-delta into itself is caught leaving with pool value on a single swap.
A silent footgun. A hook charges a fee or takes a swap share by returning a nonzero delta from
beforeSwap (a BeforeSwapDelta) or afterSwap (an int128) — but the PoolManager only reads
and applies that delta if the hook's address carries the matching *ReturnDelta permission
bit. Deploy the same hook at an address without that bit and the manager silently discards the
return value: the hook collects nothing while its own accounting believes it charged. Every
happy-path "the swap succeeds" test still passes.
import {ReturnDeltaConsistencyCheck} from "v4-hookguard/src/checks/ReturnDeltaConsistencyCheck.sol";
contract MyHookReturnDelta is ReturnDeltaConsistencyCheck {
function test_returnDeltaConsistent() public {
assertReturnDeltaConsistent(IHooks(address(myHook)), address(poolManager));
}
}assertReturnDeltaConsistent invokes the swap callbacks in isolation as the PoolManager and asserts
that any nonzero return-delta is backed by the address bit that makes the manager honor it. The
reference pair proves it: two hooks with identical 1% fee code, differing only in deployment address
— the one missing the beforeSwapReturnDelta bit is caught, the one carrying it passes.
The other half of the return-delta footgun, and a denial of service. A hook that charges a fee via a
beforeSwap specified-delta must keep it within the swap amount: v4 core adds the delta to the amount
to swap and reverts HookDeltaExceedsSwapAmount when a fee exceeds the input. So a hook that charges
a flat fee (or any fee that can exceed a small swap) doesn't just overcharge — it makes every swap
at or below that size revert. Invisible to a test that only ever swaps one large, round amount.
import {ReturnDeltaBoundsCheck} from "v4-hookguard/src/checks/ReturnDeltaBoundsCheck.sol";
contract MyHookBounds is ReturnDeltaBoundsCheck {
function test_feeWithinSwap() public {
assertReturnDeltaBounded(IHooks(address(myHook)), address(poolManager));
}
}assertReturnDeltaBounded samples exact-input swaps down to dust and asserts the fee delta stays
within each. The reference pair: a 1% hook passes at every size; a flat-fee hook is caught bricking
small swaps.
The pattern behind several real hook exploits. A hook that custodies value and pays it out — settling
a fee to a recipient, returning a tip, refunding a user — often does the payout with a raw transfer
(native value, or manager.take inside an unlock callback that hands control to the recipient). If
it zeroes the recipient's owed balance after that transfer instead of before, the recipient
re-enters the payout while its balance still reads as owed and drains the hook. A single honest claim
by an EOA passes every test; only a re-entrant contract exposes it.
import {ReentrancyGuardCheck, IClaimable, MaliciousReentrant} from "v4-hookguard/src/checks/ReentrancyGuardCheck.sol";
contract MyHookReentrancy is ReentrancyGuardCheck {
function test_payoutNotReentrant() public {
MaliciousReentrant attacker = new MaliciousReentrant(IClaimable(address(myHook)), 5);
// set myHook up so `attacker` is owed `X` and the hook holds more than `X`, then:
assertClaimNotReentrant(attacker, X);
}
}The check ships a reusable re-entrant attacker and runs it against the payout, asserting it never receives more than it was owed. The reference pair proves it: the checks-effects-interactions violation lets the attacker drain 6× its balance; the corrected hook lets it take exactly what it owed.
A stronger sibling of check 3. The invariant harness checks the hook holds no value — but a hook can skim value and forward it straight to a third party (a fee recipient, an attacker's address), ending each call holding nothing while the pool is still drained. That check passes; the value is gone all the same. This harness closes the gap by conservation: with the pool and one fuzzing LP/swapper as a closed system, the total of each currency held by the PoolManager plus that actor is fixed — operations only move value between them, so any value leaving to a third party makes the sum drop.
import {HookConservationHarness} from "v4-hookguard/src/harness/HookConservationHarness.sol";
contract MyHookConservation is HookConservationHarness {
function _setUpHook() internal override returns (IHooks) { /* deploy + return your hook */ }
}forge test fuzzes swaps and liquidity and reports invariant_poolValueConserved. The reference
proves the added coverage: a hook that leaks a cut of every swap to an external address is caught by
conservation even though it holds nothing itself — exactly the case the "retains no value" check misses.
Two static (pure/view) checks that score any hook address — including one already deployed on-chain —
without a pool or a written test. HookPermissionLint cross-checks a hook's declared
getHookPermissions() against its address bitmap: a mismatch means the hook was deployed at a
wrong/unmined address, so its intended callbacks won't fire (v4 core rejects the pool at init, but by
then the deploy is spent). HookScorecard aggregates that and the {HookAddressLint} bitmap sanity into
one structured Report.
import {HookScorecard} from "v4-hookguard/src/checks/HookScorecard.sol";
HookScorecard.Report memory r = HookScorecard.scan(address(myHook));
assertTrue(r.passed, r.addressWellFormed ? r.permissionIssue : r.addressIssue);The references prove both branches: a hook whose declaration matches its address scores clean, one whose declaration adds a callback its address lacks is flagged, and a malformed address bitmap (returnDelta without its base) is flagged — while a hand-rolled hook that declares no permissions is reported as such, not failed.
The eight checks above cover v4-hookguard's footgun set — unguarded callbacks, address-bitmap and permission-declaration lint, value retention and conservation, return-delta consistency and bounds, and reentrancy. Ongoing work is adoption: the guard-ci CI action and scaffolder, worked integrations, and a public registry that scores deployed hooks.
v4 hooks are the newest, least-charted attack surface in DeFi, and the ecosystem is growing fast. Shared, drop-in security tooling turns each hook-specific footgun into a red CI check for every team shipping a hook — useful to the whole v4 ecosystem, monetizable by no one. MIT, no token, no fees.
Built by dngr2, extending a public line of invariant-tested primitives and the invariant-kit property library.