Open platform to collect and prioritize feedback
feat(webhooks): WEBHOOK_ALLOW_PRIVATE_IPS env to allow internal targets
hody25a735d5ccf6
diff --git a/app/pkg/env/env.go b/app/pkg/env/env.goindex e456e3690..889c5d2bb 100644--- a/app/pkg/env/env.go+++ b/app/pkg/env/env.go@@ -56,6 +56,7 @@ type config struct {JWTSecret string `env:"JWT_SECRET,required"`PostCreationWithTagsEnabled bool `env:"POST_CREATION_WITH_TAGS_ENABLED,default=false"`AllowAllowedSchemes bool `env:"ALLOW_ALLOWED_SCHEMES,default=true"`+ WebhookAllowPrivateIPs bool `env:"WEBHOOK_ALLOW_PRIVATE_IPS,default=false"`Stripe struct {SecretKey string `env:"STRIPE_SECRET_KEY"`WebhookSecret string `env:"STRIPE_WEBHOOK_SECRET"`diff --git a/app/pkg/validate/general.go b/app/pkg/validate/general.goindex 6e6de317a..4c456d561 100644--- a/app/pkg/validate/general.go+++ b/app/pkg/validate/general.go@@ -56,7 +56,11 @@ func URL(ctx context.Context, rawurl string) []string {return []string{}}-// WebhookURL validates that a URL is well-formed and does not target private/internal networks+// WebhookURL validates that a URL is well-formed and, by default, does not target+// private/internal networks. Self-hosted instances that need to reach internal+// services (e.g. an integration server on a LAN) can opt out of the network+// check by setting WEBHOOK_ALLOW_PRIVATE_IPS=true; format validation (valid URL,+// scheme http/https) still applies in that mode.func WebhookURL(rawurl string) []string {u, err := url.Parse(rawurl)if err != nil || u.Host == "" {@@ -68,6 +72,10 @@ func WebhookURL(rawurl string) []string {return []string{"Only http and https URLs are allowed."}}+ if env.Config.WebhookAllowPrivateIPs {+ return []string{}+ }+hostname := u.Hostname()if strings.EqualFold(hostname, "localhost") {diff --git a/app/pkg/validate/general_test.go b/app/pkg/validate/general_test.goindex eb07ad2f6..357523880 100644--- a/app/pkg/validate/general_test.go+++ b/app/pkg/validate/general_test.go@@ -7,6 +7,7 @@ import ("github.com/getfider/fider/app/models/query". "github.com/getfider/fider/app/pkg/assert""github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/rand""github.com/getfider/fider/app/pkg/validate")@@ -114,6 +115,45 @@ func TestWebhookURL_AllowedAddresses(t *testing.T) {}}+func TestWebhookURL_PrivateIPsAllowedWhenOptedIn(t *testing.T) {+ RegisterT(t)++ original := env.Config.WebhookAllowPrivateIPs+ env.Config.WebhookAllowPrivateIPs = true+ t.Cleanup(func() { env.Config.WebhookAllowPrivateIPs = original })++ for _, rawurl := range []string{+ "http://localhost/hook",+ "http://127.0.0.1/hook",+ "http://10.0.0.1/hook",+ "http://172.16.0.1/hook",+ "http://192.168.1.1:8080/hook",+ "http://[::1]/hook",+ "http://internal.lan/hook",+ } {+ messages := validate.WebhookURL(rawurl)+ Expect(messages).HasLen(0)+ }+}++func TestWebhookURL_OptInDoesNotBypassFormatValidation(t *testing.T) {+ RegisterT(t)++ original := env.Config.WebhookAllowPrivateIPs+ env.Config.WebhookAllowPrivateIPs = true+ t.Cleanup(func() { env.Config.WebhookAllowPrivateIPs = original })++ for _, rawurl := range []string{+ "ftp://example.com/hook",+ "file:///etc/passwd",+ "not a url at all",+ "",+ } {+ messages := validate.WebhookURL(rawurl)+ Expect(len(messages) > 0).IsTrue()+ }+}+func TestInvalidCNAME(t *testing.T) {RegisterT(t)