Skip to content

Commit a2cdbb6

Browse files
[3.9] pythongh-121227: Disallow setting an empty list for NPN (pythonGH-137161)
1 parent 312de66 commit a2cdbb6

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

Lib/ssl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ def wrap_bio(self, incoming, outgoing, server_side=False,
520520

521521
def set_npn_protocols(self, npn_protocols):
522522
protos = bytearray()
523+
if not npn_protocols:
524+
raise SSLError('NPN protocols must not be empty')
523525
for protocol in npn_protocols:
524526
b = bytes(protocol, 'ascii')
525527
if len(b) == 0 or len(b) > 255:

Lib/test/test_ssl.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4219,6 +4219,12 @@ def test_npn_protocols(self):
42194219
if len(stats['server_npn_protocols']) else 'nothing'
42204220
self.assertEqual(server_result, expected, msg % (server_result, "server"))
42214221

4222+
def test_empty_npn_protocols(self):
4223+
"""npn_protocols cannot be empty, see CVE-2024-5642 & gh-121227"""
4224+
client_context, server_context, hostname = testing_context()
4225+
with self.assertRaises(ssl.SSLError):
4226+
server_context.set_npn_protocols([])
4227+
42224228
def sni_contexts(self):
42234229
server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
42244230
server_context.load_cert_chain(SIGNED_CERTFILE)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise an :exc:`SSL.SSLError` if an empty *protocols* argument is passed to
2+
:meth:`ssl.SSLContext.set_npn_protocols` to fix ``CVE-2024-5642``.

0 commit comments

Comments
 (0)