fix: limit decimal precision in ParsePercentageRatio to 18 places#129
fix: limit decimal precision in ParsePercentageRatio to 18 places#129
Conversation
Fuzz testing discovered that ParsePercentageRatio accepted unreasonably large decimal precision (100+ digits), which could cause issues downstream. Added validation to limit decimal places to 18, which is sufficient for financial calculations and matches common cryptocurrency precision standards. Also added a fuzz test to prevent regression of this issue. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
WalkthroughThe changes add decimal precision validation to 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 docstrings
🧪 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 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #129 +/- ##
==========================================
+ Coverage 68.50% 68.55% +0.05%
==========================================
Files 46 46
Lines 4648 4650 +2
==========================================
+ Hits 3184 3188 +4
+ Misses 1290 1289 -1
+ Partials 174 173 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/parser/percentage_fuzz_test.go (1)
33-43: Add an explicit assertion for the “>18 decimals must error” rule.Right now the fuzz test only checks invariants on success paths. It can miss regressions where over-precise inputs are silently accepted but normalized.
💡 Suggested hardening
package parser import ( + "strings" "testing" ) @@ f.Fuzz(func(t *testing.T, input string) { // Call ParsePercentageRatio and ensure it doesn't panic num, floatingDigits, err := ParsePercentageRatio(input) + trimmed := strings.TrimSuffix(input, "%") + if i := strings.Index(trimmed, "."); i != -1 { + scale := len(trimmed) - i - 1 + if scale > 18 && err == nil { + t.Fatalf("expected error for scale=%d (>18), input: %q", scale, input) + } + } + if err == nil { // If parsing succeeded, verify the result is reasonable if num == nil { t.Errorf("ParsePercentageRatio succeeded but returned nil num for input: %q", input) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@internal/parser/percentage_fuzz_test.go` around lines 33 - 43, The fuzz test in percentage_fuzz_test.go must assert that inputs with more than 18 fractional digits are rejected: when calling ParsePercentageRatio, compute floatingDigits as currently done and if floatingDigits > 18 then require err != nil (fail the test if err == nil), rather than only checking the success-path invariants; update the test around the ParsePercentageRatio call to explicitly assert this error case so the parser cannot silently accept or normalize >18-decimal inputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@internal/parser/percentage_fuzz_test.go`:
- Around line 33-43: The fuzz test in percentage_fuzz_test.go must assert that
inputs with more than 18 fractional digits are rejected: when calling
ParsePercentageRatio, compute floatingDigits as currently done and if
floatingDigits > 18 then require err != nil (fail the test if err == nil),
rather than only checking the success-path invariants; update the test around
the ParsePercentageRatio call to explicitly assert this error case so the parser
cannot silently accept or normalize >18-decimal inputs.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9508ce7e-5e56-4d70-9b9e-d6f214281315
📒 Files selected for processing (3)
internal/parser/parser.gointernal/parser/percentage_fuzz_test.gointernal/parser/testdata/fuzz/FuzzParsePercentageRatio/98e3857fbdb2cc2c
Summary
ParsePercentageRatioaccepted unreasonably large decimal precision (100+ digits)Bug Details
The fuzzer discovered that inputs like
"0.00000...000%"with 100+ decimal digits were accepted and returnedfloatingDigits > 100. This could potentially cause:Fix
Added a check in
ParsePercentageRatioto reject percentages with more than 18 decimal places with a clear error message.Test plan
🤖 Generated with Claude Code