Skip to content

Commit d6c6f08

Browse files
committed
[3.10] gh-143925: Reject control characters in data: URL mediatypes
(cherry picked from commit f25509e) (cherry picked from commit 2c9c746) Co-authored-by: Seth Michael Larson <seth@python.org>
1 parent 2f84024 commit d6c6f08

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
@@ -11,6 +11,7 @@
1111
from test import support
1212
from test.support import os_helper
1313
from test.support import warnings_helper
14+
from test.support import control_characters_c0
1415
import os
1516
try:
1617
import ssl
@@ -683,6 +684,13 @@ def test_invalid_base64_data(self):
683684
# missing padding character
684685
self.assertRaises(ValueError,urllib.request.urlopen,'data:;base64,Cg=')
685686

687+
def test_invalid_mediatype(self):
688+
for c0 in control_characters_c0():
689+
self.assertRaises(ValueError,urllib.request.urlopen,
690+
f'data:text/html;{c0},data')
691+
for c0 in control_characters_c0():
692+
self.assertRaises(ValueError,urllib.request.urlopen,
693+
f'data:text/html{c0};base64,ZGF0YQ==')
686694

687695
class urlretrieve_FileTests(unittest.TestCase):
688696
"""Test urllib.urlretrieve() on local files"""

Lib/urllib/request.py

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

1657+
# Disallow control characters within mediatype.
1658+
if re.search(r"[\x00-\x1F\x7F]", mediatype):
1659+
raise ValueError(
1660+
"Control characters not allowed in data: mediatype")
1661+
16571662
# even base64 encoded data URLs might be quoted so unquote in any case:
16581663
data = unquote_to_bytes(data)
16591664
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)