fix(prices): sanitize price values from oracle_prices and markets_with_stats#152
fix(prices): sanitize price values from oracle_prices and markets_with_stats#1526figpsolseeker wants to merge 1 commit intodcccrypto:mainfrom
Conversation
…h_stats Both /prices/markets and /prices/:slab returned raw rows from Supabase without validating numeric price fields. A bad row in the upstream indexer (negative, NaN, Infinity, or absurdly large) would propagate straight to the chart consumers, where lightweight-charts silently fails on a single corrupt point in an ordered series. This mirrors the sanitization already applied in src/routes/markets.ts (Number.isFinite + > 0 + <= 1_000_000_000 USD bound), with a parallel 1e15 bound for price_e6 (microUSD). Two strategies, depending on consumer expectations: - /prices/markets returns one record per market — bad fields are nulled so the rest of the row stays intact. - /prices/:slab feeds an ordered chart series — bad rows are dropped entirely so consumers never see a hole or a NaN. Also updates two pre-existing stale assertions in tests/routes/prices.test.ts that asserted ascending: false / limit(100) — the source has shipped ascending: true / limit(1500) for some time and the comment at prices.ts:30-37 explains why. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughAdded price validation bounds and sanitization logic to the prices endpoints. The Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/routes/prices.ts (1)
10-19: Centralize price-sanitization rules in a shared utility.This duplicates rules already documented as mirrored from
src/routes/markets.ts. Extracting bounds + validators into a shared helper will prevent rule drift between endpoints.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/routes/prices.ts` around lines 10 - 19, Extract MAX_SANE_PRICE_USD, MAX_SANE_PRICE_E6 and sanitizeUsdPrice into a shared utility module (e.g., a new exported helper) and import it from both places so the bounds and validator are single-source; move the constant definitions and the predicate logic into that helper, add a corresponding sanitizePriceE6 (or exported constant) for microUSD usage, update references in sanitizeUsdPrice and any callers in routes/prices.ts and routes/markets.ts to import the helper, and ensure exports are typed and tested so both endpoints use the same centralized rules.tests/routes/prices.test.ts (1)
107-108: Clean up contradictory inline fixture comment.The “wait this is > 1e9” note reads like leftover debugging text and makes the test intent harder to parse.
Suggested cleanup
- index_price: 50000000000, // valid (clamped under 1e9)... wait this is > 1e9 + index_price: 50000000000, // > 1e9 bound → null🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/routes/prices.test.ts` around lines 107 - 108, The inline fixture comment next to the index_price value is contradictory and looks like leftover debugging text; update the test fixture by either removing the phrase "wait this is > 1e9" from the comment or make the value and comment consistent (e.g., set index_price <= 1e9 if you mean "clamped under 1e9" or change the comment to reflect that index_price is intentionally > 1e9); target the test object containing the index_price and updated_at fields in the prices test and ensure the comment accurately describes the fixture.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/routes/prices.ts`:
- Around line 10-19: Extract MAX_SANE_PRICE_USD, MAX_SANE_PRICE_E6 and
sanitizeUsdPrice into a shared utility module (e.g., a new exported helper) and
import it from both places so the bounds and validator are single-source; move
the constant definitions and the predicate logic into that helper, add a
corresponding sanitizePriceE6 (or exported constant) for microUSD usage, update
references in sanitizeUsdPrice and any callers in routes/prices.ts and
routes/markets.ts to import the helper, and ensure exports are typed and tested
so both endpoints use the same centralized rules.
In `@tests/routes/prices.test.ts`:
- Around line 107-108: The inline fixture comment next to the index_price value
is contradictory and looks like leftover debugging text; update the test fixture
by either removing the phrase "wait this is > 1e9" from the comment or make the
value and comment consistent (e.g., set index_price <= 1e9 if you mean "clamped
under 1e9" or change the comment to reflect that index_price is intentionally >
1e9); target the test object containing the index_price and updated_at fields in
the prices test and ensure the comment accurately describes the fixture.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b2c56d52-96bc-4552-ba2c-e3bc4e839dd6
📒 Files selected for processing (2)
src/routes/prices.tstests/routes/prices.test.ts
Summary
Out-of-band test cleanup
This PR also updates two stale assertions in `tests/routes/prices.test.ts` that asserted `ascending: false` / `limit(100)`. The source has shipped `ascending: true` / `limit(1500)` for some time (the comment at `prices.ts:30-37` explains why). These assertions were the lone failing test on `main`. Touching them inline since I'm already editing this test file; happy to split out if preferred.
Test plan
Summary by CodeRabbit