High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: config set/get reject the autoSolver and scheduler sections, so documented keys are unreachable from the CLI (PIN-085)
Luigi Agosti788b8f22c48b
diff --git a/internal/config/editor_get.go b/internal/config/editor_get.goindex e8d9348b..dffdb317 100644--- a/internal/config/editor_get.go+++ b/internal/config/editor_get.go@@ -8,38 +8,13 @@ import (// GetConfigValue reads a dotted-path field from FileConfig and returns its string representation.// Pointer fields that are unset return an empty string. Slice fields are comma-separated.+// The section vocabulary lives once, in configSections.func GetConfigValue(fc *FileConfig, path string) (string, error) {- parts := strings.SplitN(path, ".", 2)- if len(parts) != 2 {- return "", fmt.Errorf("invalid path %q (expected section.field, e.g., server.port)", path)- }-- section, field := parts[0], parts[1]-- switch section {- case "server":- return getServerField(&fc.Server, field)- case "browser":- return getBrowserField(&fc.Browser, field)- case "browsers":- return getBrowsersField(&fc.Browsers, field)- case "instanceDefaults":- return getInstanceDefaultsField(&fc.InstanceDefaults, field)- case "security":- return getSecurityField(&fc.Security, field)- case "profiles":- return getProfilesField(&fc.Profiles, field)- case "multiInstance":- return getMultiInstanceField(&fc.MultiInstance, field)- case "timeouts":- return getTimeoutsField(&fc.Timeouts, field)- case "observability":- return getObservabilityField(&fc.Observability, field)- case "sessions":- return getSessionsField(&fc.Sessions, field)- default:- return "", fmt.Errorf("unknown section %q (valid: server, browser, browsers, instanceDefaults, security, profiles, multiInstance, timeouts, observability, sessions)", section)+ section, field, err := lookupConfigSection(path)+ if err != nil {+ return "", err}+ return section.get(fc, field)}func getServerField(s *ServerConfig, field string) (string, error) {@@ -477,3 +452,104 @@ func formatIntPtr(n *int) string {}return strconv.Itoa(*n)}++func getSchedulerField(s *SchedulerFileConfig, field string) (string, error) {+ switch field {+ case "enabled":+ return formatBoolPtr(s.Enabled), nil+ case "strategy":+ return s.Strategy, nil+ case "maxQueueSize":+ return formatIntPtr(s.MaxQueueSize), nil+ case "maxPerAgent":+ return formatIntPtr(s.MaxPerAgent), nil+ case "maxInflight":+ return formatIntPtr(s.MaxInflight), nil+ case "maxPerAgentInflight":+ return formatIntPtr(s.MaxPerAgentFlight), nil+ case "resultTTLSec":+ return formatIntPtr(s.ResultTTLSec), nil+ case "workerCount":+ return formatIntPtr(s.WorkerCount), nil+ default:+ return "", fmt.Errorf("unknown field scheduler.%s", field)+ }+}++func getAutoSolverField(a *AutoSolverFileConfig, field string) (string, error) {+ if rest, ok := strings.CutPrefix(field, "external."); ok {+ return getAutoSolverExternalField(&a.External, rest)+ }+ if rest, ok := strings.CutPrefix(field, "credentials."); ok {+ return getAutoSolverCredentialsField(&a.Credentials, rest)+ }++ switch field {+ case "enabled":+ return formatBoolPtr(a.Enabled), nil+ case "autoTrigger":+ return formatBoolPtr(a.AutoTrigger), nil+ case "triggerOnNavigate":+ return formatBoolPtr(a.TriggerOnNavigate), nil+ case "triggerOnAction":+ return formatBoolPtr(a.TriggerOnAction), nil+ case "llmFallback":+ return formatBoolPtr(a.LLMFallback), nil+ case "maxAttempts":+ return formatIntPtr(a.MaxAttempts), nil+ case "solverTimeoutSec":+ return formatIntPtr(a.SolverTimeoutSec), nil+ case "retryBaseDelayMs":+ return formatIntPtr(a.RetryBaseDelayMs), nil+ case "retryMaxDelayMs":+ return formatIntPtr(a.RetryMaxDelayMs), nil+ case "llmProvider":+ return a.LLMProvider, nil+ case "solvers":+ return strings.Join(a.Solvers, ","), nil+ default:+ return "", fmt.Errorf("unknown field autoSolver.%s", field)+ }+}++func getAutoSolverExternalField(e *AutoSolverExtConf, field string) (string, error) {+ switch field {+ case "capsolverKey":+ return e.CapsolverKey, nil+ case "twoCaptchaKey":+ return e.TwoCaptchaKey, nil+ default:+ return "", fmt.Errorf("unknown field autoSolver.external.%s", field)+ }+}++func getAutoSolverCredentialsField(c *AutoSolverCredentialsConf, field string) (string, error) {+ switch {+ case strings.HasPrefix(field, "login."):+ switch strings.TrimPrefix(field, "login.") {+ case "user":+ return c.Login.User, nil+ case "password":+ return c.Login.Password, nil+ }+ case strings.HasPrefix(field, "signup."):+ switch strings.TrimPrefix(field, "signup.") {+ case "name":+ return c.Signup.Name, nil+ case "email":+ return c.Signup.Email, nil+ case "password":+ return c.Signup.Password, nil+ }+ case strings.HasPrefix(field, "form."):+ switch strings.TrimPrefix(field, "form.") {+ case "field1":+ return c.Form.Field1, nil+ case "field2":+ return c.Form.Field2, nil+ case "email":+ return c.Form.Email, nil+ }+ }+ return "", fmt.Errorf("unknown field autoSolver.credentials.%s", field)+}diff --git a/internal/config/editor_sections.go b/internal/config/editor_sections.gonew file mode 100644index 00000000..e3744c3a--- /dev/null+++ b/internal/config/editor_sections.go@@ -0,0 +1,119 @@+package config++import (+ "fmt"+ "strings"+)++// configSection binds a settable top-level section of FileConfig to the pair of+// accessors that reach into it.+type configSection struct {+ name string+ set func(fc *FileConfig, field, value string) error+ get func(fc *FileConfig, field string) (string, error)+}++// configSections is the ONE place the section vocabulary is written down. It used to+// live in four: a switch in editor_set.go, a switch in editor_get.go, and a hand-typed+// valid-sections list inside the error string at each default arm. Nothing tied any of+// them to FileConfig, so adding a section to the struct was silently correct everywhere+// except here — which is how autoSolver and scheduler both came to be declared, honoured+// by the config file, documented, exposed by the dashboard, and rejected by the CLI.+//+// autoSolver mattered most: the key-gated solver warning hands an operator+// "autoSolver.external.capsolverKey" as the thing to set, and the CLI answered that+// exact path with unknown section.+//+// $schema and configVersion are deliberately absent. They are document metadata rather+// than configuration, and the schema walk in editor_sections_test.go excludes them by+// name so neither can be added to satisfy that test nor dropped as an oversight.+var configSections = []configSection{+ {+ name: "server",+ set: func(fc *FileConfig, f, v string) error { return setServerField(&fc.Server, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getServerField(&fc.Server, f) },+ },+ {+ name: "browser",+ set: func(fc *FileConfig, f, v string) error { return setBrowserField(&fc.Browser, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getBrowserField(&fc.Browser, f) },+ },+ {+ name: "browsers",+ set: func(fc *FileConfig, f, v string) error { return setBrowsersField(&fc.Browsers, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getBrowsersField(&fc.Browsers, f) },+ },+ {+ name: "instanceDefaults",+ set: func(fc *FileConfig, f, v string) error { return setInstanceDefaultsField(&fc.InstanceDefaults, f, v) },+ get: func(fc *FileConfig, f string) (string, error) {+ return getInstanceDefaultsField(&fc.InstanceDefaults, f)+ },+ },+ {+ name: "security",+ set: func(fc *FileConfig, f, v string) error { return setSecurityField(&fc.Security, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getSecurityField(&fc.Security, f) },+ },+ {+ name: "profiles",+ set: func(fc *FileConfig, f, v string) error { return setProfilesField(&fc.Profiles, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getProfilesField(&fc.Profiles, f) },+ },+ {+ name: "multiInstance",+ set: func(fc *FileConfig, f, v string) error { return setMultiInstanceField(&fc.MultiInstance, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getMultiInstanceField(&fc.MultiInstance, f) },+ },+ {+ name: "timeouts",+ set: func(fc *FileConfig, f, v string) error { return setTimeoutsField(&fc.Timeouts, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getTimeoutsField(&fc.Timeouts, f) },+ },+ {+ name: "scheduler",+ set: func(fc *FileConfig, f, v string) error { return setSchedulerField(&fc.Scheduler, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getSchedulerField(&fc.Scheduler, f) },+ },+ {+ name: "observability",+ set: func(fc *FileConfig, f, v string) error { return setObservabilityField(&fc.Observability, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getObservabilityField(&fc.Observability, f) },+ },+ {+ name: "sessions",+ set: func(fc *FileConfig, f, v string) error { return setSessionsField(&fc.Sessions, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getSessionsField(&fc.Sessions, f) },+ },+ {+ name: "autoSolver",+ set: func(fc *FileConfig, f, v string) error { return setAutoSolverField(&fc.AutoSolver, f, v) },+ get: func(fc *FileConfig, f string) (string, error) { return getAutoSolverField(&fc.AutoSolver, f) },+ },+}++// lookupConfigSection splits a dotted path and returns the section that owns it.+func lookupConfigSection(path string) (configSection, string, error) {+ parts := strings.SplitN(path, ".", 2)+ if len(parts) != 2 {+ return configSection{}, "", fmt.Errorf("invalid path %q (expected section.field, e.g., server.port)", path)+ }+ name, field := parts[0], parts[1]+ for _, section := range configSections {+ if section.name == name {+ return section, field, nil+ }+ }+ return configSection{}, "", fmt.Errorf("unknown section %q (valid: %s)", name, configSectionNames())+}++// configSectionNames renders the valid sections for the refusal, derived from the table+// rather than typed out, so the message cannot describe a vocabulary the resolvers no+// longer have.+func configSectionNames() string {+ names := make([]string, 0, len(configSections))+ for _, section := range configSections {+ names = append(names, section.name)+ }+ return strings.Join(names, ", ")+}diff --git a/internal/config/editor_sections_test.go b/internal/config/editor_sections_test.gonew file mode 100644index 00000000..642af664--- /dev/null+++ b/internal/config/editor_sections_test.go@@ -0,0 +1,152 @@+package config++import (+ "reflect"+ "strings"+ "testing"++ "github.com/pinchtab/pinchtab/internal/autosolver/catalog"+)++// documentMetadataSections are the two FileConfig members that are NOT configuration:+// they describe the document rather than the server. They are excluded here deliberately+// so nobody adds them to configSections to satisfy the walk below, and so nobody deletes+// the exclusion as an oversight.+var documentMetadataSections = map[string]bool{+ "$schema": true,+ "configVersion": true,+}++// The criterion that makes this class of drift impossible. autoSolver and scheduler were+// both declared on FileConfig, honoured by the config file, documented, and exposed by+// the dashboard — and rejected by config set/get, because the section vocabulary was+// typed out by hand in four places and tied to nothing. This walks the struct instead,+// so a section added to FileConfig and nowhere else fails here rather than in an+// operator's terminal.+func TestEverySettableFileConfigSectionIsReachableFromBothResolvers(t *testing.T) {+ declared := declaredFileConfigSections(t)+ if len(declared) < 10 {+ t.Fatalf("found %d sections on FileConfig; this walk would be vacuous", len(declared))+ }++ table := map[string]bool{}+ for _, section := range configSections {+ table[section.name] = true+ }++ for _, name := range declared {+ if documentMetadataSections[name] {+ if table[name] {+ t.Errorf("%q is document metadata, not configuration, but configSections claims it", name)+ }+ continue+ }+ if !table[name] {+ t.Errorf("FileConfig declares section %q but configSections does not, so config set/get reject it — add it to the table rather than to a switch", name)+ continue+ }++ // Reachability is about the SECTION, not the field: drive a field name that+ // cannot exist and require the refusal to be about the field. An unknown-section+ // error here is the defect this test exists to catch.+ var fc FileConfig+ const bogusField = "definitelyNotAField"+ if err := SetConfigValue(&fc, name+"."+bogusField, "x"); err == nil {+ t.Errorf("set %s.%s was accepted; the section resolver validates nothing", name, bogusField)+ } else if strings.Contains(err.Error(), "unknown section") {+ t.Errorf("set %s.%s reports %v — the section is unreachable", name, bogusField, err)+ }+ if _, err := GetConfigValue(&fc, name+"."+bogusField); err == nil {+ t.Errorf("get %s.%s was accepted; the section resolver validates nothing", name, bogusField)+ } else if strings.Contains(err.Error(), "unknown section") {+ t.Errorf("get %s.%s reports %v — the section is unreachable", name, bogusField, err)+ }+ }++ // The other direction: a table entry naming a section FileConfig no longer declares+ // would leave the error message advertising a vocabulary that resolves to nothing.+ declaredSet := map[string]bool{}+ for _, name := range declared {+ declaredSet[name] = true+ }+ for _, section := range configSections {+ if !declaredSet[section.name] {+ t.Errorf("configSections lists %q, which FileConfig no longer declares", section.name)+ }+ }+}++// The schema walk must not be satisfiable by accepting everything, and the refusal has to+// keep naming the valid sections now that the list is generated rather than typed.+func TestABogusSectionIsStillRefusedAndTheRefusalNamesTheValidOnes(t *testing.T) {+ var fc FileConfig++ for _, path := range []string{"bogusSection.field", "autosolver.enabled", "Scheduler.enabled"} {+ setErr := SetConfigValue(&fc, path, "x")+ if setErr == nil {+ t.Errorf("set %s was accepted", path)+ continue+ }+ if !strings.Contains(setErr.Error(), "unknown section") {+ t.Errorf("set %s = %v, want an unknown-section refusal", path, setErr)+ }+ if _, getErr := GetConfigValue(&fc, path); getErr == nil {+ t.Errorf("get %s was accepted", path)+ }+ }++ err := SetConfigValue(&fc, "bogusSection.field", "x")+ // Generated from the table, so it can never advertise a vocabulary the resolvers+ // do not have — which is what the four hand-typed copies eventually did.+ for _, want := range []string{"server", "autoSolver", "scheduler"} {+ if !strings.Contains(err.Error(), want) {+ t.Errorf("refusal %q does not name the valid section %q", err, want)+ }+ }+}++// The key-gated solver warning hands an operator a config path to set. Nothing verified+// those hand-written strings named anything real: renaming the JSON tag they point at+// left autosolver, catalog, config and handlers all green, and the warning would then+// advise a key the config no longer reads. This is the cross-reference, and it only+// became possible once the editor knew the autoSolver section.+func TestEveryKeyGatedSolverConfigKeyResolves(t *testing.T) {+ names := catalog.KeyGated()+ if len(names) == 0 {+ t.Fatal("catalog reports no key-gated solvers; this cross-reference would be vacuous")+ }++ var fc FileConfig+ for _, name := range names {+ gated, ok := catalog.KeyGatedNamed(name)+ if !ok {+ t.Errorf("catalog.KeyGated lists %q but KeyGatedNamed does not know it", name)+ continue+ }+ if gated.ConfigKey == "" {+ t.Errorf("key-gated solver %q names no config key, so its warning cannot tell an operator what to set", name)+ continue+ }+ if _, err := GetConfigValue(&fc, gated.ConfigKey); err != nil {+ t.Errorf("solver %q points operators at %q, which config get rejects: %v", name, gated.ConfigKey, err)+ }+ if err := SetConfigValue(&fc, gated.ConfigKey, "probe"); err != nil {+ t.Errorf("solver %q points operators at %q, which config set rejects: %v", name, gated.ConfigKey, err)+ }+ }+}++func declaredFileConfigSections(t *testing.T) []string {+ t.Helper()+ typ := reflect.TypeOf(FileConfig{})+ names := make([]string, 0, typ.NumField())+ for i := 0; i < typ.NumField(); i++ {+ tag := typ.Field(i).Tag.Get("json")+ if tag == "" || tag == "-" {+ t.Errorf("FileConfig field %s has no json tag, so it cannot be addressed by path", typ.Field(i).Name)+ continue+ }+ names = append(names, strings.Split(tag, ",")[0])+ }+ return names+}diff --git a/internal/config/editor_set.go b/internal/config/editor_set.goindex 17eb6d02..2649f3d6 100644--- a/internal/config/editor_set.go+++ b/internal/config/editor_set.go@@ -10,38 +10,13 @@ import ()// SetConfigValue sets a dotted path in FileConfig (e.g., "server.port", "instanceDefaults.mode").+// The section vocabulary lives once, in configSections.func SetConfigValue(fc *FileConfig, path string, value string) error {- parts := strings.SplitN(path, ".", 2)- if len(parts) != 2 {- return fmt.Errorf("invalid path %q (expected section.field, e.g., server.port)", path)- }-- section, field := parts[0], parts[1]-- switch section {- case "server":- return setServerField(&fc.Server, field, value)- case "browser":- return setBrowserField(&fc.Browser, field, value)- case "browsers":- return setBrowsersField(&fc.Browsers, field, value)- case "instanceDefaults":- return setInstanceDefaultsField(&fc.InstanceDefaults, field, value)- case "security":- return setSecurityField(&fc.Security, field, value)- case "profiles":- return setProfilesField(&fc.Profiles, field, value)- case "multiInstance":- return setMultiInstanceField(&fc.MultiInstance, field, value)- case "timeouts":- return setTimeoutsField(&fc.Timeouts, field, value)- case "observability":- return setObservabilityField(&fc.Observability, field, value)- case "sessions":- return setSessionsField(&fc.Sessions, field, value)- default:- return fmt.Errorf("unknown section %q (valid: server, browser, browsers, instanceDefaults, security, profiles, multiInstance, timeouts, observability, sessions)", section)+ section, field, err := lookupConfigSection(path)+ if err != nil {+ return err}+ return section.set(fc, field, value)}func setServerField(s *ServerConfig, field, value string) error {@@ -693,3 +668,150 @@ func setIDPIField(s *SecurityConfig, field, value string) error {}return nil}++func setSchedulerField(s *SchedulerFileConfig, field, value string) error {+ switch field {+ case "enabled":+ b, err := parseBool(value)+ if err != nil {+ return fmt.Errorf("scheduler.enabled: %w", err)+ }+ s.Enabled = &b+ case "strategy":+ s.Strategy = value+ case "maxQueueSize":+ return setIntPtrField(&s.MaxQueueSize, "scheduler.maxQueueSize", value)+ case "maxPerAgent":+ return setIntPtrField(&s.MaxPerAgent, "scheduler.maxPerAgent", value)+ case "maxInflight":+ return setIntPtrField(&s.MaxInflight, "scheduler.maxInflight", value)+ case "maxPerAgentInflight":+ return setIntPtrField(&s.MaxPerAgentFlight, "scheduler.maxPerAgentInflight", value)+ case "resultTTLSec":+ return setIntPtrField(&s.ResultTTLSec, "scheduler.resultTTLSec", value)+ case "workerCount":+ return setIntPtrField(&s.WorkerCount, "scheduler.workerCount", value)+ default:+ return fmt.Errorf("unknown field scheduler.%s", field)+ }+ return nil+}++func setAutoSolverField(a *AutoSolverFileConfig, field, value string) error {+ if rest, ok := strings.CutPrefix(field, "external."); ok {+ return setAutoSolverExternalField(&a.External, rest, value)+ }+ if rest, ok := strings.CutPrefix(field, "credentials."); ok {+ return setAutoSolverCredentialsField(&a.Credentials, rest, value)+ }++ switch field {+ case "enabled":+ b, err := parseBool(value)+ if err != nil {+ return fmt.Errorf("autoSolver.enabled: %w", err)+ }+ a.Enabled = &b+ case "autoTrigger":+ b, err := parseBool(value)+ if err != nil {+ return fmt.Errorf("autoSolver.autoTrigger: %w", err)+ }+ a.AutoTrigger = &b+ case "triggerOnNavigate":+ b, err := parseBool(value)+ if err != nil {+ return fmt.Errorf("autoSolver.triggerOnNavigate: %w", err)+ }+ a.TriggerOnNavigate = &b+ case "triggerOnAction":+ b, err := parseBool(value)+ if err != nil {+ return fmt.Errorf("autoSolver.triggerOnAction: %w", err)+ }+ a.TriggerOnAction = &b+ case "llmFallback":+ b, err := parseBool(value)+ if err != nil {+ return fmt.Errorf("autoSolver.llmFallback: %w", err)+ }+ a.LLMFallback = &b+ case "maxAttempts":+ return setIntPtrField(&a.MaxAttempts, "autoSolver.maxAttempts", value)+ case "solverTimeoutSec":+ return setIntPtrField(&a.SolverTimeoutSec, "autoSolver.solverTimeoutSec", value)+ case "retryBaseDelayMs":+ return setIntPtrField(&a.RetryBaseDelayMs, "autoSolver.retryBaseDelayMs", value)+ case "retryMaxDelayMs":+ return setIntPtrField(&a.RetryMaxDelayMs, "autoSolver.retryMaxDelayMs", value)+ case "llmProvider":+ a.LLMProvider = value+ case "solvers":+ a.Solvers = parseCSVList(value)+ default:+ return fmt.Errorf("unknown field autoSolver.%s", field)+ }+ return nil+}++func setAutoSolverExternalField(e *AutoSolverExtConf, field, value string) error {+ switch field {+ case "capsolverKey":+ e.CapsolverKey = value+ case "twoCaptchaKey":+ e.TwoCaptchaKey = value+ default:+ return fmt.Errorf("unknown field autoSolver.external.%s", field)+ }+ return nil+}++func setAutoSolverCredentialsField(c *AutoSolverCredentialsConf, field, value string) error {+ switch {+ case strings.HasPrefix(field, "login."):+ switch strings.TrimPrefix(field, "login.") {+ case "user":+ c.Login.User = value+ case "password":+ c.Login.Password = value+ default:+ return fmt.Errorf("unknown field autoSolver.credentials.%s", field)+ }+ case strings.HasPrefix(field, "signup."):+ switch strings.TrimPrefix(field, "signup.") {+ case "name":+ c.Signup.Name = value+ case "email":+ c.Signup.Email = value+ case "password":+ c.Signup.Password = value+ default:+ return fmt.Errorf("unknown field autoSolver.credentials.%s", field)+ }+ case strings.HasPrefix(field, "form."):+ switch strings.TrimPrefix(field, "form.") {+ case "field1":+ c.Form.Field1 = value+ case "field2":+ c.Form.Field2 = value+ case "email":+ c.Form.Email = value+ default:+ return fmt.Errorf("unknown field autoSolver.credentials.%s", field)+ }+ default:+ return fmt.Errorf("unknown field autoSolver.credentials.%s", field)+ }+ return nil+}++// setIntPtrField parses value into an int and points field at it, naming the full path+// in the refusal — the same shape every numeric setter in this package uses.+func setIntPtrField(field **int, path, value string) error {+ n, err := strconv.Atoi(value)+ if err != nil {+ return fmt.Errorf("%s must be a number: %w", path, err)+ }+ *field = &n+ return nil+}