ARTEX/server/target_fingerprint_dial_test.go
BaaRai 7c8bddafbb 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.
2026-11-30 23:54:02 +01:00

86 lines
3.5 KiB
Go

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)
}
})
}
}