Skip to content

Commit

Permalink
Check for invalid ALPN lists before calling OpenSSL, for consistency
Browse files Browse the repository at this point in the history
Fixes gh-1043
  • Loading branch information
njsmith committed Oct 27, 2021
1 parent 2ea5634 commit cc5c00a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,12 @@ def set_alpn_protos(self, protos):
This list should be a Python list of bytestrings representing the
protocols to offer, e.g. ``[b'http/1.1', b'spdy/2']``.
"""
# Different versions of OpenSSL are inconsistent about how they handle empty
# proto lists (see #1043), so we avoid the problem entirely by rejecting them
# ourselves.
if not protos:
raise ValueError("at least one protocol must be specified")

# Take the list of protocols and join them together, prefixing them
# with their lengths.
protostr = b"".join(
Expand Down Expand Up @@ -2449,6 +2455,12 @@ def set_alpn_protos(self, protos):
This list should be a Python list of bytestrings representing the
protocols to offer, e.g. ``[b'http/1.1', b'spdy/2']``.
"""
# Different versions of OpenSSL are inconsistent about how they handle empty
# proto lists (see #1043), so we avoid the problem entirely by rejecting them
# ourselves.
if not protos:
raise ValueError("at least one protocol must be specified")

# Take the list of protocols and join them together, prefixing them
# with their lengths.
protostr = b"".join(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ def test_alpn_call_failure(self):
protocols list. Ensure that we produce a user-visible error.
"""
context = Context(SSLv23_METHOD)
with pytest.raises(Error):
with pytest.raises(ValueError):
context.set_alpn_protos([])

def test_alpn_set_on_connection(self):
Expand Down

0 comments on commit cc5c00a

Please sign in to comment.