Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

security: SSRF protection for webhook URLs

rcourtmanbb47e1831c03
diff --git a/internal/api/audit_handlers.go b/internal/api/audit_handlers.go
index 8405c96628..ee1a5fd22c 100644
--- a/internal/api/audit_handlers.go
+++ b/internal/api/audit_handlers.go
@@ -1,6 +1,7 @@
package api
import (
+ "context"
"encoding/json"
"fmt"
"net"
@@ -17,6 +18,8 @@ import (
// validAuditEventID matches alphanumeric IDs with hyphens (UUID format)
var validAuditEventID = regexp.MustCompile(`^[a-zA-Z0-9\-]+$`)
+var resolveWebhookIPs = net.DefaultResolver.LookupIPAddr
+
// AuditHandlers provides HTTP handlers for audit log endpoints.
type AuditHandlers struct{}
@@ -262,6 +265,7 @@ func validateWebhookURL(rawURL string) error {
if isPrivateOrReservedIP(ip) {
return fmt.Errorf("private or reserved IP addresses are not allowed")
}
+ return nil
}
// Block common internal hostnames
@@ -280,6 +284,21 @@ func validateWebhookURL(rawURL string) error {
}
}
+ resolveCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ addrs, err := resolveWebhookIPs(resolveCtx, hostname)
+ if err != nil {
+ return fmt.Errorf("failed to resolve hostname")
+ }
+ if len(addrs) == 0 {
+ return fmt.Errorf("hostname did not resolve")
+ }
+ for _, addr := range addrs {
+ if isPrivateOrReservedIP(addr.IP) {
+ return fmt.Errorf("hostname resolves to private or reserved IP addresses")
+ }
+ }
+
return nil
}
diff --git a/internal/api/audit_handlers_test.go b/internal/api/audit_handlers_test.go
index 6512142bbc..c83c1b95d5 100644
--- a/internal/api/audit_handlers_test.go
+++ b/internal/api/audit_handlers_test.go
@@ -1,6 +1,7 @@
package api
import (
+ "context"
"encoding/json"
"fmt"
"net"
@@ -449,6 +450,14 @@ func TestIsPrivateOrReservedIP(t *testing.T) {
}
func TestValidateWebhookURL(t *testing.T) {
+ origResolver := resolveWebhookIPs
+ resolveWebhookIPs = func(_ context.Context, host string) ([]net.IPAddr, error) {
+ return []net.IPAddr{{IP: net.ParseIP("93.184.216.34")}}, nil
+ }
+ t.Cleanup(func() {
+ resolveWebhookIPs = origResolver
+ })
+
testCases := []struct {
url string
wantErr bool
@@ -477,6 +486,20 @@ func TestValidateWebhookURL(t *testing.T) {
}
}
+func TestValidateWebhookURLBlocksPrivateResolution(t *testing.T) {
+ origResolver := resolveWebhookIPs
+ resolveWebhookIPs = func(_ context.Context, host string) ([]net.IPAddr, error) {
+ return []net.IPAddr{{IP: net.ParseIP("10.0.0.1")}}, nil
+ }
+ t.Cleanup(func() {
+ resolveWebhookIPs = origResolver
+ })
+
+ if err := validateWebhookURL("https://example.com"); err == nil {
+ t.Fatalf("expected hostname resolving to private IP to be rejected")
+ }
+}
+
func TestHandleListAuditEvents_Filters(t *testing.T) {
setAuditLogger(t, &testAuditLogger{
events: []audit.Event{{ID: "1", EventType: "login", Success: true}},
diff --git a/pkg/audit/webhook.go b/pkg/audit/webhook.go
index deac8e3ce7..35b2540290 100644
--- a/pkg/audit/webhook.go
+++ b/pkg/audit/webhook.go
@@ -5,7 +5,10 @@ import (
"context"
"encoding/json"
"fmt"
+ "net"
"net/http"
+ "net/url"
+ "strings"
"sync"
"time"
@@ -20,6 +23,7 @@ const (
)
var webhookBackoff = []time.Duration{1 * time.Second, 5 * time.Second, 30 * time.Second}
+var resolveWebhookIPs = net.DefaultResolver.LookupIPAddr
// WebhookDelivery handles async webhook delivery with retries.
type WebhookDelivery struct {
@@ -212,6 +216,10 @@ func (w *WebhookDelivery) deliver(url string, event Event) error {
ctx, cancel := context.WithTimeout(context.Background(), webhookTimeout)
defer cancel()
+ if err := validateWebhookURL(ctx, url); err != nil {
+ return fmt.Errorf("webhook URL blocked: %w", err)
+ }
+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("failed to create webhook request: %w", err)
@@ -236,6 +244,93 @@ func (w *WebhookDelivery) deliver(url string, event Event) error {
return nil
}
+func validateWebhookURL(ctx context.Context, rawURL string) error {
+ if strings.TrimSpace(rawURL) == "" {
+ return fmt.Errorf("URL cannot be empty")
+ }
+
+ parsed, err := url.Parse(rawURL)
+ if err != nil {
+ return fmt.Errorf("invalid URL format")
+ }
+
+ if parsed.Scheme != "http" && parsed.Scheme != "https" {
+ return fmt.Errorf("URL scheme must be http or https")
+ }
+
+ if parsed.Host == "" {
+ return fmt.Errorf("URL must have a host")
+ }
+
+ hostname := parsed.Hostname()
+ if hostname == "" {
+ return fmt.Errorf("URL must have a hostname")
+ }
+
+ if hostname == "localhost" || hostname == "127.0.0.1" || hostname == "::1" {
+ return fmt.Errorf("localhost URLs are not allowed")
+ }
+
+ if ip := net.ParseIP(hostname); ip != nil {
+ if isPrivateOrReservedIP(ip) {
+ return fmt.Errorf("private or reserved IP addresses are not allowed")
+ }
+ return nil
+ }
+
+ lowerHost := strings.ToLower(hostname)
+ blockedPatterns := []string{
+ "metadata.google",
+ "169.254.169.254",
+ "metadata.azure",
+ "internal",
+ ".local",
+ ".localhost",
+ }
+ for _, pattern := range blockedPatterns {
+ if strings.Contains(lowerHost, pattern) {
+ return fmt.Errorf("internal hostnames are not allowed")
+ }
+ }
+
+ if ctx == nil {
+ ctx = context.Background()
+ }
+ resolveCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
+ defer cancel()
+ addrs, err := resolveWebhookIPs(resolveCtx, hostname)
+ if err != nil {
+ return fmt.Errorf("failed to resolve hostname")
+ }
+ if len(addrs) == 0 {
+ return fmt.Errorf("hostname did not resolve")
+ }
+ for _, addr := range addrs {
+ if isPrivateOrReservedIP(addr.IP) {
+ return fmt.Errorf("hostname resolves to private or reserved IP addresses")
+ }
+ }
+
+ return nil
+}
+
+func isPrivateOrReservedIP(ip net.IP) bool {
+ if ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
+ return true
+ }
+
+ if ip4 := ip.To4(); ip4 != nil {
+ if ip4[0] == 169 && ip4[1] == 254 {
+ return true
+ }
+ if ip4[0] == 0 {
+ return true
+ }
+ }
+
+ return false
+}
+
// QueueLength returns the current number of events in the queue.
func (w *WebhookDelivery) QueueLength() int {
return len(w.queue)