Skip to content

Fix DecimalField crash on long numbers when max_digits is None - #10123

Closed
mohamed-alired wants to merge 1 commit into
encode:mainfrom
mohamed-alired:fix-decimalfield-unbounded-precision
Closed

mohamed-alired wants to merge 1 commit into
encode:mainfrom
mohamed-alired:fix-decimalfield-unbounded-precision

Conversation

@mohamed-alired

Copy link
Copy Markdown
Contributor

Description

DecimalField.quantize() only sets the decimal context precision when max_digits is given. With max_digits=None (documented as allowed, and covered by TestNoMaxDigitsDecimalField) it keeps the default context precision of 28, so Decimal.quantize() raises decimal.InvalidOperation for any value that needs more digits once quantized. The exception is not caught, so the request fails with a 500:

class S(serializers.Serializer):
    amount = serializers.DecimalField(max_digits=None, decimal_places=2)

S(data={'amount': '1' * 27}).is_valid()  # also '1e30', 10 ** 30
# decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

to_representation() has the same problem for such values.

This change:

  • gives the context enough precision for the quantized value when max_digits is None, bounded by MAX_STRING_LENGTH like the input string itself, so a short input with a huge exponent (e.g. '1e999999999') cannot make the server build an enormous number;
  • reports values beyond that bound as a validation error (max_string_length) instead of a 500.

Behaviour with max_digits set is unchanged.

Tests: TestNoMaxDigitsLargeDecimalField covers 27-digit and 1e30 inputs, an integer input, an oversized exponent and a 30-digit output. It fails on main with InvalidOperation and passes with this change.

quantize() only set the decimal context precision when max_digits was
given, so with max_digits=None it kept the default precision of 28 and
Decimal.quantize() raised InvalidOperation for any value needing more
digits (e.g. '1' * 27 with decimal_places=2, or '1e30'): a 500 instead
of a valid value.

Give the context enough precision for the quantized value, bounded by
MAX_STRING_LENGTH like the input itself, and report values beyond that
bound (e.g. '1e1000') as a validation error.

@auvipy auvipy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which issue you are trying to fix?

@ege-arhan ege-arhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_internal_value() now turns the oversized value into a validation error, but to_representation() still raises for the same values, so the "to_representation() has the same problem" part of the description isn't covered by the diff.

from decimal import Decimal
from types import SimpleNamespace
from rest_framework import serializers

class S(serializers.Serializer):
    amount = serializers.DecimalField(max_digits=None, decimal_places=2)

S(instance=SimpleNamespace(amount=Decimal("1e999"))).data
# decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

Worst case with this branch: any value with value.adjusted() >= 998 reaches quantize() (fields.py:1150-1151), where digits is clamped to MAX_STRING_LENGTH and the quantize no longer has room. A Decimal that big can come out of Postgres numeric (max around 1e131072), so it gets here from a model instance without passing through this serializer's input at all — that was the 500 in the description.

On the input side, S(data={"amount": "1e999"}).is_valid() now reports String value too large. for a 5-character string, which sends people looking at the wrong limit (invalid, or a message that mentions the digit bound, would read better).

I'd catch InvalidOperation in to_representation() the same way, or drop the MAX_STRING_LENGTH clamp in quantize() and use the digits the quantized result needs. The clamp only makes sense for the input string, and the string is already length-checked at fields.py:1055.

Checked with pytest tests/test_fields.py -k Decimal on 2be5057: 39 passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants