Opened 3 weeks ago

Last modified 2 weeks ago

#37464 assigned Bug

StepValueValidator accepts invalid values and crashes on large numbers

Reported by: Akshat Gupta Owned by: Akshat Gupta
Component: Forms Version: dev
Severity: Low Keywords: StepValueValidator, step_size, float, OverflowError
Cc: Akshat Gupta Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

StepValueValidator uses math.remainder(). That converts values to float. Large integers and Decimals lose precision or crash.

from decimal import Decimal
from django import forms

forms.IntegerField(step_size=2).clean("9007199254740993")
# Returns 9007199254740993. It is odd, so it should be rejected.

forms.IntegerField(step_size=2).clean("9" * 400)
# OverflowError: int too large to convert to float

forms.DecimalField(step_size=Decimal("0.1")).clean("1e400")
# ValueError: math domain error

forms.DecimalField(step_size=Decimal("0.01")).clean("12345678901234567.01")
# ValidationError, but the value is a valid multiple of 0.01

The first case lets an invalid value through. The next two raise an unhandled exception, so user input can cause a 500 error.

Floats are also used for the tolerance (abs_tol=1e-9). So Decimal("1.00000000005") is accepted with a step of Decimal("1e-10").

I think int and Decimal values should be checked with exact arithmetic. The float path can stay as it is. Be careful not to turn the Decimal into a huge number: Fraction(Decimal("1e50000000")) takes about a minute.

I am happy to work on a patch once this is accepted.

Change History (4)

comment:1 by rajat4665, 2 weeks ago

Reproduced on main (a05e16a723) through the form fields, not just the validator:

  • forms.IntegerField(step_size=2) with a 401-digit input raises OverflowError: int too large to convert to float (500).
  • forms.DecimalField(step_size=Decimal("0.1")) with "1e400" raises ValueError: math domain error (500).
  • 9007199254740993 passes step_size=2, and Decimal("0.1000000000001") passes step_size=Decimal("0.1").
from decimal import Decimal
from django import forms

class F(forms.Form):
    n = forms.IntegerField(step_size=2, required=False)
    d = forms.DecimalField(step_size=Decimal("0.1"), required=False)

F({"n": "1" + "0" * 400}).is_valid()  # OverflowError
F({"d": "1e400"}).is_valid()          # ValueError
F({"n": "9007199254740993"}).is_valid()  # True, should be False

The float conversion in StepValueValidator.compare() (math.remainder() plus abs_tol=1e-9) has been there since step_size was added in #32559, so this affects 4.1+. Accepting.

(I used Claude to help write the reproduction script and verified the results locally.)

comment:2 by Sarah Boyce, 2 weeks ago

Owner: set to Akshat Gupta
Severity: Normal → Low
Status: new → assigned
Triage Stage: Unreviewed → Accepted

Thank you for the report! Happy for you to provide a patch

comment:3 by Akshat Gupta, 2 weeks ago

Has patch: set
Note: See TracTickets for help on using tickets.
Back to Top