diff --git a/CHANGELOG.md b/CHANGELOG.md index ae4ec11..84562d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable changes to this monorepo. +## backends v3.0.2 — 2026-11-15 (`bug-report-py` 3.0.2) + +One fix to `create_proxy_blueprint`, found on the seal viewer's first live day. No contract change. + +- **The proxy never asks the service to close the connection.** + - **The problem:** Azure Container Apps' internal ingress cuts a large response short when the request says `Connection: close`, and `urllib` always does. + - **The evidence:** a 761 KB screenshot through seal's proxy arrived as 65 to 311 KB, 3 times out of 3. Without that header it arrived whole, 3 times out of 3. + - **The fix:** the proxy now goes out over `http.client`, which follows no redirects, as before. +- **A body that arrives short is a 502** (`the reports service's answer came back incomplete`, logged with the byte counts). It is never a crash (500) or a short 200. + ## backends v3.0.1 — 2026-11-14 (`bug-report-py` 3.0.1) Two fixes to `create_proxy_blueprint` from the first review of the seal viewer's move. No contract change. diff --git a/packages/backend-python/README.md b/packages/backend-python/README.md index 8efd7b0..2663e36 100644 --- a/packages/backend-python/README.md +++ b/packages/backend-python/README.md @@ -116,7 +116,7 @@ not followed). Each forwarded request carries `Authorization: Bearer ` (3.0.1); an explicit `app` passes through. A `..` path segment is refused (400). A service that cannot be reached is a 502, and a token or identity that cannot be -produced is a 502 with nothing sent; the 502 bodies are generic (the reason goes to the log, not the browser). Standard library only (urllib). +produced is a 502 with nothing sent; the 502 bodies are generic (the reason goes to the log, not the browser). Standard library only (`http.client`; it never sends `Connection: close`, which Azure Container Apps' internal ingress answers by cutting large responses short). ## The service side: who is calling (3.0.0) diff --git a/packages/backend-python/bug_report/proxy.py b/packages/backend-python/bug_report/proxy.py index f3eda4c..031a46b 100644 --- a/packages/backend-python/bug_report/proxy.py +++ b/packages/backend-python/bug_report/proxy.py @@ -21,15 +21,20 @@ admin, sign in to vote) pass through. Any ``Authorization`` or ``X-Leap-*`` header the browser sent is dropped: the app's own identity, not the page's claim, is what the service looks at. Standard library only. + +The request goes out over ``http.client`` and never says ``Connection: close`` +(3.0.2). Azure Container Apps' internal ingress cuts a large response short when +the request asks to close the connection - ``urllib`` always does - so a 761 KB +screenshot arrived as 65-311 KB and the proxy crashed (2026-11-15). A body that +still arrives short is a 502, never a truncated 200. """ from __future__ import annotations +import http.client import json import logging -import urllib.error -import urllib.request from typing import Callable -from urllib.parse import quote +from urllib.parse import quote, urlsplit from flask import Blueprint, Response, request @@ -38,14 +43,6 @@ log = logging.getLogger(__name__) -class _NoRedirect(urllib.request.HTTPRedirectHandler): - """A 302 (a signed screenshot URL) goes back to the browser as it is.""" - def redirect_request(self, *a, **kw): - return None - - -_OPENER = urllib.request.build_opener(_NoRedirect) - #: request headers passed on from the browser; everything else is dropped _FORWARD = ("Content-Type", "Accept") #: response headers passed back: the contract's, plus the redirect target @@ -63,6 +60,10 @@ def create_proxy_blueprint( ) -> Blueprint: bp = Blueprint(f"bug_report_proxy_{app}", __name__) base = central_url.rstrip("/") + target = urlsplit(base) + if target.scheme not in ("http", "https") or not target.hostname: + raise ValueError(f"central_url must be an http(s) URL: {central_url!r}") + connection = http.client.HTTPSConnection if target.scheme == "https" else http.client.HTTPConnection def _ascii(v: str) -> str: return quote(v or "", safe=" @.+-_'") @@ -72,14 +73,14 @@ def _ascii(v: str) -> str: def forward(rest: str): if any(seg in ("..", ".") or "\\" in seg for seg in rest.split("/")): return _err(400, "invalid path") - url = base + ("/" + rest if rest else "") + path = target.path + ("/" + rest if rest else "") query = request.query_string.decode("latin-1") # The list is this app's own reports unless the caller says otherwise (3.0.1): the open count and # the queue must not show every app's, which is what the service gives a trusted app with no filter. if request.method == "GET" and not rest and "app" not in request.args: query = (query + "&" if query else "") + "app=" + quote(app, safe="") if query: - url += "?" + query + path += "?" + query try: person = person_provider() or {} headers = {h: request.headers[h] for h in _FORWARD if h in request.headers} @@ -93,18 +94,21 @@ def forward(rest: str): log.warning("reports proxy (%s): could not get a token or identity: %s", app, e) return _err(502, "could not authenticate to the reports service") data = request.get_data() if request.method in ("POST", "PATCH", "DELETE") else None - req = urllib.request.Request(url, data=data, headers=headers, method=request.method) + conn = connection(target.hostname, target.port, timeout=timeout) try: - with _OPENER.open(req, timeout=timeout) as r: - return _relay(r.status, r.read(), r.headers) - except urllib.error.HTTPError as e: # a 3xx/4xx/5xx from the service is its answer - try: - return _relay(e.code, e.read(), e.headers) - finally: - e.close() - except (urllib.error.URLError, OSError, TimeoutError) as e: - log.warning("reports proxy (%s): %s unreachable: %s", app, url, getattr(e, "reason", e)) + # Any status - 3xx (a signed screenshot URL goes back as it is), 4xx, 5xx - is the service's answer. + conn.request(request.method, path, body=data, headers=headers) + r = conn.getresponse() + return _relay(r.status, r.read(), r.headers) + except http.client.IncompleteRead as e: + log.warning("reports proxy (%s): %s came back short: %d bytes, %s more expected", + app, path, len(e.partial), e.expected) + return _err(502, "the reports service's answer came back incomplete") + except (http.client.HTTPException, OSError, TimeoutError) as e: + log.warning("reports proxy (%s): %s unreachable: %s", app, path, e) return _err(502, "the reports service is not reachable") + finally: + conn.close() def _relay(status, body, upstream_headers): if status in (401, 403) and upstream_headers.get(TRUST_REFUSAL_HEADER): diff --git a/packages/backend-python/pyproject.toml b/packages/backend-python/pyproject.toml index f9d2be5..796dc58 100644 --- a/packages/backend-python/pyproject.toml +++ b/packages/backend-python/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "bug-report-py" -version = "3.0.1" +version = "3.0.2" description = "Flask blueprint + storage interface implementing the bug-report-widget API spec." readme = "README.md" license = { text = "MIT" } diff --git a/packages/backend-python/tests/test_proxy.py b/packages/backend-python/tests/test_proxy.py index 45f75dd..d891323 100644 --- a/packages/backend-python/tests/test_proxy.py +++ b/packages/backend-python/tests/test_proxy.py @@ -283,3 +283,40 @@ def test_the_central_service_itself_refuses_a_request_with_no_token(central): raise AssertionError("expected 401") except urllib.error.HTTPError as e: assert e.code == 401 + + +# ---- 3.0.2: never ask the service to close the connection; a short body is a 502 ------------------- + +def test_the_forwarded_request_never_asks_to_close_the_connection(central, front): + # Azure Container Apps' internal ingress cut a 761 KB screenshot short whenever the request said + # "Connection: close" (urllib always does): 3 of 3 short, 3 of 3 whole without it (2026-11-15). + _, _, seen = central + file(front) + front.c.get("/api/bugs") + assert seen and all(h.get("Connection", "").lower() != "close" for h in seen), [h.get("Connection") for h in seen] + + +def test_a_body_that_arrives_short_is_a_502_not_a_crash_or_a_short_200(): + import socket + + lsock = socket.socket() + lsock.bind(("127.0.0.1", 0)) + lsock.listen(1) + port = lsock.getsockname()[1] + + def serve_short(): + conn, _ = lsock.accept() + conn.recv(65536) + conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: 100000\r\n\r\n" + b"x" * 1000) + conn.close() + + t = threading.Thread(target=serve_short, daemon=True) + t.start() + flask_app = Flask(__name__) + flask_app.register_blueprint(create_proxy_blueprint( + f"http://127.0.0.1:{port}/bugs", "placements", lambda: "ok:client-placements", + lambda: {"email": "admin@example.com"}), url_prefix="/api") + r = flask_app.test_client().get("/api/bugs/bug-20261103-133031-e19b68/screenshot") + t.join(3) + lsock.close() + assert r.status_code == 502 and "incomplete" in r.get_json()["error"]