ARTEX/traffic/separate_client_test.go
BaaRai b1c9d4e06e fix(traffic): never send a plain-HTTP request on another host's connection
GHSA-q37j: with capture enabled, go-mitmproxy dials one upstream per client
connection and reuses it for later plain-HTTP requests, so a request for host
B, with B's Cookie and Authorization headers, was delivered to host A. The
sink now sets UseSeparateClient when the request names another host:port than
the connection's upstream, so each host gets its own connection. Intercepted
TLS tunnels are unchanged.
2026-11-30 23:54:03 +01:00

117 lines
4.4 KiB
Go

package traffic
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
"testing"
"github.com/Autumn-27/artex/egress"
mproxy "github.com/lqqyt2423/go-mitmproxy/proxy"
)
// go-mitmproxy dials one upstream connection per client connection, for the
// host of the first plain-HTTP request, and sends every later request of that
// client connection over it unless an addon rewrote the URL. The sink makes a
// request for another host:port take the separate client, which dials the
// request's own host.
func TestRequestheadersSeparatesPlainHTTPHostChange(t *testing.T) {
prev := egress.CurrentProfile()
t.Cleanup(func() { egress.SetProfile(prev) })
egress.SetProfile(egress.Profile{Name: "off"})
s := &sink{}
for _, c := range []struct {
name, method, rawURL string
upstream string // ServerConn.Address; "" = none dialled yet
tls bool // a request inside an intercepted TLS tunnel
want bool
}{
{"other host", http.MethodGet, "http://b.example/x", "a.example:80", false, true},
{"other port", http.MethodGet, "http://a.example:8080/x", "a.example:80", false, true},
{"other IPv6 host", http.MethodGet, "http://[::2]/x", "[::1]:80", false, true},
{"same host, default port", http.MethodGet, "http://a.example/x", "a.example:80", false, false},
{"same host, explicit port", http.MethodPost, "http://a.example:8080/x", "a.example:8080", false, false},
{"same host, other case", http.MethodGet, "http://A.Example/x", "a.example:80", false, false},
{"first request, nothing dialled", http.MethodGet, "http://b.example/x", "", false, false},
{"inside a TLS tunnel", http.MethodGet, "https://b.example/x", "a.example:443", true, false},
{"CONNECT", http.MethodConnect, "//b.example:443", "a.example:80", false, false},
} {
u, err := url.Parse(c.rawURL)
if err != nil {
t.Fatal(err)
}
cc := &mproxy.ConnContext{ClientConn: &mproxy.ClientConn{Tls: c.tls}}
if c.upstream != "" {
cc.ServerConn = &mproxy.ServerConn{Address: c.upstream}
}
f := &mproxy.Flow{Request: &mproxy.Request{Method: c.method, URL: u, Header: http.Header{}}, ConnContext: cc}
s.Requestheaders(f)
if f.UseSeparateClient != c.want {
t.Errorf("%s: UseSeparateClient = %v, want %v", c.name, f.UseSeparateClient, c.want)
}
}
}
// End to end through the real recording proxy: a keep-alive client sends a
// plain-HTTP request to one host, then a request carrying a Cookie and an
// Authorization header to another host. Go's transport reuses one proxy
// connection for plain-HTTP targets on different hosts. The second request
// must reach its own host, never the first one.
func TestPlainHTTPKeepAliveDoesNotCrossHosts(t *testing.T) {
prev := egress.CurrentProfile()
t.Cleanup(func() { egress.SetProfile(prev) })
egress.SetProfile(egress.Profile{Name: "off"})
type seen struct{ path, cookie, auth string }
var mu sync.Mutex
got := map[string][]seen{}
server := func(name string) *httptest.Server {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
got[name] = append(got[name], seen{r.URL.Path, r.Header.Get("Cookie"), r.Header.Get("Authorization")})
mu.Unlock()
_, _ = io.WriteString(w, name)
}))
t.Cleanup(srv.Close)
return srv
}
a, b := server("a"), server("b")
tr, _, proxyURL := startProxy(t)
client := proxyClient(t, tr, proxyURL)
t.Cleanup(client.CloseIdleConnections)
body := func(req *http.Request) string {
t.Helper()
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
raw, _ := io.ReadAll(resp.Body)
return string(raw)
}
first, _ := http.NewRequest(http.MethodGet, a.URL+"/first", nil)
if out := body(first); out != "a" {
t.Fatalf("first request answered %q, want a", out)
}
second, _ := http.NewRequest(http.MethodGet, b.URL+"/second", nil)
second.Header.Set("Cookie", "session=b-secret")
second.Header.Set("Authorization", "Bearer b-token")
out := body(second)
mu.Lock()
defer mu.Unlock()
for _, r := range got["a"] {
if r.path == "/second" || strings.Contains(r.cookie, "b-secret") || strings.Contains(r.auth, "b-token") {
t.Fatalf("host a received the request meant for host b: %+v (all of a: %+v)", r, got["a"])
}
}
if out != "b" || len(got["b"]) != 1 || got["b"][0].cookie != "session=b-secret" || got["b"][0].auth != "Bearer b-token" {
t.Fatalf("second request answered %q; host b received %+v, want it once with its credentials", out, got["b"])
}
}