Why Hooks Need Isolation
The settlement contract operates as a privileged context that:- Holds accrued protocol fees in various ERC20 tokens
- Possesses approval to spend user tokens for executing trades
- Controls critical settlement logic affecting multiple orders
- Steal fees by calling
ERC20.transfer(attacker, amount)from the settlement contract context - Drain user-approved tokens
- Manipulate settlement state
- Hooks cannot access settlement contract balances
- Hooks have no special approvals or permissions
- Settlement logic remains isolated and protected
Access Control: The onlySettlement Modifier
Implementation
onlySettlement modifier is applied to the execute() function:
Security Benefits
- Settlement-Only Execution: Only the authorized settlement contract can trigger hook execution
- No Direct Calls: Users or attackers cannot directly call the trampoline to execute arbitrary hooks
- Atomic Settlement Context: Hooks are guaranteed to execute as part of a settlement transaction
Testing Access Control
FromHooksTrampoline.t.sol:17-22:
NotASettlement error.
Protection Against Fee Theft
The trampoline architecture provides multiple layers of protection against fee theft:1. Unprivileged Execution Context
Hooks execute from the trampoline contract’s address, not the settlement contract:- The trampoline has no token balances or approvals
- Even if a hook calls
ERC20.transfer(), there are no funds to steal - The settlement contract’s state remains completely isolated
2. No Token Access
The trampoline contract:- Does not hold protocol fees
- Does not have approval to spend user tokens
- Cannot interact with the settlement contract’s balances
3. Call Isolation
The hook execution uses standard Solidity external calls:- Execute in the target contract’s context
- Have no special privileges
- Cannot escalate to settlement contract permissions
The trampoline effectively creates a security boundary between user-defined hooks and the privileged settlement contract, ensuring that hooks can only interact with contracts they explicitly target.
Verifying Settlement Context
Hook contracts can verify they’re being called within a legitimate settlement by checking the caller:Why This Matters
This pattern enables hooks to:- Enforce Settlement-Only Logic: Prevent direct calls outside of settlements
- Trust the Context: Know that execution is part of a CoW Protocol trade
- Implement Semi-Permissioned Behavior: Allow certain actions only during settlements
README.md:29-35:
In addition, the HooksTrampoline also only allows calls from the settlement contract. This means that hook implementations can add checks that ensure that they are only called from within a settlement.
Example: Settlement-Gated Hook
Security Guarantees Summary
| Security Property | How It’s Enforced |
|---|---|
| No Fee Theft | Hooks execute from unprivileged trampoline context with no token access |
| Settlement-Only Execution | onlySettlement modifier prevents unauthorized callers |
| Call Isolation | Standard external calls with no privilege escalation |
| Hook Verification | Hooks can check msg.sender == trampoline to verify settlement context |
| State Protection | Settlement contract state is completely isolated from hooks |
The trampoline’s security model is designed on the principle of least privilege: hooks receive only the minimum access necessary to execute their intended functionality, with no path to escalate to settlement contract privileges.
Additional Reading
- Gas Management — How gas limits protect against resource exhaustion
- Settlement Flow — Understanding the complete settlement lifecycle