Skip to content

v4 Security Foundations

Security-first guide for building Uniswap v4 hooks. Hook vulnerabilities can drain user funds -- understand these concepts before writing any hook code.

Invocation

text
/v4-security-foundations

Or describe your requirements naturally:

text
Review the security of my v4 hook before deployment

What It Does

This skill helps you:

  • Understand the v4 threat model: Caller verification, sender identity, router context, state exposure, and reentrancy surfaces
  • Assess permission risk: All 14 hook permissions mapped to risk levels from LOW to CRITICAL
  • Prevent NoOp rug pull attacks: Detect and avoid the most dangerous hook vulnerability (beforeSwapReturnDelta)
  • Implement access control: PoolManager verification, router allowlisting, and user identity patterns
  • Audit before deployment: Pre-deployment checklist with risk scoring and audit tier recommendations

Threat Model

Threat AreaDescriptionMitigation
Caller VerificationOnly PoolManager should invoke hook functionsVerify msg.sender == address(poolManager)
Sender Identitymsg.sender always equals PoolManager, never the end userUse sender parameter for user identity
Router ContextThe sender parameter identifies the router, not the userImplement router allowlisting
State ExposureHook state is readable during mid-transaction executionAvoid storing sensitive data on-chain
Reentrancy SurfaceExternal calls from hooks can enable reentrancyUse reentrancy guards; minimize external calls
tx.origin Phishingtx.origin exposes the original signer, enabling relay attacksNever use tx.origin for authorization

Permission Flags Risk Matrix

Permission FlagRisk LevelSecurity Notes
beforeInitializeLOWValidate pool parameters
afterInitializeLOWSafe for state initialization
beforeAddLiquidityMEDIUMCan block legitimate LPs
afterAddLiquidityLOWSafe for tracking/rewards
beforeRemoveLiquidityHIGHCan trap user funds
afterRemoveLiquidityLOWSafe for tracking
beforeSwapHIGHCan manipulate prices
afterSwapMEDIUMCan observe final state
beforeDonateLOWAccess control only
afterDonateLOWSafe for tracking
beforeSwapReturnDeltaCRITICALNoOp attack vector
afterSwapReturnDeltaHIGHCan extract value
afterAddLiquidityReturnDeltaHIGHCan shortchange LPs
afterRemoveLiquidityReturnDeltaHIGHCan steal funds

Key Topics Covered

  • NoOp rug pull attacks: How BEFORE_SWAP_RETURNS_DELTA can be exploited, detection methods, and legitimate use cases (JIT liquidity, custom AMM curves, intent-based trading)
  • Delta accounting: The credit/debit system, settlement patterns, and common mistakes
  • Access control patterns: PoolManager verification, router allowlisting, and the msg.sender trap
  • Token handling hazards: Fee-on-transfer, rebasing, ERC-777, pausable, and low-decimal tokens
  • Security checklist: 13-point pre-deployment checklist covering all critical areas
  • Risk scoring: Calculate your hook's risk score (0-33) with audit tier recommendations

Released under the MIT License.