Skip to content

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

refactor: one owner for solver identity strings, and two of the three default solver lists are unguarded (PIN-125)

Luigi Agosti442aff0d55ec
diff --git a/internal/autosolver/external/capsolver.go b/internal/autosolver/external/capsolver.go
index cd16ce9b..c67b48e7 100644
--- a/internal/autosolver/external/capsolver.go
+++ b/internal/autosolver/external/capsolver.go
@@ -67,7 +67,7 @@ func (c *Capsolver) CanHandle(ctx context.Context, page autosolver.Page) (bool,
// (create task → poll result → inject token) must be filled in
// with the Capsolver API v1 protocol.
func (c *Capsolver) Solve(ctx context.Context, page autosolver.Page, executor autosolver.ActionExecutor) (*autosolver.Result, error) {
- result := &autosolver.Result{SolverUsed: autosolver.CapsolverSolverName}
+ result := &autosolver.Result{SolverUsed: c.Name()}
if c.config.APIKey == "" {
result.Error = "capsolver API key not configured"
diff --git a/internal/autosolver/external/external_test.go b/internal/autosolver/external/external_test.go
index 24217e6e..bb956bf9 100644
--- a/internal/autosolver/external/external_test.go
+++ b/internal/autosolver/external/external_test.go
@@ -7,12 +7,9 @@ import (
"github.com/pinchtab/pinchtab/internal/autosolver"
)
-// The reported solver identity has to be the registered one. Both were spelled as
-// literals beside a Name() that already read the constant, so renaming the constant
-// changed what registers while leaving what the Result reports — and nothing in the
-// tree noticed. These solvers are skeletons today, which is why it stayed latent
-// rather than shipping a wrong name; it is also why it would have survived until
-// the first real implementation.
+// The reported solver identity has to be the registered one. Solve now builds it
+// from Name(), which makes drift impossible rather than merely detectable; this
+// test reds if either one goes back to spelling the name a second time.
func TestSolverUsedMatchesTheRegisteredName(t *testing.T) {
for _, solver := range []autosolver.Solver{
NewCapsolver(CapsolverConfig{}),
diff --git a/internal/autosolver/external/twocaptcha.go b/internal/autosolver/external/twocaptcha.go
index 20d14cef..71f53969 100644
--- a/internal/autosolver/external/twocaptcha.go
+++ b/internal/autosolver/external/twocaptcha.go
@@ -63,7 +63,7 @@ func (t *TwoCaptcha) CanHandle(ctx context.Context, page autosolver.Page) (bool,
// This is a skeleton implementation. The actual HTTP client logic
// (submit → poll → inject) must be filled in with the 2Captcha API protocol.
func (t *TwoCaptcha) Solve(ctx context.Context, page autosolver.Page, executor autosolver.ActionExecutor) (*autosolver.Result, error) {
- result := &autosolver.Result{SolverUsed: autosolver.TwoCaptchaSolverName}
+ result := &autosolver.Result{SolverUsed: t.Name()}
if t.config.APIKey == "" {
result.Error = "2captcha API key not configured"
diff --git a/internal/autosolver/solvers/cloudflare.go b/internal/autosolver/solvers/cloudflare.go
index b80a0b4b..e0d777f0 100644
--- a/internal/autosolver/solvers/cloudflare.go
+++ b/internal/autosolver/solvers/cloudflare.go
@@ -30,7 +30,7 @@ func (s *Cloudflare) CanHandle(_ context.Context, page autosolver.Page) (bool, e
// Solve attempts to resolve the Cloudflare challenge by locating the
// Turnstile widget and clicking the checkbox.
func (s *Cloudflare) Solve(ctx context.Context, page autosolver.Page, executor autosolver.ActionExecutor) (*autosolver.Result, error) {
- result := &autosolver.Result{SolverUsed: "cloudflare"}
+ result := &autosolver.Result{SolverUsed: s.Name()}
if !isCFChallenge(page.Title()) {
result.Solved = true
diff --git a/internal/autosolver/solvers/legacy.go b/internal/autosolver/solvers/legacy.go
index 505ca71c..94f47130 100644
--- a/internal/autosolver/solvers/legacy.go
+++ b/internal/autosolver/solvers/legacy.go
@@ -35,20 +35,21 @@ func (a *LegacyAdapter) CanHandle(ctx context.Context, _ autosolver.Page) (bool,
return a.solver.CanHandle(ctx)
}
-// Solve delegates to the legacy solver's Solve method.
-// The legacy solver uses the chromedp context directly from ctx.
+// Solve delegates to the legacy solver's Solve method. Both paths report
+// a.Name() as the solver identity: legacyResult.Solver is json:"omitempty" and a
+// legacy solver may leave it unset, which reported an empty name on success.
func (a *LegacyAdapter) Solve(ctx context.Context, page autosolver.Page, _ autosolver.ActionExecutor) (*autosolver.Result, error) {
legacyResult, err := a.solver.Solve(ctx, legacySolver.Options{MaxAttempts: 3})
if err != nil {
return &autosolver.Result{
- SolverUsed: a.solver.Name(),
+ SolverUsed: a.Name(),
Error: err.Error(),
}, err
}
return &autosolver.Result{
Solved: legacyResult.Solved,
- SolverUsed: legacyResult.Solver,
+ SolverUsed: a.Name(),
Attempts: legacyResult.Attempts,
FinalTitle: legacyResult.Title,
}, nil
diff --git a/internal/autosolver/solvers/legacy_test.go b/internal/autosolver/solvers/legacy_test.go
new file mode 100644
index 00000000..db5d3264
--- /dev/null
+++ b/internal/autosolver/solvers/legacy_test.go
@@ -0,0 +1,61 @@
+package solvers
+
+import (
+ "context"
+ "errors"
+ "testing"
+
+ legacySolver "github.com/pinchtab/pinchtab/internal/solver"
+)
+
+// stubLegacySolver reports a Result whose Solver field is unset, which
+// json:"solver,omitempty" makes a legitimate shape for a legacy solver.
+type stubLegacySolver struct {
+ name string
+ result *legacySolver.Result
+ solveErr error
+}
+
+func (s *stubLegacySolver) Name() string { return s.name }
+func (s *stubLegacySolver) CanHandle(context.Context) (bool, error) { return true, nil }
+func (s *stubLegacySolver) Solve(context.Context, legacySolver.Options) (*legacySolver.Result, error) {
+ return s.result, s.solveErr
+}
+
+func TestLegacyAdapterReportsItsOwnNameOnBothPaths(t *testing.T) {
+ tests := []struct {
+ name string
+ stub *stubLegacySolver
+ wantErr bool
+ }{
+ {
+ name: "success with an unset Solver field",
+ stub: &stubLegacySolver{name: "legacy-cf", result: &legacySolver.Result{Solved: true, Title: "done"}},
+ },
+ {
+ name: "success with a Solver field the legacy solver did fill in",
+ stub: &stubLegacySolver{name: "legacy-cf", result: &legacySolver.Result{Solved: true, Solver: "legacy-cf"}},
+ },
+ {
+ name: "failure",
+ stub: &stubLegacySolver{name: "legacy-cf", solveErr: errors.New("boom")},
+ wantErr: true,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ adapter := NewLegacyAdapter(tc.stub, 5)
+ result, err := adapter.Solve(context.Background(), nil, nil)
+ if tc.wantErr != (err != nil) {
+ t.Fatalf("Solve() error = %v, wantErr %v", err, tc.wantErr)
+ }
+ if result == nil {
+ t.Fatal("Solve returned no Result to check")
+ }
+ if result.SolverUsed != adapter.Name() {
+ t.Errorf("Result.SolverUsed = %q, want %q — the adapter must report its own identity on every path", result.SolverUsed, adapter.Name())
+ }
+ })
+ }
+}
diff --git a/internal/autosolver/types.go b/internal/autosolver/types.go
index 21a47b11..4723dc7f 100644
--- a/internal/autosolver/types.go
+++ b/internal/autosolver/types.go
@@ -4,8 +4,8 @@ import "time"
// SemanticSolverName is what the built-in semantic stage answers to. It is a
// stage rather than a registry solver, so it has no Name() method to own its
-// name — this constant is that owner, for the attempt entries it reports and
-// for the names autoSolver.solvers accepts.
+// name — this constant is that owner, and catalog reads it to include the stage
+// among the names autoSolver.solvers accepts.
const SemanticSolverName = "semantic"
// IntentType classifies the detected page state.
diff --git a/internal/bridge/cloudflare.go b/internal/bridge/cloudflare.go
index 2acee5f6..54a7c57b 100644
--- a/internal/bridge/cloudflare.go
+++ b/internal/bridge/cloudflare.go
@@ -11,8 +11,15 @@ import (
"github.com/pinchtab/pinchtab/internal/solver"
)
+// cloudflareSolverName is this package's owner of the legacy registry identity:
+// the registration key, Name() and Result.Solver all read it. autosolver's
+// solvers.Cloudflare answers to the same spelling in the other registry, and that
+// equality is deliberately uncoupled — two registries with two Solver interfaces
+// means renaming one must not rename the other.
+const cloudflareSolverName = "cloudflare"
+
func init() {
- solver.MustRegister("cloudflare", &CloudflareSolver{})
+ solver.MustRegister(cloudflareSolverName, &CloudflareSolver{})
}
// CloudflareSolver detects and solves Cloudflare Turnstile/Interstitial challenges.
@@ -20,7 +27,7 @@ func init() {
// and polls for resolution.
type CloudflareSolver struct{}
-func (s *CloudflareSolver) Name() string { return "cloudflare" }
+func (s *CloudflareSolver) Name() string { return cloudflareSolverName }
// CanHandle checks the page title for known Cloudflare challenge indicators.
func (s *CloudflareSolver) CanHandle(ctx context.Context) (bool, error) {
@@ -38,7 +45,7 @@ func (s *CloudflareSolver) Solve(ctx context.Context, opts solver.Options) (*sol
maxAttempts = 3
}
- result := &solver.Result{Solver: "cloudflare"}
+ result := &solver.Result{Solver: s.Name()}
var title string
if err := chromedp.Run(ctx, chromedp.Title(&title)); err != nil {
diff --git a/internal/config/config_file.go b/internal/config/config_file.go
index f0abe1e8..b71956a1 100644
--- a/internal/config/config_file.go
+++ b/internal/config/config_file.go
@@ -4,6 +4,8 @@ import (
"path/filepath"
"strconv"
"strings"
+
+ "github.com/pinchtab/pinchtab/internal/autosolver"
)
// CurrentConfigVersion is bumped when config schema changes require migration or wizard re-run.
@@ -276,7 +278,7 @@ func DefaultFileConfig() FileConfig {
SolverTimeoutSec: &autoSolverSolverTimeoutSec,
RetryBaseDelayMs: &autoSolverRetryBaseDelayMs,
RetryMaxDelayMs: &autoSolverRetryMaxDelayMs,
- Solvers: []string{"cloudflare", "semantic"},
+ Solvers: autosolver.DefaultConfig().Solvers,
LLMFallback: &autoSolverLLMFallback,
},
}
diff --git a/internal/config/config_load.go b/internal/config/config_load.go
index 5af5d54c..3442fe98 100644
--- a/internal/config/config_load.go
+++ b/internal/config/config_load.go
@@ -11,6 +11,7 @@ import (
"sync"
"time"
+ "github.com/pinchtab/pinchtab/internal/autosolver"
"github.com/pinchtab/pinchtab/internal/browsers"
)
@@ -279,7 +280,7 @@ func LoadConfig() (*RuntimeConfig, []LoadDiagnostic, error) {
SolverTimeoutSec: 30,
RetryBaseDelayMs: 500,
RetryMaxDelayMs: 10000,
- Solvers: []string{"cloudflare", "semantic"},
+ Solvers: autosolver.DefaultConfig().Solvers,
LLMFallback: false,
},
}