fix(server): dial the target fingerprint through the pinned target gate

GHSA-589f: FingerprintTarget checked the target before the request but built
its client on http.DefaultTransport, so the environment proxy and a fresh DNS
resolution could reach loopback, private or instance-metadata addresses after
the check. The client now uses its own transport, never the *_PROXY
environment, and dials through the gate's pinned dialer, which resolves again
and refuses an address the pin does not cover. The probe requests reuse the
same transport.
This commit is contained in:
BaaRai 2026-11-30 23:54:02 +01:00
parent d48a3a74e8
commit 7c8bddafbb
2 changed files with 110 additions and 1 deletions

View file

@ -131,8 +131,11 @@ func (s *Server) FingerprintTarget(ctx context.Context, t *Task, goalURL string)
var redirects []string
var crossHostRedirect string
transport := fingerprintTransport()
defer transport.CloseIdleConnections()
client := &http.Client{
Timeout: 3 * time.Second,
Transport: transport,
Timeout: 3 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
req.URL.User = nil
req.Header.Del("Authorization")
@ -312,6 +315,26 @@ func (s *Server) FingerprintTarget(ctx context.Context, t *Task, goalURL string)
return fp, factID, nil
}
// fingerprintTransport is the transport of the fingerprint client. It never
// reads the *_PROXY environment: a proxy would be dialled instead of the target
// and would resolve the name itself. It connects through the target gate's
// pinned dialer: in a gated scope every connection (the goal URL, a redirect
// hop, a probe path) resolves the name again at the dial and goes only to the
// addresses a pin covers, so a name that answers another address after the
// gate check (loopback, private, instance metadata) is not reached. The
// fingerprint always runs with the task's scope on its context; a scope lost
// on the way fails the dial closed.
func fingerprintTransport() *http.Transport {
base, ok := http.DefaultTransport.(*http.Transport)
if !ok {
base = &http.Transport{}
}
tr := base.Clone()
tr.Proxy = nil
tr.DialContext = intercept.PinnedDialer("target_fingerprint", true)
return tr
}
func probeHeadThenGet(ctx context.Context, client *http.Client, target string) int {
req, err := http.NewRequestWithContext(ctx, http.MethodHead, target, nil)
if err != nil {

View file

@ -0,0 +1,86 @@
package server
import (
"net/http"
"net/url"
"slices"
"strings"
"testing"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/artex/intercept"
)
// The target fingerprint connects through its own transport: no environment
// proxy, and in a gated scope the pinned dialer, which resolves the name again
// at the dial and connects only to addresses a pin covers.
// fingerprintGate is a gated task whose goal host app.example.com:<port> is
// pinned at 203.0.113.7, routed to a local counting server. Clients cloned from
// http.DefaultTransport fail the test, through its dialer and through an
// "environment" proxy, so only the fingerprint's own transport can connect.
func fingerprintGate(t *testing.T) (g *gateServer, task *Task, res *countingResolver, dialer *routeDialer, goal, port string, hits func() int64) {
t.Helper()
g = newGateServer(t)
on := true
task = g.newTask(t, db.TaskCreateOptions{TargetGate: &on})
srv, h, _, port := countingServer(t)
res = &countingResolver{answers: map[string][]string{"app.example.com": {"203.0.113.7"}}}
g.m.interceptor.SetResolver(res)
dialer = newRouteDialer(g)
dialer.route("203.0.113.7:"+port, srv.Listener.Addr().String())
key := "app.example.com:" + port
goal = "http://" + key + "/"
if r := intercept.GateRenderedURL(gatedCtx(task, 0), "target_fingerprint", goal); r.Code != intercept.TargetApprovalPending {
t.Fatalf("goal before the approval: %+v", r)
}
id, _, _, ok := openTargetRequest(t, g.m.pg, task.ID, key)
if !ok {
t.Fatalf("no request on %s", key)
}
decide(t, g, id, "allowed")
failDefaultTransportDials(t)
envProxy, _ := url.Parse("http://env-proxy.invalid:3128")
http.DefaultTransport.(*http.Transport).Proxy = http.ProxyURL(envProxy)
return g, task, res, dialer, goal, port, h.Load
}
// A public, pinned target is fetched at its pinned address, by the
// fingerprint's own transport.
func TestFingerprintDialsPinnedAddress(t *testing.T) {
g, task, _, dialer, goal, port, hits := fingerprintGate(t)
fp, _, err := g.s.FingerprintTarget(t.Context(), task, goal)
if err != nil || fp == nil || fp.Status != http.StatusOK || hits() != 1 {
t.Fatalf("pinned target: fp=%+v err=%v hits=%d", fp, err, hits())
}
if got := dialer.calls(); !slices.Equal(got, []string{"203.0.113.7:" + port}) {
t.Fatalf("dialled %v, want the pinned address once", got)
}
}
// Once the name answers loopback, a private or a metadata address, the
// rendered-URL gate passes on its cached answer and the dial refuses: nothing
// connects. The refusal comes from the dial ("was not opened"), not from the
// URL gate. One task per address: the refused dial opens an approval request,
// which the URL gate of a later call on the same task would wait on.
func TestFingerprintRefusesReboundTargetAtDial(t *testing.T) {
for _, ip := range []string{"127.0.0.1", "10.0.0.5", "169.254.169.254"} {
t.Run(ip, func(t *testing.T) {
g, task, res, dialer, goal, _, hits := fingerprintGate(t)
res.set("app.example.com", ip)
fp, _, err := g.s.FingerprintTarget(t.Context(), task, goal)
if err == nil || fp != nil || !strings.Contains(err.Error(), intercept.TargetApprovalPending+": the connection to") ||
!strings.Contains(err.Error(), "was not opened") {
t.Fatalf("rebound to %s: fp=%+v err=%v, want a refusal at the dial", ip, fp, err)
}
if hits() != 0 {
t.Fatalf("rebound to %s: the target was reached (%d hits)", ip, hits())
}
if got := dialer.calls(); len(got) != 0 {
t.Fatalf("dialled %v, want nothing", got)
}
})
}
}