From 3c8db323c4677e1ec64ab94dc8b2cb1102e09eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=81=D0=BB=D0=BE=D0=B2?= Date: Sun, 22 Nov 2026 19:37:57 +0300 Subject: [PATCH] fix(tls): flush early API responses before request body teardown --- Dockerfile | 3 +- evidence/upload-limit-expect-only-failed.json | 14 ++++++++ infra/tls/gateway.go | 5 +++ infra/tls/gateway_test.go | 33 ++++++++++++++++--- 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 evidence/upload-limit-expect-only-failed.json diff --git a/Dockerfile b/Dockerfile index 510ec64..28756d0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,8 @@ FROM library/golang:1.27.4-alpine@sha256:70bd7ddea8b6cbe5d6943cde279d6b67feb10cc WORKDIR /source COPY infra/tls/*.go ./ RUN CGO_ENABLED=0 GOMAXPROCS=2 go test -count=1 gateway.go gateway_test.go \ - && CGO_ENABLED=0 GOMAXPROCS=2 go build -trimpath -ldflags="-s -w" -o /out/tls-gateway gateway.go + && CGO_ENABLED=0 GOMAXPROCS=2 go build -trimpath -ldflags="-s -w" -o /out/tls-gateway gateway.go \ + && rm -rf /root/.cache/go-build FROM ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1334471a90c4a1d93bb490 AS uv FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS builder diff --git a/evidence/upload-limit-expect-only-failed.json b/evidence/upload-limit-expect-only-failed.json new file mode 100644 index 0000000..a24e1e7 --- /dev/null +++ b/evidence/upload-limit-expect-only-failed.json @@ -0,0 +1,14 @@ +{ + "complete": false, + "application_image_revision": "87d71cb", + "six_guard_tests": { + "passed": 5, + "failed": 1, + "seconds": 9.49 + }, + "expected_oversized_status": 413, + "observed_public_status": 502, + "direct_api_verified_mtls_status": 413, + "native_closed_loopback_status": 413, + "finding": "Expect handshake alone did not resolve intermittent truncation at the load balancer; early response flush is being tested" +} diff --git a/infra/tls/gateway.go b/infra/tls/gateway.go index e3dbbed..8e50833 100644 --- a/infra/tls/gateway.go +++ b/infra/tls/gateway.go @@ -361,6 +361,11 @@ func (l *boundedListener) Accept() (net.Conn, error) { } func loopbackProxy(c Config, target *url.URL) *httputil.ReverseProxy { proxy := httputil.NewSingleHostReverseProxy(target) + if c.Role == "api" { + // Deliver the application's early error before request-body teardown. + // Otherwise a large upload can close the hop before buffered JSON drains. + proxy.FlushInterval = -1 + } director := proxy.Director proxy.Director = func(r *http.Request) { director(r) diff --git a/infra/tls/gateway_test.go b/infra/tls/gateway_test.go index 2910edb..e66c8ff 100644 --- a/infra/tls/gateway_test.go +++ b/infra/tls/gateway_test.go @@ -17,6 +17,7 @@ import ( "os" "path/filepath" "strings" + "sync" "sync/atomic" "testing" "time" @@ -24,7 +25,15 @@ import ( type countedBody struct { io.ReadCloser - bytes *atomic.Int64 + bytes *atomic.Int64 + release <-chan struct{} +} + +func (b *countedBody) Close() error { + if b.release != nil { + <-b.release + } + return b.ReadCloser.Close() } func (b *countedBody) Read(data []byte) (int, error) { @@ -55,21 +64,37 @@ func TestEarlyRejectionDoesNotForwardBodyAndAcceptedBodyStillStreams(t *testing. proxy := loopbackProxy(Config{Role: "api"}, target) defer proxy.Transport.(*http.Transport).CloseIdleConnections() var forwarded atomic.Int64 + release := make(chan struct{}) + var released sync.Once + unhold := func() { released.Do(func() { close(release) }) } director := proxy.Director proxy.Director = func(r *http.Request) { director(r) - r.Body = &countedBody{ReadCloser: r.Body, bytes: &forwarded} + body := &countedBody{ReadCloser: r.Body, bytes: &forwarded} + if reject { + body.release = release + } + r.Body = body } - frontend := httptest.NewServer(proxy) + frontend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Connection", "close") + proxy.ServeHTTP(w, r) + })) defer frontend.Close() + defer unhold() payload := strings.Repeat("x", 10*1024*1024+1) client := &http.Client{Timeout: 5 * time.Second} - response, err := client.Post(frontend.URL, "image/png", strings.NewReader(payload)) + request, _ := http.NewRequest("POST", frontend.URL, strings.NewReader(payload)) + request.Header.Set("Content-Type", "image/png") + request.Close = true + response, err := client.Do(request) if err != nil { + unhold() t.Fatal(err) } defer response.Body.Close() body, err := io.ReadAll(response.Body) + unhold() if err != nil || response.Header.Get("X-Instance-Id") != "synthetic-api" { t.Fatalf("incomplete application response: %v", err) }