$npx -y skills add quillai-network/quillshield_skills --skill oracle-flashloan-analysisDetects price oracle manipulation and flash loan attack vectors in DeFi smart contracts. Classifies oracle trust models (Chainlink, TWAP, spot price, custom), identifies stale price risks, circular price dependencies, and flash loan atomicity exploitation patterns. Use when audit
| 1 | # Oracle & Flash Loan Analysis |
| 2 | |
| 3 | Detect vulnerabilities where **external price data can be manipulated** or **flash loans can exploit protocol logic** within a single transaction. These two attack vectors are often combined and represent the most common DeFi attack pattern. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - Auditing any DeFi protocol that reads external price data (lending, DEX, derivatives, yield aggregators) |
| 8 | - Reviewing Chainlink, Uniswap TWAP, Band Protocol, or custom oracle integrations |
| 9 | - Analyzing protocols that interact with or are accessible via flash loans |
| 10 | - Threat modeling for MEV, sandwich attacks, and price manipulation |
| 11 | - When a protocol uses `balanceOf()`, pool reserves, or spot prices for critical calculations |
| 12 | |
| 13 | ## When NOT to Use |
| 14 | |
| 15 | - Contracts with no price dependencies or external data feeds |
| 16 | - Pure access control analysis (use semantic-guard-analysis) |
| 17 | - State-to-state invariant checking (use state-invariant-detection) |
| 18 | |
| 19 | ## Core Concept: The Oracle Trust Hierarchy |
| 20 | |
| 21 | Not all price sources are equally secure. Oracle vulnerabilities stem from the gap between **assumed trust** and **actual manipulation resistance**. |
| 22 | |
| 23 | ``` |
| 24 | Trust Level (highest to lowest): |
| 25 | ┌─────────────────────────────────────────────┐ |
| 26 | │ Level 5: Multi-oracle consensus + circuit │ |
| 27 | │ breakers + TWAP + staleness checks │ |
| 28 | ├─────────────────────────────────────────────┤ |
| 29 | │ Level 4: Chainlink with full validation │ |
| 30 | │ (staleness, sequencer, min answers) │ |
| 31 | ├─────────────────────────────────────────────┤ |
| 32 | │ Level 3: Uniswap V3 TWAP (long window) │ |
| 33 | │ Multi-block manipulation cost │ |
| 34 | ├─────────────────────────────────────────────┤ |
| 35 | │ Level 2: Uniswap V2 TWAP (short window) │ |
| 36 | │ or Chainlink WITHOUT staleness check │ |
| 37 | ├─────────────────────────────────────────────┤ |
| 38 | │ Level 1: Spot price from single pool │ ← Manipulable via flash loan |
| 39 | │ or balanceOf() for pricing │ |
| 40 | └─────────────────────────────────────────────┘ |
| 41 | ``` |
| 42 | |
| 43 | ## The Four-Phase Detection Architecture |
| 44 | |
| 45 | ### Phase 1: Oracle Source Identification |
| 46 | |
| 47 | Locate every point where the contract reads external price/value data. |
| 48 | |
| 49 | **Search for these patterns:** |
| 50 | |
| 51 | | Pattern | Oracle Type | Risk Level | |
| 52 | |---------|------------|------------| |
| 53 | | `latestRoundData()` | Chainlink | Medium (depends on validation) | |
| 54 | | `latestAnswer()` | Chainlink (deprecated) | HIGH (no round validation) | |
| 55 | | `observe()` / `consult()` | Uniswap TWAP | Medium (depends on window) | |
| 56 | | `getReserves()` | AMM spot price | **CRITICAL** (flash-loan manipulable) | |
| 57 | | `balanceOf(address(this))` | Self-balance | **CRITICAL** (donation attack) | |
| 58 | | `slot0()` / `sqrtPriceX96` | Uniswap V3 spot | **CRITICAL** (single-block manipulable) | |
| 59 | | Custom `getPrice()` | Unknown | Requires investigation | |
| 60 | |
| 61 | **Build an Oracle Dependency Map:** |
| 62 | |
| 63 | ``` |
| 64 | Contract: LendingPool |
| 65 | ├── borrowLimit() → uses getCollateralPrice() |
| 66 | │ └── getCollateralPrice() → calls chainlinkOracle.latestRoundData() |
| 67 | ├── liquidate() → uses getDebtPrice() |
| 68 | │ └── getDebtPrice() → calls uniswapPool.slot0() ← SPOT PRICE! |
| 69 | └── calculateInterest() → uses getUtilizationRate() |
| 70 | └── getUtilizationRate() → reads internal state (safe) |
| 71 | ``` |
| 72 | |
| 73 | ### Phase 2: Oracle Validation Verification |
| 74 | |
| 75 | For each oracle source, verify that proper safety checks are in place. |
| 76 | |
| 77 | **Chainlink Validation Checklist:** |
| 78 | |
| 79 | ```solidity |
| 80 | // COMPLETE Chainlink integration |
| 81 | (uint80 roundId, int256 price, , uint256 updatedAt, uint80 answeredInRound) = |
| 82 | priceFeed.latestRoundData(); |
| 83 | |
| 84 | require(price > 0, "Invalid price"); // Check 1: Non-negative |
| 85 | require(updatedAt > 0, "Round not complete"); // Check 2: Round complete |
| 86 | require(answeredInRound >= roundId, "Stale price"); // Check 3: Not stale |
| 87 | require(block.timestamp - updatedAt < HEARTBEAT, // Check 4: Fresh |
| 88 | "Price too old"); |
| 89 | |
| 90 | // L2-specific |
| 91 | require(!sequencerFeed.isDown(), "Sequencer down"); // Check 5: L2 sequencer |
| 92 | require(block.timestamp - sequencerUptime > GRACE, // Check 6: Grace period |
| 93 | "Grace period"); |
| 94 | ``` |
| 95 | |
| 96 | **Missing Check Severity:** |
| 97 | |
| 98 | | Missing Check | Severity | Impact | |
| 99 | |---------------|----------|--------| |
| 100 | | `price > 0` | HIGH | Zero/negative price → infinite borrowing or free liquidations | |
| 101 | | `updatedAt > 0` | MEDIUM | Incomplete round data used | |
| 102 | | `answeredInRound >= roundId` | HIGH | Stale price from previous round | |
| 103 | | Heartbeat/freshness | HIGH | Hours-old price during volatile markets | |
| 104 | | L2 sequencer check | HIGH | Stale price dur |