1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reject signed hexadecimal numbers while validating numericality

This commit is contained in:
nimish 2020-04-22 12:15:57 +00:00
parent cf1abb775e
commit b3c308dd20
2 changed files with 4 additions and 2 deletions

View file

@ -13,6 +13,8 @@ module ActiveModel
INTEGER_REGEX = /\A[+-]?\d+\z/ INTEGER_REGEX = /\A[+-]?\d+\z/
HEXADECIMAL_REGEX = /\A[+-]?0[xX]/
def check_validity! def check_validity!
keys = CHECKS.keys - [:odd, :even] keys = CHECKS.keys - [:odd, :even]
options.slice(*keys).each do |option, value| options.slice(*keys).each do |option, value|
@ -106,7 +108,7 @@ module ActiveModel
end end
def is_hexadecimal_literal?(raw_value) def is_hexadecimal_literal?(raw_value)
/\A0[xX]/.match?(raw_value.to_s) HEXADECIMAL_REGEX.match?(raw_value.to_s)
end end
def filtered_options(value) def filtered_options(value)

View file

@ -21,7 +21,7 @@ class NumericalityValidationTest < ActiveModel::TestCase
FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
INTEGERS = [0, 10, -10] + INTEGER_STRINGS INTEGERS = [0, 10, -10] + INTEGER_STRINGS
BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal(bd) } BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal(bd) }
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"] JUNK = ["not a number", "42 not a number", "0xdeadbeef", "-0xdeadbeef", "+0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
INFINITY = [1.0 / 0.0] INFINITY = [1.0 / 0.0]
def test_default_validates_numericality_of def test_default_validates_numericality_of