Skip to content

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

fix: harden authenticated site runs

Luigi Agostie97825120a6c
diff --git a/internal/cli/actions/actions_audit.go b/internal/cli/actions/actions_audit.go
index a19df07b..acfd980d 100644
--- a/internal/cli/actions/actions_audit.go
+++ b/internal/cli/actions/actions_audit.go
@@ -106,9 +106,21 @@ func setRunCookies(client *http.Client, base, token, url string, cookies []audit
if tab.TabID != "" {
body["tabId"] = tab.TabID
}
- if _, err := apiclient.DoPostRawE(client, base, token, "/cookies", body); err != nil {
+ raw, err = apiclient.DoPostRawE(client, base, token, "/cookies", body)
+ if err != nil {
return err
}
+ var result struct {
+ Set int `json:"set"`
+ Failed int `json:"failed"`
+ Total int `json:"total"`
+ }
+ if err := json.Unmarshal(raw, &result); err != nil {
+ return fmt.Errorf("parse cookie injection result: %w", err)
+ }
+ if result.Failed != 0 || result.Set != len(cookies) {
+ return fmt.Errorf("cookie injection incomplete: set %d of %d cookies (%d failed)", result.Set, len(cookies), result.Failed)
+ }
return nil
}
@@ -179,9 +191,9 @@ func waitForIsolatedInstance(client *http.Client, base, token, instanceID string
}
}
-// resolveProfileBase routes the run at the instance owning the named
-// profile: the orchestrator base for the default-routed instance, or the
-// instance's own URL otherwise.
+// resolveProfileBase routes the run at the instance owning the named profile
+// through the orchestrator proxy. Child URLs normally bind to localhost and
+// are therefore not reachable by remote CLI clients.
func resolveProfileBase(client *http.Client, base, token, profile string) (string, error) {
raw, err := apiclient.DoGetRawE(client, base, token, "/instances", nil)
if err != nil {
@@ -191,7 +203,6 @@ func resolveProfileBase(client *http.Client, base, token, profile string) (strin
ID string `json:"id"`
ProfileName string `json:"profileName"`
Status string `json:"status"`
- URL string `json:"url"`
}
if err := json.Unmarshal(raw, &instances); err != nil {
return "", fmt.Errorf("parse instances: %w", err)
@@ -200,25 +211,11 @@ func resolveProfileBase(client *http.Client, base, token, profile string) (strin
if inst.ProfileName != profile || inst.Status != "running" {
continue
}
- if inst.ID == defaultInstanceID(client, base, token) {
- return base, nil
- }
- return strings.TrimSuffix(inst.URL, "/"), nil
+ return strings.TrimSuffix(base, "/") + "/instances/" + url.PathEscape(inst.ID), nil
}
return "", fmt.Errorf("no running instance for profile %q (start one: pinchtab instance start --profile %s)", profile, profile)
}
-func defaultInstanceID(client *http.Client, base, token string) string {
- raw := apiclient.DoGetRaw(client, base, token, "/health", nil)
- var health struct {
- DefaultInstance struct {
- ID string `json:"id"`
- } `json:"defaultInstance"`
- }
- _ = json.Unmarshal(raw, &health)
- return health.DefaultInstance.ID
-}
-
// validateAuditFlags rejects invalid flag combinations pre-flight, before
// any server work starts — a full audit must never run just to report a bad
// invocation.
diff --git a/internal/cli/actions/actions_audit_test.go b/internal/cli/actions/actions_audit_test.go
index aea79ff4..b2b4f6a3 100644
--- a/internal/cli/actions/actions_audit_test.go
+++ b/internal/cli/actions/actions_audit_test.go
@@ -1,12 +1,14 @@
package actions
import (
+ "encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
+ "github.com/pinchtab/pinchtab/internal/audit"
"github.com/spf13/cobra"
)
@@ -31,6 +33,24 @@ func newAuditTestCmd(args ...string) *cobra.Command {
return cmd
}
+func newCompareTestCmd(args ...string) *cobra.Command {
+ cmd := &cobra.Command{Use: "compare"}
+ cmd.Flags().String("pages", "", "")
+ cmd.Flags().Bool("visual-diff", true, "")
+ cmd.Flags().String("output-dir", "", "")
+ cmd.Flags().Int("concurrency", 0, "")
+ cmd.Flags().Bool("json", false, "")
+ cmd.Flags().Bool("fail-on-diff", false, "")
+ cmd.Flags().String("format", "json", "")
+ cmd.Flags().StringArray("cookie", nil, "")
+ cmd.Flags().String("cookies-file", "", "")
+ cmd.Flags().String("profile", "", "")
+ if err := cmd.Flags().Parse(args); err != nil {
+ panic(err)
+ }
+ return cmd
+}
+
func TestValidateAuditFlags(t *testing.T) {
if err := validateAuditFlags(newAuditTestCmd("--format", "pdf")); err == nil {
t.Error("pdf without output-dir should error")
@@ -138,3 +158,80 @@ func TestApplyRunAuthRejectsProfileWithCookies(t *testing.T) {
t.Fatalf("applyRunAuth() error = %v, want profile/cookie conflict", err)
}
}
+
+func TestResolveProfileBaseUsesOrchestratorProxy(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/instances" {
+ t.Fatalf("path = %s, want /instances", r.URL.Path)
+ }
+ _, _ = w.Write([]byte(`[{"id":"work-instance","profileName":"work","status":"running","url":"http://localhost:9870"}]`))
+ }))
+ defer srv.Close()
+
+ base, err := resolveProfileBase(http.DefaultClient, srv.URL, "", "work")
+ if err != nil {
+ t.Fatalf("resolveProfileBase() error = %v", err)
+ }
+ if want := srv.URL + "/instances/work-instance"; base != want {
+ t.Fatalf("resolveProfileBase() = %q, want %q", base, want)
+ }
+}
+
+func TestSetRunCookiesRejectsPartialWrite(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/tab":
+ _, _ = w.Write([]byte(`{"tabId":"temporary-tab"}`))
+ case "/cookies":
+ _, _ = w.Write([]byte(`{"set":0,"failed":1,"total":1}`))
+ case "/close":
+ _, _ = w.Write([]byte(`{}`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ defer srv.Close()
+
+ err := setRunCookies(http.DefaultClient, srv.URL, "", "https://example.com", []audit.Cookie{{Name: "session", Value: "temporary"}})
+ if err == nil || !strings.Contains(err.Error(), "cookie injection incomplete") {
+ t.Fatalf("setRunCookies() error = %v, want incomplete cookie injection", err)
+ }
+}
+
+func TestCompareInjectsShorthandCookiesForBothSites(t *testing.T) {
+ var cookieURLs []string
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/instances/start":
+ _, _ = w.Write([]byte(`{"id":"isolated","url":"http://localhost:9870"}`))
+ case "/instances/isolated":
+ _, _ = w.Write([]byte(`{"id":"isolated","status":"running"}`))
+ case "/instances/isolated/tab":
+ _, _ = w.Write([]byte(`{"tabId":"temporary-tab"}`))
+ case "/instances/isolated/cookies":
+ var body struct {
+ URL string `json:"url"`
+ }
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Fatalf("decode cookie body: %v", err)
+ }
+ cookieURLs = append(cookieURLs, body.URL)
+ _, _ = w.Write([]byte(`{"set":1,"failed":0,"total":1}`))
+ case "/instances/isolated/close", "/instances/isolated/stop":
+ _, _ = w.Write([]byte(`{}`))
+ case "/instances/isolated/audit":
+ _, _ = w.Write([]byte(`{"pages":[]}`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ defer srv.Close()
+
+ err := Compare(http.DefaultClient, srv.URL, "", newCompareTestCmd("--cookie", "session=temporary"), "https://live.example", "https://staging.example")
+ if err != nil {
+ t.Fatalf("Compare() error = %v", err)
+ }
+ if want := []string{"https://live.example", "https://staging.example"}; strings.Join(cookieURLs, ",") != strings.Join(want, ",") {
+ t.Fatalf("cookie URLs = %v, want %v", cookieURLs, want)
+ }
+}
diff --git a/internal/cli/actions/actions_compare.go b/internal/cli/actions/actions_compare.go
index 6e4ec526..9e1a0f7f 100644
--- a/internal/cli/actions/actions_compare.go
+++ b/internal/cli/actions/actions_compare.go
@@ -34,6 +34,21 @@ func Compare(client *http.Client, base, token string, cmd *cobra.Command, liveBa
}
}
}()
+ if cookies, err := loadRunCookies(cmd); err != nil {
+ return err
+ } else if len(cookies) > 0 {
+ stagingCookies := make([]audit.Cookie, 0, len(cookies))
+ for _, cookie := range cookies {
+ if strings.TrimSpace(cookie.Domain) == "" {
+ stagingCookies = append(stagingCookies, cookie)
+ }
+ }
+ if len(stagingCookies) > 0 {
+ if err := setRunCookies(client, base, token, stagingBase, stagingCookies); err != nil {
+ return fmt.Errorf("set staging run cookies: %w", err)
+ }
+ }
+ }
longClient := &http.Client{Transport: client.Transport, Timeout: auditTimeout}
runSide := func(siteBase string) (audit.AuditReport, error) {