Skip to content

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

fix: route isolated cookie runs through orchestrator

Luigi Agosti2f4f73244edf
diff --git a/internal/cli/actions/actions_audit.go b/internal/cli/actions/actions_audit.go
index a79a0050..a19df07b 100644
--- a/internal/cli/actions/actions_audit.go
+++ b/internal/cli/actions/actions_audit.go
@@ -136,7 +136,47 @@ func startIsolatedInstance(client *http.Client, base, token string) (string, fun
}
return nil
}
- return strings.TrimSuffix(instance.URL, "/"), cleanup, nil
+ if err := waitForIsolatedInstance(client, base, token, instance.ID); err != nil {
+ if cleanupErr := cleanup(); cleanupErr != nil {
+ return "", nil, fmt.Errorf("wait for isolated instance: %w (also failed to stop isolated instance: %v)", err, cleanupErr)
+ }
+ return "", nil, fmt.Errorf("wait for isolated instance: %w", err)
+ }
+ // Child instance URLs bind to the orchestrator's local interface. They are
+ // therefore not generally reachable by a remote CLI client (including the
+ // E2E runner container). Use the orchestrator's per-instance proxy instead.
+ return strings.TrimSuffix(base, "/") + "/instances/" + url.PathEscape(instance.ID), cleanup, nil
+}
+
+func waitForIsolatedInstance(client *http.Client, base, token, instanceID string) error {
+ const timeout = 30 * time.Second
+ deadline := time.Now().Add(timeout)
+ path := "/instances/" + url.PathEscape(instanceID)
+
+ for {
+ raw, err := apiclient.DoGetRawE(client, base, token, path, nil)
+ if err == nil {
+ var instance struct {
+ Status string `json:"status"`
+ }
+ if err := json.Unmarshal(raw, &instance); err != nil {
+ return fmt.Errorf("parse instance status: %w", err)
+ }
+ switch instance.Status {
+ case "running":
+ return nil
+ case "error", "stopped":
+ return fmt.Errorf("instance %q entered %s state", instanceID, instance.Status)
+ }
+ }
+ if time.Now().After(deadline) {
+ if err != nil {
+ return fmt.Errorf("instance %q did not become ready within %s: %w", instanceID, timeout, err)
+ }
+ return fmt.Errorf("instance %q did not become ready within %s", instanceID, timeout)
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
}
// resolveProfileBase routes the run at the instance owning the named
diff --git a/internal/cli/actions/actions_audit_test.go b/internal/cli/actions/actions_audit_test.go
index b09dd70d..aea79ff4 100644
--- a/internal/cli/actions/actions_audit_test.go
+++ b/internal/cli/actions/actions_audit_test.go
@@ -1,7 +1,6 @@
package actions
import (
- "fmt"
"net/http"
"net/http/httptest"
"strings"
@@ -80,21 +79,27 @@ func TestAuditPDFWithoutOutputDirReturnsBeforePOST(t *testing.T) {
func TestAuditCookieRunStopsIsolatedInstanceOnFailure(t *testing.T) {
var stopped atomic.Bool
var deletedCookies atomic.Bool
- var srv *httptest.Server
- srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var instancePaths []string
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/instances/start":
- _, _ = fmt.Fprintf(w, `{"id":"isolated","url":%q}`, srv.URL)
- case "/tab":
+ _, _ = w.Write([]byte(`{"id":"isolated","url":"http://localhost:9870"}`))
+ case "/instances/isolated":
+ _, _ = w.Write([]byte(`{"id":"isolated","status":"running"}`))
+ case "/instances/isolated/tab":
+ instancePaths = append(instancePaths, r.URL.Path)
_, _ = w.Write([]byte(`{"tabId":"temporary-tab"}`))
- case "/cookies":
+ case "/instances/isolated/cookies":
+ instancePaths = append(instancePaths, r.URL.Path)
if r.Method == http.MethodDelete {
deletedCookies.Store(true)
}
_, _ = w.Write([]byte(`{"set":1}`))
- case "/close":
+ case "/instances/isolated/close":
+ instancePaths = append(instancePaths, r.URL.Path)
_, _ = w.Write([]byte(`{}`))
- case "/audit":
+ case "/instances/isolated/audit":
+ instancePaths = append(instancePaths, r.URL.Path)
http.Error(w, `{"error":"upstream failed"}`, http.StatusBadGateway)
case "/instances/isolated/stop":
stopped.Store(true)
@@ -116,6 +121,15 @@ func TestAuditCookieRunStopsIsolatedInstanceOnFailure(t *testing.T) {
if deletedCookies.Load() {
t.Fatal("cookie-authenticated audit cleared cookies instead of discarding its isolated instance")
}
+ wantPaths := []string{
+ "/instances/isolated/tab",
+ "/instances/isolated/cookies",
+ "/instances/isolated/close",
+ "/instances/isolated/audit",
+ }
+ if strings.Join(instancePaths, ",") != strings.Join(wantPaths, ",") {
+ t.Fatalf("isolated instance paths = %v, want %v", instancePaths, wantPaths)
+ }
}
func TestApplyRunAuthRejectsProfileWithCookies(t *testing.T) {
diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.go
index c435b34a..412f5d8a 100644
--- a/internal/orchestrator/handlers.go
+++ b/internal/orchestrator/handlers.go
@@ -78,6 +78,14 @@ func (o *Orchestrator) registerHandlers(mux *http.ServeMux, skipLaunch bool) {
mux.HandleFunc("GET /instances/{id}/tabs", o.handleInstanceTabs)
mux.HandleFunc("POST /instances/{id}/tabs/open", o.handleInstanceTabOpen)
mux.HandleFunc("POST /instances/{id}/tab", o.proxyToInstance)
+ // Disposable, cookie-authenticated CLI runs access their isolated child
+ // through these routes because a child's loopback URL is not reachable by
+ // remote clients.
+ mux.HandleFunc("POST /instances/{id}/close", o.proxyToInstance)
+ cookiesMeta, _ := routes.Meta(routes.CapCookies)
+ registerCapabilityRoute(mux, "POST /instances/{id}/cookies", o.Allows(routes.CapCookies), cookiesMeta.Label, cookiesMeta.Setting, cookiesMeta.DisabledCode, o.proxyToInstance)
+ mux.HandleFunc("POST /instances/{id}/audit", o.proxyToInstance)
+ mux.HandleFunc("POST /instances/{id}/scrape", o.proxyToInstance)
screencastMeta, _ := routes.Meta(routes.CapScreencast)
registerCapabilityRoute(mux, "GET /instances/{id}/proxy/screencast", o.Allows(routes.CapScreencast), screencastMeta.Label, screencastMeta.Setting, screencastMeta.DisabledCode, o.handleProxyScreencast)
registerCapabilityRoute(mux, "GET /instances/{id}/screencast", o.Allows(routes.CapScreencast), screencastMeta.Label, screencastMeta.Setting, screencastMeta.DisabledCode, o.proxyToInstance)
diff --git a/internal/orchestrator/orchestrator_test.go b/internal/orchestrator/orchestrator_test.go
index 786bfc8b..7c1a6d1d 100644
--- a/internal/orchestrator/orchestrator_test.go
+++ b/internal/orchestrator/orchestrator_test.go
@@ -1195,6 +1195,7 @@ func TestOrchestrator_RegisterHandlers_LocksSensitiveRoutes(t *testing.T) {
{method: "GET", path: "/tabs/tab1/download", setting: "security.allowDownload"},
{method: "GET", path: "/tabs/tab1/cookies", setting: "security.allowCookies"},
{method: "DELETE", path: "/tabs/tab1/cookies", setting: "security.allowCookies"},
+ {method: "POST", path: "/instances/inst1/cookies", body: `{}`, setting: "security.allowCookies"},
{method: "POST", path: "/tabs/tab1/upload", body: `{}`, setting: "security.allowUpload"},
{method: "GET", path: "/instances/inst1/screencast", setting: "security.allowScreencast"},
}