package httpapi import ( "net/netip" "testing" ) func TestWebhookNetworkRestrictions(t *testing.T) { for _, raw := range []string{"http://example.com/callback", "https://127.0.0.1/cb", "https://[::1]/cb", "https://[::ffff:127.0.0.1]/cb", "https://10.0.0.1/cb", "https://100.64.1.2/cb", "https://169.254.169.254/latest", "https://user:password@example.com/cb", "https://example.com:9443/cb", "https://example.com/cb?token=secret", "https://localhost/cb"} { if _, e := validateWebhookURL(raw); e == nil { t.Fatalf("accepted unsafe callback %q", raw) } } if _, e := validateWebhookURL("https://billing.example.com/callback"); e != nil { t.Fatal(e) } for _, ip := range []string{"192.168.1.2", "0.0.0.0", "224.0.0.1", "198.18.0.1", "2001:db8::1", "fc00::1"} { if publicWebhookIP(netip.MustParseAddr(ip)) { t.Fatalf("accepted %s", ip) } } } func TestWebhookSignatureBindsTimeEventAndExactBody(t *testing.T) { signature := webhookSignature("secret", "100", "event", []byte(`{"id":1}`)) for _, s := range []string{webhookSignature("other", "100", "event", []byte(`{"id":1}`)), webhookSignature("secret", "101", "event", []byte(`{"id":1}`)), webhookSignature("secret", "100", "another", []byte(`{"id":1}`)), webhookSignature("secret", "100", "event", []byte(`{ "id":1}`))} { if signature == s { t.Fatal("signature failed to bind request") } } }