High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: vulnerability
Luigi Agosti5f0fac765944
diff --git a/internal/handlers/download.go b/internal/handlers/download.goindex c95b160b..8371f59d 100644--- a/internal/handlers/download.go+++ b/internal/handlers/download.go@@ -4,9 +4,12 @@ import ("context""encoding/base64""fmt"+ "net""net/http"+ "net/url""os""path/filepath"+ "strings""time""github.com/chromedp/cdproto/network"@@ -14,6 +17,40 @@ import ("github.com/pinchtab/pinchtab/internal/web")+// validateDownloadURL blocks file://, internal hosts, private IPs, and cloud metadata.+// Only public http/https URLs are allowed.+func validateDownloadURL(rawURL string) error {+ parsed, err := url.Parse(rawURL)+ if err != nil {+ return fmt.Errorf("invalid URL")+ }++ if parsed.Scheme != "http" && parsed.Scheme != "https" {+ return fmt.Errorf("only http/https schemes are allowed")+ }++ host := strings.ToLower(parsed.Hostname())+ if host == "" || host == "localhost" || host == "127.0.0.1" || host == "::1" {+ return fmt.Errorf("internal or blocked host")+ }++ ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)+ defer cancel()++ ips, err := net.DefaultResolver.LookupIP(ctx, "ip", host)+ if err != nil {+ return fmt.Errorf("could not resolve host")+ }++ for _, ip := range ips {+ if ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsUnspecified() {+ return fmt.Errorf("private/internal IP blocked")+ }+ }++ return nil+}+// HandleDownload fetches a URL using the browser's session (cookies, stealth)// and returns the content. This preserves authentication and fingerprint.//@@ -25,6 +62,11 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {return}+ if err := validateDownloadURL(dlURL); err != nil {+ web.Error(w, 400, fmt.Errorf("unsafe URL: %w", err))+ return+ }+output := r.URL.Query().Get("output")filePath := r.URL.Query().Get("path")raw := r.URL.Query().Get("raw") == "true"diff --git a/internal/handlers/download_test.go b/internal/handlers/download_test.goindex 4234266d..ec69121f 100644--- a/internal/handlers/download_test.go+++ b/internal/handlers/download_test.go@@ -28,6 +28,50 @@ func TestHandleDownload_EmptyURL(t *testing.T) {}}+func TestValidateDownloadURL(t *testing.T) {+ tests := []struct {+ name string+ url string+ wantErr bool+ }{+ {"valid https", "https://example.com/file.pdf", false},+ {"valid http", "http://example.com/page", false},+ {"file scheme", "file:///etc/passwd", true},+ {"ftp scheme", "ftp://example.com/file", true},+ {"data scheme", "data:text/html,hello", true},+ {"localhost", "http://localhost:8080/secret", true},+ {"loopback ipv4", "http://127.0.0.1/secret", true},+ {"loopback ipv6", "http://[::1]/secret", true},+ {"empty scheme", "://example.com", true},+ {"no scheme", "example.com", true},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ err := validateDownloadURL(tt.url)+ if (err != nil) != tt.wantErr {+ t.Errorf("validateDownloadURL(%q) error = %v, wantErr %v", tt.url, err, tt.wantErr)+ }+ })+ }+}++func TestHandleDownload_SSRFBlocked(t *testing.T) {+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)+ urls := []string{+ "file:///etc/passwd",+ "http://localhost:8080",+ "http://127.0.0.1/admin",+ }+ for _, u := range urls {+ req := httptest.NewRequest("GET", "/download?url="+u, nil)+ w := httptest.NewRecorder()+ h.HandleDownload(w, req)+ if w.Code != http.StatusBadRequest {+ t.Errorf("expected 400 for SSRF URL %q, got %d", u, w.Code)+ }+ }+}+func TestHandleTabDownload_MissingTabID(t *testing.T) {h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)req := httptest.NewRequest("GET", "/tabs//download?url=https://example.com", nil)