High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: support browser-free seaportal audits
Luigi Agosti7d199d3ded0b
diff --git a/internal/cli/actions/actions_audit.go b/internal/cli/actions/actions_audit.goindex acfd980d..695c348f 100644--- a/internal/cli/actions/actions_audit.go+++ b/internal/cli/actions/actions_audit.go@@ -35,7 +35,7 @@ func mustBool(cmd *cobra.Command, name string) bool {// applyRunAuth resolves --profile routing and injects --cookie /// --cookies-file cookies before the run. Cookie-authenticated runs receive a// disposable instance so they never alter a persistent browser profile.-func applyRunAuth(client *http.Client, base, token string, cmd *cobra.Command, scopeURL string) (string, func() error, error) {+func applyRunAuth(client *http.Client, base, token string, cmd *cobra.Command, scopeURLs ...string) (string, func() error, error) {cookies, err := loadRunCookies(cmd)if err != nil {return "", nil, err@@ -52,7 +52,8 @@ func applyRunAuth(client *http.Client, base, token string, cmd *cobra.Command, sif mustString(cmd, "profile") != "" {return "", nil, fmt.Errorf("--profile cannot be combined with --cookie or --cookies-file; cookie-authenticated runs use an isolated temporary instance")}- if scopeURL == "" {+ scopes := cookieScopeURLs(scopeURLs)+ if len(scopes) == 0 {return "", nil, fmt.Errorf("cookie injection requires a URL target")}@@ -60,15 +61,34 @@ func applyRunAuth(client *http.Client, base, token string, cmd *cobra.Command, sif err != nil {return "", nil, err}- if err := setRunCookies(client, instanceBase, token, scopeURL, cookies); err != nil {- if cleanupErr := cleanup(); cleanupErr != nil {- return "", nil, fmt.Errorf("set run cookies: %w (also failed to stop isolated instance: %v)", err, cleanupErr)+ for _, scope := range scopes {+ if err := setRunCookies(client, instanceBase, token, scope, cookies); err != nil {+ if cleanupErr := cleanup(); cleanupErr != nil {+ return "", nil, fmt.Errorf("set run cookies: %w (also failed to stop isolated instance: %v)", err, cleanupErr)+ }+ return "", nil, fmt.Errorf("set run cookies: %w", err)}- return "", nil, fmt.Errorf("set run cookies: %w", err)}return instanceBase, cleanup, nil}+func cookieScopeURLs(urls []string) []string {+ seen := make(map[string]bool, len(urls))+ scopes := make([]string, 0, len(urls))+ for _, raw := range urls {+ u, err := url.Parse(strings.TrimSpace(raw))+ if err != nil || u.Scheme == "" || u.Host == "" {+ continue+ }+ scope := (&url.URL{Scheme: u.Scheme, Host: u.Host, Path: "/"}).String()+ if !seen[scope] {+ seen[scope] = true+ scopes = append(scopes, scope)+ }+ }+ return scopes+}+func loadRunCookies(cmd *cobra.Command) ([]audit.Cookie, error) {flags, _ := cmd.Flags().GetStringArray("cookie")var fileData []byte@@ -238,21 +258,8 @@ func Audit(client *http.Client, base, token string, cmd *cobra.Command, target sreturn err}- base, cleanup, err := applyRunAuth(client, base, token, cmd, target)- if err != nil {- return err- }- defer func() {- if cleanupErr := cleanup(); cleanupErr != nil {- if err == nil {- err = cleanupErr- } else {- err = fmt.Errorf("%w (also %v)", err, cleanupErr)- }- }- }()-body := map[string]any{}+ authScopes := []string{target}switch {case mustString(cmd, "seaportal-report") != "":path := mustString(cmd, "seaportal-report")@@ -260,6 +267,14 @@ func Audit(client *http.Client, base, token string, cmd *cobra.Command, target sif err != nil {return fmt.Errorf("read seaportal report: %w", err)}+ pages, err := audit.ParseSeaportalReport(data)+ if err != nil {+ return err+ }+ authScopes = make([]string, 0, len(pages))+ for _, page := range pages {+ authScopes = append(authScopes, page.URL)+ }body["seaportalResults"] = json.RawMessage(data)body["seaportalFile"] = pathif v, _ := cmd.Flags().GetBool("enrich-all"); v {@@ -271,6 +286,20 @@ func Audit(client *http.Client, base, token string, cmd *cobra.Command, target sbody["urls"] = []string{target}}+ base, cleanup, err := applyRunAuth(client, base, token, cmd, authScopes...)+ if err != nil {+ return err+ }+ defer func() {+ if cleanupErr := cleanup(); cleanupErr != nil {+ if err == nil {+ err = cleanupErr+ } else {+ err = fmt.Errorf("%w (also %v)", err, cleanupErr)+ }+ }+ }()+screenshot, _ := cmd.Flags().GetBool("screenshot")network, _ := cmd.Flags().GetBool("network-monitor")body["options"] = map[string]any{"screenshot": screenshot, "network": network}diff --git a/internal/cli/actions/actions_audit_test.go b/internal/cli/actions/actions_audit_test.goindex b2b4f6a3..4ed4b496 100644--- a/internal/cli/actions/actions_audit_test.go+++ b/internal/cli/actions/actions_audit_test.go@@ -4,6 +4,8 @@ import ("encoding/json""net/http""net/http/httptest"+ "os"+ "path/filepath""strings""sync/atomic""testing"@@ -231,7 +233,49 @@ func TestCompareInjectsShorthandCookiesForBothSites(t *testing.T) {if err != nil {t.Fatalf("Compare() error = %v", err)}- if want := []string{"https://live.example", "https://staging.example"}; strings.Join(cookieURLs, ",") != strings.Join(want, ",") {+ if want := []string{"https://live.example/", "https://staging.example"}; strings.Join(cookieURLs, ",") != strings.Join(want, ",") {+ t.Fatalf("cookie URLs = %v, want %v", cookieURLs, want)+ }+}++func TestAuditSeaportalReportInjectsCookiesWithoutPositionalURL(t *testing.T) {+ reportPath := filepath.Join(t.TempDir(), "seaportal.json")+ if err := os.WriteFile(reportPath, []byte(`[{"url":"https://example.com/a","profile":{"browserRecommended":false}}]`), 0o600); err != nil {+ t.Fatal(err)+ }+ 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 := Audit(http.DefaultClient, srv.URL, "", newAuditTestCmd("--seaportal-report", reportPath, "--cookie", "session=temporary"), "")+ if err != nil {+ t.Fatalf("Audit() error = %v", err)+ }+ if want := []string{"https://example.com/"}; strings.Join(cookieURLs, ",") != strings.Join(want, ",") {t.Fatalf("cookie URLs = %v, want %v", cookieURLs, want)}}diff --git a/internal/handlers/audit.go b/internal/handlers/audit.goindex 3ed1b004..2b3e0aa9 100644--- a/internal/handlers/audit.go+++ b/internal/handlers/audit.go@@ -43,23 +43,27 @@ func (h *Handlers) HandleAudit(w http.ResponseWriter, r *http.Request) {}}- routing, ok := h.resolveNavigateBrowser(w, r, "", "")- if !ok {- return- }- if !h.ensureBrowserOrRespond(w, routing.EffectiveCfg) {- return+ effectiveCfg := h.Config+ if auditNeedsBrowser(req, seaportalPages) {+ routing, ok := h.resolveNavigateBrowser(w, r, "", "")+ if !ok {+ return+ }+ effectiveCfg = routing.EffectiveCfg+ if !h.ensureBrowserOrRespond(w, effectiveCfg) {+ return+ }}// A multi-page audit legitimately outlives the server's WriteTimeout.httpx.ExtendWriteDeadline(w, auditRunTimeout)auditor := func(url string, opts audit.PageOptions) audit.PageAudit {- targets, err := h.validateAuditTarget(url, routing.EffectiveCfg)+ targets, err := h.validateAuditTarget(url, effectiveCfg)if err != nil {return audit.NewPageAuditError(url, err)}- return h.auditPage(r.Context(), url, opts, routing.EffectiveCfg, targets)+ return h.auditPage(r.Context(), url, opts, effectiveCfg, targets)}report, err := audit.RunAudit(@@ -71,7 +75,7 @@ func (h *Handlers) HandleAudit(w http.ResponseWriter, r *http.Request) {EnrichAll: req.EnrichAll,Page: req.Options.pageOptions(),},- h.fetchSitemap(routing.EffectiveCfg),+ h.fetchSitemap(effectiveCfg),auditor,)if err != nil {@@ -81,6 +85,18 @@ func (h *Handlers) HandleAudit(w http.ResponseWriter, r *http.Request) {httpx.JSON(w, 200, report)}+func auditNeedsBrowser(req auditRequest, seaportalPages []audit.SeaportalPage) bool {+ if len(seaportalPages) == 0 || req.EnrichAll {+ return true+ }+ for _, page := range seaportalPages {+ if page.BrowserRecommended {+ return true+ }+ }+ return false+}+// fetchSitemap returns a SitemapFetcher that applies the same URL/IDPI/SSRF// validation as page navigation, then discovers pages through// seaportal.FlattenSitemap (recursive sitemap-index support). The crawldiff --git a/internal/handlers/audit_handler_test.go b/internal/handlers/audit_handler_test.gonew file mode 100644index 00000000..9e29dd2c--- /dev/null+++ b/internal/handlers/audit_handler_test.go@@ -0,0 +1,28 @@+package handlers++import (+ "bytes"+ "errors"+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/pinchtab/pinchtab/internal/config"+)++func TestHandleAuditSeaportalStaticPagesDoNotRequireBrowser(t *testing.T) {+ bridge := &mockBridge{ensureBrowserErr: errors.New("browser unavailable")}+ h := New(bridge, &config.RuntimeConfig{}, nil, nil, nil)+ req := httptest.NewRequest(http.MethodPost, "/audit", bytes.NewBufferString(`{"seaportalResults":[{"url":"https://example.com/a","title":"Static page","statusCode":200,"profile":{"browserRecommended":false}}]}`))+ req.Header.Set("Content-Type", "application/json")+ w := httptest.NewRecorder()++ h.HandleAudit(w, req)++ if w.Code != http.StatusOK {+ t.Fatalf("status = %d, want 200: %s", w.Code, w.Body.String())+ }+ if bridge.ensureBrowserCall != 0 {+ t.Fatalf("EnsureBrowser called %d times, want 0", bridge.ensureBrowserCall)+ }+}diff --git a/tests/e2e/scenarios/cli/audit-seaportal-basic.sh b/tests/e2e/scenarios/cli/audit-seaportal-basic.shindex 54a58c03..7edba3d4 100755--- a/tests/e2e/scenarios/cli/audit-seaportal-basic.sh+++ b/tests/e2e/scenarios/cli/audit-seaportal-basic.sh@@ -59,6 +59,20 @@ fiend_test+# ─────────────────────────────────────────────────────────────────+start_test "seaportal-only audit accepts cookie authentication"++pt_ok audit --seaportal-report "$SEAPORTAL_REPORT" --cookie session=e2evalue --screenshot=false --json++PAGE_COUNT=$(echo "$PT_OUT" | jq '.pages | length' 2>/dev/null)+if [ "${PAGE_COUNT:-0}" -eq 3 ] 2>/dev/null; then+ pass_assert "cookie-authenticated seaportal report ran without a positional URL"+else+ fail_assert "cookie-authenticated seaportal report ran without a positional URL (pages: $PAGE_COUNT)"+fi++end_test+# ─────────────────────────────────────────────────────────────────start_test "--enrich-all overrides browserRecommended routing"