mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Fixes an issue when decoding a json string which looks like a date but is invalid. This DateTime parse error is now caught and the original string is instead passed back [#6286 state:resolved]
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
This commit is contained in:
committed by
Santiago Pastorino
parent
f23bf7dbdb
commit
b17d8d727f
@@ -26,7 +26,11 @@ module ActiveSupport
|
||||
when nil
|
||||
nil
|
||||
when DATE_REGEX
|
||||
DateTime.parse(data)
|
||||
begin
|
||||
DateTime.parse(data)
|
||||
rescue ArgumentError
|
||||
data
|
||||
end
|
||||
when Array
|
||||
data.map! { |d| convert_dates_from(d) }
|
||||
when Hash
|
||||
|
||||
@@ -23,7 +23,11 @@ module ActiveSupport
|
||||
when nil
|
||||
nil
|
||||
when DATE_REGEX
|
||||
DateTime.parse(data)
|
||||
begin
|
||||
DateTime.parse(data)
|
||||
rescue ArgumentError
|
||||
data
|
||||
end
|
||||
when Array
|
||||
data.map! { |d| convert_dates_from(d) }
|
||||
when Hash
|
||||
|
||||
@@ -36,7 +36,7 @@ module ActiveSupport
|
||||
quoting = char
|
||||
pos = scanner.pos
|
||||
elsif quoting == char
|
||||
if json[pos..scanner.pos-2] =~ DATE_REGEX
|
||||
if valid_date?(json[pos..scanner.pos-2])
|
||||
# found a date, track the exact positions of the quotes so we can
|
||||
# overwrite them with spaces later.
|
||||
times << pos
|
||||
@@ -94,6 +94,16 @@ module ActiveSupport
|
||||
output
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def valid_date?(date_string)
|
||||
begin
|
||||
date_string =~ DATE_REGEX && DateTime.parse(date_string)
|
||||
rescue ArgumentError
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -19,6 +19,8 @@ class TestJSONDecoding < ActiveSupport::TestCase
|
||||
%({"a": "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
|
||||
# no time zone
|
||||
%({"a": "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
|
||||
# invalid date
|
||||
%({"a": "1089-10-40"}) => {'a' => "1089-10-40"},
|
||||
# needs to be *exact*
|
||||
%({"a": " 2007-01-01 01:12:34 Z "}) => {'a' => " 2007-01-01 01:12:34 Z "},
|
||||
%({"a": "2007-01-01 : it's your birthday"}) => {'a' => "2007-01-01 : it's your birthday"},
|
||||
|
||||
Reference in New Issue
Block a user