Skip to content

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

7.7 KB
Raw Blame
package handlers

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"sync/atomic"
	"time"

	"github.com/pinchtab/pinchtab/internal/bridge"
	"github.com/pinchtab/pinchtab/internal/config"
	"github.com/pinchtab/pinchtab/internal/httpx"
	"github.com/pinchtab/pinchtab/internal/idpi"
	"github.com/pinchtab/pinchtab/internal/netguard"
	"github.com/pinchtab/pinchtab/internal/remedy"
)

// Aliased to the bridge sentinel so errors from either layer classify identically.
var errDownloadTooLarge = bridge.ErrDownloadTooLarge

const codeDownloadHostBlocked = "download_host_blocked"

var errDownloadHostBlocked = errors.New("internal or blocked host")

// The restart is named in the hint, not this executable remedy: the download
// guard answers on a bridge as well as a server, and `pinchtab server restart`
// would stop a bridge, so only the mode-neutral config write is executable here.
var downloadHostGrant = remedy.Declare(
	`pinchtab config set security.downloadAllowedDomains "$(pinchtab config get security.downloadAllowedDomains),<host>"`)

type downloadHostBlockedError struct{ host string }

func (e downloadHostBlockedError) Error() string { return errDownloadHostBlocked.Error() }
func (e downloadHostBlockedError) Unwrap() error { return errDownloadHostBlocked }

type downloadURLGuard struct {
	allowedDomains []string
}

func newDownloadURLGuard(allowedDomains []string) *downloadURLGuard {
	return &downloadURLGuard{allowedDomains: append([]string(nil), allowedDomains...)}
}

func (g *downloadURLGuard) allowsHost(host string) bool {
	host = netguard.NormalizeHost(host)
	if host == "" {
		return false
	}
	return g.isDomainAllowed("https://" + host)
}

func (g *downloadURLGuard) isDomainAllowed(rawURL string) bool {
	if len(g.allowedDomains) == 0 {
		return false
	}
	result := idpi.CheckDomain(rawURL, config.IDPIConfig{
		Enabled:    true,
		StrictMode: true,
	}, g.allowedDomains)
	return !result.Blocked
}

func (g *downloadURLGuard) Validate(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 := netguard.NormalizeHost(parsed.Hostname())
	if host == "" {
		return downloadHostBlockedError{}
	}
	if g.allowsHost(host) {
		return nil
	}
	if netguard.IsLocalHost(host) {
		return downloadHostBlockedError{host: host}
	}

	if len(g.allowedDomains) > 0 {
		result := idpi.CheckDomain(rawURL, config.IDPIConfig{
			Enabled:    true,
			StrictMode: true,
		}, g.allowedDomains)
		if result.Blocked {
			return fmt.Errorf("domain not allowed by security.downloadAllowedDomains")
		}
		return nil
	}

	ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
	defer cancel()

	if _, err := netguard.ResolveAndValidatePublicIPs(ctx, host); err != nil {
		if errors.Is(err, netguard.ErrResolveHost) {
			return fmt.Errorf("could not resolve host")
		}
		if errors.Is(err, netguard.ErrPrivateInternalIP) {
			return fmt.Errorf("private/internal IP blocked")
		}
		return fmt.Errorf("could not resolve host")
	}
	return nil
}

func validateDownloadRemoteIPAddress(raw string) error {
	raw = strings.TrimSpace(raw)
	if raw == "" {
		// Best-effort mitigation: some responses may not expose a remote IP
		// (for example, cached responses). Skip the post-connect check then.
		return nil
	}

	normalized := netguard.NormalizeRemoteIP(raw)
	if err := netguard.ValidateRemoteIPAddress(raw); err != nil {
		if errors.Is(err, netguard.ErrUnparseableRemoteIP) {
			return fmt.Errorf("download connected to an unparseable remote IP %q", normalized)
		}
		if errors.Is(err, netguard.ErrPrivateInternalIP) {
			return fmt.Errorf("download connected to blocked remote IP %s", normalized)
		}
		return fmt.Errorf("download connected to an unparseable remote IP %q", raw)
	}
	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(nil).Validate(rawURL)
}

