@@ -545,7 +545,9 @@ class IntegerField(Field):
545
545
'invalid' : _ ('A valid integer is required.' ),
546
546
'max_value' : _ ('Ensure this value is less than or equal to {max_value}.' ),
547
547
'min_value' : _ ('Ensure this value is greater than or equal to {min_value}.' ),
548
+ 'max_string_length' : _ ('String value too large' )
548
549
}
550
+ MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
549
551
550
552
def __init__ (self , ** kwargs ):
551
553
max_value = kwargs .pop ('max_value' , None )
@@ -559,8 +561,11 @@ def __init__(self, **kwargs):
559
561
self .validators .append (MinValueValidator (min_value , message = message ))
560
562
561
563
def to_internal_value (self , data ):
564
+ if isinstance (data , six .text_type ) and len (data ) > self .MAX_STRING_LENGTH :
565
+ self .fail ('max_string_length' )
566
+
562
567
try :
563
- data = int (six . text_type ( data ) )
568
+ data = int (data )
564
569
except (ValueError , TypeError ):
565
570
self .fail ('invalid' )
566
571
return data
@@ -574,7 +579,9 @@ class FloatField(Field):
574
579
'invalid' : _ ("A valid number is required." ),
575
580
'max_value' : _ ('Ensure this value is less than or equal to {max_value}.' ),
576
581
'min_value' : _ ('Ensure this value is greater than or equal to {min_value}.' ),
582
+ 'max_string_length' : _ ('String value too large' )
577
583
}
584
+ MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
578
585
579
586
def __init__ (self , ** kwargs ):
580
587
max_value = kwargs .pop ('max_value' , None )
@@ -587,9 +594,12 @@ def __init__(self, **kwargs):
587
594
message = self .error_messages ['min_value' ].format (min_value = min_value )
588
595
self .validators .append (MinValueValidator (min_value , message = message ))
589
596
590
- def to_internal_value (self , value ):
597
+ def to_internal_value (self , data ):
598
+ if isinstance (data , six .text_type ) and len (data ) > self .MAX_STRING_LENGTH :
599
+ self .fail ('max_string_length' )
600
+
591
601
try :
592
- return float (value )
602
+ return float (data )
593
603
except (TypeError , ValueError ):
594
604
self .fail ('invalid' )
595
605
@@ -604,8 +614,10 @@ class DecimalField(Field):
604
614
'min_value' : _ ('Ensure this value is greater than or equal to {min_value}.' ),
605
615
'max_digits' : _ ('Ensure that there are no more than {max_digits} digits in total.' ),
606
616
'max_decimal_places' : _ ('Ensure that there are no more than {max_decimal_places} decimal places.' ),
607
- 'max_whole_digits' : _ ('Ensure that there are no more than {max_whole_digits} digits before the decimal point.' )
617
+ 'max_whole_digits' : _ ('Ensure that there are no more than {max_whole_digits} digits before the decimal point.' ),
618
+ 'max_string_length' : _ ('String value too large' )
608
619
}
620
+ MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
609
621
610
622
coerce_to_string = api_settings .COERCE_DECIMAL_TO_STRING
611
623
@@ -621,16 +633,19 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
621
633
message = self .error_messages ['min_value' ].format (min_value = min_value )
622
634
self .validators .append (MinValueValidator (min_value , message = message ))
623
635
624
- def to_internal_value (self , value ):
636
+ def to_internal_value (self , data ):
625
637
"""
626
638
Validates that the input is a decimal number. Returns a Decimal
627
639
instance. Returns None for empty values. Ensures that there are no more
628
640
than max_digits in the number, and no more than decimal_places digits
629
641
after the decimal point.
630
642
"""
631
- value = smart_text (value ).strip ()
643
+ data = smart_text (data ).strip ()
644
+ if len (data ) > self .MAX_STRING_LENGTH :
645
+ self .fail ('max_string_length' )
646
+
632
647
try :
633
- value = decimal .Decimal (value )
648
+ value = decimal .Decimal (data )
634
649
except decimal .DecimalException :
635
650
self .fail ('invalid' )
636
651
0 commit comments