Skip to content

Commit 4313d6a

Browse files
committed
gh-162462: Require canonical Windows code page names
Validate the numeric suffix before converting it to a code page number. The codec registry caches successful lookups by name, so alternate spellings of the same number can create duplicate codec entries.
1 parent 22691a3 commit 4313d6a

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎Doc/library/codecs.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,9 @@ This module implements the following functions:
16181618

16191619
.. function:: win32_code_page_search_function(encoding)
16201620

1621-
Search for a Windows code page encoding *encoding* of the form ``cpXXXX``.
1621+
Search for a Windows code page encoding *encoding* of the form ``cpXXXX``,
1622+
where ``XXXX`` is a non-negative integer written in ASCII decimal digits
1623+
without leading zeros.
16221624

16231625
If the code page is valid and supported, return a :class:`codecs.CodecInfo`
16241626
object for it.

‎Lib/encodings/__init__.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,14 @@ def win32_code_page_search_function(encoding):
163163
encoding = encoding.lower()
164164
if not encoding.startswith('cp'):
165165
return None
166+
# The codec registry caches results by name.
167+
code_page = encoding[2:]
168+
if not code_page.isascii() or not code_page.isdecimal():
169+
return None
170+
if code_page != '0' and code_page.startswith('0'):
171+
return None
166172
try:
167-
cp = int(encoding[2:])
173+
cp = int(code_page)
168174
except ValueError:
169175
return None
170176
# Test if the code page is supported

‎Lib/test/test_codecs.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3454,6 +3454,16 @@ def test_invalid_code_page(self):
34543454
self.assertRaises(OSError, codecs.code_page_encode, 123, 'a')
34553455
self.assertRaises(OSError, codecs.code_page_decode, 123, b'a')
34563456

3457+
def test_lookup_code_page(self):
3458+
# Code page 0 selects the current Windows ANSI code page.
3459+
self.assertEqual(codecs.lookup('CP0').name, 'cp0')
3460+
self.assertEqual(codecs.lookup('windows-1252').name, 'cp1252')
3461+
3462+
def test_lookup_invalid_code_page_names(self):
3463+
for encoding in ('cp01252', 'cp1_252', 'cp+1252', 'cp-1'):
3464+
with self.subTest(encoding=encoding):
3465+
self.assertRaises(LookupError, codecs.lookup, encoding)
3466+
34573467
def test_code_page_name(self):
34583468
self.assertRaisesRegex(UnicodeEncodeError, 'cp932',
34593469
codecs.code_page_encode, 932, '\xff')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reject noncanonical ``cpXXX`` codec names in the Windows code page lookup,
2+
including numbers with leading zeros, signs, or underscores.

0 commit comments

Comments
 (0)