Skip to content

Commit b3c308d

Browse files
committed
Reject signed hexadecimal numbers while validating numericality
1 parent cf1abb7 commit b3c308d

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

activemodel/lib/active_model/validations/numericality.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class NumericalityValidator < EachValidator # :nodoc:
1313

1414
INTEGER_REGEX = /\A[+-]?\d+\z/
1515

16+
HEXADECIMAL_REGEX = /\A[+-]?0[xX]/
17+
1618
def check_validity!
1719
keys = CHECKS.keys - [:odd, :even]
1820
options.slice(*keys).each do |option, value|
@@ -106,7 +108,7 @@ def is_integer?(raw_value)
106108
end
107109

108110
def is_hexadecimal_literal?(raw_value)
109-
/\A0[xX]/.match?(raw_value.to_s)
111+
HEXADECIMAL_REGEX.match?(raw_value.to_s)
110112
end
111113

112114
def filtered_options(value)

activemodel/test/cases/validations/numericality_validation_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def teardown
2121
FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
2222
INTEGERS = [0, 10, -10] + INTEGER_STRINGS
2323
BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal(bd) }
24-
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"]
24+
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"]
2525
INFINITY = [1.0 / 0.0]
2626

2727
def test_default_validates_numericality_of

0 commit comments

Comments
 (0)