From 04adfd95f145bde8aa005a60a04ce84a96f57852 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 9 Aug 2026 02:37:59 +0000 Subject: [PATCH] Add secure HTTP client to prevent SSRF and resource exhaustion Co-authored-by: himattm <6266621+himattm@users.noreply.github.com> --- .jules/sentinel.md | 4 ++++ internal/plugin/manager.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 80620ce..caa4f84 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -16,3 +16,7 @@ **Vulnerability:** Writing fully buffered in-memory data to temporary disk files solely for parsing. **Learning:** This increases attack surface, risks disk exhaustion, and violates the principle of least privilege. **Prevention:** Refactor parsing functions to accept byte slices or `io.Reader` directly to process data in memory. +## 2024-05-30 - Prevent SSRF with Dialer Control Hook +**Vulnerability:** Relying on `net/url.Parse` for SSRF protection leaves the application vulnerable to DNS rebinding or IP obfuscation, and `http.Get` lacks timeouts. +**Learning:** A `net.Dialer` with a `Control` hook evaluates the actual IP address *after* DNS resolution but *before* the socket connects, effectively mitigating SSRF against cloud metadata like `169.254.169.254`. +**Prevention:** Use a custom `http.Client` with a `net.Dialer` `Control` hook to inspect resolved IPs, and always set timeouts to prevent resource exhaustion. diff --git a/internal/plugin/manager.go b/internal/plugin/manager.go index 7aad027..dc901a5 100644 --- a/internal/plugin/manager.go +++ b/internal/plugin/manager.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "io" + "net" "net/http" "net/url" "os" @@ -16,6 +17,7 @@ import ( "runtime" "sort" "strings" + "syscall" "time" "github.com/himattm/prism/internal/fsutil" @@ -410,7 +412,8 @@ func (m *Manager) addScriptPlugin(owner, repo, pluginName string) error { fmt.Printf("Fetching script from: %s\n", rawURL) - resp, err := http.Get(rawURL) + client := secureHTTPClient() + resp, err := client.Get(rawURL) if err != nil { return fmt.Errorf("failed to fetch plugin: %w", err) } @@ -468,7 +471,8 @@ func (m *Manager) addFromDirectURL(rawURL string) error { return fmt.Errorf("unsupported URL scheme: %s", parsedURL.Scheme) } - resp, err := http.Get(parsedURL.String()) + client := secureHTTPClient() + resp, err := client.Get(parsedURL.String()) if err != nil { return fmt.Errorf("failed to fetch plugin: %w", err) } @@ -860,3 +864,32 @@ func CompareVersions(a, b string) int { return 0 } + +// secureHTTPClient creates an HTTP client with timeouts and SSRF protection +func secureHTTPClient() *http.Client { + dialer := &net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + Control: func(network, address string, c syscall.RawConn) error { + host, _, err := net.SplitHostPort(address) + if err != nil { + return err + } + if idx := strings.IndexByte(host, '%'); idx != -1 { + host = host[:idx] + } + ip := net.ParseIP(host) + if ip != nil && ip.Equal(net.ParseIP("169.254.169.254")) { + return fmt.Errorf("SSRF restricted IP: %s", ip.String()) + } + return nil + }, + } + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.DialContext = dialer.DialContext + transport.DisableKeepAlives = true + return &http.Client{ + Timeout: 10 * time.Second, + Transport: transport, + } +}