]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-143925: Reject control characters in data: URL mediatypes (#144084)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 23 Jan 2026 11:45:00 +0000 (12:45 +0100)
committerGitHub <noreply@github.com>
Fri, 23 Jan 2026 11:45:00 +0000 (13:45 +0200)
Co-authored-by: Seth Michael Larson <seth@python.org>
Lib/test/test_urllib.py
Lib/urllib/request.py
Misc/NEWS.d/next/Security/2026-01-16-11-51-19.gh-issue-143925.mrtcHW.rst [new file with mode: 0644]

index ae524c5ffba6b1f6cafb7fa20bdebf3712acfe4b..2dd739b77b8e4d1c8d021f22a00b170c9c7fcd6e 100644 (file)
@@ -10,6 +10,7 @@ import unittest
 from test import support
 from test.support import os_helper
 from test.support import socket_helper
+from test.support import control_characters_c0
 import os
 import socket
 try:
@@ -590,6 +591,13 @@ class urlopen_DataTests(unittest.TestCase):
         # missing padding character
         self.assertRaises(ValueError,urllib.request.urlopen,'data:;base64,Cg=')
 
+    def test_invalid_mediatype(self):
+        for c0 in control_characters_c0():
+            self.assertRaises(ValueError,urllib.request.urlopen,
+                              f'data:text/html;{c0},data')
+        for c0 in control_characters_c0():
+            self.assertRaises(ValueError,urllib.request.urlopen,
+                              f'data:text/html{c0};base64,ZGF0YQ==')
 
 class urlretrieve_FileTests(unittest.TestCase):
     """Test urllib.urlretrieve() on local files"""
index 566b8087aec2771e1f5dc3b776bbd68bd67e56e2..8d7470a22739ab7e31ae82bec4d7924cbd40f2a9 100644 (file)
@@ -1634,6 +1634,11 @@ class DataHandler(BaseHandler):
         scheme, data = url.split(":",1)
         mediatype, data = data.split(",",1)
 
+        # Disallow control characters within mediatype.
+        if re.search(r"[\x00-\x1F\x7F]", mediatype):
+            raise ValueError(
+                "Control characters not allowed in data: mediatype")
+
         # even base64 encoded data URLs might be quoted so unquote in any case:
         data = unquote_to_bytes(data)
         if mediatype.endswith(";base64"):
diff --git a/Misc/NEWS.d/next/Security/2026-01-16-11-51-19.gh-issue-143925.mrtcHW.rst b/Misc/NEWS.d/next/Security/2026-01-16-11-51-19.gh-issue-143925.mrtcHW.rst
new file mode 100644 (file)
index 0000000..46109df
--- /dev/null
@@ -0,0 +1 @@
+Reject control characters in ``data:`` URL media types.