Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75bc88f

Browse files
committed
bpo-39280: Don't allow datetime parsing to accept non-Ascii digits
1 parent864c031 commit75bc88f

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

‎Lib/_strptime.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,25 @@ def __init__(self, locale_time=None):
183183
base=super()
184184
base.__init__({
185185
# The " [1-9]" part of the regex is to make %c from ANSI C work
186-
'd':r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
186+
'd':r"(?P<d>3[0-1]|[1-2][0-9]|0[1-9]|[1-9]| [1-9])",
187187
'f':r"(?P<f>[0-9]{1,6})",
188-
'H':r"(?P<H>2[0-3]|[0-1]\d|\d)",
188+
'H':r"(?P<H>2[0-3]|[0-1][0-9]|[0-9])",
189189
'I':r"(?P<I>1[0-2]|0[1-9]|[1-9])",
190-
'G':r"(?P<G>\d\d\d\d)",
191-
'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])",
190+
'G':r"(?P<G>[0-9][0-9][0-9][0-9])",
191+
'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])",
192192
'm':r"(?P<m>1[0-2]|0[1-9]|[1-9])",
193-
'M':r"(?P<M>[0-5]\d|\d)",
194-
'S':r"(?P<S>6[0-1]|[0-5]\d|\d)",
195-
'U':r"(?P<U>5[0-3]|[0-4]\d|\d)",
193+
'M':r"(?P<M>[0-5][0-9]|[0-9])",
194+
'S':r"(?P<S>6[0-1]|[0-5][0-9]|[0-9])",
195+
'U':r"(?P<U>5[0-3]|[0-4][0-9]|[0-9])",
196196
'w':r"(?P<w>[0-6])",
197197
'u':r"(?P<u>[1-7])",
198-
'V':r"(?P<V>5[0-3]|0[1-9]|[1-4]\d|\d)",
198+
'V':r"(?P<V>5[0-3]|0[1-9]|[1-4][0-9]|[0-9])",
199199
# W is set below by using 'U'
200-
'y':r"(?P<y>\d\d)",
200+
'y':r"(?P<y>[0-9][0-9])",
201201
#XXX: Does 'Y' need to worry about having less or more than
202202
# 4 digits?
203-
'Y':r"(?P<Y>\d\d\d\d)",
204-
'z':r"(?P<z>[+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?|Z)",
203+
'Y':r"(?P<Y>[0-9][0-9][0-9][0-9])",
204+
'z':r"(?P<z>[+-][0-9][0-9]:?[0-5][0-9](:?[0-5][0-9](\.[0-9]{1,6})?)?|Z)",
205205
'A':self.__seqToRE(self.locale_time.f_weekday,'A'),
206206
'a':self.__seqToRE(self.locale_time.a_weekday,'a'),
207207
'B':self.__seqToRE(self.locale_time.f_month[1:],'B'),

‎Lib/test/datetimetester.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,39 @@ def test_strptime_single_digit(self):
26172617
newdate=strptime(string,format)
26182618
self.assertEqual(newdate,target,msg=reason)
26192619

2620+
deftest_rejecting_non_ascii_digits(self):
2621+
# bpo-39280: Don't allow datetime parsing to accept non-Ascii digits
2622+
eastern_arabic_digits= [chr(1632+i)foriinrange(10)]
2623+
full_width_digits= [chr(65296+i)foriinrange(10)]
2624+
2625+
# Making sure we're using legit Unicode digits:
2626+
fori,eastern_arabic_digit,full_width_digitinzip(
2627+
itertools.count(),eastern_arabic_digits,full_width_digits):
2628+
asserti==int(eastern_arabic_digit)==int(full_width_digit)
2629+
2630+
formats= ['%Y %m %d %H %M %S %f',
2631+
'%y %m %d %I %M %p',
2632+
'%Y %j',
2633+
'%G %V %u',
2634+
'%Y %U %w',
2635+
'%Y %W %w']
2636+
2637+
my_datetime=datetime.fromisoformat('2020-01-09T23:41:45.973450')
2638+
forformatinformats:
2639+
datetime_string=my_datetime.strftime(format)
2640+
self.assertEqual(my_datetime,datetime.strptime(datetime_string,format))
2641+
digit_indices= [ifori,cinenumerate(datetime_string)ifc.isdigit()]
2642+
assertlen(digit_indices)>=6
2643+
foralternate_digitsin (eastern_arabic_digits,full_width_digits):
2644+
foriindigit_indices:
2645+
tainted_datetime_string= (
2646+
datetime_string[:i]+
2647+
alternate_digits[int(datetime_string[i])]+
2648+
datetime_string[i+1]
2649+
)
2650+
withself.assertRaises(ValueError):
2651+
datetime.strptime(tainted_datetime_string,format)
2652+
26202653
deftest_more_timetuple(self):
26212654
# This tests fields beyond those tested by the TestDate.test_timetuple.
26222655
t=self.theclass(2004,12,31,6,22,33)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Don't allow datetime parsing to accept non-Ascii digits.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp