Skip to content

Commit f336af1

Browse files
committed
[3.12] 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 4802b96 commit f336af1

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/test/test_urllib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
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
16+
from test.support.testcase import ExtraAssertions
1517
import os
1618
try:
1719
import ssl
@@ -688,6 +690,13 @@ def test_invalid_base64_data(self):
688690
# missing padding character
689691
self.assertRaises(ValueError,urllib.request.urlopen,'data:;base64,Cg=')
690692

693+
def test_invalid_mediatype(self):
694+
for c0 in control_characters_c0():
695+
self.assertRaises(ValueError,urllib.request.urlopen,
696+
f'data:text/html;{c0},data')
697+
for c0 in control_characters_c0():
698+
self.assertRaises(ValueError,urllib.request.urlopen,
699+
f'data:text/html{c0};base64,ZGF0YQ==')
691700

692701
class urlretrieve_FileTests(unittest.TestCase):
693702
"""Test urllib.urlretrieve() on local files"""

Lib/urllib/request.py

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

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