forked from mtusi2026/mtusi2026lab7
fix(tls): drain rejected API input within a bounded deadline
This commit is contained in:
+26
-27
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
@@ -9,15 +10,16 @@ import (
|
||||
"crypto/x509/pkix"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -25,15 +27,7 @@ import (
|
||||
|
||||
type countedBody struct {
|
||||
io.ReadCloser
|
||||
bytes *atomic.Int64
|
||||
release <-chan struct{}
|
||||
}
|
||||
|
||||
func (b *countedBody) Close() error {
|
||||
if b.release != nil {
|
||||
<-b.release
|
||||
}
|
||||
return b.ReadCloser.Close()
|
||||
bytes *atomic.Int64
|
||||
}
|
||||
|
||||
func (b *countedBody) Read(data []byte) (int, error) {
|
||||
@@ -64,37 +58,42 @@ 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)
|
||||
body := &countedBody{ReadCloser: r.Body, bytes: &forwarded}
|
||||
if reject {
|
||||
body.release = release
|
||||
}
|
||||
r.Body = body
|
||||
r.Body = &countedBody{ReadCloser: r.Body, bytes: &forwarded}
|
||||
}
|
||||
frontend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Connection", "close")
|
||||
proxy.ServeHTTP(w, r)
|
||||
serveLoopbackProxy(Config{Role: "api"}, proxy, w, r)
|
||||
}))
|
||||
defer frontend.Close()
|
||||
defer unhold()
|
||||
payload := strings.Repeat("x", 10*1024*1024+1)
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
request, _ := http.NewRequest("POST", frontend.URL, strings.NewReader(payload))
|
||||
request.Header.Set("Content-Type", "image/png")
|
||||
request.Close = true
|
||||
response, err := client.Do(request)
|
||||
conn, err := net.DialTimeout("tcp", frontend.Listener.Addr().String(), time.Second)
|
||||
if err != nil {
|
||||
unhold()
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
_ = conn.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: synthetic\r\nConnection: close\r\nContent-Type: image/png\r\nContent-Length: %d\r\n\r\n", len(payload))
|
||||
// A load balancer can finish writing before reading the early reply.
|
||||
if _, err := io.WriteString(conn, payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reader := bufio.NewReader(conn)
|
||||
var response *http.Response
|
||||
for {
|
||||
response, err = http.ReadResponse(reader, &http.Request{Method: "POST"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.StatusCode >= 200 {
|
||||
break
|
||||
}
|
||||
response.Body.Close()
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user