Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,26 @@ def __init__(self, locale_time=None):
self.locale_time = LocaleTime()
base = super()
base.__init__({
# The " \d" part of the regex is to make %c from ANSI C work
'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
# The " [1-9]" part of the regex is to make %c from ANSI C work
'd': r"(?P<d>3[0-1]|[1-2][0-9]|0[1-9]|[1-9]| [1-9])",
'f': r"(?P<f>[0-9]{1,6})",
'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
'H': r"(?P<H>2[0-3]|[0-1][0-9]|[0-9])",
'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
'G': r"(?P<G>\d\d\d\d)",
'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
'G': r"(?P<G>[0-9][0-9][0-9][0-9])",
'j': r"(?P<j>36[0-6]|3[0-5][0-9]|[1-2][0-9][0-9]|0[1-9][0-9]|00[1-9]|[1-9][0-9]|0[1-9]|[1-9])",
'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
'M': r"(?P<M>[0-5]\d|\d)",
'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
'M': r"(?P<M>[0-5][0-9]|[0-9])",
'S': r"(?P<S>6[0-1]|[0-5][0-9]|[0-9])",
'U': r"(?P<U>5[0-3]|[0-4][0-9]|[0-9])",
'w': r"(?P<w>[0-6])",
'u': r"(?P<u>[1-7])",
'V': r"(?P<V>5[0-3]|0[1-9]|[1-4]\d|\d)",
'V': r"(?P<V>5[0-3]|0[1-9]|[1-4][0-9]|[0-9])",
# W is set below by using 'U'
'y': r"(?P<y>\d\d)",
'y': r"(?P<y>[0-9][0-9])",
#XXX: Does 'Y' need to worry about having less or more than
# 4 digits?
'Y': r"(?P<Y>\d\d\d\d)",
'z': r"(?P<z>[+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?|Z)",
'Y': r"(?P<Y>[0-9][0-9][0-9][0-9])",
'z': r"(?P<z>[+-][0-9][0-9]:?[0-5][0-9](:?[0-5][0-9](\.[0-9]{1,6})?)?|Z)",
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'),
Expand Down
33 changes: 33 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,39 @@ def test_strptime_single_digit(self):
newdate = strptime(string, format)
self.assertEqual(newdate, target, msg=reason)

def test_rejecting_non_ascii_digits(self):
# bpo-39280: Don't allow datetime parsing to accept non-Ascii digits
eastern_arabic_digits = [chr(1632 + i) for i in range(10)]
full_width_digits = [chr(65296 + i) for i in range(10)]

# Making sure we're using legit Unicode digits:
for i, eastern_arabic_digit, full_width_digit in zip(
itertools.count(), eastern_arabic_digits, full_width_digits):
assert i == int(eastern_arabic_digit) == int(full_width_digit)

formats = ['%Y %m %d %H %M %S %f',
'%y %m %d %I %M %p',
'%Y %j',
'%G %V %u',
'%Y %U %w',
'%Y %W %w']

my_datetime = datetime.fromisoformat('2020-01-09T23:41:45.973450')
for format in formats:
datetime_string = my_datetime.strftime(format)
self.assertEqual(my_datetime, datetime.strptime(datetime_string, format))
digit_indices = [i for i, c in enumerate(datetime_string) if c.isdigit()]
assert len(digit_indices) >= 6
for alternate_digits in (eastern_arabic_digits, full_width_digits):
for i in digit_indices:
tainted_datetime_string = (
datetime_string[:i] +
alternate_digits[int(datetime_string[i])] +
datetime_string[i + 1]
)
with self.assertRaises(ValueError):
datetime.strptime(tainted_datetime_string, format)

def test_more_timetuple(self):
# This tests fields beyond those tested by the TestDate.test_timetuple.
t = self.theclass(2004, 12, 31, 6, 22, 33)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't allow datetime parsing to accept non-Ascii digits.