forked from mtusi2026/mtusi2026lab7
fix(tls): flush early API responses before request body teardown
This commit is contained in:
+2
-1
@@ -2,7 +2,8 @@ FROM library/golang:1.27.4-alpine@sha256:70bd7ddea8b6cbe5d6943cde279d6b67feb10cc
|
|||||||
WORKDIR /source
|
WORKDIR /source
|
||||||
COPY infra/tls/*.go ./
|
COPY infra/tls/*.go ./
|
||||||
RUN CGO_ENABLED=0 GOMAXPROCS=2 go test -count=1 gateway.go gateway_test.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 ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1334471a90c4a1d93bb490 AS uv
|
||||||
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS builder
|
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS builder
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
@@ -361,6 +361,11 @@ func (l *boundedListener) Accept() (net.Conn, error) {
|
|||||||
}
|
}
|
||||||
func loopbackProxy(c Config, target *url.URL) *httputil.ReverseProxy {
|
func loopbackProxy(c Config, target *url.URL) *httputil.ReverseProxy {
|
||||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
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
|
director := proxy.Director
|
||||||
proxy.Director = func(r *http.Request) {
|
proxy.Director = func(r *http.Request) {
|
||||||
director(r)
|
director(r)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -24,7 +25,15 @@ import (
|
|||||||
|
|
||||||
type countedBody struct {
|
type countedBody struct {
|
||||||
io.ReadCloser
|
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) {
|
func (b *countedBody) Read(data []byte) (int, error) {
|
||||||
@@ -55,21 +64,37 @@ func TestEarlyRejectionDoesNotForwardBodyAndAcceptedBodyStillStreams(t *testing.
|
|||||||
proxy := loopbackProxy(Config{Role: "api"}, target)
|
proxy := loopbackProxy(Config{Role: "api"}, target)
|
||||||
defer proxy.Transport.(*http.Transport).CloseIdleConnections()
|
defer proxy.Transport.(*http.Transport).CloseIdleConnections()
|
||||||
var forwarded atomic.Int64
|
var forwarded atomic.Int64
|
||||||
|
release := make(chan struct{})
|
||||||
|
var released sync.Once
|
||||||
|
unhold := func() { released.Do(func() { close(release) }) }
|
||||||
director := proxy.Director
|
director := proxy.Director
|
||||||
proxy.Director = func(r *http.Request) {
|
proxy.Director = func(r *http.Request) {
|
||||||
director(r)
|
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 frontend.Close()
|
||||||
|
defer unhold()
|
||||||
payload := strings.Repeat("x", 10*1024*1024+1)
|
payload := strings.Repeat("x", 10*1024*1024+1)
|
||||||
client := &http.Client{Timeout: 5 * time.Second}
|
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 {
|
if err != nil {
|
||||||
|
unhold()
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
body, err := io.ReadAll(response.Body)
|
body, err := io.ReadAll(response.Body)
|
||||||
|
unhold()
|
||||||
if err != nil || response.Header.Get("X-Instance-Id") != "synthetic-api" {
|
if err != nil || response.Header.Get("X-Instance-Id") != "synthetic-api" {
|
||||||
t.Fatalf("incomplete application response: %v", err)
|
t.Fatalf("incomplete application response: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user