Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

Merge commit from fork

Luigi Agostie0c3e5748c1c
diff --git a/internal/handlers/download.go b/internal/handlers/download.go
index 68913939..aeb4ed87 100644
--- a/internal/handlers/download.go
+++ b/internal/handlers/download.go
@@ -3,13 +3,16 @@ package handlers
import (
"context"
"encoding/base64"
+ "errors"
"fmt"
"net"
"net/http"
+ "net/netip"
"net/url"
"os"
"path/filepath"
"strings"
+ "sync"
"time"
"sync/atomic"
@@ -26,9 +29,16 @@ var resolveDownloadHostIPs = func(ctx context.Context, network, host string) ([]
return net.DefaultResolver.LookupIP(ctx, network, host)
}
-// validateDownloadURL blocks file://, internal hosts, private IPs, and cloud metadata.
-// Only public http/https URLs are allowed.
-func validateDownloadURL(rawURL string) error {
+var blockedDownloadPrefixes = []netip.Prefix{
+ netip.MustParsePrefix("100.64.0.0/10"), // Carrier-grade NAT/shared address space.
+ netip.MustParsePrefix("198.18.0.0/15"), // Benchmark/testing networks.
+}
+
+type downloadURLGuard struct{}
+
+func newDownloadURLGuard() *downloadURLGuard { return &downloadURLGuard{} }
+
+func (g *downloadURLGuard) Validate(rawURL string) error {
parsed, err := url.Parse(rawURL)
if err != nil {
return fmt.Errorf("invalid URL")
@@ -38,11 +48,15 @@ func validateDownloadURL(rawURL string) error {
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" {
+ host := strings.TrimSuffix(strings.ToLower(parsed.Hostname()), ".")
+ if host == "" || host == "localhost" || strings.HasSuffix(host, ".localhost") {
return fmt.Errorf("internal or blocked host")
}
+ if ip := net.ParseIP(host); ip != nil {
+ return validateDownloadIP(ip)
+ }
+
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
@@ -50,16 +64,84 @@ func validateDownloadURL(rawURL string) error {
if err != nil {
return fmt.Errorf("could not resolve host")
}
-
for _, ip := range ips {
- if ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsUnspecified() {
+ if err := validateDownloadIP(ip); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func validateDownloadIP(ip net.IP) error {
+ if ip == nil {
+ return fmt.Errorf("private/internal IP blocked")
+ }
+
+ addr, ok := netip.AddrFromSlice(ip)
+ if !ok {
+ return fmt.Errorf("private/internal IP blocked")
+ }
+ addr = addr.Unmap()
+ if addr.IsPrivate() || addr.IsLoopback() || addr.IsLinkLocalUnicast() || addr.IsLinkLocalMulticast() || addr.IsInterfaceLocalMulticast() || addr.IsMulticast() || addr.IsUnspecified() {
+ return fmt.Errorf("private/internal IP blocked")
+ }
+ for _, prefix := range blockedDownloadPrefixes {
+ if prefix.Contains(addr) {
return fmt.Errorf("private/internal IP blocked")
}
}
+ return nil
+}
+
+// validateDownloadURL blocks file://, internal hosts, private IPs, and cloud metadata.
+// Only public http/https URLs are allowed.
+func validateDownloadURL(rawURL string) error {
+ return newDownloadURLGuard().Validate(rawURL)
+}
+
+type downloadRequestGuard struct {
+ validator *downloadURLGuard
+ maxRedirects int
+ redirects atomic.Int32
+
+ mu sync.Mutex
+ blockedErr error
+}
+func newDownloadRequestGuard(validator *downloadURLGuard, maxRedirects int) *downloadRequestGuard {
+ return &downloadRequestGuard{
+ validator: validator,
+ maxRedirects: maxRedirects,
+ }
+}
+
+func (g *downloadRequestGuard) Validate(rawURL string, redirected bool) error {
+ if err := g.validator.Validate(rawURL); err != nil {
+ return fmt.Errorf("unsafe browser request: %w", err)
+ }
+ if redirected && g.maxRedirects >= 0 {
+ count := int(g.redirects.Add(1))
+ if count > g.maxRedirects {
+ return fmt.Errorf("%w: got %d, max %d", bridge.ErrTooManyRedirects, count, g.maxRedirects)
+ }
+ }
return nil
}
+func (g *downloadRequestGuard) NoteBlocked(err error) {
+ g.mu.Lock()
+ if g.blockedErr == nil {
+ g.blockedErr = err
+ }
+ g.mu.Unlock()
+}
+
+func (g *downloadRequestGuard) BlockedError() error {
+ g.mu.Lock()
+ defer g.mu.Unlock()
+ return g.blockedErr
+}
+
// HandleDownload fetches a URL using the browser's session (cookies, stealth)
// and returns the content. This preserves authentication and fingerprint.
//
@@ -77,7 +159,8 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
return
}
- if err := validateDownloadURL(dlURL); err != nil {
+ validator := newDownloadURLGuard()
+ if err := validator.Validate(dlURL); err != nil {
web.Error(w, 400, fmt.Errorf("unsafe URL: %w", err))
return
}
@@ -99,25 +182,23 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
var requestID network.RequestID
var responseMIME string
var responseStatus int
- maxRedirects := h.Config.MaxRedirects
- var redirectCount atomic.Int32
- var redirectBlocked atomic.Bool
+ requestGuard := newDownloadRequestGuard(validator, h.Config.MaxRedirects)
+ var mainFrameID cdp.FrameID
done := make(chan struct{}, 1)
- // If redirect limiting is enabled, use Fetch domain to intercept.
- if maxRedirects >= 0 {
- if err := chromedp.Run(tCtx, chromedp.ActionFunc(func(ctx context.Context) error {
- return fetch.Enable().Do(ctx)
- })); err != nil {
- web.Error(w, 500, fmt.Errorf("fetch enable: %w", err))
- return
- }
- defer func() {
- _ = chromedp.Run(tCtx, chromedp.ActionFunc(func(ctx context.Context) error {
- return fetch.Disable().Do(ctx)
- }))
- }()
+ // Intercept every browser-side request so redirects and follow-on navigations
+ // cannot escape the public-only URL policy enforced for /download.
+ if err := chromedp.Run(tCtx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return fetch.Enable().Do(ctx)
+ })); err != nil {
+ web.Error(w, 500, fmt.Errorf("fetch enable: %w", err))
+ return
}
+ defer func() {
+ _ = chromedp.Run(tCtx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return fetch.Disable().Do(ctx)
+ }))
+ }()
chromedp.ListenTarget(tCtx, func(ev interface{}) {
switch e := ev.(type) {
@@ -125,18 +206,29 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
// Handle in goroutine to avoid deadlocking the event dispatcher.
go func() {
reqID := e.RequestID
- if e.RedirectedRequestID != "" && maxRedirects >= 0 {
- count := int(redirectCount.Add(1))
- if count > maxRedirects {
- redirectBlocked.Store(true)
- _ = fetch.FailRequest(reqID, network.ErrorReasonBlockedByClient).Do(cdp.WithExecutor(tCtx, chromedp.FromContext(tCtx).Target))
- return
+ if err := requestGuard.Validate(e.Request.URL, e.RedirectedRequestID != ""); err != nil {
+ requestGuard.NoteBlocked(err)
+ select {
+ case done <- struct{}{}:
+ default:
}
+ _ = fetch.FailRequest(reqID, network.ErrorReasonBlockedByClient).Do(cdp.WithExecutor(tCtx, chromedp.FromContext(tCtx).Target))
+ return
}
_ = fetch.ContinueRequest(reqID).Do(cdp.WithExecutor(tCtx, chromedp.FromContext(tCtx).Target))
}()
+ case *network.EventRequestWillBeSent:
+ if e.Type != network.ResourceTypeDocument {
+ return
+ }
+ if mainFrameID == "" {
+ mainFrameID = e.FrameID
+ }
+ if e.FrameID == mainFrameID {
+ requestID = e.RequestID
+ }
case *network.EventResponseReceived:
- if e.Response.URL == dlURL && requestID == "" {
+ if e.RequestID == requestID && requestID != "" {
requestID = e.RequestID
responseMIME = e.Response.MimeType
responseStatus = int(e.Response.Status)
@@ -172,8 +264,12 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
// Navigate the temp tab to the URL — uses browser's cookie jar and stealth.
if err := chromedp.Run(tCtx, chromedp.Navigate(dlURL)); err != nil {
- if redirectBlocked.Load() {
- web.Error(w, 422, fmt.Errorf("download: %w: got %d, max %d", bridge.ErrTooManyRedirects, redirectCount.Load(), maxRedirects))
+ if blockedErr := requestGuard.BlockedError(); blockedErr != nil {
+ if errors.Is(blockedErr, bridge.ErrTooManyRedirects) {
+ web.Error(w, 422, fmt.Errorf("download: %w", blockedErr))
+ return
+ }
+ web.Error(w, 400, blockedErr)
return
}
web.Error(w, 502, fmt.Errorf("navigate to download URL: %w", err))
@@ -184,18 +280,35 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
select {
case <-done:
case <-tCtx.Done():
- if redirectBlocked.Load() {
- web.Error(w, 422, fmt.Errorf("download: %w: got %d, max %d", bridge.ErrTooManyRedirects, redirectCount.Load(), maxRedirects))
+ if blockedErr := requestGuard.BlockedError(); blockedErr != nil {
+ if errors.Is(blockedErr, bridge.ErrTooManyRedirects) {
+ web.Error(w, 422, fmt.Errorf("download: %w", blockedErr))
+ return
+ }
+ web.Error(w, 400, blockedErr)
return
}
web.Error(w, 504, fmt.Errorf("download timed out"))
return
}
+ if blockedErr := requestGuard.BlockedError(); blockedErr != nil {
+ if errors.Is(blockedErr, bridge.ErrTooManyRedirects) {
+ web.Error(w, 422, fmt.Errorf("download: %w", blockedErr))
+ return
+ }
+ web.Error(w, 400, blockedErr)
+ return
+ }
+
if responseStatus >= 400 {
web.Error(w, 502, fmt.Errorf("remote server returned HTTP %d", responseStatus))
return
}
+ if requestID == "" {
+ web.Error(w, 502, fmt.Errorf("download response was not captured"))
+ return
+ }
// Get response body via CDP.
var body []byte
diff --git a/internal/handlers/download_test.go b/internal/handlers/download_test.go
index 2477398c..21d51e39 100644
--- a/internal/handlers/download_test.go
+++ b/internal/handlers/download_test.go
@@ -6,8 +6,10 @@ import (
"net"
"net/http"
"net/http/httptest"
+ "strings"
"testing"
+ "github.com/pinchtab/pinchtab/internal/bridge"
"github.com/pinchtab/pinchtab/internal/config"
)
@@ -60,6 +62,11 @@ func TestValidateDownloadURL(t *testing.T) {
{"localhost", "http://localhost:8080/secret", true},
{"loopback ipv4", "http://127.0.0.1/secret", true},
{"loopback ipv6", "http://[::1]/secret", true},
+ {"private ipv4 literal", "http://192.168.1.10/secret", true},
+ {"shared address space", "http://100.64.0.1/secret", true},
+ {"benchmark network", "http://198.18.0.1/secret", true},
+ {"cloud metadata", "http://169.254.169.254/latest/meta-data", true},
+ {"localhost suffix", "http://foo.localhost/secret", true},
{"empty scheme", "://pinchtab.com", true},
{"no scheme", "pinchtab.com", true},
}
@@ -79,6 +86,10 @@ func TestHandleDownload_SSRFBlocked(t *testing.T) {
"file:///etc/passwd",
"http://localhost:8080",
"http://127.0.0.1/admin",
+ "http://192.168.1.10/admin",
+ "http://100.64.0.1/admin",
+ "http://198.18.0.1/admin",
+ "http://169.254.169.254/latest/meta-data",
}
for _, u := range urls {
req := httptest.NewRequest("GET", "/download?url="+u, nil)
@@ -120,3 +131,48 @@ func TestHandleDownload_Disabled(t *testing.T) {
t.Errorf("expected 403 when download disabled, got %d", w.Code)
}
}
+
+func TestDownloadRequestGuard_BlocksBrowserSideRequests(t *testing.T) {
+ originalResolver := resolveDownloadHostIPs
+ t.Cleanup(func() {
+ resolveDownloadHostIPs = originalResolver
+ })
+ resolveDownloadHostIPs = func(ctx context.Context, network, host string) ([]net.IP, error) {
+ switch host {
+ case "pinchtab.com":
+ return []net.IP{net.ParseIP("93.184.216.34")}, nil
+ default:
+ return nil, errors.New("not found")
+ }
+ }
+
+ guard := newDownloadRequestGuard(newDownloadURLGuard(), -1)
+ err := guard.Validate("http://127.0.0.1:1337/increment", false)
+ if err == nil {
+ t.Fatal("expected browser-side localhost request to be blocked")
+ }
+ if !strings.Contains(err.Error(), "unsafe browser request") {
+ t.Fatalf("expected unsafe browser request error, got %v", err)
+ }
+}
+
+func TestDownloadRequestGuard_TracksRedirectLimits(t *testing.T) {
+ originalResolver := resolveDownloadHostIPs
+ t.Cleanup(func() {
+ resolveDownloadHostIPs = originalResolver
+ })
+ resolveDownloadHostIPs = func(ctx context.Context, network, host string) ([]net.IP, error) {
+ switch host {
+ case "pinchtab.com":
+ return []net.IP{net.ParseIP("93.184.216.34")}, nil
+ default:
+ return nil, errors.New("not found")
+ }
+ }
+
+ guard := newDownloadRequestGuard(newDownloadURLGuard(), 0)
+ err := guard.Validate("https://pinchtab.com/redirected", true)
+ if !errors.Is(err, bridge.ErrTooManyRedirects) {
+ t.Fatalf("expected too-many-redirects error, got %v", err)
+ }
+}
diff --git a/tests/e2e/scenarios-recent/46-download-ssrf.sh b/tests/e2e/scenarios-recent/46-download-ssrf.sh
new file mode 100755
index 00000000..a84774a8
--- /dev/null
+++ b/tests/e2e/scenarios-recent/46-download-ssrf.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+# 46-download-ssrf.sh — Download SSRF regression coverage
+#
+# Covers:
+# - direct internal/private targets rejected at initial validation
+# - redirected internal targets rejected during browser-side navigation
+
+source "$(dirname "$0")/common.sh"
+
+build_download_redirect_url() {
+ local target_url="$1"
+ local encoded_target
+ local attacker_url
+
+ encoded_target=$(jq -rn --arg u "$target_url" '$u|@uri')
+ attacker_url="https://httpbin.org/redirect-to?url=${encoded_target}"
+ jq -rn --arg u "$attacker_url" '$u|@uri'
+}
+
+start_test "download security: direct internal target blocked"
+
+pt_get "/download?url=${FIXTURES_URL}/sample.txt"
+assert_http_status 400 "direct internal target blocked"
+assert_contains "$RESULT" "blocked\|private" "direct SSRF error message"
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "download security: redirected internal target blocked"
+
+ATTACKER_URL=$(build_download_redirect_url "http://127.0.0.1:9999/health")
+pt_get "/download?url=${ATTACKER_URL}"
+assert_http_status 400 "redirected internal target blocked"
+assert_contains "$RESULT" "unsafe browser request\|blocked\|private" "redirect SSRF error message"
+
+end_test
diff --git a/tests/e2e/scenarios/09-download.sh b/tests/e2e/scenarios/09-download.sh
index ae93b853..f0092d92 100755
--- a/tests/e2e/scenarios/09-download.sh
+++ b/tests/e2e/scenarios/09-download.sh
@@ -6,6 +6,16 @@
source "$(dirname "$0")/common.sh"
+build_download_redirect_url() {
+ local target_url="$1"
+ local encoded_target
+ local attacker_url
+
+ encoded_target=$(jq -rn --arg u "$target_url" '$u|@uri')
+ attacker_url="https://httpbin.org/redirect-to?url=${encoded_target}"
+ jq -rn --arg u "$attacker_url" '$u|@uri'
+}
+
# ─────────────────────────────────────────────────────────────────
start_test "pinchtab download (public URL)"
@@ -27,6 +37,16 @@ assert_contains "$RESULT" "blocked\|private" "SSRF error message"
end_test
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab download (redirected internal target blocked)"
+
+ATTACKER_URL=$(build_download_redirect_url "http://127.0.0.1:9999/health")
+pt_get "/download?url=${ATTACKER_URL}"
+assert_http_status 400 "redirected internal target blocked"
+assert_contains "$RESULT" "unsafe browser request\|blocked\|private" "redirect SSRF error message"
+
+end_test
+
# ─────────────────────────────────────────────────────────────────
start_test "pinchtab download --tab <id>"