]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] gh-121227: Disallow setting an empty list for NPN (GH-137161)
authorStan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Tue, 7 Oct 2025 12:09:33 +0000 (13:09 +0100)
committerGitHub <noreply@github.com>
Tue, 7 Oct 2025 12:09:33 +0000 (14:09 +0200)
Lib/ssl.py
Lib/test/test_ssl.py
Misc/NEWS.d/next/Security/2025-07-28-10-35-59.gh-issue-121227.Orp1wf.rst [new file with mode: 0644]

index cb5ec51681e1ca1842a4a21f767c897d4a4645fc..a78e6acbfbc982d7127acae4097bdc09833e72b0 100644 (file)
@@ -520,6 +520,8 @@ class SSLContext(_SSLContext):
 
     def set_npn_protocols(self, npn_protocols):
         protos = bytearray()
+        if not npn_protocols:
+            raise SSLError('NPN protocols must not be empty')
         for protocol in npn_protocols:
             b = bytes(protocol, 'ascii')
             if len(b) == 0 or len(b) > 255:
index b9163ae0d5e361ddb22c202918053bd1c84e4b0a..a2e771ed7fd669338f8c8603d4578f53319fc8f2 100644 (file)
@@ -4219,6 +4219,12 @@ class ThreadedTests(unittest.TestCase):
                 if len(stats['server_npn_protocols']) else 'nothing'
             self.assertEqual(server_result, expected, msg % (server_result, "server"))
 
+    def test_empty_npn_protocols(self):
+        """npn_protocols cannot be empty, see CVE-2024-5642 & gh-121227"""
+        client_context, server_context, hostname = testing_context()
+        with self.assertRaises(ssl.SSLError):
+            server_context.set_npn_protocols([])
+
     def sni_contexts(self):
         server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
         server_context.load_cert_chain(SIGNED_CERTFILE)
diff --git a/Misc/NEWS.d/next/Security/2025-07-28-10-35-59.gh-issue-121227.Orp1wf.rst b/Misc/NEWS.d/next/Security/2025-07-28-10-35-59.gh-issue-121227.Orp1wf.rst
new file mode 100644 (file)
index 0000000..6350f74
--- /dev/null
@@ -0,0 +1,2 @@
+Raise an :exc:`SSL.SSLError` if an empty *protocols* argument is passed to
+:meth:`ssl.SSLContext.set_npn_protocols` to fix ``CVE-2024-5642``.