Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Fix SSRF vulnerability in webhook URLs (GHSA-g445-xwm7-594r)

Matt Robertsf7db86037234
diff --git a/app/actions/webhook.go b/app/actions/webhook.go
index 537a9eb31..a1df81179 100644
--- a/app/actions/webhook.go
+++ b/app/actions/webhook.go
@@ -73,7 +73,7 @@ func (action *CreateEditWebhook) Validate(ctx context.Context, _ *entity.User) *
if previewWebhook.Result.Url.Error != "" {
result.AddFieldFailure("url", "URL template must compile to enable the Webhook.")
- } else if messages := validate.URL(ctx, previewWebhook.Result.Url.Value); len(messages) > 0 {
+ } else if messages := validate.WebhookURL(previewWebhook.Result.Url.Value); len(messages) > 0 {
result.AddFieldFailure("url", messages...)
}
diff --git a/app/pkg/validate/general.go b/app/pkg/validate/general.go
index 0843c49ed..6e6de317a 100644
--- a/app/pkg/validate/general.go
+++ b/app/pkg/validate/general.go
@@ -2,6 +2,7 @@ package validate
import (
"context"
+ "net"
"net/url"
"regexp"
"strings"
@@ -55,6 +56,53 @@ 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
+func WebhookURL(rawurl string) []string {
+ u, err := url.Parse(rawurl)
+ if err != nil || u.Host == "" {
+ return []string{"This URL is not valid."}
+ }
+
+ scheme := strings.ToLower(u.Scheme)
+ if scheme != "http" && scheme != "https" {
+ return []string{"Only http and https URLs are allowed."}
+ }
+
+ hostname := u.Hostname()
+
+ if strings.EqualFold(hostname, "localhost") {
+ return []string{"This URL is not allowed because it targets a private or internal network address."}
+ }
+
+ if ip := net.ParseIP(hostname); ip != nil {
+ if isBlockedIP(ip) {
+ return []string{"This URL is not allowed because it targets a private or internal network address."}
+ }
+ return []string{}
+ }
+
+ addrs, err := net.LookupHost(hostname)
+ if err != nil {
+ return []string{"Could not resolve the hostname in this URL."}
+ }
+
+ for _, addr := range addrs {
+ if ip := net.ParseIP(addr); ip != nil && isBlockedIP(ip) {
+ return []string{"This URL is not allowed because it targets a private or internal network address."}
+ }
+ }
+
+ return []string{}
+}
+
+func isBlockedIP(ip net.IP) bool {
+ return ip.IsLoopback() ||
+ ip.IsPrivate() ||
+ ip.IsLinkLocalUnicast() ||
+ ip.IsLinkLocalMulticast() ||
+ ip.IsUnspecified()
+}
+
//CNAME validates given cname
func CNAME(ctx context.Context, cname string) []string {
cname = strings.ToLower(cname)
diff --git a/app/pkg/validate/general_test.go b/app/pkg/validate/general_test.go
index 67291e51a..eb07ad2f6 100644
--- a/app/pkg/validate/general_test.go
+++ b/app/pkg/validate/general_test.go
@@ -72,6 +72,48 @@ func TestValidURL(t *testing.T) {
}
}
+func TestWebhookURL_BlockedAddresses(t *testing.T) {
+ RegisterT(t)
+
+ for _, rawurl := range []string{
+ "http://localhost/hook",
+ "http://localhost:8080/hook",
+ "http://LOCALHOST/hook",
+ "http://127.0.0.1/hook",
+ "http://127.0.0.2/hook",
+ "http://[::1]/hook",
+ "http://10.0.0.1/hook",
+ "http://10.255.255.255/hook",
+ "http://172.16.0.1/hook",
+ "http://172.31.255.255/hook",
+ "http://192.168.1.1/hook",
+ "http://192.168.0.100:8080/hook",
+ "http://169.254.169.254/latest/meta-data/",
+ "http://169.254.1.1/hook",
+ "http://0.0.0.0/hook",
+ "ftp://example.com/hook",
+ "file:///etc/passwd",
+ "gopher://example.com",
+ } {
+ messages := validate.WebhookURL(rawurl)
+ Expect(len(messages) > 0).IsTrue()
+ }
+}
+
+func TestWebhookURL_AllowedAddresses(t *testing.T) {
+ RegisterT(t)
+
+ for _, rawurl := range []string{
+ "https://hooks.slack.com/services/T00/B00/xxx",
+ "https://discord.com/api/webhooks/123/abc",
+ "http://example.com/webhook",
+ "https://203.0.113.1/hook",
+ } {
+ messages := validate.WebhookURL(rawurl)
+ Expect(messages).HasLen(0)
+ }
+}
+
func TestInvalidCNAME(t *testing.T) {
RegisterT(t)
diff --git a/app/services/webhook/webhook.go b/app/services/webhook/webhook.go
index 367d148a4..634da9355 100644
--- a/app/services/webhook/webhook.go
+++ b/app/services/webhook/webhook.go
@@ -14,6 +14,7 @@ import (
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/tpl"
+ "github.com/getfider/fider/app/pkg/validate"
"github.com/getfider/fider/app/pkg/webhook"
)
@@ -83,6 +84,9 @@ func triggerWebhook(ctx context.Context, webhook *entity.Webhook, props webhook.
if err != nil {
return resultWithError(ctx, "Could not parse webhook URL template", err.Error(), result)
}
+ if msgs := validate.WebhookURL(result.Url); len(msgs) > 0 {
+ return resultWithError(ctx, "Webhook URL targets a blocked address", strings.Join(msgs, "; "), result)
+ }
result.Content, err = executeTemplate(fmt.Sprintf("%s-content", fullName), webhook.Content, props)
if err != nil {
return resultWithError(ctx, "Could not parse webhook content template", err.Error(), result)