fix: add wall-clock time gate to H1 deviation-acceptance to block rapid dual-source manipulation - #363
Conversation
…id dual-source manipulation The H1 bypass (DEVIATION_ACCEPT_AFTER=5) was count-only. An attacker who could move a thin liquidity pool that both DexScreener and Jupiter index could push a >30% deviated price: cross-source validation passes (both feeds agree within 10%), then after 5 * ORACLE_RATE_LIMIT_MS (~25s) the count gate fires and the manipulated price is pushed on-chain. Add a second gate: the first consecutive rejection for a market must be at least ORACLE_DEVIATION_ACCEPT_AFTER_MS old (default 5 minutes) before H1 fires. A sustained legitimate move (>30% over 5+ minutes) still accepts; a 25-second coordinated manipulation does not. The time threshold is configurable via ORACLE_DEVIATION_ACCEPT_AFTER_MS for markets with high expected volatility. Operators should set a value appropriate to the expected natural price velocity of each market.
|
Warning Review limit reached
More reviews will be available in 22 minutes and 26 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please see our Fair Usage Limits Policy for further information. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Independent verification — not an approval (QA/Security own that). Blocking finding: this PR turns two existing tests red, and they haven't been updated. The fix itself looks right to me — I'll get to that — but CI would go red on merge. The breakageBoth failures are the existing H1 acceptance tests:
Both drive 5 consecutive rejections and assert the price is finally accepted. With the new That's the new guard working as designed — but it means these two tests encode the old count-only contract and need updating rather than the change being wrong. They're worth updating, not deletingThe first one guards a real property: H1 must eventually accept, or a sustained legitimate move bricks the market permanently. That property still holds under this PR — it just now requires elapsed time as well as count. Deleting the test would drop a genuine safety guarantee; updating it keeps it and pins the new semantics. This should be cheap: the clock is already injectable — On the fix itselfReads well, and three details I'd call out as good:
One thing I could not verifyThere is no test for the new gate itself. Once the two above are updated, the natural companion is: 5 rejections + clock advanced less than the threshold → still rejected; advanced past it → accepted. Without that, a later refactor could drop the time gate and the suite would stay green — the count-only behaviour this PR exists to remove. Given the injectable clock, that's a handful of lines in the same file. |
Closes #362
What
Add a time gate to
oracle.tsso that a deviated price requires bothDEVIATION_ACCEPT_AFTERconsecutive rejections AND at leastORACLE_DEVIATION_ACCEPT_AFTER_MS(default 5 minutes) of elapsed wall-clock time before acceptance.Why
The count-only gate allowed a deviated price to be accepted after 5 *
ORACLE_RATE_LIMIT_MS(~25 s). An attacker who moves a pool indexed by both DexScreener and Jupiter satisfies cross-source validation (both feeds agree on the manipulated price) and then triggers the count gate after 25 s.A 5-minute time gate makes a sustained oracle drift distinguishable from a short-term manipulation. The count gate is kept to avoid accepting a fast-moving real price after a single cycle.
Changes
src/services/oracle.ts:deviationFirstRejectedAt: Map<string, number>to track when each slab's current rejection streak started.DEVIATION_ACCEPT_AFTER_MSstatic (default5 * 60 * 1000, overridable viaORACLE_DEVIATION_ACCEPT_AFTER_MSenv var).consecutiveCount === 1), recordnow()in the map.countGateandtimeGatemust be true before the price is accepted.deviationFirstRejectedAtentry when a price is accepted.