Files
ARTEX/traffic/pace.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

97 lines
3.6 KiB
Go

package traffic
import (
"context"
"log/slog"
"net"
"net/http"
"strings"
"time"
"github.com/Autumn-27/artex/egress"
mproxy "github.com/lqqyt2423/go-mitmproxy/proxy"
)
// paceWaitMax bounds the pacing wait of one request. Past it the request is
// sent unpaced: pacing never blocks a command indefinitely.
const paceWaitMax = 30 * time.Second
// Requestheaders runs for every request the proxy sees: the CONNECT that opens
// a tunnel, each request inside an intercepted tunnel, and each plain-HTTP
// request. The recording proxy takes no credentials, so a Proxy-Authorization
// header is only ever meant for it (clients send the proxy URL's userinfo
// there): it is dropped here, before anything is forwarded, and never reaches
// the target or the recording. When it carries the pace marker
// (egress.PaceUser), the client connection is marked; every request on it,
// including those inside its tunnel, is then paced by the current noise
// profile before it goes upstream. Unmarked connections are not paced here.
// A plain-HTTP request for another host than the client connection's upstream
// connection is sent on a connection of its own (otherUpstream).
func (s *sink) Requestheaders(f *mproxy.Flow) {
if f.Request == nil || f.ConnContext == nil || f.ConnContext.ClientConn == nil {
return
}
id := f.ConnContext.ClientConn.Id
if egress.IsPaceMarker(f.Request.Header.Get("Proxy-Authorization")) {
s.paced.Store(id, struct{}{})
}
f.Request.Header.Del("Proxy-Authorization")
if f.Request.Method == http.MethodConnect || f.Request.URL == nil {
return
}
if otherUpstream(f) {
f.UseSeparateClient = true
}
if _, marked := s.paced.Load(id); marked {
pace(f)
}
}
// otherUpstream reports whether a plain-HTTP request names another host:port
// than the upstream connection go-mitmproxy keeps for its client connection.
// go-mitmproxy (v1.9.2) dials that connection for the host of the first
// plain-HTTP request and sends every later request of the client connection
// over it, unless an addon rewrote the URL. A keep-alive client (Go, curl)
// shares one proxy connection across plain-HTTP hosts, so a request for host
// b, with b's Cookie and Authorization headers, was delivered to host a. Such
// a request takes go-mitmproxy's separate client instead, which dials the
// request's own host through the same upstream proxy setting. Requests inside
// an intercepted TLS tunnel are left alone: the tunnel names its server.
func otherUpstream(f *mproxy.Flow) bool {
cc := f.ConnContext
if cc.ServerConn == nil || cc.ClientConn.Tls || !strings.EqualFold(f.Request.URL.Scheme, "http") {
return false
}
port := f.Request.URL.Port()
if port == "" {
port = "80"
}
return !strings.EqualFold(net.JoinHostPort(f.Request.URL.Hostname(), port), cc.ServerConn.Address)
}
// ClientDisconnected forgets a closed connection's mark, and asks for a sweep
// of the request snapshots its flows may have left (rewrite_flow.go).
func (s *sink) ClientDisconnected(c *mproxy.ClientConn) {
if c != nil {
s.paced.Delete(c.Id)
}
if s.t != nil {
s.t.snaps.requestSweep()
}
}
// pace waits for the request's host under the current noise profile, bounded
// by paceWaitMax and by the request's own context. Off returns at once.
func pace(f *mproxy.Flow) {
ctx := context.Background()
if raw := f.Request.Raw(); raw != nil {
ctx = raw.Context()
}
ctx, cancel := context.WithTimeout(ctx, paceWaitMax)
defer cancel()
host := egress.HostKey(f.Request.URL)
if err := egress.Wait(ctx, host); err != nil {
slog.Warn("[traffic] pacing request stopped waiting, sending unpaced", "host", host, "err", err)
}
}