From 08abe4d2607898cf4fbac12646116f6a21ce9859 Mon Sep 17 00:00:00 2001 From: Deep-Sixed <281237415+Deep-Sixed@users.noreply.github.com> Date: Sat, 26 Sep 2026 00:13:21 +0000 Subject: [PATCH] Sweep the unread-body reset out of the wheel verifier's own client PR #39 and #40 fixed this race in `Client` in src/system/tests/test_web_surface.py and stopped there; `ops/tools/verify_wheel.py` carries its own `ask()` helper, and the AST pass #40 relied on covered the test client rather than this file. The surface settles several refusals on the envelope -- provenance, media type, declared length, declared source -- before a byte of the body is read. A body actually sent at one of those addresses stays unread in the receive buffer, and closing on unread bytes is a reset rather than a clean shutdown on Windows, so whether the refusal or the reset arrives first is a race. It surfaced once as a `ConnectionAbortedError` on the 415 check under verify (windows-latest, 3.14). `ask()` now speaks http.client instead of urllib, because `urlopen` has no way to declare a body and withhold it and no tolerance for the reset either. `transmit` sends the headers and none of the body; `length` declares something other than the truth. Both mirror `Client.send`, so the two clients now hold the same controls for the same reason. Eleven call sites were sending a body to a refusal settled before the read, not just the 415: Origin and token on the status, decision and outward commands; both intake addresses on a launch with no evaluation profile; and the missing and blank source declarations. Each declares its body and sends none. The 413 check, already declaring rather than sending, now declares against the real message instead of a placeholder byte. Verified mechanically rather than by reading: with the handler instrumented to record whether each POST read its body, no request sends one to an address that refuses before reading, and no withheld body belongs to an address that reads -- a withheld body there would hang to the timeout rather than pass quietly. Gates: pytest (1686 passed), ruff check, ruff format --check, verify_tree, verify_secrets, verify_wheel. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01SZrRh6P6mTsdapYFzsGF5a --- ops/tools/verify_wheel.py | 103 ++++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 31 deletions(-) diff --git a/ops/tools/verify_wheel.py b/ops/tools/verify_wheel.py index b35c5b9..302481e 100644 --- a/ops/tools/verify_wheel.py +++ b/ops/tools/verify_wheel.py @@ -53,11 +53,10 @@ # the generated script rather than to this one: without it they would be folded into real # line breaks here and arrive there as an unterminated literal. WEB_CHECK = r""" +import http.client import json import os import threading -import urllib.error -import urllib.request from importlib import resources from data.repository import Repository @@ -89,21 +88,42 @@ def ask(path, method="GET", token=True, body=None, origin=True, media="application/json", - headers=()): - request = urllib.request.Request(surface.origin + path, method=method, data=body) - if token: - request.add_header(TOKEN_HEADER, surface.token) - if body is not None: - request.add_header("Content-Type", media) - for name, value in headers: - request.add_header(name, value) - if origin: - request.add_header("Origin", surface.origin) + headers=(), length=None, transmit=True): + '''One request, spoken to the surface rather than handed to urllib. + + `urlopen` cannot declare a body and withhold it, which several checks below need. This + surface settles a refusal on the envelope -- provenance, media type, declared length, + declared source -- before a byte of the body is read, so bytes actually sent at one of + those addresses stay unread in the receive buffer. Closing on unread bytes is a reset + rather than a clean shutdown on Windows, and whether the refusal or the reset reaches + the client first is a race no check should be made to win. + + So `transmit=False` sends the headers and none of the body, and every call below that + is refused before the read uses it. `length` declares something other than the truth, + which is the only way to reach the size ceiling without writing at it. `Client.send` in + src/system/tests/test_web_surface.py holds the same two controls for the same reason. + + Withholding a body an address *does* read would hang until the timeout instead, so this + is for refusals settled before the read and nothing else. + ''' + connection = http.client.HTTPConnection(*surface.server_address, timeout=30) try: - with urllib.request.urlopen(request, timeout=30) as response: - return response.status, response.read() - except urllib.error.HTTPError as refused: - return refused.code, refused.read() + connection.putrequest(method, path, skip_host=True, skip_accept_encoding=True) + connection.putheader("Host", surface.authority) + if token: + connection.putheader(TOKEN_HEADER, surface.token) + if body is not None: + connection.putheader("Content-Type", media) + connection.putheader("Content-Length", str(len(body) if length is None else length)) + for name, value in headers: + connection.putheader(name, value) + if origin: + connection.putheader("Origin", surface.origin) + connection.endheaders(body if transmit else None) + response = connection.getresponse() + return response.status, response.read() + finally: + connection.close() assert surface.server_address[0] == "127.0.0.1", surface.server_address @@ -143,8 +163,10 @@ def ask(path, method="GET", token=True, body=None, origin=True, media="applicati assert status == 409, (status, body) assert json.loads(body)["error"] == "status_conflict", body -assert ask(address, method="POST", body=order, origin=False)[0] == 403 -assert ask(address, method="POST", body=order, token=False)[0] == 401 +# Declared and withheld: provenance is settled before the body is read, so these two +# never send one. The malformed body below is sent, and has to be read to be refused. +assert ask(address, method="POST", body=order, origin=False, transmit=False)[0] == 403 +assert ask(address, method="POST", body=order, token=False, transmit=False)[0] == 401 assert ask(address, method="POST", body=b"{oops", origin=True)[0] == 400 history = json.loads(ask("/api/v1/opportunities/" + chosen)[1])["history"] @@ -194,8 +216,12 @@ def ask(path, method="GET", token=True, body=None, origin=True, media="applicati assert ask(verdict, method="POST", body=b'{"approved": true}')[0] == 400 assert ask(verdict, method="POST", body=b'{"approved": false, "expected": {}}')[0] == 400 assert ask(verdict, method="POST", body=b'{"approved": false, "actor": "somebody"}')[0] == 400 -assert ask(verdict, method="POST", body=b'{"approved": false}', origin=False)[0] == 403 -assert ask(verdict, method="POST", body=b'{"approved": false}', token=False)[0] == 401 +assert ask( + verdict, method="POST", body=b'{"approved": false}', origin=False, transmit=False +)[0] == 403 +assert ask( + verdict, method="POST", body=b'{"approved": false}', token=False, transmit=False +)[0] == 401 assert ask("/api/v1/reviews/nope/decision", method="POST", body=b'{"approved": false}')[0] == 404 status, body = ask(verdict, method="POST", body=b'{"approved": false}') assert status == 200, (status, body) @@ -213,8 +239,12 @@ def ask(path, method="GET", token=True, body=None, origin=True, media="applicati assert json.loads(body)["outcome"] == "refused", body # The address carries nothing. A field is refused rather than ignored. assert ask(outward, method="POST", body=b'{"actor": "somebody"}')[0] == 400, command - assert ask(outward, method="POST", body=b"{}", origin=False)[0] == 403, command - assert ask(outward, method="POST", body=b"{}", token=False)[0] == 401, command + assert ask( + outward, method="POST", body=b"{}", origin=False, transmit=False + )[0] == 403, command + assert ask( + outward, method="POST", body=b"{}", token=False, transmit=False + )[0] == 401, command # Reading a command address is 404 rather than 405: the read router simply has no # such route, and these two answer POST only. assert ask(outward, method="GET")[0] == 404, command @@ -284,7 +314,11 @@ def ask(path, method="GET", token=True, body=None, origin=True, media="applicati ("/api/v1/intake/eml", WHEEL_MESSAGE, RFC822, DECLARED), ("/api/v1/intake/gmail", b"{}", "application/json", ()), ): - status, refused = ask(address, method="POST", body=body, media=media, headers=headers) + # Whether this launch takes anything in at all is settled before the body is read, so + # both addresses declare theirs and send none. + status, refused = ask( + address, method="POST", body=body, media=media, headers=headers, transmit=False + ) assert status == 409, (address, status, refused) assert json.loads(refused)["error"] == "intake_unavailable", refused assert json.loads(ask("/api/v1/communications")[1]) == [], "a refused intake still stored a message" @@ -334,24 +368,31 @@ def ask(path, method="GET", token=True, body=None, origin=True, media="applicati "/api/v1/intake/eml", method="POST", body=b"Subject: nothing\r\n\r\n", media=RFC822, headers=DECLARED, )[0] == 400 -assert ask("/api/v1/intake/eml", method="POST", body=WHEEL_MESSAGE, media=RFC822)[0] == 400 +# The last three are settled on the envelope -- a missing source, a blank one, a media type +# this address does not accept -- so each declares its body and sends none. The unusable +# material above is the one refusal here that the surface has to read to reach. +assert ask( + "/api/v1/intake/eml", method="POST", body=WHEEL_MESSAGE, media=RFC822, transmit=False +)[0] == 400 assert ask( "/api/v1/intake/eml", method="POST", body=WHEEL_MESSAGE, media=RFC822, - headers=[(NAMESPACE_HEADER, " ")], + headers=[(NAMESPACE_HEADER, " ")], transmit=False, )[0] == 400 assert ask( "/api/v1/intake/eml", method="POST", body=WHEEL_MESSAGE, media="application/json", - headers=DECLARED, + headers=DECLARED, transmit=False, )[0] == 415 -# Declared rather than actually sent. The surface refuses on the declaration, before the -# body is read, which is the property being checked -- and writing two megabytes at a server -# that has already answered and closed would only prove which side notices first. +# Declared rather than actually sent, for the same reason and one more: the surface refuses +# on the declaration, which is the property being checked, and writing two megabytes at a +# server that has already answered and closed would only prove which side notices first. assert ask( "/api/v1/intake/eml", method="POST", - body=b"x", + body=WHEEL_MESSAGE, media=RFC822, - headers=[*DECLARED, ("Content-Length", str(MAX_MESSAGE_BYTES + 1))], + headers=DECLARED, + length=MAX_MESSAGE_BYTES + 1, + transmit=False, )[0] == 413 # Gmail without a read credential stays a launch-capability refusal, not a network attempt. assert ask("/api/v1/intake/gmail", method="POST", body=b"{}")[0] == 409