Skip to content

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

9.4 KB
Raw Blame
package handlers

import (
	"context"
	"encoding/base64"
	"errors"
	"fmt"
	"log/slog"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/pinchtab/pinchtab/internal/activity"
	"github.com/pinchtab/pinchtab/internal/authn"
	"github.com/pinchtab/pinchtab/internal/bridge"
	"github.com/pinchtab/pinchtab/internal/httpx"
	"github.com/pinchtab/pinchtab/internal/routes"
)

// HandleDownload fetches a URL using the browser's session (cookies, stealth)
// and returns the content. This preserves authentication and fingerprint.
//
// GET /download?url=<url>[&tabId=<id>][&output=file&path=/tmp/file][&raw=true]
func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
	if !h.Config.AllowDownload {
		h.writeCapabilityDisabled(w, routes.CapDownload)
		return
	}
	dlURL := r.URL.Query().Get("url")
	if dlURL == "" {
		httpx.Error(w, 400, fmt.Errorf("url parameter required"))
		return
	}
	maxDownloadBytes := h.Config.EffectiveDownloadMaxBytes()

	// Download allowlist: prefer the explicit per-feature list, but fall
	// back to the unified security.allowedDomains. This way operators only
	// need to configure trusted domains once.
	allowed := h.Config.DownloadAllowedDomains
	if len(allowed) == 0 {
		allowed = h.Config.AllowedDomains
	}
	validator := newDownloadURLGuard(allowed)
	if err := validator.Validate(dlURL); err != nil {
		writeDownloadGuardError(w, fmt.Errorf("unsafe URL: %w", err), maxDownloadBytes)
		return
	}

	tabID := strings.TrimSpace(r.URL.Query().Get("tabId"))
	if tabID != "" {
		if !h.enforceDownloadTabPolicy(w, r, tabID, dlURL) {
			return
		}
	}

	output := r.URL.Query().Get("output")
	filePath := r.URL.Query().Get("path")
	raw := r.URL.Query().Get("raw") == "true"

	// Re-check scheme before navigation (validateDownloadURL already enforces this,
	// but inline check satisfies CodeQL SSRF analysis).
	if parsed, err := url.Parse(dlURL); err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") {
		httpx.Error(w, 400, fmt.Errorf("invalid download URL scheme"))
		return
	}

	tCtx, tCancel := context.WithTimeout(r.Context(), 30*time.Second)
	defer tCancel()
	go httpx.CancelOnClientDone(r.Context(), tCancel)

	requestGuard := newDownloadRequestGuard(validator, h.Config.MaxRedirects)
	browserCtx := h.Bridge.BrowserContext()

	result, err := h.Bridge.DownloadURL(tCtx, dlURL, bridge.DownloadOpts{
		MaxBytes:     maxDownloadBytes,
		MaxRedirects: h.Config.MaxRedirects,
		ValidateURL: func(rawURL string, isRedirect bool) error {
			return requestGuard.Validate(rawURL, isRedirect)
		},
		ValidateRemoteIP: func(remoteIP string) error {
			return validateDownloadRemoteIPAddress(remoteIP)
		},
		IsDomainAllowed: func(rawURL string) bool {
			return validator.isDomainAllowed(rawURL)
		},
		ParseContentLength: func(headers map[string]interface{}) (int64, bool) {
			return parseContentLengthHeaderGeneric(headers)
		},
	})
	if err != nil {
		if isNavigationAborted(err) {
			h.tryDirectDownloadFallback(w, r, tCtx, browserCtx, dlURL, validator, maxDownloadBytes, output, filePath, raw)
			return
		}
		writeDownloadError(w, err, result, maxDownloadBytes)
		return
	}

	h.recordActivity(r, activity.Update{Action: "download", URL: dlURL})
	h.writeDownloadResponse(w, result.Body, result.MIMEType, dlURL, output, filePath, raw, maxDownloadBytes)
}

// enforceDownloadTabPolicy applies tab-scoped policy (lease, current-tab domain
// policy, and cookie-auth download-scope) for a tab-targeted download. It writes
// the error response and returns false on any failure.
func (h *Handlers) enforceDownloadTabPolicy(w http.ResponseWriter, r *http.Request, tabID, dlURL string) bool {
	ctx, resolvedTabID, err := h.tabContext(r, tabID)
	if err != nil {
		WriteTabContextError(w, err, 404)
		return false
	}
	owner := resolveOwner(r, "")
	if err := h.enforceTabLease(resolvedTabID, owner); err != nil {
		httpx.ErrorCode(w, http.StatusLocked, "tab_locked", err.Error(), false, nil)
		return false
	}
	currentURL, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDialogBlocked|guardDomainPolicy|guardHandoffPause)
	if !ok {
		return false
	}
	if currentURL == "" {
		if provider, ok := bridgeAs[tabPolicyStateProvider](h.Bridge); ok {
			if state, ok := provider.GetTabPolicyState(resolvedTabID); ok && state.CurrentURL != "" {
				currentURL = state.CurrentURL
			}
		}
	}
	if currentURL == "" {
		lookupCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
		defer cancel()
		var lookupErr error
		currentURL, lookupErr = h.Bridge.CurrentURL(lookupCtx)
		if lookupErr != nil {
			httpx.Error(w, 500, fmt.Errorf("resolve current tab url: %w", lookupErr))
			return false
		}
	}
	if authn.CredentialsFromRequest(r).Method == authn.MethodCookie {
		if err := validateTabScopedDownloadURL(currentURL, dlURL); err != nil {
			httpx.ErrorCode(w, http.StatusForbidden, "download_scope_forbidden", err.Error(), false, map[string]any{
				"currentURL":   currentURL,
				"requestedURL": dlURL,
			})
			return false
		}
	}
	return true
}

