Skip to content

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

fix: validate port is numeric and within 1-65535 range

Boschc8fd5343fbcc
diff --git a/cmd/pinchtab/cmd_dashboard.go b/cmd/pinchtab/cmd_dashboard.go
index 8048fb51..fffbfc97 100644
--- a/cmd/pinchtab/cmd_dashboard.go
+++ b/cmd/pinchtab/cmd_dashboard.go
@@ -3,8 +3,10 @@ package main
import (
"fmt"
"net"
+ "os"
"os/exec"
"runtime"
+ "strconv"
"time"
"github.com/pinchtab/pinchtab/internal/cli"
@@ -31,7 +33,11 @@ func init() {
}
func runDashboardCommand(cfgPort, cfgBind, cfgToken string, noOpen bool, portOverride string) {
- port := resolvePort(cfgPort, portOverride)
+ port, err := resolvePort(cfgPort, portOverride)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error: %s\n", err)
+ os.Exit(1)
+ }
host := resolveHost(cfgBind)
url := fmt.Sprintf("http://%s:%s", host, port)
@@ -67,14 +73,22 @@ func runDashboardCommand(cfgPort, cfgBind, cfgToken string, noOpen bool, portOve
}
}
-func resolvePort(cfgPort, override string) string {
+func resolvePort(cfgPort, override string) (string, error) {
+ port := "9870"
+ if cfgPort != "" {
+ port = cfgPort
+ }
if override != "" {
- return override
+ port = override
}
- if cfgPort != "" {
- return cfgPort
+ n, err := strconv.Atoi(port)
+ if err != nil {
+ return "", fmt.Errorf("invalid port %q: must be a number", port)
+ }
+ if n < 1 || n > 65535 {
+ return "", fmt.Errorf("invalid port %d: must be between 1 and 65535", n)
}
- return "9870"
+ return port, nil
}
func resolveHost(bind string) string {
diff --git a/cmd/pinchtab/cmd_dashboard_test.go b/cmd/pinchtab/cmd_dashboard_test.go
index acbaa045..50596de9 100644
--- a/cmd/pinchtab/cmd_dashboard_test.go
+++ b/cmd/pinchtab/cmd_dashboard_test.go
@@ -8,15 +8,33 @@ func TestResolvePort(t *testing.T) {
cfgPort string
override string
want string
+ wantErr bool
}{
- {"default when empty", "", "", "9870"},
- {"uses config port", "8080", "", "8080"},
- {"override wins", "8080", "7777", "7777"},
- {"override wins over empty config", "", "7777", "7777"},
+ {"default when empty", "", "", "9870", false},
+ {"uses config port", "8080", "", "8080", false},
+ {"override wins", "8080", "7777", "7777", false},
+ {"override wins over empty config", "", "7777", "7777", false},
+ {"min valid port", "", "1", "1", false},
+ {"max valid port", "", "65535", "65535", false},
+ {"rejects zero", "", "0", "", true},
+ {"rejects negative", "", "-1", "", true},
+ {"rejects too high", "", "65536", "", true},
+ {"rejects non-numeric", "", "abc", "", true},
+ {"rejects non-numeric config", "notaport", "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- got := resolvePort(tt.cfgPort, tt.override)
+ got, err := resolvePort(tt.cfgPort, tt.override)
+ if tt.wantErr {
+ if err == nil {
+ t.Errorf("resolvePort(%q, %q) expected error, got %q", tt.cfgPort, tt.override, got)
+ }
+ return
+ }
+ if err != nil {
+ t.Errorf("resolvePort(%q, %q) unexpected error: %v", tt.cfgPort, tt.override, err)
+ return
+ }
if got != tt.want {
t.Errorf("resolvePort(%q, %q) = %q, want %q", tt.cfgPort, tt.override, got, tt.want)
}