Skip to content

Commit a35ca3b

Browse files
authored
[3.13] gh-143925: Reject control characters in data: URL mediatypes (#144111)
(cherry picked from commit f25509e)
1 parent 8072d67 commit a35ca3b

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/test/test_urllib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from test.support import os_helper
1313
from test.support import socket_helper
1414
from test.support import warnings_helper
15+
from test.support import control_characters_c0
1516
from test.support.testcase import ExtraAssertions
1617
import os
1718
try:
@@ -677,6 +678,13 @@ def test_invalid_base64_data(self):
677678
# missing padding character
678679
self.assertRaises(ValueError,urllib.request.urlopen,'data:;base64,Cg=')
679680

681+
def test_invalid_mediatype(self):
682+
for c0 in control_characters_c0():
683+
self.assertRaises(ValueError,urllib.request.urlopen,
684+
f'data:text/html;{c0},data')
685+
for c0 in control_characters_c0():
686+
self.assertRaises(ValueError,urllib.request.urlopen,
687+
f'data:text/html{c0};base64,ZGF0YQ==')
680688

681689
class urlretrieve_FileTests(unittest.TestCase):
682690
"""Test urllib.urlretrieve() on local files"""

Lib/urllib/request.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,11 @@ def data_open(self, req):
16361636
scheme, data = url.split(":",1)
16371637
mediatype, data = data.split(",",1)
16381638

1639+
# Disallow control characters within mediatype.
1640+
if re.search(r"[\x00-\x1F\x7F]", mediatype):
1641+
raise ValueError(
1642+
"Control characters not allowed in data: mediatype")
1643+
16391644
# even base64 encoded data URLs might be quoted so unquote in any case:
16401645
data = unquote_to_bytes(data)
16411646
if mediatype.endswith(";base64"):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reject control characters in ``data:`` URL media types.

0 commit comments

Comments
 (0)