feat: add ExpMaxLimit and ExpMinLimit to prevent OOM / DoS via huge exponents#427
Open
singhlovepreet9 wants to merge 1 commit into
Open
feat: add ExpMaxLimit and ExpMinLimit to prevent OOM / DoS via huge exponents#427singhlovepreet9 wants to merge 1 commit into
singhlovepreet9 wants to merge 1 commit into
Conversation
singhlovepreet9
marked this pull request as ready for review
July 12, 2026 12:14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces global
ExpMaxLimitandExpMinLimitvariables (defaulting tomath.MaxInt32andmath.MinInt32to preserve backward compatibility).The Vulnerability
Currently,
shopspring/decimalis vulnerable to Denial of Service (DoS) attacks via memory exhaustion and CPU starvation. If an attacker submits a JSON payload containing an extremely large scientific exponent (e.g.,{"value": "1E2000000"}),NewFromStringparses the exponent successfully into itsint32representation.While parsing is fast, any subsequent operation that relies on expanding or comparing this value (e.g.,$\approx 3.32$ billion bits, instantly consuming ~415 MB of RAM. A web server receiving just a few concurrent requests of this nature will rapidly exhaust memory and crash.
.String(),.Add(),.Cmp()) forces the underlyingmath/big.Intto allocate huge amounts of memory and stall the CPU.For example, parsing
"1E1000000000"(an exponent of 1 billion) causes the allocation of a singlebig.IntcontainingThe Fix
This adds two exported global limits,
decimal.ExpMaxLimitanddecimal.ExpMinLimit, checking them insideNewFromString.Users running web services can now opt-in to bounded exponent parsing (e.g.,
decimal.ExpMaxLimit = 100000) globally on initialization, preventing OOM panics at the unmarshaling boundary without requiring wrapper structs.