Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ def wrap_bio(self, incoming, outgoing, server_side=False,

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:
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4219,6 +4219,12 @@ def test_npn_protocols(self):
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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``.
Loading