func validateTabScopedDownloadURL(currentURL, requestedURL string) error {
	currentParsed, err := url.Parse(strings.TrimSpace(currentURL))
	if err != nil || currentParsed.Scheme == "" || currentParsed.Host == "" {
		return fmt.Errorf("tab-scoped downloads require an active http(s) page")
	}
	if currentParsed.Scheme != "http" && currentParsed.Scheme != "https" {
		return fmt.Errorf("tab-scoped downloads require an active http(s) page")
	}

	requestedParsed, err := url.Parse(strings.TrimSpace(requestedURL))
	if err != nil || requestedParsed.Scheme == "" || requestedParsed.Host == "" {
		return fmt.Errorf("invalid download URL")
	}

	if strings.EqualFold(currentParsed.Scheme, requestedParsed.Scheme) &&
		strings.EqualFold(currentParsed.Host, requestedParsed.Host) {
		return nil
	}

	return fmt.Errorf("tab-scoped downloads are limited to the current page origin")
}

type downloadRequestGuard struct {
	validator    *downloadURLGuard
	maxRedirects int
	redirects    atomic.Int32
}

func newDownloadRequestGuard(validator *downloadURLGuard, maxRedirects int) *downloadRequestGuard {
	return &downloadRequestGuard{
		validator:    validator,
		maxRedirects: maxRedirects,
	}
}

func (g *downloadRequestGuard) Validate(rawURL string, redirected bool) error {
	// Skip validation for Chrome internal URLs (about:blank, chrome-error://, etc.)
	// that fire during tab creation before the actual navigation begins.
	if rawURL == "about:blank" || strings.HasPrefix(rawURL, "chrome") {
		return nil
	}
	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 downloadTooLargeError(size int64, maxBytes int) error {
	return fmt.Errorf("%w: received %d bytes, max %d", errDownloadTooLarge, size, maxBytes)
}

func parseContentLengthHeader(headers map[string]interface{}) (int64, bool) {
	for key, raw := range headers {
		if !strings.EqualFold(strings.TrimSpace(key), "Content-Length") {
			continue
		}
		value := strings.TrimSpace(fmt.Sprint(raw))
		if value == "" {
			return 0, false
		}
		size, err := strconv.ParseInt(value, 10, 64)
		if err != nil || size < 0 {
			return 0, false
		}
		return size, true
	}
	return 0, false
}

// parseContentLengthHeaderGeneric is a type-compatible alias for bridge callbacks.
func parseContentLengthHeaderGeneric(headers map[string]interface{}) (int64, bool) {
	return parseContentLengthHeader(headers)
}

func downloadHostBlockedDetails(host string) map[string]any {
	if host == "" {
		return nil
	}
	details := remedy.Details(
		"Name the host in security.downloadAllowedDomains to let the download endpoint reach it, then restart PinchTab to apply the change; a loopback entry exposes services on the server's own machine.",
		downloadHostGrant.Fill(host))
	details["host"] = host
	details["setting"] = "security.downloadAllowedDomains"
	return details
}

func writeDownloadGuardError(w http.ResponseWriter, err error, maxBytes int) bool {
	if err == nil {
		return false
	}
	var blocked downloadHostBlockedError
	switch {
	case errors.As(err, &blocked):
		httpx.ErrorCode(w, http.StatusBadRequest, codeDownloadHostBlocked, err.Error(), false,
			downloadHostBlockedDetails(blocked.host))
	case errors.Is(err, bridge.ErrTooManyRedirects):
		httpx.Error(w, 422, fmt.Errorf("download: %w", err))
	case errors.Is(err, errDownloadTooLarge):
		httpx.ErrorCode(w, http.StatusRequestEntityTooLarge, "download_too_large", err.Error(), false, map[string]any{
			"maxBytes": maxBytes,
		})
	default:
		httpx.Error(w, 400, err)
	}
	return true
}