High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: four module-wide censuses outside the converted pair still hand-roll their walk, so a nested git worktree reds autosolver, stealth, fileout and cdptk with false positives (PIN-254)
Luigi Agostiabfd2b275101
diff --git a/internal/autosolver/challenge_detection_test.go b/internal/autosolver/challenge_detection_test.goindex e00170b4..9cec8789 100644--- a/internal/autosolver/challenge_detection_test.go+++ b/internal/autosolver/challenge_detection_test.go@@ -1,11 +1,11 @@package autosolverimport (- "io/fs"- "os""path/filepath""strings""testing"++ "github.com/pinchtab/pinchtab/internal/srccensus")// cloudflareTriggerTitles are the three titles isTurnstileChallenge matches.@@ -50,37 +50,20 @@ func cutAfter(line, token string) (string, bool) {}func TestChallengeTypeHasOneProducer(t *testing.T) {- root := filepath.Join("..", "..")+ // srccensus.Tree owns the enumeration (with the nested-checkout skip the old+ // name-list walk lacked); its keys are module-relative slash paths, so the+ // expected producer reads "internal/..." rather than the old walk-root-relative+ // "../../internal/...". Message change only — the rule is unchanged.+ files := srccensus.Tree(t, filepath.Join("..", ".."), 100)+var producers []string- err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {- if err != nil {- return err- }- if d.IsDir() {- switch d.Name() {- case ".git", "node_modules", "dist", "vendor":- return fs.SkipDir- }- return nil- }- if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {- return nil- }- body, readErr := os.ReadFile(path)- if readErr != nil {- return readErr- }- src := string(body)- if strings.Contains(src, "ChallengeType:") || assignsIntentChallengeType(filepath.ToSlash(path), src) {- producers = append(producers, filepath.ToSlash(filepath.Clean(path)))+ for _, file := range files {+ if strings.Contains(file.Text, "ChallengeType:") || assignsIntentChallengeType(file.Name, file.Text) {+ producers = append(producers, file.Name)}- return nil- })- if err != nil {- t.Fatalf("walk: %v", err)}- want := []string{"../../internal/autosolver/challenge_detection.go"}+ want := []string{"internal/autosolver/challenge_detection.go"}if strings.Join(producers, ",") != strings.Join(want, ",") {t.Fatalf("ChallengeType must be produced only by challenge_detection.go, got %v", producers)}diff --git a/internal/cdptk/screenshot_test.go b/internal/cdptk/screenshot_test.goindex d88adb06..a6c7cc01 100644--- a/internal/cdptk/screenshot_test.go+++ b/internal/cdptk/screenshot_test.go@@ -3,12 +3,10 @@ package cdptk_testimport ("context""encoding/base64"- "fmt""go/ast""go/parser""go/token"- "io/fs"- "os"+ "path""path/filepath""strings""testing"@@ -17,6 +15,7 @@ import ("github.com/chromedp/cdproto/page""github.com/chromedp/chromedp""github.com/pinchtab/pinchtab/internal/cdptk"+ "github.com/pinchtab/pinchtab/internal/srccensus""github.com/pinchtab/pinchtab/internal/testbrowser")@@ -351,53 +350,28 @@ func TestClipViewportAppliesTheNonZeroScaleRule(t *testing.T) {// this is the browserless half: exactly one conversion of a cdptk.ScreenshotClip into a// page.Viewport in non-test code, and it is the one in this package.func TestOnlyCdptkConvertsAScreenshotClipToAViewport(t *testing.T) {- root, err := filepath.Abs("../..")- if err != nil {- t.Fatal(err)- }-- var scanned, converters int- walkErr := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {- if err != nil {- return err- }- if d.IsDir() {- if d.Name() == ".git" || d.Name() == "node_modules" || d.Name() == "dist" {- return fs.SkipDir- }- return nil+ // srccensus.Tree owns the enumeration (and with it the nested-checkout skip a+ // hand-rolled name list missed); its keys are module-relative slash paths, so+ // the owning-directory check below reads "internal/cdptk" rather than a+ // walk-root-relative "../..". Message change only — the rule is unchanged.+ files := srccensus.Tree(t, filepath.Join("..", ".."), 100)++ converters := 0+ for _, file := range files {+ if !strings.Contains(file.Text, "page.Viewport{") {+ continue}- if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {- return nil- }- body, readErr := os.ReadFile(path) // #nosec G304 -- files walked from this repo's own tree.- if readErr != nil {- return readErr- }- scanned++- src := string(body)- if !strings.Contains(src, "page.Viewport{") {- return nil- }- converts, parseErr := buildsViewportFromAReceiversFields(src)+ converts, parseErr := buildsViewportFromAReceiversFields(file.Text)if parseErr != nil {- return fmt.Errorf("parse %s: %w", path, parseErr)+ t.Fatalf("parse %s: %v", file.Name, parseErr)}if !converts {- return nil+ continue}converters++- rel, _ := filepath.Rel(root, path)- if filepath.Dir(rel) != filepath.Join("internal", "cdptk") {- t.Errorf("%s builds a page.Viewport from a clip's fields; that conversion carries the non-zero-scale rule and belongs to cdptk.ClipViewport alone", rel)+ if path.Dir(file.Name) != "internal/cdptk" {+ t.Errorf("%s builds a page.Viewport from a clip's fields; that conversion carries the non-zero-scale rule and belongs to cdptk.ClipViewport alone", file.Name)}- return nil- })- if walkErr != nil {- t.Fatalf("cannot scan the repo, so this census checks nothing: %v", walkErr)- }- if scanned < 100 {- t.Fatalf("scanned only %d Go files; this census would pass vacuously", scanned)}if converters == 0 {t.Fatal("found no clip-to-viewport conversion at all; if ClipViewport was renamed or restructured, re-point this census rather than deleting it")diff --git a/internal/devtools/gotestsum_test.go b/internal/devtools/gotestsum_test.goindex 89428e87..191212be 100644--- a/internal/devtools/gotestsum_test.go+++ b/internal/devtools/gotestsum_test.go@@ -9,6 +9,8 @@ import ("path/filepath""strings""testing"++ "github.com/pinchtab/pinchtab/internal/srccensus")const gotestsumBinary = "gotestsum"@@ -26,7 +28,7 @@ func TestNoGotestsumInvocationHidesTheOutputSummary(t *testing.T) {return err}if d.IsDir() {- if skipScanDir(d.Name()) && path != root {+ if path != root && (srccensus.ExcludedDir(path) || d.Name() == ".tools") {return fs.SkipDir}return nil@@ -281,14 +283,13 @@ func mayHoldInvocations(path string) bool {return hasScannableExtension(path) || filepath.Ext(path) == ""}-func skipScanDir(name string) bool {- switch name {- case ".git", "node_modules", "dist", "vendor", ".tools":- return true- }- return false-}-+// The walk stays hand-rolled because its subject is scripts and workflow files, which+// srccensus.Tree (a Go-source enumerator) cannot see — but the directory EXCLUSIONS,+// including the nested-checkout skip whose absence let a worktree's copy of the test+// script be counted as a module invocation, are inherited via srccensus.ExcludedDir+// rather than copied. .tools is a local extra: vendored tool checkouts hold no+// invocation of this repo's suite.+//// repoRoot walks up to the directory holding go.mod rather than counting "../" hops, so// moving this package does not silently point the scan at a subtree.func repoRoot(t *testing.T) string {diff --git a/internal/fileout/autonaming_census_test.go b/internal/fileout/autonaming_census_test.goindex 258f7923..ca4024dd 100644--- a/internal/fileout/autonaming_census_test.go+++ b/internal/fileout/autonaming_census_test.go@@ -1,11 +1,11 @@package fileoutimport (- "io/fs"- "os""path/filepath""strings""testing"++ "github.com/pinchtab/pinchtab/internal/srccensus")// This census lives beside the rule it defends, and it walks the MODULE. Two@@ -14,10 +14,9 @@ import (// them they covered the two packages that had the defect and nothing else, so the next// auto-naming site, in any third package, would have shipped unseen. Walking from the// module root is the same fix the isolated-world boundary census in internal/bridge got-// after the same blind spot.-//-// Not built on internal/srccensus deliberately: that helper reads ONE package's AST for-// calls and assignments, and this rule is a literal scan across every package.+// after the same blind spot. srccensus.Tree owns the enumeration — the rule stays a+// literal scan over each file's text, but the walk exclusions (including the+// nested-checkout skip a hand-rolled name list missed) are inherited, not remembered.// timestampLayouts are the second-resolution spellings this census recognises. A stamp is// what makes two outputs in the same second collide, so the layout is the signal.@@ -71,46 +70,18 @@ var autoNamingSites = map[string]string{// and an accounted entry whose stamp is gone reds too, so the recorded boundary cannot// drift from the real one — a stale entry is how a site that stopped reserving hides.func TestNoPackageAutoNamesAFileItThenOverwrites(t *testing.T) {- root := moduleRoot(t)+ // Tree's keys are module-relative slash paths — the shape autoNamingSites was+ // already keyed on, so the accounted map is unchanged. The vacuity floor the+ // scanned counter carried moves to Tree's minFiles at the same value.+ files := srccensus.Tree(t, filepath.Join("..", ".."), 200)found := map[string]bool{}- scanned := 0- err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {- if err != nil {- return err- }- if d.IsDir() {- if skipCensusDir(d.Name()) && path != root {- return fs.SkipDir- }- return nil- }- name := d.Name()- if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {- return nil- }- body, readErr := os.ReadFile(path) // #nosec G304 -- files walked from this repo's own tree.- if readErr != nil {- return readErr+ for _, file := range files {+ if buildsASecondResolutionStamp(file.Text) {+ found[file.Name] = true}- scanned++- if !buildsASecondResolutionStamp(string(body)) {- return nil- }- rel, relErr := filepath.Rel(root, path)- if relErr != nil {- return relErr- }- found[filepath.ToSlash(rel)] = true- return nil- })- if err != nil {- t.Fatalf("cannot walk the module, so this census checks nothing: %v", err)}- if scanned < 200 {- t.Fatalf("scanned only %d non-test Go files under %s; the walk is not reaching the module, so this census would pass vacuously", scanned, root)- }if len(found) == 0 {t.Fatalf("found no file building a second-resolution timestamp anywhere in the module; either every auto-named output is gone or the layouts this census recognises (%s) no longer appear — re-point it at whatever replaced them rather than deleting it",strings.Join(timestampLayouts, ", "))@@ -140,32 +111,3 @@ func buildsASecondResolutionStamp(body string) bool {}return false}--func skipCensusDir(name string) bool {- switch name {- case ".git", "node_modules", "dist", "vendor", ".tools", "testdata":- return true- }- return false-}--// moduleRoot walks up to the directory holding go.mod rather than counting "../" hops, so-// moving this package does not silently point the census at a subtree.-func moduleRoot(t *testing.T) string {- t.Helper()-- dir, err := os.Getwd()- if err != nil {- t.Fatal(err)- }- for {- if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {- return dir- }- parent := filepath.Dir(dir)- if parent == dir {- t.Fatal("no go.mod found above the working directory; cannot locate the module root")- }- dir = parent- }-}diff --git a/internal/safelog/handler_test.go b/internal/safelog/handler_test.goindex dc64975e..a329cbd0 100644--- a/internal/safelog/handler_test.go+++ b/internal/safelog/handler_test.go@@ -188,6 +188,12 @@ const (minServerSourceFiles = 5)+// sourcePackageDirs is safe from the nested-worktree false positives that hit the+// module-ROOT censuses by SCOPE, not by exclusion: it walks three named subtrees+// (cmd, internal, pkg), and a worktree created at the repo root sits outside all of+// them. That safety is conditional — adding a root here, or a worktree created+// INSIDE one of these subtrees, puts this walk in the vulnerable class, and then the+// enumeration belongs on srccensus like the module-wide censuses.func sourcePackageDirs(t *testing.T) []string {t.Helper()diff --git a/internal/srccensus/srccensus.go b/internal/srccensus/srccensus.goindex 11971307..df69990f 100644--- a/internal/srccensus/srccensus.go+++ b/internal/srccensus/srccensus.go@@ -156,6 +156,15 @@ func TestTree(t testing.TB, root string, minFiles int) []SourceFile {})}+// ExcludedDir is the walk-exclusion rule itself — the name skips plus the+// nested-checkout check — exported for a module-wide walker whose subject is+// not Go source (scripts, workflows) and so cannot enumerate through Tree.+// Such a walker inherits the rule by consulting this instead of copying it;+// a copy is how the nested-worktree hazard regenerated twice already.+func ExcludedDir(path string) bool {+ return treeSkipDirs[filepath.Base(path)] || nestedCheckout(path)+}+func tree(t testing.TB, root string, minFiles int, kind string, include func(path string) bool) []SourceFile {t.Helper()@@ -173,7 +182,7 @@ func tree(t testing.TB, root string, minFiles int, kind string, include func(patif path == abs {return nil}- if treeSkipDirs[entry.Name()] || nestedCheckout(path) {+ if ExcludedDir(path) {return fs.SkipDir}return nildiff --git a/internal/srccensus/srccensus_test.go b/internal/srccensus/srccensus_test.goindex 83ecf601..5ed445de 100644--- a/internal/srccensus/srccensus_test.go+++ b/internal/srccensus/srccensus_test.go@@ -429,3 +429,48 @@ func TestTestTreeFloorNamesTheFileClassItCounted(t *testing.T) {t.Errorf("floor failure said %q, want it to name the file class counted", got)}}++// The census over the census files. Four module-wide guards hand-rolled their walks with+// name-only skip lists and all four redded under a nested git worktree — the exclusions+// live here precisely so they are inherited, and a fresh WalkDir opts out of them without+// anyone deciding to. The rule: a _test.go that enumerates files FROM THE MODULE ROOT must+// obtain its enumeration from this package — Tree/TestTree for Go-source subjects, or+// ExcludedDir for a walker whose subject Tree cannot see (devtools' script census).+// Subtree-scoped walkers (a single package or named subtrees) are outside the rule: a+// repo-root worktree is outside every root they visit, which safelog records beside its+// walk.+func TestEveryModuleRootWalkerObtainsItsEnumerationFromSrccensus(t *testing.T) {+ files := TestTree(t, filepath.Join("..", ".."), 100)++ moduleRootAnchors := []string{`filepath.Join("..", "..")`, `"../.."`, `"go.mod"`}+ enumerators := []string{"filepath.WalkDir(", "filepath.Walk(", "filepath.Glob("}++ walkers := 0+ for _, file := range files {+ // The owner is exempt by construction: this package's own tests exercise the+ // walker it exports, and being in-package they reference it unqualified.+ if strings.HasPrefix(file.Name, "internal/srccensus/") {+ continue+ }+ if !containsAnyMarker(file.Text, moduleRootAnchors) || !containsAnyMarker(file.Text, enumerators) {+ continue+ }+ walkers+++ if strings.Contains(file.Text, "srccensus.") {+ continue+ }+ t.Errorf("%s enumerates files from the module root with its own walk and never consults srccensus; obtain the file list from srccensus.Tree or srccensus.TestTree, or — for a subject Tree cannot see — inherit the directory exclusions via srccensus.ExcludedDir. A hand-rolled walk loses the nested-checkout skip, and a worktree's copies then red this census with paths that vanish, or silently double its counts", file.Name)+ }+ if walkers < 2 {+ t.Fatalf("found only %d module-root-anchored walker(s) in the whole module; the detector matched almost nothing and would pass vacuously — if the last hand-rooted walkers were converted to Tree, re-point this floor at whatever still walks", walkers)+ }+}++func containsAnyMarker(text string, markers []string) bool {+ for _, marker := range markers {+ if strings.Contains(text, marker) {+ return true+ }+ }+ return false+}diff --git a/internal/stealth/bundle_test.go b/internal/stealth/bundle_test.goindex 100a5840..8b534db4 100644--- a/internal/stealth/bundle_test.go+++ b/internal/stealth/bundle_test.go@@ -1,7 +1,6 @@package stealthimport (- "io/fs""os""path/filepath""strings"@@ -10,6 +9,7 @@ import (_ "github.com/pinchtab/pinchtab/internal/browsers/chrome"_ "github.com/pinchtab/pinchtab/internal/browsers/cloak""github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/srccensus")func TestNewBundleIncludesSeedLevelAndPopupGuard(t *testing.T) {@@ -394,34 +394,14 @@ func equalStringSlices(a, b []string) bool {const chromeTemplateMarker = "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"func TestChromeUserAgentTemplateHasOneOwner(t *testing.T) {- root, err := filepath.Abs("../..")- if err != nil {- t.Fatal(err)- }-+ // srccensus.Tree owns the enumeration (with the nested-checkout skip the old+ // walk lacked entirely — it skipped no directory at all). Keys were already+ // module-relative slash paths, so the owner key is unchanged.owners := map[string]int{}- walkErr := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {- if err != nil {- return err- }- if d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {- return nil+ for _, file := range srccensus.Tree(t, filepath.Join("..", ".."), 100) {+ if n := strings.Count(file.Text, chromeTemplateMarker); n > 0 {+ owners[file.Name] = n}- raw, readErr := os.ReadFile(path)- if readErr != nil {- return readErr- }- if n := strings.Count(string(raw), chromeTemplateMarker); n > 0 {- rel, relErr := filepath.Rel(root, path)- if relErr != nil {- return relErr- }- owners[filepath.ToSlash(rel)] = n- }- return nil- })- if walkErr != nil {- t.Fatal(walkErr)}const owner = "internal/stealth/ua.go"