A source is keyed by a uuid, owned by the organisation or one person, and labelled within that scope. The webhook path is a separate unique column, so renaming either leaves stored events, event types, triggers, groups, mappings and drafts alone. Delivery policies, sealed secrets and group keys use the id.
75 lines
4.0 KiB
Go
75 lines
4.0 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{ID: hookID, OrgID: "default", WebhookPath: "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("path", "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 " + hookID.String() + " " + tt.want; len(policy.asked) != 1 || policy.asked[0] != want {
|
|
t.Fatalf("delivery policy asked %v, want %q", policy.asked, want)
|
|
}
|
|
})
|
|
}
|
|
}
|