High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
docs: use PINCHTAB_TOKEN instead of deprecated BRIDGE_TOKEN
Dataa782149e0c79
diff --git a/docs/references/configuration.md b/docs/references/configuration.mdindex 17beb3cb..478a5f71 100644--- a/docs/references/configuration.md+++ b/docs/references/configuration.md@@ -37,7 +37,7 @@ Complete reference for all PinchTab environment variables and configuration opti| Variable | Default | Description ||---|---|---|-| `BRIDGE_TOKEN` | Disabled | API authentication token (if set, all requests must include `Authorization: Bearer {token}`) |+| `PINCHTAB_TOKEN` | Disabled | API authentication token (if set, all requests must include `Authorization: Bearer {token}`) || `PINCHTAB_ALLOW_EVALUATE` | `false` | Enable `POST /evaluate` and `POST /tabs/{id}/evaluate` endpoints (disabled by default) |### Debugging & Logging@@ -78,7 +78,7 @@ BRIDGE_PORT=9868 ./pinchtabBRIDGE_BIND=0.0.0.0 BRIDGE_PORT=9867 ./pinchtab```-**⚠️ Security Warning:** Only use `0.0.0.0` on trusted networks. Consider using `BRIDGE_TOKEN` for authentication.+**⚠️ Security Warning:** Only use `0.0.0.0` on trusted networks. Consider using `PINCHTAB_TOKEN` for authentication.### Headed Mode with Profile@@ -110,7 +110,7 @@ Speeds up page loading by blocking ad domains.### API Authentication```bash-BRIDGE_TOKEN=my-secret-token ./pinchtab+PINCHTAB_TOKEN=my-secret-token ./pinchtab```Then all API requests must include:@@ -136,7 +136,7 @@ BRIDGE_HEADLESS=false \BRIDGE_STEALTH=full \BRIDGE_BLOCK_ADS=true \BRIDGE_PROFILE=dev \-BRIDGE_TOKEN=secret \+PINCHTAB_TOKEN=secret \./pinchtab```@@ -248,7 +248,7 @@ BRIDGE_BIND=0.0.0.0 ./pinchtabAccessible from any machine on the network. **Requires authentication:**```bash-BRIDGE_BIND=0.0.0.0 BRIDGE_TOKEN=secret ./pinchtab+BRIDGE_BIND=0.0.0.0 PINCHTAB_TOKEN=secret ./pinchtab```### Specific Interface@@ -293,7 +293,7 @@ Maximum stealth (`full`) adds significant overhead.```bash# Check token format-BRIDGE_TOKEN=my-token ./pinchtab+PINCHTAB_TOKEN=my-token ./pinchtab# Request must include the tokencurl -H "Authorization: Bearer my-token" http://localhost:9867/health@@ -303,9 +303,9 @@ curl -H "Authorization: Bearer my-token" http://localhost:9867/health## Security Best Practices-1. **Use `BRIDGE_TOKEN` when exposing to network:**+1. **Use `PINCHTAB_TOKEN` when exposing to network:**```bash- BRIDGE_BIND=0.0.0.0 BRIDGE_TOKEN=$(openssl rand -hex 32) ./pinchtab+ BRIDGE_BIND=0.0.0.0 PINCHTAB_TOKEN=$(openssl rand -hex 32) ./pinchtab```2. **Use localhost by default:**diff --git a/tests/integration/main_test.go b/tests/integration/main_test.goindex f224e8dc..1ef1f4bd 100644--- a/tests/integration/main_test.go+++ b/tests/integration/main_test.go@@ -3,405 +3,106 @@package integrationimport (- "encoding/json""fmt"- "io"- "net/http""os"- "os/exec"- "path/filepath"- "strings"- "syscall""testing"- "time"++ "github.com/pinchtab/pinchtab/tests/testutil")var (- serverURL string- currentTabID string // Track current tab for action operations+ server *testutil.Server+ client *testutil.Client+ currentTabID string)func TestMain(m *testing.M) {- port := os.Getenv("PINCHTAB_TEST_PORT")- if port == "" {- port = "19867"- }- serverURL = fmt.Sprintf("http://localhost:%s", port)+ cfg := testutil.DefaultConfig()- // Single parent temp dir for all test artifacts (binary, state, profiles).- // Cleaned up explicitly before os.Exit — defer won't run after os.Exit.- testDir, err := os.MkdirTemp("", "pinchtab-test-*")+ var err error+ server, err = testutil.StartServer(cfg)if err != nil {- fmt.Fprintf(os.Stderr, "failed to create test dir: %v\n", err)- os.Exit(1)- }- fmt.Fprintf(os.Stderr, "TestMain: test dir: %s\n", testDir)-- cleanup := func() {- if os.Getenv("PINCHTAB_TEST_KEEP_DIR") != "" {- fmt.Fprintf(os.Stderr, "TestMain: keeping test dir (PINCHTAB_TEST_KEEP_DIR set): %s\n", testDir)- return- }- os.RemoveAll(testDir)- }-- binaryPath := filepath.Join(testDir, "pinchtab")- stateDir := filepath.Join(testDir, "state")- profileDir := filepath.Join(testDir, "profiles")-- if err := os.MkdirAll(stateDir, 0o755); err != nil {- fmt.Fprintf(os.Stderr, "failed to create state dir: %v\n", err)- cleanup()- os.Exit(1)- }- if err := os.MkdirAll(profileDir, 0o755); err != nil {- fmt.Fprintf(os.Stderr, "failed to create profile dir: %v\n", err)- cleanup()- os.Exit(1)- }-- // Build the binary into the test dir- build := exec.Command("go", "build", "-o", binaryPath, "./cmd/pinchtab/")- build.Dir = findRepoRoot()- build.Stdout = os.Stdout- build.Stderr = os.Stderr- if err := build.Run(); err != nil {- fmt.Fprintf(os.Stderr, "failed to build pinchtab: %v\n", err)- cleanup()- os.Exit(1)- }-- // Start server in its own process group so we can kill Chrome children on shutdown.- cmd := exec.Command(binaryPath)-- // Build environment for subprocess- // Start with a filtered set of inherited env vars, then add test-specific ones- baseEnv := os.Environ()- env := []string{}- for _, e := range baseEnv {- // Skip any pre-existing BRIDGE_* and PINCHTAB_* vars to avoid conflicts- if !strings.HasPrefix(e, "BRIDGE_") && !strings.HasPrefix(e, "PINCHTAB_") {- env = append(env, e)- }- }-- // Add test-specific environment (use PINCHTAB_* names, BRIDGE_* are deprecated)- env = append(env,- "PINCHTAB_PORT="+port,- "PINCHTAB_HEADLESS=true",- "PINCHTAB_NO_RESTORE=true",- "PINCHTAB_STEALTH=light",- "PINCHTAB_STATE_DIR="+stateDir,- "PINCHTAB_PROFILE_DIR="+profileDir,- )-- // Pass PINCHTAB_ALLOW_EVALUATE if set (for tests that need /evaluate)- if allowEval := os.Getenv("PINCHTAB_ALLOW_EVALUATE"); allowEval != "" {- env = append(env, "PINCHTAB_ALLOW_EVALUATE="+allowEval)- }-- // Pass CHROME_BINARY if set by CI workflow or environment- if chromeBinary := os.Getenv("CHROME_BINARY"); chromeBinary != "" {- env = append(env, "CHROME_BINARY="+chromeBinary)- }-- cmd.Env = env- cmd.Stdout = os.Stdout- cmd.Stderr = os.Stderr- cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}-- if err := cmd.Start(); err != nil {- fmt.Fprintf(os.Stderr, "failed to start pinchtab: %v\n", err)- cleanup()+ fmt.Fprintf(os.Stderr, "failed to start server: %v\n", err)os.Exit(1)}- // Wait for server to be ready (longer timeout in CI)- healthTimeout := 30 * time.Second- if os.Getenv("CI") == "true" {- healthTimeout = 60 * time.Second- }- if !waitForHealth(serverURL, healthTimeout) {- fmt.Fprintf(os.Stderr, "pinchtab did not become healthy within timeout (%v)\n", healthTimeout)- _ = cmd.Process.Kill()- cleanup()- os.Exit(1)- }+ client = testutil.NewClient(server.URL)// Launch a test instance for orchestrator-mode tests- // This ensures /navigate and other proxy endpoints work in CI- if err := launchTestInstance(serverURL); err != nil {+ if _, err := testutil.LaunchInstance(server.URL); err != nil {fmt.Fprintf(os.Stderr, "failed to launch test instance: %v\n", err)- _ = cmd.Process.Kill()- cleanup()+ server.Stop()os.Exit(1)}code := m.Run()-- // Shutdown — send SIGTERM to the process group to also kill Chrome children.- if pgid, err := syscall.Getpgid(cmd.Process.Pid); err == nil {- _ = syscall.Kill(-pgid, syscall.SIGTERM)- } else {- _ = cmd.Process.Signal(os.Interrupt)- }- done := make(chan error, 1)- go func() { done <- cmd.Wait() }()- select {- case <-done:- case <-time.After(10 * time.Second):- if pgid, err := syscall.Getpgid(cmd.Process.Pid); err == nil {- _ = syscall.Kill(-pgid, syscall.SIGKILL)- }- _ = cmd.Process.Kill()- }-- cleanup()+ server.Stop()os.Exit(code)}-func findRepoRoot() string {- dir, _ := os.Getwd()- for {- if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {- return dir- }- parent := filepath.Dir(dir)- if parent == dir {- break- }- dir = parent- }- return filepath.Join("..", "..")-}--func waitForHealth(base string, timeout time.Duration) bool {- deadline := time.Now().Add(timeout)- for time.Now().Before(deadline) {- resp, err := http.Get(base + "/health")- if err == nil && resp.StatusCode == 200 {- _ = resp.Body.Close()- return true- }- if resp != nil {- _ = resp.Body.Close()- }- time.Sleep(500 * time.Millisecond)- }- return false-}--// helpers--func httpGet(t *testing.T, path string) (int, []byte) {- t.Helper()- resp, err := http.Get(serverURL + path)- if err != nil {- t.Fatalf("GET %s failed: %v", path, err)- }- defer resp.Body.Close() //nolint:errcheck- body, _ := io.ReadAll(resp.Body)- return resp.StatusCode, body-}--func httpPost(t *testing.T, path string, payload any) (int, []byte) {- t.Helper()- var reader io.Reader- if payload != nil {- data, err := json.Marshal(payload)- if err != nil {- t.Fatalf("marshal failed: %v", err)- }- reader = strings.NewReader(string(data))- }- resp, err := http.Post(serverURL+path, "application/json", reader)- if err != nil {- t.Fatalf("POST %s failed: %v", path, err)- }- defer resp.Body.Close() //nolint:errcheck- body, _ := io.ReadAll(resp.Body)- return resp.StatusCode, body-}--func httpPostRaw(t *testing.T, path string, body string) (int, []byte) {- t.Helper()- resp, err := http.Post(serverURL+path, "application/json", strings.NewReader(body))- if err != nil {- t.Fatalf("POST %s failed: %v", path, err)- }- defer resp.Body.Close() //nolint:errcheck- data, _ := io.ReadAll(resp.Body)- return resp.StatusCode, data-}--func jsonField(t *testing.T, data []byte, key string) string {- t.Helper()- var m map[string]any- if err := json.Unmarshal(data, &m); err != nil {- t.Fatalf("json parse failed: %v (body: %s)", err, string(data))- }- v, ok := m[key]- if !ok {- return ""- }- switch val := v.(type) {- case string:- return val- default:- b, _ := json.Marshal(val)- return string(b)- }-}-func navigate(t *testing.T, url string) {t.Helper()- // Use retry logic for better stability- code, body := httpPostWithRetry(t, "/navigate", map[string]any{"url": url}, 2)+ code, body := client.PostWithRetry(t, "/navigate", map[string]any{"url": url}, 2)if code != 200 {t.Fatalf("navigate to %s failed with %d: %s", url, code, string(body))}- // Extract tabId from response for subsequent action calls- var result map[string]any- if err := json.Unmarshal(body, &result); err != nil {- t.Logf("warning: failed to parse navigate response: %v", err)- return- }-- if id, ok := result["tabId"].(string); ok {+ if id := testutil.JSONField(t, body, "tabId"); id != "" {currentTabID = idt.Logf("current tab: %s", currentTabID)- // Auto-close tab on test completion to prevent Chrome hitting tab limit.- // Safe to call even if the test also defers closeCurrentTab (idempotent).t.Cleanup(func() { closeCurrentTab(t) })}}-// closeCurrentTab closes the current tab to clean up resourcesfunc closeCurrentTab(t *testing.T) {t.Helper()if currentTabID == "" {return}- // Close the tab- _, _ = httpPost(t, "/tab", map[string]any{+ _, _ = client.Post(t, "/tab", map[string]any{"tabId": currentTabID,"action": "close",})currentTabID = ""}-// navigateInstance creates a fresh tab on the given instance and navigates it.func navigateInstance(t *testing.T, instID, url string) (int, []byte, string) {t.Helper()- openCode, openBody := httpPostWithRetry(t, fmt.Sprintf("/instances/%s/tabs/open", instID), map[string]any{+ openCode, openBody := client.PostWithRetry(t, fmt.Sprintf("/instances/%s/tabs/open", instID), map[string]any{"url": "about:blank",}, 2)if openCode != 200 {return openCode, openBody, ""}- tabID := jsonField(t, openBody, "tabId")+ tabID := testutil.JSONField(t, openBody, "tabId")if tabID == "" {return 500, []byte(`{"error":"missing tabId from open tab response"}`), ""}path := fmt.Sprintf("/tabs/%s/navigate", tabID)- code, body := httpPostWithRetry(t, path, map[string]any{"url": url}, 2)+ code, body := client.PostWithRetry(t, path, map[string]any{"url": url}, 2)return code, body, tabID}-// waitForInstanceReady waits for an instance to be ready for navigation-// Uses a simple navigate to about:blank to check readinessfunc waitForInstanceReady(t *testing.T, instID string) {t.Helper()- deadline := time.Now().Add(15 * time.Second)- for time.Now().Before(deadline) {- code, _, _ := navigateInstance(t, instID, "about:blank")- if code == 200 {- t.Logf("instance %s is ready", instID)- return- }- time.Sleep(500 * time.Millisecond)+ if !testutil.WaitForHealth(server.URL+"/instances/"+instID, 15*1e9) {+ t.Logf("warning: instance %s did not become ready within 15 seconds", instID)}- t.Logf("warning: instance %s did not become ready within 15 seconds", instID)}-// launchTestInstance launches a default test instance for orchestrator-mode tests-// This is called once during TestMain setup so that /navigate and proxy endpoints work-// It waits for the instance to be fully ready before returning-func launchTestInstance(base string) error {- resp, err := http.Post(- base+"/instances/launch",- "application/json",- strings.NewReader(`{"mode":"headless"}`),- )- if err != nil {- return err- }- defer resp.Body.Close() //nolint:errcheck+// Backward-compatible wrappers — delegate to testutil.Client.+// Existing tests use these package-level functions; new tests should use client.* directly.- if resp.StatusCode != 201 && resp.StatusCode != 200 {- body, _ := io.ReadAll(resp.Body)- return fmt.Errorf("launch failed with status %d: %s", resp.StatusCode, string(body))- }-- // Parse response to get instance ID- var result map[string]any- body, _ := io.ReadAll(resp.Body)- if err := json.Unmarshal(body, &result); err != nil {- return fmt.Errorf("failed to parse launch response: %v", err)- }-- id, ok := result["id"].(string)- if !ok {- return fmt.Errorf("no instance id in launch response: %v", result)- }-- fmt.Fprintf(os.Stderr, "TestMain: launched test instance %s\n", id)-- // Wait for instance to be ready (can take 2-5 seconds for Chrome to start)- deadline := time.Now().Add(30 * time.Second)- for time.Now().Before(deadline) {- // Open a tab first, then navigate it to check if instance is ready.- openResp, err := http.Post(- base+"/instances/"+id+"/tabs/open",- "application/json",- strings.NewReader(`{"url":"about:blank"}`),- )- if err == nil {- var tabID string- if openResp.StatusCode == 200 {- openBody, _ := io.ReadAll(openResp.Body)- var open map[string]any- if err := json.Unmarshal(openBody, &open); err == nil {- if idValue, ok := open["tabId"].(string); ok {- tabID = idValue- }- }- }- _ = openResp.Body.Close()- if tabID != "" {- navResp, err := http.Post(- base+"/tabs/"+tabID+"/navigate",- "application/json",- strings.NewReader(`{"url":"about:blank"}`),- )- if err == nil {- if navResp.StatusCode == 200 {- _ = navResp.Body.Close()- fmt.Fprintf(os.Stderr, "TestMain: instance %s is ready\n", id)- return nil- }- navBody, _ := io.ReadAll(navResp.Body)- _ = navResp.Body.Close()- fmt.Fprintf(os.Stderr, "TestMain: instance not ready yet (%d): %s\n", navResp.StatusCode, string(navBody))- }- }- }- time.Sleep(500 * time.Millisecond)- }-- return fmt.Errorf("instance %s did not become ready within 30 seconds", id)+func httpGet(t *testing.T, path string) (int, []byte) { return client.Get(t, path) }+func httpPost(t *testing.T, path string, p any) (int, []byte) { return client.Post(t, path, p) }+func httpPostRaw(t *testing.T, path string, b string) (int, []byte) {+ return client.PostRaw(t, path, b)+}+func httpPostWithRetry(t *testing.T, path string, body any, retries int) (int, []byte) {+ return client.PostWithRetry(t, path, body, retries)}+func jsonField(t *testing.T, data []byte, key string) string { return testutil.JSONField(t, data, key) }+func findRepoRoot() string { return testutil.FindRepoRoot() }