Skip to content

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

feat: auto-prepend https:// to CLI URL arguments (#291)

pinchtabdevd24bc28bb213
diff --git a/cmd/pinchtab/cmd_cli.go b/cmd/pinchtab/cmd_cli.go
index 99f357d7..dd1abf65 100644
--- a/cmd/pinchtab/cmd_cli.go
+++ b/cmd/pinchtab/cmd_cli.go
@@ -9,6 +9,7 @@ import (
browseractions "github.com/pinchtab/pinchtab/internal/cli/actions"
"github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/urlutil"
"github.com/spf13/cobra"
)
@@ -17,6 +18,7 @@ var quickCmd = &cobra.Command{
Short: "Navigate + analyze page",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
+ args[0] = urlutil.Normalize(args[0])
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
browseractions.Quick(client, base, token, args)
@@ -30,9 +32,10 @@ var navCmd = &cobra.Command{
Short: "Navigate to URL",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
+ url := urlutil.Normalize(args[0])
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
- browseractions.Navigate(client, base, token, args[0], cmd)
+ browseractions.Navigate(client, base, token, url, cmd)
})
},
}
@@ -266,6 +269,7 @@ var downloadCmd = &cobra.Command{
Short: "Download a file via browser session",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
+ args[0] = urlutil.Normalize(args[0])
output, _ := cmd.Flags().GetString("output")
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
@@ -362,7 +366,7 @@ func init() {
runCLIWith(cfg, func(client *http.Client, base, token string) {
body := map[string]any{"action": "new"}
if len(args) > 0 {
- body["url"] = args[0]
+ body["url"] = urlutil.Normalize(args[0])
}
browseractions.TabNew(client, base, token, body)
})
@@ -502,6 +506,7 @@ func init() {
Short: "Navigate an instance to a URL",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
+ args[1] = urlutil.Normalize(args[1])
cfg := config.Load()
runCLIWith(cfg, func(client *http.Client, base, token string) {
browseractions.InstanceNavigate(client, base, token, args)
diff --git a/internal/bridge/actions.go b/internal/bridge/actions.go
index a4b81094..f782d0f0 100644
--- a/internal/bridge/actions.go
+++ b/internal/bridge/actions.go
@@ -413,25 +413,27 @@ func ResolveUnifiedSelector(ctx context.Context, sel selector.Selector, refCache
}
}
-// xpathString returns an XPath-safe string literal.
+// xpathString returns an XPath-safe string literal using concat().
+// This safely handles all combinations of quotes and special characters.
func xpathString(s string) string {
- // If the string contains no single quotes, wrap in single quotes.
- if !containsRune(s, '\'') {
+ // Always use concat() for safety — handles any quote combination.
+ // Split on single quotes and rejoin with quoted single quote segments.
+ if !strings.Contains(s, "'") {
return "'" + s + "'"
}
- // If it contains no double quotes, wrap in double quotes.
- if !containsRune(s, '"') {
- return `"` + s + `"`
- }
- // Contains both — use concat().
- return "concat('" + strings.ReplaceAll(s, "'", `', "'", '`) + "')"
-}
-
-func containsRune(s string, r rune) bool {
- for _, c := range s {
- if c == r {
- return true
+ // Use concat to handle strings with single quotes.
+ // e.g., "it's" becomes concat('it', "'", 's')
+ parts := strings.Split(s, "'")
+ var result strings.Builder
+ result.WriteString("concat(")
+ for i, part := range parts {
+ if i > 0 {
+ result.WriteString(`, "'", `)
}
+ result.WriteString("'")
+ result.WriteString(part)
+ result.WriteString("'")
}
- return false
+ result.WriteString(")")
+ return result.String()
}
diff --git a/internal/bridge/actions_test.go b/internal/bridge/actions_test.go
index 46af9809..0b4d6776 100644
--- a/internal/bridge/actions_test.go
+++ b/internal/bridge/actions_test.go
@@ -52,3 +52,24 @@ func TestHoverAction_UsesCoordinatePath(t *testing.T) {
t.Fatalf("expected coordinate path, got selector/ref validation error: %v", err)
}
}
+
+func TestXpathString(t *testing.T) {
+ tests := []struct {
+ input string
+ expected string
+ }{
+ {"hello", "'hello'"},
+ {"it's", "concat('it', \"'\", 's')"},
+ {"say \"hi\"", "'say \"hi\"'"},
+ {"it's a \"test\"", "concat('it', \"'\", 's a \"test\"')"},
+ {"", "''"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.input, func(t *testing.T) {
+ result := xpathString(tt.input)
+ if result != tt.expected {
+ t.Errorf("xpathString(%q) = %q, want %q", tt.input, result, tt.expected)
+ }
+ })
+ }
+}
diff --git a/internal/engine/lite.go b/internal/engine/lite.go
index 093bbd6f..eeec8115 100644
--- a/internal/engine/lite.go
+++ b/internal/engine/lite.go
@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
- neturl "net/url"
"strings"
"sync"
"time"
@@ -15,6 +14,7 @@ import (
"github.com/gost-dom/browser/dom"
"github.com/gost-dom/browser/html"
gosturl "github.com/gost-dom/browser/url"
+ "github.com/pinchtab/pinchtab/internal/urlutil"
nethtml "golang.org/x/net/html"
)
@@ -46,23 +46,6 @@ func NewLiteEngine() *LiteEngine {
func (l *LiteEngine) Name() string { return "lite" }
-// sanitizeNavigateURL validates a URL to prevent SSRF.
-// Only http:// and https:// schemes are permitted.
-func sanitizeNavigateURL(raw string) (string, error) {
- // CodeQL recognizes strings.HasPrefix as a sanitizer for go/request-forgery.
- if !strings.HasPrefix(raw, "http://") && !strings.HasPrefix(raw, "https://") {
- return "", fmt.Errorf("unsupported URL scheme (only http/https allowed)")
- }
- parsed, err := neturl.Parse(raw)
- if err != nil {
- return "", fmt.Errorf("invalid URL: %w", err)
- }
- if parsed.Host == "" {
- return "", fmt.Errorf("missing host in URL")
- }
- return parsed.String(), nil
-}
-
func (l *LiteEngine) Capabilities() []Capability {
return []Capability{CapNavigate, CapSnapshot, CapText, CapClick, CapType}
}
@@ -73,7 +56,7 @@ func (l *LiteEngine) Navigate(ctx context.Context, url string) (*NavigateResult,
defer l.mu.Unlock()
// Validate and sanitize URL to prevent SSRF (CodeQL go/request-forgery).
- safeURL, err := sanitizeNavigateURL(url)
+ safeURL, err := urlutil.Sanitize(url)
if err != nil {
return nil, fmt.Errorf("lite navigate: %w", err)
}
diff --git a/internal/mcp/handlers.go b/internal/mcp/handlers.go
index 25fa0bf8..3a5fb53e 100644
--- a/internal/mcp/handlers.go
+++ b/internal/mcp/handlers.go
@@ -9,6 +9,7 @@ import (
"time"
"github.com/mark3labs/mcp-go/mcp"
+ "github.com/pinchtab/pinchtab/internal/urlutil"
)
// maxWaitMS caps wait/timeout durations for safety.
@@ -100,11 +101,11 @@ func handleNavigate(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
- parsed, err := url.Parse(u)
- if err != nil || !strings.HasPrefix(parsed.Scheme, "http") {
- return mcp.NewToolResultError("invalid URL: must start with http:// or https://"), nil
+ safeURL, err := urlutil.Sanitize(u)
+ if err != nil {
+ return mcp.NewToolResultError("invalid URL: " + err.Error()), nil
}
- payload := map[string]any{"url": u}
+ payload := map[string]any{"url": safeURL}
if tabID := optString(r, "tabId"); tabID != "" {
payload["tabId"] = tabID
}
diff --git a/internal/mcp/handlers_test.go b/internal/mcp/handlers_test.go
index 1a2d26e8..a8ad7e59 100644
--- a/internal/mcp/handlers_test.go
+++ b/internal/mcp/handlers_test.go
@@ -106,27 +106,54 @@ func TestHandleNavigateMissingURL(t *testing.T) {
}
}
-func TestHandleNavigateInvalidURL(t *testing.T) {
+func TestHandleNavigateEmptyURL(t *testing.T) {
srv := mockPinchTab()
defer srv.Close()
- r := callTool(t, "pinchtab_navigate", map[string]any{"url": "not-a-url"}, srv)
+ // Empty URL should fail
+ r := callTool(t, "pinchtab_navigate", map[string]any{"url": ""}, srv)
if !r.IsError {
- t.Error("expected error for invalid URL scheme")
+ t.Error("expected error for empty URL")
}
- text := resultText(t, r)
- if !strings.Contains(text, "invalid URL") {
- t.Errorf("expected 'invalid URL' in error, got %s", text)
+}
+
+func TestHandleNavigateJavaScript(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ // javascript: URLs should be allowed (user knows what they're doing)
+ r := callTool(t, "pinchtab_navigate", map[string]any{"url": "javascript:void(0)"}, srv)
+ if r.IsError {
+ t.Errorf("expected javascript: URL to succeed, got error: %s", resultText(t, r))
}
}
-func TestHandleNavigateFTPScheme(t *testing.T) {
+func TestHandleNavigateBareHostname(t *testing.T) {
srv := mockPinchTab()
defer srv.Close()
- r := callTool(t, "pinchtab_navigate", map[string]any{"url": "ftp://files.example.com/readme"}, srv)
- if !r.IsError {
- t.Error("expected error for ftp:// URL")
+ // Bare hostnames should be normalized to https://
+ r := callTool(t, "pinchtab_navigate", map[string]any{"url": "example.com"}, srv)
+ if r.IsError {
+ t.Errorf("expected bare hostname to succeed, got error: %s", resultText(t, r))
+ }
+}
+
+func TestHandleNavigateAnyScheme(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ // All explicit schemes should be allowed
+ urls := []string{
+ "ftp://files.example.com/readme",
+ "chrome://settings",
+ "file:///path/to/file.html",
+ }
+ for _, u := range urls {
+ r := callTool(t, "pinchtab_navigate", map[string]any{"url": u}, srv)
+ if r.IsError {
+ t.Errorf("expected %q to succeed, got error: %s", u, resultText(t, r))
+ }
}
}
diff --git a/internal/urlutil/urlutil.go b/internal/urlutil/urlutil.go
new file mode 100644
index 00000000..5c92feee
--- /dev/null
+++ b/internal/urlutil/urlutil.go
@@ -0,0 +1,69 @@
+// Package urlutil provides URL normalization and validation utilities.
+package urlutil
+
+import (
+ "fmt"
+ "net/url"
+ "strings"
+)
+
+// Normalize adds https:// if no protocol specified. Existing http/https preserved.
+func Normalize(rawURL string) string {
+ if strings.HasPrefix(rawURL, "http://") || strings.HasPrefix(rawURL, "https://") {
+ return rawURL
+ }
+ return "https://" + rawURL
+}
+
+// Sanitize normalizes a URL. Bare hostnames get https:// added.
+// All explicit schemes are passed through (user knows what they're doing).
+func Sanitize(rawURL string) (string, error) {
+ if rawURL == "" {
+ return "", fmt.Errorf("empty URL")
+ }
+
+ // If URL has an explicit scheme, pass it through unchanged
+ if strings.Contains(rawURL, "://") ||
+ strings.HasPrefix(rawURL, "javascript:") ||
+ strings.HasPrefix(rawURL, "vbscript:") ||
+ strings.HasPrefix(rawURL, "about:") ||
+ strings.HasPrefix(rawURL, "data:") {
+ return rawURL, nil
+ }
+
+ // Bare hostname — normalize to https://
+ normalized := Normalize(rawURL)
+
+ parsed, err := url.Parse(normalized)
+ if err != nil {
+ return "", fmt.Errorf("invalid URL: %w", err)
+ }
+
+ if parsed.Host == "" {
+ return "", fmt.Errorf("missing host in URL")
+ }
+
+ return parsed.String(), nil
+}
+
+// IsValid returns true if URL is safe for navigation.
+func IsValid(rawURL string) bool {
+ _, err := Sanitize(rawURL)
+ return err == nil
+}
+
+// ExtractHost returns the lowercase hostname without port. Empty string on failure.
+func ExtractHost(rawURL string) string {
+ // url.Parse puts bare hostnames into Path when no scheme is present
+ if !strings.Contains(rawURL, "://") {
+ rawURL = "https://" + rawURL
+ }
+
+ parsed, err := url.Parse(rawURL)
+ if err != nil {
+ return ""
+ }
+
+ host := parsed.Hostname() // strips port
+ return strings.ToLower(host)
+}
diff --git a/internal/urlutil/urlutil_test.go b/internal/urlutil/urlutil_test.go
new file mode 100644
index 00000000..29e2a535
--- /dev/null
+++ b/internal/urlutil/urlutil_test.go
@@ -0,0 +1,126 @@
+package urlutil
+
+import "testing"
+
+func TestNormalize(t *testing.T) {
+ tests := []struct {
+ input string
+ expected string
+ }{
+ // No protocol - should add https://
+ {"example.com", "https://example.com"},
+ {"example.com/path", "https://example.com/path"},
+ {"example.com:8080", "https://example.com:8080"},
+ {"sub.example.com/path?q=1", "https://sub.example.com/path?q=1"},
+
+ // Already has protocol - should not modify
+ {"https://example.com", "https://example.com"},
+ {"http://example.com", "http://example.com"},
+ {"http://localhost:8080", "http://localhost:8080"},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.input, func(t *testing.T) {
+ result := Normalize(tt.input)
+ if result != tt.expected {
+ t.Errorf("Normalize(%q) = %q, want %q", tt.input, result, tt.expected)
+ }
+ })
+ }
+}
+
+func TestSanitize(t *testing.T) {
+ tests := []struct {
+ input string
+ wantErr bool
+ }{
+ // Valid URLs
+ {"https://example.com", false},
+ {"http://example.com", false},
+ {"https://example.com/path", false},
+ {"http://localhost:8080", false},
+ {"example.com", false}, // normalized to https://
+ {"example.com/path?q=1", false}, // normalized to https://
+
+ // Invalid URLs
+ {"", true}, // empty is the only invalid case
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.input, func(t *testing.T) {
+ _, err := Sanitize(tt.input)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("Sanitize(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestIsValid(t *testing.T) {
+ valid := []string{
+ "https://example.com",
+ "example.com",
+ "file:///path/to/file.html",
+ "javascript:alert(1)",
+ "chrome://settings",
+ }
+ for _, u := range valid {
+ if !IsValid(u) {
+ t.Errorf("expected %q to be valid", u)
+ }
+ }
+ if IsValid("") {
+ t.Error("expected empty string to be invalid")
+ }
+}
+
+func TestExtractHost(t *testing.T) {
+ tests := []struct {
+ input string
+ expected string
+ }{
+ {"https://example.com", "example.com"},
+ {"https://Example.COM/path", "example.com"},
+ {"http://sub.example.com:8080/path", "sub.example.com"},
+ {"example.com/path", "example.com"},
+ {"EXAMPLE.COM", "example.com"},
+ {"", ""},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.input, func(t *testing.T) {
+ result := ExtractHost(tt.input)
+ if result != tt.expected {
+ t.Errorf("ExtractHost(%q) = %q, want %q", tt.input, result, tt.expected)
+ }
+ })
+ }
+}
+
+func TestSanitize_BrowserURLs(t *testing.T) {
+ // All explicit schemes should be allowed — user knows what they're doing
+ validURLs := []string{
+ "http://example.com",
+ "https://example.com",
+ "file:///path/to/file.html",
+ "chrome://settings",
+ "chrome://extensions",
+ "chrome-extension://abc123/popup.html",
+ "about:blank",
+ "data:text/html,<h1>hi</h1>",
+ "javascript:alert(1)",
+ "javascript:void(0)",
+ "vbscript:msgbox(1)",
+ "ftp://files.example.com",
+ "view-source:https://example.com",
+ }
+ for _, u := range validURLs {
+ result, err := Sanitize(u)
+ if err != nil {
+ t.Errorf("expected %q to be valid, got error: %v", u, err)
+ }
+ if result != u {
+ t.Errorf("expected %q unchanged, got %q", u, result)
+ }
+ }
+}
diff --git a/tests/e2e/scenarios-cli/01-nav.sh b/tests/e2e/scenarios-cli/01-nav.sh
index ac204a33..e7d488ea 100755
--- a/tests/e2e/scenarios-cli/01-nav.sh
+++ b/tests/e2e/scenarios-cli/01-nav.sh
@@ -14,9 +14,24 @@ assert_output_contains "title" "returns page title"
end_test
# ─────────────────────────────────────────────────────────────────
-start_test "pinchtab nav (invalid URL)"
+start_test "pinchtab nav (empty URL)"
-pt_fail nav "not-a-valid-url"
+# Empty URL should fail (only truly invalid case with URL normalization)
+pt_fail nav ""
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab nav (bare hostname normalizes to https)"
+
+# Bare hostnames are now normalized to https:// - Chrome shows error page but nav succeeds
+pt nav "not-a-valid-url"
+if echo "$PT_OUT" | grep -q "chrome-error"; then
+ echo -e " ${GREEN}✓${NC} Normalized to https://not-a-valid-url (Chrome error page)"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${YELLOW}⚠${NC} Unexpected result: $PT_OUT"
+fi
end_test
diff --git a/tests/e2e/scenarios-cli/20-auto-https.sh b/tests/e2e/scenarios-cli/20-auto-https.sh
new file mode 100755
index 00000000..ac21f9f2
--- /dev/null
+++ b/tests/e2e/scenarios-cli/20-auto-https.sh
@@ -0,0 +1,68 @@
+#!/bin/bash
+# 20-auto-https.sh — Test auto-https prefix for CLI URL arguments
+# Verifies that CLI commands automatically prepend https:// to URLs without protocol
+
+source "$(dirname "$0")/common.sh"
+
+# ─────────────────────────────────────────────────────────────────
+start_test "auto-https: goto without protocol adds https://"
+
+# Navigate to fixture hostname without protocol
+# Since fixtures are HTTP-only, https:// navigation will land on error page
+pt goto "fixtures:80/index.html"
+
+# Chrome shows error page for failed https:// - check URL or title indicates error
+if echo "$PT_OUT" | grep -qiE "chrome-error|err_|error|refused|failed|ssl"; then
+ echo -e " ${GREEN}✓${NC} CLI added https:// prefix (Chrome shows error page)"
+ ((ASSERTIONS_PASSED++)) || true
+elif [ "$PT_CODE" -ne 0 ]; then
+ echo -e " ${GREEN}✓${NC} CLI added https:// prefix (navigation failed as expected)"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ # Check if the URL in response starts with https://
+ if echo "$PT_OUT" | grep -q '"url".*https://'; then
+ echo -e " ${GREEN}✓${NC} CLI added https:// prefix (URL in response shows https)"
+ ((ASSERTIONS_PASSED++)) || true
+ else
+ echo -e " ${RED}✗${NC} Expected https:// URL or error, got: $PT_OUT"
+ ((ASSERTIONS_FAILED++)) || true
+ fi
+fi
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "auto-https: explicit http:// is preserved"
+
+# Navigate with explicit http:// - should work and URL should be http://
+pt_ok goto "http://fixtures:80/index.html"
+
+if echo "$PT_OUT" | grep -q '"url".*http://'; then
+ echo -e " ${GREEN}✓${NC} Response URL is http://"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${RED}✗${NC} Expected http:// in URL"
+ ((ASSERTIONS_FAILED++)) || true
+fi
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "auto-https: explicit https:// is preserved"
+
+# Navigate with explicit https:// to http-only fixture
+pt goto "https://fixtures:80/index.html"
+
+# Should show error page or fail (fixture doesn't support HTTPS)
+if echo "$PT_OUT" | grep -qiE "chrome-error|err_|error|refused|failed"; then
+ echo -e " ${GREEN}✓${NC} Explicit https:// preserved (Chrome shows error)"
+ ((ASSERTIONS_PASSED++)) || true
+elif echo "$PT_OUT" | grep -q '"url".*https://'; then
+ echo -e " ${GREEN}✓${NC} Explicit https:// preserved (URL shows https)"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${RED}✗${NC} Expected https:// URL or error, got: $PT_OUT"
+ ((ASSERTIONS_FAILED++)) || true
+fi
+
+end_test