// tryDirectDownloadFallback handles a Chrome navigation abort (binary downloads
// like .gz) by retrying with a direct Go HTTP fetch using the browser's cookies.
// It always writes a response.
func (h *Handlers) tryDirectDownloadFallback(w http.ResponseWriter, r *http.Request, tCtx, browserCtx context.Context, dlURL string, validator *downloadURLGuard, maxDownloadBytes int, output, filePath string, raw bool) {
	slog.Info("download: Chrome navigation aborted, falling back to direct fetch", "url", dlURL)
	body, mime, status, fetchErr := h.fetchDirectWithCookies(tCtx, browserCtx, dlURL, validator, maxDownloadBytes)
	if fetchErr != nil {
		errMsg := fetchErr.Error()
		if strings.Contains(errMsg, "blocked") || strings.Contains(errMsg, "private") {
			httpx.Error(w, 400, fmt.Errorf("unsafe browser request: %w", fetchErr))
			return
		}
		httpx.Error(w, 502, fmt.Errorf("download fallback: %w", fetchErr))
		return
	}
	if status >= 400 {
		httpx.Error(w, 502, fmt.Errorf("remote server returned HTTP %d", status))
		return
	}
	h.recordActivity(r, activity.Update{Action: "download", URL: dlURL})
	h.writeDownloadResponse(w, body, mime, dlURL, output, filePath, raw, maxDownloadBytes)
}

// writeDownloadError maps a non-aborted DownloadURL error to an HTTP response.
func writeDownloadError(w http.ResponseWriter, err error, result *bridge.DownloadResult, maxDownloadBytes int) {
	if errors.Is(err, bridge.ErrTooManyRedirects) {
		httpx.Error(w, 422, fmt.Errorf("download: %w", err))
		return
	}
	if errors.Is(err, bridge.ErrDownloadTooLarge) {
		httpx.ErrorCode(w, http.StatusRequestEntityTooLarge, "download_too_large", err.Error(), false, map[string]any{
			"maxBytes": maxDownloadBytes,
		})
		return
	}
	if errors.Is(err, bridge.ErrDownloadTimeout) {
		httpx.Error(w, 504, fmt.Errorf("download timed out"))
		return
	}
	// A populated result with a 4xx/5xx status is unambiguously a remote
	// HTTP-error download here (redirect/size/timeout errors are typed and
	// already handled above) — key off the structured status, not the message.
	if result != nil && result.StatusCode >= 400 {
		httpx.Error(w, 502, err)
		return
	}
	errMsg := err.Error()
	if strings.Contains(errMsg, "blocked") || strings.Contains(errMsg, "private") || strings.Contains(errMsg, "unsafe") {
		httpx.Error(w, 400, fmt.Errorf("unsafe browser request: %w", err))
		return
	}
	httpx.Error(w, 502, fmt.Errorf("navigate to download URL: %w", err))
}

func (h *Handlers) writeDownloadResponse(w http.ResponseWriter, body []byte, mime, dlURL, output, filePath string, raw bool, maxBytes int) {
	if len(body) > maxBytes {
		httpx.ErrorCode(w, http.StatusRequestEntityTooLarge, "download_too_large",
			downloadTooLargeError(int64(len(body)), maxBytes).Error(), false, map[string]any{
				"maxBytes": maxBytes,
			})
		return
	}

	if mime == "" {
		mime = "application/octet-stream"
	}

	if output == "file" {
		if filePath == "" {
			httpx.Error(w, 400, fmt.Errorf("path required when output=file"))
			return
		}
		safe, pathErr := httpx.SafeCreatePath(h.Config.StateDir, filePath)
		if pathErr != nil {
			httpx.Error(w, 400, fmt.Errorf("invalid path: %w", pathErr))
			return
		}
		absBase, _ := filepath.Abs(h.Config.StateDir)
		absPath, pathErr := filepath.Abs(safe)
		if pathErr != nil || !strings.HasPrefix(absPath, absBase+string(filepath.Separator)) {
			httpx.Error(w, 400, fmt.Errorf("invalid output path"))
			return
		}
		filePath = absPath
		if err := os.MkdirAll(filepath.Dir(filePath), 0750); err != nil {
			httpx.Error(w, 500, fmt.Errorf("failed to create directory: %w", err))
			return
		}
		if err := os.WriteFile(filePath, body, 0600); err != nil {
			httpx.Error(w, 500, fmt.Errorf("failed to write file: %w", err))
			return
		}
		httpx.JSON(w, 200, map[string]any{
			"status":      "saved",
			"path":        filePath,
			"size":        len(body),
			"contentType": mime,
		})
		return
	}

	if raw {
		w.Header().Set("Content-Type", mime)
		w.Header().Set("Content-Length", fmt.Sprintf("%d", len(body)))
		w.WriteHeader(200)
		_, _ = w.Write(body)
		return
	}

	httpx.JSON(w, 200, map[string]any{
		"data":        base64.StdEncoding.EncodeToString(body),
		"contentType": mime,
		"size":        len(body),
		"url":         dlURL,
	})
}

// @Endpoint GET /tabs/{id}/download
func (h *Handlers) HandleTabDownload(w http.ResponseWriter, r *http.Request) {
	tabID, ok := requirePathTabID(w, r)
	if !ok {
		return
	}
	if _, _, err := h.tabContext(r, tabID); err != nil {
		WriteTabContextError(w, err, 404)
		return
	}
	h.HandleDownload(w, cloneWithTabIDQuery(r, tabID))
}