feat(traffic): replay an edited request through the recording proxy

Traffic.Replay sends an edited request (method, URL, headers, body) through the local proxy, which records the exchange like any other, and returns the response bounded to 2 MiB. POST /api/traffic/replay exposes it to operators only (no agent tool). Invalid input is rejected with ErrReplayInvalid; hop-by-hop headers are dropped, redirects are not followed, and a Host that differs from the URL is refused because the proxy routes by Host.
This commit is contained in:
BaaRai
2026-11-16 15:32:02 +01:00
parent 2edf93a0cd
commit e770caa28e
4 changed files with 677 additions and 0 deletions
+62
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
@@ -826,6 +827,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /api/traffic/stream", s.getTrafficStream)
mux.HandleFunc("GET /api/traffic/blob", s.getTrafficBlob)
mux.HandleFunc("POST /api/traffic/ingest", s.ingestTraffic)
mux.HandleFunc("POST /api/traffic/replay", s.replayTraffic)
mux.HandleFunc("GET /api/commands", s.pgListCommands)
mux.HandleFunc("GET /api/commands/stats", s.pgToolStats) // aggregate call counts per tool
mux.HandleFunc("GET /api/llm/records", s.pgListLLMRecords)
@@ -3485,6 +3487,66 @@ func (s *Server) getTrafficStream(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{"items": msgs, "total": total})
}
// maxReplayRequestBytes bounds the replay request body (JSON, with the payload
// possibly base64-encoded); traffic.Replay applies its own body limit after
// decoding.
const maxReplayRequestBytes = 16 << 20
// replayTraffic sends an edited request through the recording proxy and returns
// the response; the proxy records the exchange like any other.
//
// Operator-only: this is the equivalent of pointing a browser at the proxy, and
// no agent tool exposes it. If an agent tool is ever added, it must go through
// the target gate and the task's scope like every other outbound request.
func (s *Server) replayTraffic(w http.ResponseWriter, r *http.Request) {
tr := s.m.Traffic()
if tr == nil {
writeErr(w, 503, "traffic disabled")
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxReplayRequestBytes)
var body struct {
Method string `json:"method"`
URL string `json:"url"`
Headers []traffic.ReplayHeader `json:"headers"`
Body string `json:"body"`
BodyBase64 bool `json:"body_base64"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
var tooLarge *http.MaxBytesError
if errors.As(err, &tooLarge) {
writeErr(w, 413, "replay request too large")
return
}
writeErr(w, 400, "bad json: "+err.Error())
return
}
payload := []byte(body.Body)
if body.BodyBase64 {
decoded, err := base64.StdEncoding.DecodeString(body.Body)
if err != nil {
writeErr(w, 400, "body is not valid base64: "+err.Error())
return
}
payload = decoded
}
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
defer cancel()
res, err := tr.Replay(ctx, traffic.ReplayRequest{Method: body.Method, URL: body.URL, Headers: body.Headers, Body: payload})
switch {
case errors.Is(err, traffic.ErrReplayInvalid):
writeErr(w, 400, err.Error())
return
case errors.Is(err, context.DeadlineExceeded):
writeErr(w, 504, "replay timed out")
return
case err != nil:
writeErr(w, 502, err.Error())
return
}
writeJSON(w, 200, map[string]any{"status": res.Status, "raw": res.Raw, "duration_ms": res.DurationMS, "truncated": res.Truncated})
}
// getTrafficBlob streams one oversized body by its sha256. Bodies past the inline
// threshold are not carried by the exchange endpoint — it returns a preview plus
// an "@blob sha256:<hash>" pointer — so this is how the UI fetches them whole.
+154
View File
@@ -0,0 +1,154 @@
package server
import (
"context"
"encoding/base64"
"encoding/json"
"io"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/traffic"
)
// replayServer wires a Server like ingestServer. With traffic off,
// s.m.Traffic() is nil. With trafficOn but not live, the store is open and the
// proxy is not listening. With live, NewManager opens the store and starts the
// recording proxy on a free loopback port, as in production; the proxy has no
// exported stop, so it keeps listening until the test binary exits.
func replayServer(t *testing.T, trafficOn, live bool) (*Server, func(body string) *httptest.ResponseRecorder) {
t.Helper()
skipWithoutDSN(t)
proxyAddr := ""
if live {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
proxyAddr = ln.Addr().String()
ln.Close()
}
m, err := NewManager(t.TempDir(), proxyAddr)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { m.Close() })
if live {
if m.traffic == nil {
t.Fatal("NewManager did not open the traffic store")
}
deadline := time.Now().Add(5 * time.Second)
for {
c, err := net.Dial("tcp", proxyAddr)
if err == nil {
c.Close()
break
}
if time.Now().After(deadline) {
t.Fatalf("proxy did not listen on %s: %v", proxyAddr, err)
}
time.Sleep(10 * time.Millisecond)
}
} else if trafficOn {
if m.traffic, err = traffic.Open(filepath.Join(m.dir, "traffic"), ":0"); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())
s.archiveWG.Wait()
<-s.side.done
token, err := signJWT(s.jwtKey)
if err != nil {
t.Fatal(err)
}
return s, func(body string) *httptest.ResponseRecorder {
r := httptest.NewRequest("POST", "/api/traffic/replay", strings.NewReader(body))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, r)
return w
}
}
func TestTrafficReplayRouteTrafficOff(t *testing.T) {
_, replay := replayServer(t, false, false)
if w := replay(`{"method":"GET","url":"http://example.com/"}`); w.Code != 503 {
t.Fatalf("traffic off = %d %s, want 503", w.Code, w.Body)
}
}
func TestTrafficReplayRouteValidation(t *testing.T) {
_, replay := replayServer(t, true, false)
for name, c := range map[string]struct {
body string
want int
}{
"bad json": {`{"method":`, 400},
"CONNECT": {`{"method":"CONNECT","url":"http://example.com/"}`, 400},
"ftp url": {`{"method":"GET","url":"ftp://example.com/x"}`, 400},
"no host": {`{"method":"GET","url":"http:///x"}`, 400},
"bad base64": {`{"method":"POST","url":"http://example.com/","body":"%%%","body_base64":true}`, 400},
"header CRLF": {`{"method":"GET","url":"http://example.com/","headers":[{"name":"X-A","value":"a\r\nb"}]}`, 400},
"too large": {`{"method":"POST","url":"http://example.com/","body":"` + strings.Repeat("a", 17<<20) + `"}`, 413},
"proxy down": {`{"method":"GET","url":"http://127.0.0.1:1/"}`, 502},
"body too big": {`{"method":"POST","url":"http://example.com/","body":"` + base64.StdEncoding.EncodeToString(make([]byte, 9<<20)) + `","body_base64":true}`, 400},
} {
if w := replay(c.body); w.Code != c.want {
t.Errorf("%s: %d %s, want %d", name, w.Code, strings.TrimSpace(w.Body.String()), c.want)
}
}
}
func TestTrafficReplayRouteEndToEnd(t *testing.T) {
s, replay := replayServer(t, true, true)
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
w.Header().Set("X-Seen", r.Method+" "+r.Header.Get("X-Custom"))
_, _ = w.Write(append([]byte("got:"), b...))
}))
t.Cleanup(target.Close)
url := target.URL + "/replayed?x=1"
req, _ := json.Marshal(map[string]any{
"method": "PUT", "url": url,
"headers": []map[string]string{{"name": "X-Custom", "value": "edited"}},
"body": base64.StdEncoding.EncodeToString([]byte("payload")), "body_base64": true,
})
w := replay(string(req))
if w.Code != 200 {
t.Fatalf("replay = %d %s", w.Code, w.Body)
}
var res struct {
Status int `json:"status"`
Raw string `json:"raw"`
DurationMS int64 `json:"duration_ms"`
Truncated bool `json:"truncated"`
}
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
t.Fatal(err)
}
if res.Status != 200 || res.Truncated || !strings.Contains(res.Raw, "X-Seen: PUT edited") || !strings.HasSuffix(res.Raw, "got:payload") {
t.Fatalf("result %+v", res)
}
deadline := time.Now().Add(5 * time.Second)
for {
var n int
if err := s.m.Traffic().DB().QueryRow(`SELECT COUNT(*) FROM exchanges WHERE method='PUT' AND url=?`, url).Scan(&n); err != nil {
t.Fatal(err)
}
if n == 1 {
break
}
if time.Now().After(deadline) {
t.Fatalf("replayed exchange not recorded (%d rows)", n)
}
time.Sleep(20 * time.Millisecond)
}
}
+235
View File
@@ -0,0 +1,235 @@
package traffic
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
)
// Replay sends an edited HTTP request through the local recording proxy, so the
// exchange is captured by the proxy's sink like any other traffic and shows up
// in the Traffic list. It is driven only by an operator (POST /api/traffic/replay).
const (
// maxReplayBody bounds the request body an operator can replay.
maxReplayBody = 8 * 1024 * 1024
// maxReplayResponse bounds the response body returned to the caller; the
// recorded exchange keeps whatever the proxy captured.
maxReplayResponse = 2 * 1024 * 1024
// maxReplayHeaders bounds the number of request header lines.
maxReplayHeaders = 200
)
// ErrReplayInvalid marks a replay request rejected before anything is sent.
var ErrReplayInvalid = errors.New("invalid replay request")
// ReplayHeader is one request header line, kept as a list so duplicates and
// their order survive.
type ReplayHeader struct {
Name string `json:"name"`
Value string `json:"value"`
}
// ReplayRequest is the edited request to send.
type ReplayRequest struct {
Method string
URL string
Headers []ReplayHeader
Body []byte
}
// ReplayResult is the response as received by the client side of the proxy.
type ReplayResult struct {
Status int
Raw string // status line, headers and (possibly truncated) body
DurationMS int64
Truncated bool // the body exceeded maxReplayResponse and was cut
}
// replayDropped are the headers the client must not send as given: hop-by-hop
// fields and framing that net/http recomputes (Content-Length,
// Transfer-Encoding). Upgrade is rejected rather than dropped, see Replay.
var replayDropped = map[string]bool{
"Content-Length": true,
"Transfer-Encoding": true,
"Connection": true,
"Proxy-Connection": true,
"Keep-Alive": true,
"Te": true,
"Trailer": true,
"Expect": true,
}
func replayInvalid(format string, args ...any) error {
return fmt.Errorf("%w: %s", ErrReplayInvalid, fmt.Sprintf(format, args...))
}
// validHeaderName reports whether name is an RFC 9110 field name (a token).
func validHeaderName(name string) bool {
if name == "" {
return false
}
for i := 0; i < len(name); i++ {
c := name[i]
switch {
case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
case strings.IndexByte("!#$%&'*+-.^_`|~", c) >= 0:
default:
return false
}
}
return true
}
// Replay sends req through the recording proxy and returns the response. The
// proxy's sink records the exchange; Replay never calls record() itself. A host
// the proxy tunnels without interception (passthrough, see maybePassthrough)
// still answers, but that exchange is not recorded.
//
// Redirects are not followed, no cookie jar is kept, and compression is not
// negotiated: the edited headers go out as given, without an Accept-Encoding
// added by the transport.
func (t *Traffic) Replay(ctx context.Context, req ReplayRequest) (ReplayResult, error) {
method := strings.TrimSpace(req.Method)
if method == "" || strings.EqualFold(method, http.MethodConnect) {
return ReplayResult{}, replayInvalid("method must be set and cannot be CONNECT")
}
if !validHeaderName(method) {
return ReplayResult{}, replayInvalid("method %q is not a valid token", method)
}
target, err := url.Parse(strings.TrimSpace(req.URL))
if err != nil {
return ReplayResult{}, replayInvalid("url: %v", err)
}
if target.Scheme != "http" && target.Scheme != "https" {
return ReplayResult{}, replayInvalid("url must be absolute http or https")
}
if target.Host == "" || target.Hostname() == "" {
return ReplayResult{}, replayInvalid("url has no host")
}
if target.User != nil {
return ReplayResult{}, replayInvalid("url must not carry user info; send an Authorization header instead")
}
if len(req.Body) > maxReplayBody {
return ReplayResult{}, replayInvalid("body is %d bytes, over the %d-byte limit", len(req.Body), maxReplayBody)
}
if len(req.Headers) > maxReplayHeaders {
return ReplayResult{}, replayInvalid("%d headers, over the limit of %d", len(req.Headers), maxReplayHeaders)
}
header := http.Header{}
host := ""
for _, h := range req.Headers {
name := strings.TrimSpace(h.Name)
if !validHeaderName(name) {
return ReplayResult{}, replayInvalid("invalid header name %q", h.Name)
}
if strings.ContainsAny(h.Value, "\r\n\x00") {
return ReplayResult{}, replayInvalid("header %s contains a line break", name)
}
canonical := http.CanonicalHeaderKey(name)
switch {
case canonical == "Upgrade":
// A protocol upgrade (WebSocket, h2c) cannot be replayed as one request.
return ReplayResult{}, replayInvalid("Upgrade requests cannot be replayed")
case canonical == "Host":
// The recording proxy routes a request by its Host, not by the URL, so
// a different Host would silently send the request to another server.
host = strings.TrimSpace(h.Value)
if !strings.EqualFold(host, target.Host) {
return ReplayResult{}, replayInvalid("Host %q differs from the URL host %q; the proxy routes by Host, so edit the URL instead", host, target.Host)
}
case replayDropped[canonical]:
// recomputed by net/http, or hop-by-hop
default:
header.Add(canonical, h.Value)
}
}
if _, ok := header["User-Agent"]; !ok {
// An empty value keeps net/http from adding its default User-Agent: a
// header the operator removed stays removed.
header["User-Agent"] = []string{""}
}
client, closeIdle, err := t.replayClient()
if err != nil {
return ReplayResult{}, err
}
defer closeIdle()
var body io.Reader
if len(req.Body) > 0 {
body = bytes.NewReader(req.Body)
}
httpReq, err := http.NewRequestWithContext(ctx, method, target.String(), body)
if err != nil {
return ReplayResult{}, replayInvalid("%v", err)
}
httpReq.Header = header
if host != "" {
httpReq.Host = host
}
start := time.Now()
resp, err := client.Do(httpReq)
if err != nil {
// errors.Is(err, context.DeadlineExceeded) stays true through *url.Error.
return ReplayResult{}, fmt.Errorf("replay %s %s: %w", method, target.Redacted(), err)
}
defer resp.Body.Close()
data, err := io.ReadAll(io.LimitReader(resp.Body, maxReplayResponse+1))
if err != nil {
return ReplayResult{}, fmt.Errorf("replay %s %s: reading response: %w", method, target.Redacted(), err)
}
res := ReplayResult{Status: resp.StatusCode}
if len(data) > maxReplayResponse {
data = data[:maxReplayResponse]
res.Truncated = true
}
res.DurationMS = time.Since(start).Milliseconds()
res.Raw = fmt.Sprintf("%s %s\n%s\n%s", resp.Proto, resp.Status, headerLines(resp.Header), data)
return res, nil
}
// replayClient builds a one-shot client that goes through the local proxy and
// trusts its CA. The system roots stay trusted too, so a passthrough host (whose
// real certificate reaches the client) still verifies.
func (t *Traffic) replayClient() (*http.Client, func(), error) {
proxyURL, err := url.Parse(t.ProxyAddr())
if err != nil {
return nil, nil, fmt.Errorf("proxy address: %w", err)
}
if proxyURL.Hostname() == "0.0.0.0" {
proxyURL.Host = "127.0.0.1:" + proxyURL.Port()
}
pem, err := os.ReadFile(t.CACertPath())
if err != nil {
return nil, nil, fmt.Errorf("proxy CA: %w", err)
}
roots, err := x509.SystemCertPool()
if err != nil || roots == nil {
roots = x509.NewCertPool()
}
if !roots.AppendCertsFromPEM(pem) {
return nil, nil, fmt.Errorf("proxy CA: no certificate in %s", t.CACertPath())
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{RootCAs: roots, MinVersion: tls.VersionTLS12},
DisableCompression: true,
}
client := &http.Client{
Transport: transport,
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
}
return client, transport.CloseIdleConnections, nil
}
+226
View File
@@ -0,0 +1,226 @@
package traffic
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
// echoed is what the echo target saw.
type echoed struct {
Method string `json:"method"`
Path string `json:"path"`
Query string `json:"query"`
Custom string `json:"custom"`
Connection string `json:"connection"`
ContentLength int64 `json:"content_length"`
Body string `json:"body"`
Host string `json:"host"`
}
func echoHandler(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Echo", "yes")
_ = json.NewEncoder(w).Encode(echoed{
Method: r.Method, Path: r.URL.Path, Query: r.URL.RawQuery, Custom: r.Header.Get("X-Custom"),
Connection: r.Header.Get("Connection"), ContentLength: r.ContentLength, Body: string(b), Host: r.Host,
})
}
// replayBody returns the JSON body of a replayed echo response.
func replayBody(t *testing.T, res ReplayResult) echoed {
t.Helper()
i := strings.Index(res.Raw, "\n{")
if i < 0 {
t.Fatalf("no JSON body in raw response:\n%s", res.Raw)
}
var e echoed
if err := json.Unmarshal([]byte(strings.TrimSpace(res.Raw[i+1:])), &e); err != nil {
t.Fatalf("decode echo: %v in %q", err, res.Raw)
}
return e
}
func recordedExchanges(t *testing.T, tr *Traffic, method, url string) int {
t.Helper()
var n int
if err := tr.DB().QueryRow(`SELECT COUNT(*) FROM exchanges WHERE method=? AND url=?`, method, url).Scan(&n); err != nil {
t.Fatal(err)
}
return n
}
func TestReplayThroughProxy(t *testing.T) {
tr, _, _ := startProxy(t)
plain := httptest.NewServer(http.HandlerFunc(echoHandler))
t.Cleanup(plain.Close)
tlsSrv := httptest.NewTLSServer(http.HandlerFunc(echoHandler))
t.Cleanup(tlsSrv.Close)
for _, tc := range []struct{ name, base string }{
{"http", plain.URL},
{"https", localhostURL(tlsSrv.URL)}, // trusted through the proxy CA
} {
t.Run(tc.name, func(t *testing.T) {
target := tc.base + "/edited/path?q=1&r=two"
res, err := tr.Replay(t.Context(), ReplayRequest{
Method: "PATCH",
URL: target,
Headers: []ReplayHeader{{Name: "X-Custom", Value: "edited"}, {Name: "Content-Type", Value: "text/plain"}},
Body: []byte("edited body"),
})
if err != nil {
t.Fatal(err)
}
if res.Status != 200 || res.Truncated || !strings.HasPrefix(res.Raw, "HTTP/1.1 200 OK\n") || !strings.Contains(res.Raw, "X-Echo: yes") {
t.Fatalf("result status=%d truncated=%v raw:\n%s", res.Status, res.Truncated, res.Raw)
}
got := replayBody(t, res)
if got.Method != "PATCH" || got.Path != "/edited/path" || got.Query != "q=1&r=two" || got.Custom != "edited" || got.Body != "edited body" {
t.Fatalf("target received %+v", got)
}
waitFor(t, "replayed exchange recorded", func() bool { return recordedExchanges(t, tr, "PATCH", target) == 1 })
})
}
}
func TestReplayDropsHopByHopHeaders(t *testing.T) {
tr, _, _ := startProxy(t)
srv := httptest.NewServer(http.HandlerFunc(echoHandler))
t.Cleanup(srv.Close)
res, err := tr.Replay(t.Context(), ReplayRequest{
Method: "POST",
URL: srv.URL + "/hop",
Headers: []ReplayHeader{
{Name: "Connection", Value: "close"},
{Name: "Content-Length", Value: "999"},
{Name: "Keep-Alive", Value: "timeout=5"},
{Name: "Host", Value: strings.TrimPrefix(srv.URL, "http://")},
},
Body: []byte("abc"),
})
if err != nil {
t.Fatal(err)
}
got := replayBody(t, res)
if got.Connection != "" || got.ContentLength != 3 || got.Body != "abc" {
t.Fatalf("target received connection=%q content_length=%d body=%q, want no Connection and a recomputed length", got.Connection, got.ContentLength, got.Body)
}
if got.Host != strings.TrimPrefix(srv.URL, "http://") {
t.Fatalf("target saw Host %q", got.Host)
}
}
func TestReplayDoesNotFollowRedirects(t *testing.T) {
tr, _, _ := startProxy(t)
var followed atomic.Bool
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/next" {
followed.Store(true)
return
}
http.Redirect(w, r, "/next", http.StatusFound)
}))
t.Cleanup(srv.Close)
res, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: srv.URL + "/start"})
if err != nil {
t.Fatal(err)
}
if res.Status != 302 || !strings.Contains(res.Raw, "Location: /next") || followed.Load() {
t.Fatalf("status %d followed=%v raw:\n%s", res.Status, followed.Load(), res.Raw)
}
}
func TestReplayTruncatesLargeResponse(t *testing.T) {
tr, _, _ := startProxy(t)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, strings.Repeat("x", maxReplayResponse+100))
}))
t.Cleanup(srv.Close)
res, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: srv.URL + "/big"})
if err != nil {
t.Fatal(err)
}
head, body, _ := strings.Cut(res.Raw, "\n\n")
if !res.Truncated || len(body) != maxReplayResponse || head == "" {
t.Fatalf("truncated=%v body %d bytes, want %d", res.Truncated, len(body), maxReplayResponse)
}
}
func TestReplayTimeoutIsRecognizable(t *testing.T) {
tr, _, _ := startProxy(t)
release := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-release:
case <-time.After(5 * time.Second):
}
}))
t.Cleanup(func() { close(release); srv.Close() })
ctx, cancel := context.WithTimeout(t.Context(), 200*time.Millisecond)
defer cancel()
_, err := tr.Replay(ctx, ReplayRequest{Method: "GET", URL: srv.URL + "/slow"})
if !errors.Is(err, context.DeadlineExceeded) || errors.Is(err, ErrReplayInvalid) {
t.Fatalf("err = %v, want a deadline error", err)
}
}
// A passthrough host is tunneled, not intercepted: the client sees the target's
// own certificate (here an untrusted httptest one, so the TLS handshake fails)
// and nothing is recorded. Pins the documented limitation.
func TestReplayPassthroughHostIsNotIntercepted(t *testing.T) {
tr, _, _ := startProxy(t)
srv := httptest.NewTLSServer(http.HandlerFunc(echoHandler))
t.Cleanup(srv.Close)
tr.pass.Store("localhost", struct{}{})
target := localhostURL(srv.URL) + "/tunneled"
_, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: target})
if err == nil || !strings.Contains(err.Error(), "certificate") {
t.Fatalf("err = %v, want the target's own certificate to be rejected", err)
}
if n := recordedExchanges(t, tr, "GET", target); n != 0 {
t.Fatalf("%d exchanges recorded for a passthrough host", n)
}
}
// Validation happens before anything is sent, so the proxy need not run.
func TestReplayRejectsInvalidRequests(t *testing.T) {
tr, err := Open(t.TempDir(), "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = tr.Close() })
many := make([]ReplayHeader, maxReplayHeaders+1)
for i := range many {
many[i] = ReplayHeader{Name: fmt.Sprintf("X-H%d", i), Value: "v"}
}
for name, req := range map[string]ReplayRequest{
"ftp scheme": {Method: "GET", URL: "ftp://example.com/x"},
"relative url": {Method: "GET", URL: "/x"},
"no host": {Method: "GET", URL: "http:///x"},
"user info": {Method: "GET", URL: "http://user:pass@example.com/"},
"empty method": {Method: " ", URL: "http://example.com/"},
"CONNECT": {Method: "CONNECT", URL: "http://example.com/"},
"method not token": {Method: "GE T", URL: "http://example.com/"},
"body too large": {Method: "POST", URL: "http://example.com/", Body: make([]byte, maxReplayBody+1)},
"too many headers": {Method: "GET", URL: "http://example.com/", Headers: many},
"CR in header": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "X-A", Value: "a\r\nX-Injected: 1"}}},
"LF in header": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "X-A", Value: "a\nb"}}},
"invalid name": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Bad Name", Value: "v"}}},
"upgrade websocket": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Upgrade", Value: "websocket"}}},
"host differs": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Host", Value: "other.example"}}},
} {
if _, err := tr.Replay(t.Context(), req); !errors.Is(err, ErrReplayInvalid) {
t.Errorf("%s: err = %v, want ErrReplayInvalid", name, err)
}
}
}