Skip to content

Commit 45c5678

Browse files
authored
Check for invalid ALPN lists before calling OpenSSL, for consistency (#1056)
* Check for invalid ALPN lists before calling OpenSSL, for consistency Fixes gh-1043 * Soothe flake8
1 parent 2ea5634 commit 45c5678

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/OpenSSL/SSL.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,12 @@ def set_alpn_protos(self, protos):
14211421
This list should be a Python list of bytestrings representing the
14221422
protocols to offer, e.g. ``[b'http/1.1', b'spdy/2']``.
14231423
"""
1424+
# Different versions of OpenSSL are inconsistent about how they handle
1425+
# empty proto lists (see #1043), so we avoid the problem entirely by
1426+
# rejecting them ourselves.
1427+
if not protos:
1428+
raise ValueError("at least one protocol must be specified")
1429+
14241430
# Take the list of protocols and join them together, prefixing them
14251431
# with their lengths.
14261432
protostr = b"".join(
@@ -2449,6 +2455,12 @@ def set_alpn_protos(self, protos):
24492455
This list should be a Python list of bytestrings representing the
24502456
protocols to offer, e.g. ``[b'http/1.1', b'spdy/2']``.
24512457
"""
2458+
# Different versions of OpenSSL are inconsistent about how they handle
2459+
# empty proto lists (see #1043), so we avoid the problem entirely by
2460+
# rejecting them ourselves.
2461+
if not protos:
2462+
raise ValueError("at least one protocol must be specified")
2463+
24522464
# Take the list of protocols and join them together, prefixing them
24532465
# with their lengths.
24542466
protostr = b"".join(

tests/test_ssl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ def test_alpn_call_failure(self):
19281928
protocols list. Ensure that we produce a user-visible error.
19291929
"""
19301930
context = Context(SSLv23_METHOD)
1931-
with pytest.raises(Error):
1931+
with pytest.raises(ValueError):
19321932
context.set_alpn_protos([])
19331933

19341934
def test_alpn_set_on_connection(self):

0 commit comments

Comments
 (0)