fix: validate price data before BigInt operations in getFeeQuote#175
Conversation
|
WalkthroughAdded runtime validations in the fallback fee-calculation path: a module-local Changes
Sequence Diagram(s)sequenceDiagram
participant Caller as getFeeQuote caller
participant Estimate as estimate-fee.ts
participant Math as FixedPointArithmetic
Caller->>Estimate: request fallback fee quote
Note right of Estimate: obtain feeAssetPrice, tokenAssetPrice, feeDecimals, tokenDecimals
Estimate->>Estimate: validatePrice(feeAssetPrice.price, ...)
Estimate->>Estimate: validatePrice(tokenAssetPrice.price, ...)
Estimate->>Estimate: validateDecimals(feeDecimals, tokenDecimals, MAX_DECIMALS)
alt validations pass
Estimate->>Math: perform fixed-point BigInt arithmetic
Math-->>Estimate: computed fee
Estimate-->>Caller: return fee quote
else validation fails
Estimate-->>Caller: throw validation error
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ 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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/intents-sdk/src/lib/estimate-fee.ts(2 hunks)
🔇 Additional comments (1)
packages/intents-sdk/src/lib/estimate-fee.ts (1)
70-72: Excellent placement of validation calls.The validation is correctly positioned after the null checks and before the BigInt operations, preventing runtime errors early in the fallback path.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/intents-sdk/src/lib/estimate-fee.ts (2)
81-99: Validate decimals as integers before BigInt conversion.The assertions correctly enforce
MAX_DECIMALS = 24nand non-negative bounds, addressing PR objective #165. However, iffeeAssetPrice.decimalsortokenAssetPrice.decimalsis a float,NaN, orInfinity, theBigInt()constructor at lines 81-82 will throw aRangeErrorbefore your assertions run, resulting in a less informative error message.Apply this diff to validate decimals before conversion:
const tokenPriceScaled = BigInt( Math.round(tokenAssetPrice.price * USD_SCALE), ); + + assert( + Number.isInteger(feeAssetPrice.decimals) && feeAssetPrice.decimals >= 0, + `Fee asset decimals must be a non-negative integer, got: ${feeAssetPrice.decimals}`, + ); + assert( + Number.isInteger(tokenAssetPrice.decimals) && tokenAssetPrice.decimals >= 0, + `Token asset decimals must be a non-negative integer, got: ${tokenAssetPrice.decimals}`, + ); + const feeDecimals = BigInt(feeAssetPrice.decimals); const tokenDecimals = BigInt(tokenAssetPrice.decimals); const MAX_DECIMALS = 24n; assert( feeDecimals <= MAX_DECIMALS, `Fee asset decimals (${feeDecimals}) exceeds maximum allowed (${MAX_DECIMALS})`, ); assert( tokenDecimals <= MAX_DECIMALS, `Token asset decimals (${tokenDecimals}) exceeds maximum allowed (${MAX_DECIMALS})`, ); - - assert( - feeDecimals >= 0n && tokenDecimals >= 0n, - "Decimals cannot be negative", - );Note: The diff removes the redundant non-negative check at lines 96-99 since it's now handled before BigInt conversion with more specific error messages.
84-84: Informational: PR description mentions MAX_DECIMALS = 18n, but implementation correctly uses 24n.The PR description states
MAX_DECIMALS = 18n, but the implementation correctly uses24nto support NEAR's 24 decimals (as indicated in past review feedback). Consider updating the PR description to reflect the actual implementation.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/intents-sdk/src/lib/estimate-fee.ts(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: semgrep-cloud-platform/scan
- GitHub Check: semgrep-cloud-platform/scan
🔇 Additional comments (3)
packages/intents-sdk/src/lib/estimate-fee.ts (3)
2-2: LGTM! Usingassertfor validation aligns with past feedback.The import of
assertfrom internal-utils is correct and addresses the previous review feedback to useassert()for validation errors.
75-76: LGTM! Price validation correctly placed before BigInt operations.The calls to
validatePriceensure both prices are validated before scaling and BigInt conversion (lines 77-80), preventing the division-by-zero and invalid BigInt conversion issues described in PR objective #166.
150-160: LGTM! Price validation logic is correct and addresses all past feedback.The
validatePricehelper correctly:
- Enforces finite, positive prices
- Uses
1/USD_SCALEfor the minimum threshold (as suggested in past feedback), ensuringMath.round(price * USD_SCALE) >= 1and preventing division by zero- Uses
assert(as suggested in past feedback) for consistent error handlingThis addresses all requirements from PR objective #166.
Closes #166
Closes #165
Adds validation for price data in the
getFeeQuotefallback calculation to preventruntime errors from invalid external API responses.
Vulnerability Details
The
getFeeQuotefunction insrc/lib/estimate-fee.ts(lines 73-83) performs BigIntarithmetic using price data from
tokensUsdPricesHttpClientwithout validation:Invalid price values can cause:
Solution
Implement price validation before BigInt operations:
As well as validate decimals