Skip to content

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

3.7 KB
Raw Blame
package handlers

import (
	"encoding/json"
	"fmt"
	"log/slog"
	"net/http"
	"strings"
	"sync"

	"github.com/pinchtab/pinchtab/internal/activity"
	"github.com/pinchtab/pinchtab/internal/httpx"
)

type clipboardRequest struct {
	Text *string `json:"text"`
}

const maxClipboardTextBytes = 64 << 10

// clipboardStore is a simple in-memory clipboard shared across all requests.
// In headless Chrome, navigator.clipboard and execCommand are unreliable,
// so we maintain clipboard state server-side.
type clipboardStore struct {
	mu   sync.RWMutex
	text string
}

func (s *clipboardStore) Read() string {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.text
}

func (s *clipboardStore) Write(text string) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.text = text
}

func (h *Handlers) clipboardEnabled() bool {
	return h != nil && h.Config != nil && h.Config.AllowClipboard
}

// clipboardSetting is the config path gating the clipboard endpoints. Clipboard
// has no entry in the route catalogue's capability table, so unlike every other
// gate its setting has no owner there and is declared here instead.
const clipboardSetting = "security.allowClipboard"

// writeClipboardDisabled emits the clipboard capability refusal. It exists so the
// read and write gates cannot drift apart, and so the clipboard carries the same
// guidance — including the restart — as every catalogued capability.
func writeClipboardDisabled(w http.ResponseWriter) {
	httpx.ErrorCode(w, 403, "clipboard_disabled",
		httpx.DisabledEndpointMessage("clipboard", clipboardSetting), false,
		httpx.DisabledEndpointDetails(clipboardSetting))
}

func rejectClipboardTabID(w http.ResponseWriter, r *http.Request) bool {
	if strings.TrimSpace(r.URL.Query().Get("tabId")) != "" {
		httpx.Error(w, http.StatusBadRequest, fmt.Errorf("tabId is not supported for shared clipboard operations"))
		return true
	}
	return false
}

// HandleClipboardRead reads text from the clipboard.
func (h *Handlers) HandleClipboardRead(w http.ResponseWriter, r *http.Request) {
	if !h.clipboardEnabled() {
		writeClipboardDisabled(w)
		return
	}
	if rejectClipboardTabID(w, r) {
		return
	}

	text := h.clipboard.Read()

	h.recordActivity(r, activity.Update{Action: "clipboard.read"})

	slog.Info("clipboard: read",
		"textLen", len(text),
		"remoteAddr", r.RemoteAddr,
	)

	httpx.JSON(w, http.StatusOK, map[string]any{
		"text": text,
	})
}

// HandleClipboardWrite writes text to the clipboard.
func (h *Handlers) HandleClipboardWrite(w http.ResponseWriter, r *http.Request) {
	if !h.clipboardEnabled() {
		writeClipboardDisabled(w)
		return
	}
	if rejectClipboardTabID(w, r) {
		return
	}

	var req clipboardRequest
	dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize))
	dec.DisallowUnknownFields()
	if err := dec.Decode(&req); err != nil {
		httpx.Error(w, http.StatusBadRequest, fmt.Errorf("decode: %w", err))
		return
	}
	if req.Text == nil {
		httpx.Error(w, http.StatusBadRequest, fmt.Errorf("text required"))
		return
	}
	if len(*req.Text) > maxClipboardTextBytes {
		httpx.Error(w, http.StatusBadRequest, fmt.Errorf("text too large (max %d bytes)", maxClipboardTextBytes))
		return
	}

	h.clipboard.Write(*req.Text)

	h.recordActivity(r, activity.Update{Action: "clipboard.write"})

	slog.Info("clipboard: write",
		"textLen", len(*req.Text),
		"remoteAddr", r.RemoteAddr,
	)

	httpx.JSON(w, http.StatusOK, map[string]any{
		"success": true,
	})
}

// HandleClipboardCopy is an alias for HandleClipboardWrite.
func (h *Handlers) HandleClipboardCopy(w http.ResponseWriter, r *http.Request) {
	h.HandleClipboardWrite(w, r)
}

// HandleClipboardPaste reads from clipboard (paste = read clipboard content).
func (h *Handlers) HandleClipboardPaste(w http.ResponseWriter, r *http.Request) {
	h.HandleClipboardRead(w, r)
}