T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/add-liquidity.mjs:46
- Finding
- Liquidity Transactions Use Zero Slippage Protection and Excessive Token Allowances<![CDATA[ ## Vulnerability Details **File Location**: `scripts/add-liquidity.mjs:46-80` **Vulnerability Type**: Excessive ERC-20 allowance and missing minimum-output protection **Risk Level**: High ### Vulnerable Code ```js if (allow0 < amountWei) { console.log("Approving currency0..."); const tx = await walletClient.writeContract({ address: CONTRACTS.currency0, abi: ERC20_ABI, functionName: "approve", args: [CONTRACTS.hook, amountWei * 10n], }); await publicClient.waitForTransactionReceipt({ hash: tx }); } if (allow1 < amountWei) { console.log("Approving currency1..."); const tx = await walletClient.writeContract({ address: CONTRACTS.currency1, abi: ERC20_ABI, functionName: "approve", args: [CONTRACTS.hook, amountWei * 10n], }); await publicClient.waitForTransactionReceipt({ hash: tx }); } // Salt: bit 0 = autoRebalance const salt = autoRebalance ? "0x0000000000000000000000000000000000000000000000000000000000000001" : "0x0000000000000000000000000000000000000000000000000000000000000000"; const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600); console.log("Sending addLiquidity transaction..."); const txHash = await walletClient.writeContract({ address: CONTRACTS.hook, abi: HOOK_ABI, functionName: "addLiquidity", args: [{ amount0Desired: amountWei, amount1Desired: amountWei, amount0Min: 0n, amount1Min: 0n, deadline, tickLower, tickUpper, userInputSalt: salt, }], }); ``` ### Technical Analysis The script grants the hook an allowance equal to ten times the amount required for the current operation. ERC-20 allowances generally remain active until consumed or explicitly changed, so the unused portion persists after liquidity is added. The transaction also sets `amount0Min` and `amount1Min` to zero. These fields are intended to define the least favorable execution the user is willing to accept. Setting both to zero removes client-side protection against an unexpected token ratio, ad ...[truncated 1681 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Approve only the exact amount required for the current transaction: ```js args: [CONTRACTS.hook, amountWei] ``` 2. Reset or revoke any unused allowance after the operation completes. 3. Consider an approve-to-zero transition before replacing an existing nonzero allowance for compatibility with nonstandard ERC-20 implementations. 4. Obtain a user-selected slippage tolerance and calculate nonzero `amount0Min` and `amount1Min` values from current pool state. 5. Simulate the complete `addLiquidity` call immediately before signing and display the expected token amounts to the user. 6. Shorten the deadline and require explicit user confirmation of the amount, token addresses, hook address, tick range, allowance, minimum amounts, and network. 7. Verify the deployed hook bytecode and administrative controls against reviewed source before authorizing it to transfer tokens. ]]>
