Skip to content

Commit 5f2a6a8

Browse files
Assert on the ALPN extension in test_tunnel_sets_http_11_alpn (#5232)
The test looked for `http/1.1` and `h2` in the ClientHello as a whole, but the random bytes, the session id and the key shares can spell either one out by accident. Locally that made it fail roughly once in 400 runs, and more often on CI, where the key share carries a post-quantum group and the record is correspondingly larger.
1 parent c34a8be commit 5f2a6a8

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎changelog/5232.misc.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a flaky proxy test that searched the whole TLS ClientHello for ALPN protocol names.

‎test/with_dummyserver/test_socketlevel.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,24 +1309,30 @@ def echo_socket_handler(listener: socket.socket) -> None:
13091309

13101310
def test_tunnel_sets_http_11_alpn(self) -> None:
13111311
done_receiving = Event()
1312-
self.buf = b""
1312+
alpn_protocol: str | None = None
13131313

13141314
def socket_handler(listener: socket.socket) -> None:
1315+
nonlocal alpn_protocol
13151316
sock = listener.accept()[0]
13161317

1317-
self.buf = sock.recv(65536) # We only accept one packet
1318-
done_receiving.set() # let the test know it can proceed
1319-
sock.close()
1318+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
1319+
context.load_cert_chain(DEFAULT_CERTS["certfile"], DEFAULT_CERTS["keyfile"])
1320+
# Prefer h2 so the assertion fails if the client offers it.
1321+
context.set_alpn_protocols(["h2", "http/1.1"])
1322+
try:
1323+
with context.wrap_socket(sock, server_side=True) as ssl_sock:
1324+
alpn_protocol = ssl_sock.selected_alpn_protocol()
1325+
finally:
1326+
done_receiving.set() # let the test know it can proceed
13201327

13211328
self._start_server(socket_handler)
13221329
base_url = f"https://{self.host}:{self.port}"
1323-
with proxy_from_url(base_url) as proxy:
1330+
with proxy_from_url(base_url, ca_certs=DEFAULT_CA) as proxy:
13241331
with pytest.raises(MaxRetryError):
13251332
proxy.request("GET", "https://localhost/")
13261333

13271334
done_receiving.wait()
1328-
assert b"http/1.1" in self.buf
1329-
assert b"h2" not in self.buf
1335+
assert alpn_protocol == "http/1.1"
13301336

13311337
def test_connect_reconn(self) -> None:
13321338
def proxy_ssl_one(listener: socket.socket) -> None:

0 commit comments

Comments
 (0)