Files
archie/modules/events/clientaddr_test.go
T
sam 4707427759 feat: trust configured proxies for webhook intake
- add trusted proxy settings, migration, API, and UI
- derive client addresses from X-Forwarded-For behind trusted hops
- enforce delivery policy for listener batches and record denials
2026-11-22 22:29:36 +11:00

75 lines
3.9 KiB
Go

package events
import (
"context"
"errors"
"log/slog"
"net/http"
"net/http/httptest"
"net/netip"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/platform/httpjson"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/events/internal/store"
"github.com/samcharles93/archie-core/modules/governance"
)
// TestIntakeEvaluatesThePolicyAgainstTheClientBehindTrustedProxies guards the
// address passed to the delivery policy: X-Forwarded-For is used only when the
// peer is a configured proxy, and is read from right to left past trusted
// hops, so a client can neither hide behind the proxy's address nor spoof one.
func TestIntakeEvaluatesThePolicyAgainstTheClientBehindTrustedProxies(t *testing.T) {
proxies := []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8"), netip.MustParsePrefix("192.0.2.1/32")}
tests := []struct {
name string
peer string
headers []string
trusted []netip.Prefix
readErr error
want string
}{
{name: "no proxies configured uses the peer", peer: "203.0.113.9:4711", want: "203.0.113.9"},
{name: "a direct client can't forge a forwarded address", peer: "203.0.113.9:4711", headers: []string{"198.51.100.7"}, trusted: proxies, want: "203.0.113.9"},
{name: "a trusted proxy reports the client", peer: "10.0.0.5:4711", headers: []string{"198.51.100.7"}, trusted: proxies, want: "198.51.100.7"},
{name: "a forged entry left of the real client is ignored", peer: "10.0.0.5:4711", headers: []string{"1.2.3.4, 198.51.100.7"}, trusted: proxies, want: "198.51.100.7"},
{name: "a chain of trusted proxies is skipped", peer: "10.0.0.5:4711", headers: []string{"198.51.100.7, 10.0.0.9, 192.0.2.1"}, trusted: proxies, want: "198.51.100.7"},
{name: "multiple header lines are joined into one list", peer: "10.0.0.5:4711", headers: []string{"1.2.3.4", "198.51.100.7"}, trusted: proxies, want: "198.51.100.7"},
{name: "a trusted proxy with no header uses the peer", peer: "10.0.0.5:4711", trusted: proxies, want: "10.0.0.5"},
{name: "an unreadable entry uses the peer", peer: "10.0.0.5:4711", headers: []string{"198.51.100.7, unknown"}, trusted: proxies, want: "10.0.0.5"},
{name: "an unreadable proxy list uses the peer", peer: "10.0.0.5:4711", headers: []string{"198.51.100.7"}, readErr: errors.New("store down"), want: "10.0.0.5"},
{name: "an IPv6 client address has no brackets", peer: "10.0.0.5:4711", headers: []string{"2001:db8::7"}, trusted: proxies, want: "2001:db8::7"},
}
const token = "not-a-real-token" //nolint:gosec // fixture bearer token the verifier must accept
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
policy := &deliveryPolicy{decision: governance.Allowed()}
m := &Module{
orgID: "default", log: slog.New(slog.DiscardHandler), inserter: &onceInserter{stored: map[string]bool{}}, deliveries: noEcho{},
reader: fakeSources{store.Source{OrgID: "default", Name: "hook", Connector: "c", Verification: store.Token, TokenSHA256: httpjson.TokenHash(token)}},
parse: func(context.Context, string, []byte, map[string]string) ([]connectors.Event, error) {
return []connectors.Event{{Type: "alert", SourceEventID: "evt-1"}}, nil
},
}
m.UseDeliveryPolicy(policy)
m.UseTrustedProxies(func(context.Context) ([]netip.Prefix, error) { return tt.trusted, tt.readErr })
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/hooks/hook", strings.NewReader(`{}`))
req.SetPathValue("source", "hook")
req.RemoteAddr = tt.peer
req.Header.Set("Authorization", "Bearer "+token)
for _, h := range tt.headers {
req.Header.Add("X-Forwarded-For", h)
}
rec := httptest.NewRecorder()
m.intake(rec, req)
if rec.Code != http.StatusAccepted {
t.Fatalf("status %d, want %d", rec.Code, http.StatusAccepted)
}
if want := "org-sys hook " + tt.want; len(policy.asked) != 1 || policy.asked[0] != want {
t.Fatalf("delivery policy asked %v, want %q", policy.asked, want)
}
})
}
}