Skip to content

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

fix: address CodeQL security issues and lint errors

Mario2a408dea742a
diff --git a/cmd/pinchtab/cmd_cli.go b/cmd/pinchtab/cmd_cli.go
index 713a1d81..df9713a9 100644
--- a/cmd/pinchtab/cmd_cli.go
+++ b/cmd/pinchtab/cmd_cli.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
+ "log"
"net/http"
"net/url"
"os"
@@ -1087,7 +1088,9 @@ func doGet(client *http.Client, base, token, path string, params url.Values) map
// Parse and return result
var result map[string]any
- json.Unmarshal(body, &result)
+ if err := json.Unmarshal(body, &result); err != nil {
+ log.Printf("warning: error unmarshaling response: %v", err)
+ }
return result
}
@@ -1142,7 +1145,9 @@ func doPost(client *http.Client, base, token, path string, body map[string]any)
// Parse and return result for suggestions
var result map[string]any
- json.Unmarshal(respBody, &result)
+ if err := json.Unmarshal(respBody, &result); err != nil {
+ log.Printf("warning: error unmarshaling response: %v", err)
+ }
return result
}
@@ -1174,7 +1179,7 @@ Learn more: https://github.com/pinchtab/pinchtab#quick-start
fmt.Fprintf(os.Stderr, "❌ Cannot connect to Pinchtab server: %v\n", err)
return false
}
- defer resp.Body.Close()
+ defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == 401 {
fmt.Fprintf(os.Stderr, "❌ Authentication required. Set BRIDGE_TOKEN or PINCHTAB_TOKEN environment variable.\n")
diff --git a/cmd/pinchtab/cmd_cli_test.go b/cmd/pinchtab/cmd_cli_test.go
index ef6953ac..d6923191 100644
--- a/cmd/pinchtab/cmd_cli_test.go
+++ b/cmd/pinchtab/cmd_cli_test.go
@@ -616,7 +616,7 @@ func TestCheckServerAndGuide(t *testing.T) {
result2 := checkServerAndGuide(client2, m2.base(), "")
- w.Close()
+ _ = w.Close()
os.Stderr = oldStderr
output, _ := io.ReadAll(r)
diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.go
index 5f254cfd..73e4f682 100644
--- a/internal/orchestrator/handlers.go
+++ b/internal/orchestrator/handlers.go
@@ -3,7 +3,10 @@ package orchestrator
import (
"encoding/json"
"fmt"
+ "log"
+ "net"
"net/http"
+ "net/url"
"time"
"github.com/pinchtab/pinchtab/internal/web"
@@ -319,6 +322,23 @@ func (o *Orchestrator) proxyToInstance(w http.ResponseWriter, r *http.Request) {
targetURL += "?" + r.URL.RawQuery
}
+ // Validate proxy URL to prevent SSRF attacks
+ parsedURL, err := url.Parse(targetURL)
+ if err != nil {
+ web.Error(w, 400, fmt.Errorf("invalid proxy URL: %w", err))
+ return
+ }
+
+ // Ensure target is localhost only
+ host, _, err := net.SplitHostPort(parsedURL.Host)
+ if err != nil {
+ host = parsedURL.Hostname()
+ }
+ if host != "localhost" && host != "127.0.0.1" && host != "::1" {
+ web.Error(w, 400, fmt.Errorf("invalid proxy target: only localhost allowed"))
+ return
+ }
+
// Create proxy request
proxyReq, err := http.NewRequest(r.Method, targetURL, r.Body)
if err != nil {
@@ -453,7 +473,9 @@ func (o *Orchestrator) handleTabList(w http.ResponseWriter, r *http.Request) {
if tabResp.StatusCode == 200 {
var instanceTabs []map[string]any
- json.NewDecoder(tabResp.Body).Decode(&instanceTabs)
+ if err := json.NewDecoder(tabResp.Body).Decode(&instanceTabs); err != nil {
+ log.Printf("error decoding tabs from instance: %v", err)
+ }
// Add instanceId to each tab
for _, tab := range instanceTabs {
tab["instanceId"] = instanceID
@@ -482,7 +504,9 @@ func (o *Orchestrator) handleTabList(w http.ResponseWriter, r *http.Request) {
if tabResp.StatusCode == 200 {
var instanceTabs []map[string]any
- json.NewDecoder(tabResp.Body).Decode(&instanceTabs)
+ if err := json.NewDecoder(tabResp.Body).Decode(&instanceTabs); err != nil {
+ log.Printf("error decoding tabs from instance: %v", err)
+ }
// Add instanceId to each tab
for _, tab := range instanceTabs {
tab["instanceId"] = inst.ID
@@ -520,7 +544,9 @@ func (o *Orchestrator) handleTabGet(w http.ResponseWriter, r *http.Request) {
if tabResp.StatusCode == 200 {
var tab map[string]any
- json.NewDecoder(tabResp.Body).Decode(&tab)
+ if err := json.NewDecoder(tabResp.Body).Decode(&tab); err != nil {
+ log.Printf("error decoding tab from instance: %v", err)
+ }
tab["instanceId"] = inst.ID
web.JSON(w, 200, tab)
return
diff --git a/internal/profiles/profiles.go b/internal/profiles/profiles.go
index 28f2ad64..badeb079 100644
--- a/internal/profiles/profiles.go
+++ b/internal/profiles/profiles.go
@@ -23,6 +23,21 @@ func profileID(name string) string {
return idMgr.ProfileID(name)
}
+// validateProfileName checks for path traversal and other unsafe patterns
+// Prevents directory traversal attacks (../, ..\, /, \)
+func validateProfileName(name string) error {
+ if name == "" {
+ return fmt.Errorf("profile name cannot be empty")
+ }
+ if strings.Contains(name, "..") {
+ return fmt.Errorf("profile name cannot contain '..'")
+ }
+ if strings.ContainsAny(name, "/\\") {
+ return fmt.Errorf("profile name cannot contain '/' or '\\'")
+ }
+ return nil
+}
+
type ProfileManager struct {
baseDir string
tracker *ActionTracker
@@ -59,6 +74,9 @@ func NewProfileManager(baseDir string) *ProfileManager {
}
func (pm *ProfileManager) Exists(name string) bool {
+ if err := validateProfileName(name); err != nil {
+ return false
+ }
dir := filepath.Join(pm.baseDir, name)
info, err := os.Stat(dir)
return err == nil && info.IsDir()
@@ -111,6 +129,9 @@ func (pm *ProfileManager) List() ([]bridge.ProfileInfo, error) {
}
func (pm *ProfileManager) profileInfo(name string) (ProfileDetailedInfo, error) {
+ if err := validateProfileName(name); err != nil {
+ return ProfileDetailedInfo{}, err
+ }
dir := filepath.Join(pm.baseDir, name)
fi, err := os.Stat(dir)
if err != nil {
@@ -148,6 +169,9 @@ func (pm *ProfileManager) profileInfo(name string) (ProfileDetailedInfo, error)
}
func (pm *ProfileManager) Import(name, sourcePath string) error {
+ if err := validateProfileName(name); err != nil {
+ return err
+ }
pm.mu.Lock()
defer pm.mu.Unlock()
@@ -194,6 +218,9 @@ func (pm *ProfileManager) ImportWithMeta(name, sourcePath string, meta ProfileMe
}
func (pm *ProfileManager) Create(name string) error {
+ if err := validateProfileName(name); err != nil {
+ return err
+ }
pm.mu.Lock()
defer pm.mu.Unlock()
@@ -216,6 +243,9 @@ func (pm *ProfileManager) CreateWithMeta(name string, meta ProfileMeta) error {
}
func (pm *ProfileManager) Reset(name string) error {
+ if err := validateProfileName(name); err != nil {
+ return err
+ }
pm.mu.Lock()
defer pm.mu.Unlock()
@@ -259,6 +289,9 @@ func (pm *ProfileManager) Reset(name string) error {
}
func (pm *ProfileManager) Delete(name string) error {
+ if err := validateProfileName(name); err != nil {
+ return err
+ }
pm.mu.Lock()
defer pm.mu.Unlock()