Skip to content

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

fix: observability.activity.stateDir is settable and persisted but the runtime discards it, so config set succeeds and changes nothing (PIN-174)

Luigi Agosti6abf2d282432
diff --git a/docs/reference/config.md b/docs/reference/config.md
index 0fbf94ff..4f6b8848 100644
--- a/docs/reference/config.md
+++ b/docs/reference/config.md
@@ -662,6 +662,12 @@ headers correctly.
}
```
+Activity logs are always written to `<server.stateDir>/activity`, so two instances cannot
+end up sharing one log directory. `observability.activity.stateDir` is therefore not
+settable — `config set` refuses it and a file that carries it loads with a warning — and
+`config get observability.activity.stateDir` reports the derived directory in effect. Move
+the logs with `server.stateDir`.
+
`server.trustProxyHeaders` should stay `false` unless PinchTab is behind a trusted reverse proxy that overwrites `Forwarded` and `X-Forwarded-*` headers. Do not enable it on direct-exposure deployments or behind proxies that pass client-supplied forwarding headers through unchanged.
## Legacy Flat Format
diff --git a/internal/config/config_apply_census_test.go b/internal/config/config_apply_census_test.go
new file mode 100644
index 00000000..3b54341c
--- /dev/null
+++ b/internal/config/config_apply_census_test.go
@@ -0,0 +1,204 @@
+package config
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/browsers"
+)
+
+// refusedKey records a key the product deliberately does not carry: patch writes the
+// ignored value the census looks for, and reason is the wording the refusal must keep, so
+// a user who reaches for the key learns why it is not on offer.
+type refusedKey struct {
+ reason string
+ patch string
+ value string
+}
+
+// refusedRatherThanCarried holds the keys `config set` says no to instead of accepting and
+// dropping. A key silently ignored is worse than a key with an invisible default, so an
+// entry here is a promise the refusal exists and explains itself.
+var refusedRatherThanCarried = map[string]refusedKey{
+ "observability.activity.stateDir": {
+ reason: ActivityStateDirRefusal,
+ patch: `{"observability":{"activity":{"stateDir":"/tmp/census-elsewhere"}}}`,
+ value: "/tmp/census-elsewhere",
+ },
+}
+
+// acceptedAndDropped holds the keys `config set` still takes and the value in effect never
+// reflects. Each is a filed defect, not a licence: the entry names the mechanism and the
+// card that owns it, and the census reds again the moment one of them starts working.
+var acceptedAndDropped = map[string]string{
+ "browser.proxy.username": "PIN-178: a proxy block with no server counts as absent, so the save drops it",
+ "browser.proxy.password": "PIN-178: a proxy block with no server counts as absent, so the save drops it",
+ "browser.proxy.bypassList": "PIN-178: a proxy block with no server counts as absent, so the save drops it",
+ "browser.proxy.geo.timezone": "PIN-178: a proxy block with no server counts as absent, so the save drops it",
+ "browser.proxy.geo.locale": "PIN-178: a proxy block with no server counts as absent, so the save drops it",
+ "browser.proxy.geo.webrtcIP": "PIN-178: a proxy block with no server counts as absent, so the save drops it",
+ "browser.proxy.geo.countryISO": "PIN-178: a proxy block with no server counts as absent, so the save drops it",
+}
+
+// censusValueFor holds the values the generic candidate list cannot guess: a key whose
+// setter or loader takes only a fixed vocabulary needs a member of it, or the census would
+// measure the rejection instead of the round-trip.
+var censusValueFor = map[string]string{
+ "server.logLevel": "error",
+ "server.port": "9123",
+ "server.bind": "0.0.0.0",
+ "browser.extraFlags": "--census-flag",
+ "instanceDefaults.mode": "headed",
+ "instanceDefaults.stealthLevel": "full",
+ "instanceDefaults.tabEvictionPolicy": "close_oldest",
+ "instanceDefaults.tabPolicy.eviction": "reject",
+ "instanceDefaults.tabPolicy.lifecycle": "close_idle",
+ "multiInstance.strategy": "explicit",
+ "multiInstance.allocationPolicy": "round_robin",
+ "scheduler.strategy": "fair-fifo",
+ "security.attach.allowSchemes": "wss",
+ "security.attach.allowHosts": "10.0.0.1",
+ "security.allowedDomains": "census.example.com",
+ "security.downloadAllowedDomains": "census.example.com",
+ "security.trustedProxyCIDRs": "10.0.0.0/8",
+ "security.trustedResolveCIDRs": "10.0.0.0/8",
+ "autoSolver.solvers": "semantic",
+}
+
+// censusCandidates are tried in order for every other key: the first one the setter takes
+// and that changes the file value is what the round-trip is measured with.
+var censusCandidates = []string{"census-value", "17", "false", "true", "/tmp/census-dir"}
+
+// TestEverySettableConfigKeyReachesTheRuntime is the standing census behind this
+// behaviour: a key `config set` accepts and writes to the file, and that the value in
+// effect never reflects, changes nothing and says so nowhere. Adding such a key fails here
+// until the value arrives or the product refuses it with a reason.
+func TestEverySettableConfigKeyReachesTheRuntime(t *testing.T) {
+ paths := addressableConfigPaths(t)
+ if len(paths) < 100 {
+ t.Fatalf("found only %d addressable config paths, so this census is not walking FileConfig", len(paths))
+ }
+
+ settable := 0
+ for _, path := range paths {
+ file, want, ok := censusFileValue(path)
+ if !ok {
+ checkRefusedKey(t, path)
+ continue
+ }
+ settable++
+
+ got := censusRuntimeValue(t, path, file)
+ reason, accounted := acceptedAndDropped[path]
+ switch {
+ case got == want && accounted:
+ t.Errorf("%s now survives into the value in effect; drop its entry so the census keeps meaning what it says", path)
+ case got == want:
+ case accounted:
+ t.Logf("accounted: %s is set to %q and the value in effect is %q — %s", path, want, got, reason)
+ default:
+ t.Errorf("config set %s is accepted but the value never comes back: file says %q, the value in effect is %q; carry it, or refuse it with the reason", path, want, got)
+ }
+ }
+ if settable < 100 {
+ t.Fatalf("only %d of %d addressable paths took a census value, so this census is barely measuring anything", settable, len(paths))
+ }
+
+ for path := range acceptedAndDropped {
+ if !slicesContains(paths, path) {
+ t.Errorf("%s is accounted as dropped but is no longer an addressable key; drop the entry", path)
+ }
+ }
+ for path := range refusedRatherThanCarried {
+ if !slicesContains(paths, path) {
+ t.Errorf("%s is accounted as refused but is no longer an addressable key; drop the entry", path)
+ }
+ }
+}
+
+// checkRefusedKey covers the keys `config set` will not take. Most are file-only fields and
+// none of this census's business; the ones a refusal was written for must keep both halves
+// of the bargain — the refusal carries its reason, and the value still does not arrive by
+// the file route the refusal leaves open.
+func checkRefusedKey(t *testing.T, path string) {
+ t.Helper()
+
+ entry, ok := refusedRatherThanCarried[path]
+ if !ok {
+ return
+ }
+
+ fc := DefaultFileConfig()
+ err := SetConfigValue(&fc, path, entry.value)
+ if err == nil {
+ t.Errorf("config set %s accepted %q; a key the runtime does not carry must be refused", path, entry.value)
+ return
+ }
+ if !strings.Contains(err.Error(), entry.reason) {
+ t.Errorf("config set %s refusal = %q, want it to carry the reason %q", path, err.Error(), entry.reason)
+ }
+
+ patched := DefaultFileConfig()
+ if err := PatchConfigJSON(&patched, entry.patch); err != nil {
+ t.Fatalf("PatchConfigJSON(%s) error = %v", path, err)
+ }
+ // Contains, not equality: a key that starts working may be a root the runtime joins a
+ // subdirectory onto, and that still means the file value arrived.
+ if got := censusRuntimeValue(t, path, &patched); strings.Contains(got, entry.value) {
+ t.Errorf("%s now reaches the runtime (%q); drop its entry and stop refusing it", path, got)
+ }
+}
+
+// censusFileValue picks a value the setter takes and that lands in the file, and hands back
+// the whole file so the caller loads exactly what was written.
+func censusFileValue(path string) (*FileConfig, string, bool) {
+ candidates := censusCandidates
+ if override, ok := censusValueFor[path]; ok {
+ candidates = append([]string{override}, candidates...)
+ }
+ candidates = append(candidates, browsers.IDs()...)
+
+ for _, candidate := range candidates {
+ fc := DefaultFileConfig()
+ before, err := GetConfigValue(&fc, path)
+ if err != nil {
+ return nil, "", false
+ }
+ if err := SetConfigValue(&fc, path, candidate); err != nil {
+ continue
+ }
+ after, err := GetConfigValue(&fc, path)
+ if err != nil || after == before || strings.TrimSpace(after) == "" {
+ continue
+ }
+ return &fc, after, true
+ }
+ return nil, "", false
+}
+
+// censusRuntimeValue writes the census file and reads the path back the way `config get`
+// does — through the loaded RuntimeConfig, never from the file bytes, which is the whole
+// point: the file value is what is under suspicion.
+func censusRuntimeValue(t *testing.T, path string, file *FileConfig) string {
+ t.Helper()
+
+ body, err := json.Marshal(file)
+ if err != nil {
+ t.Fatalf("marshal census config for %s: %v", path, err)
+ }
+ configPath := filepath.Join(t.TempDir(), "config.json")
+ if err := os.WriteFile(configPath, body, 0o600); err != nil {
+ t.Fatalf("WriteFile() error = %v", err)
+ }
+ t.Setenv("PINCHTAB_CONFIG", configPath)
+ t.Setenv("PINCHTAB_TOKEN", "")
+
+ got, err := resolvedConfigValue(path)
+ if err != nil {
+ t.Fatalf("resolvedConfigValue(%q) error = %v", path, err)
+ }
+ return got
+}
diff --git a/internal/config/config_methods.go b/internal/config/config_methods.go
index f985d34a..4249a91b 100644
--- a/internal/config/config_methods.go
+++ b/internal/config/config_methods.go
@@ -56,3 +56,11 @@ func (cfg *RuntimeConfig) ActivityLogDir() string {
}
const activityLogSubdir = "activity"
+
+// ActivityStateDirRefusal is the one wording behind refusing observability.activity.stateDir
+// at `config set` and warning about it at validation: the location is derived, so a file
+// value would be accepted and then discarded. It names the derived path and why the
+// derivation exists, because a user reaching for the key wants the logs elsewhere and
+// deserves to learn why that is not on offer.
+const ActivityStateDirRefusal = "observability.activity.stateDir is not settable: activity logs are always written to <server.stateDir>/" + activityLogSubdir +
+ " so two instances cannot share a log directory; set server.stateDir to move them"
diff --git a/internal/config/editor_get.go b/internal/config/editor_get.go
index 97f4f063..b3213e88 100644
--- a/internal/config/editor_get.go
+++ b/internal/config/editor_get.go
@@ -580,7 +580,8 @@ func EffectiveConfigValue(path string) (string, error) {
// answer, so reading the file would report something nothing acts on:
//
// scheduler.* a configured zero means "use the default" here
-// observability.activity.stateDir applyFileConfig never copies it; the path derives
+// observability.activity.stateDir the path derives from server.stateDir; a file value
+// is refused at config set and warned about at load
func runtimeSettlesTheValue(path string) bool {
return strings.HasPrefix(path, "scheduler.") || path == "observability.activity.stateDir"
}
diff --git a/internal/config/editor_set.go b/internal/config/editor_set.go
index 2649f3d6..a8545ebc 100644
--- a/internal/config/editor_set.go
+++ b/internal/config/editor_set.go
@@ -1,6 +1,7 @@
package config
import (
+ "errors"
"fmt"
"strconv"
"strings"
@@ -239,7 +240,7 @@ func setActivityField(a *ActivityFileConfig, field, value string) error {
}
a.RetentionDays = &n
case "stateDir":
- a.StateDir = value
+ return errors.New(ActivityStateDirRefusal)
default:
return fmt.Errorf("unknown field observability.activity.%s", field)
}
diff --git a/internal/config/editor_set_test.go b/internal/config/editor_set_test.go
index f89dcaa5..b5cfaa70 100644
--- a/internal/config/editor_set_test.go
+++ b/internal/config/editor_set_test.go
@@ -1,6 +1,9 @@
package config
-import "testing"
+import (
+ "strings"
+ "testing"
+)
func TestSetConfigValue_AllowedDomainsValidation(t *testing.T) {
tests := []struct {
@@ -419,3 +422,36 @@ func TestSetBrowsersDefaultRejectsUnknownBrowser(t *testing.T) {
t.Fatalf("Default = %q, want normalized cloak", fc.Browsers.Default)
}
}
+
+// The refusal has to say more than no: it names the directory the logs go to, the reason
+// the location is not the operator's to choose, and what to set instead. Properties rather
+// than the sentence, so a reword stays free.
+func TestActivityStateDirRefusalCarriesTheReasonAndTheRemedy(t *testing.T) {
+ for _, want := range []string{
+ "observability.activity.stateDir",
+ "<server.stateDir>/activity",
+ "cannot share a log directory",
+ "set server.stateDir",
+ } {
+ if !strings.Contains(ActivityStateDirRefusal, want) {
+ t.Errorf("ActivityStateDirRefusal = %q, want it to carry %q", ActivityStateDirRefusal, want)
+ }
+ }
+}
+
+func TestSetActivityStateDirIsRefusedRatherThanSilentlyIgnored(t *testing.T) {
+ fc := &FileConfig{}
+ err := SetConfigValue(fc, "observability.activity.stateDir", "/tmp/elsewhere")
+ if err == nil {
+ t.Fatal("config set observability.activity.stateDir was accepted; the runtime never reads it")
+ }
+ if err.Error() != ActivityStateDirRefusal {
+ t.Errorf("refusal = %q, want %q", err.Error(), ActivityStateDirRefusal)
+ }
+ if fc.Observability.Activity.StateDir != "" {
+ t.Errorf("StateDir = %q, want the refused value left unwritten", fc.Observability.Activity.StateDir)
+ }
+ if err := SetConfigValue(fc, "observability.activity.retentionDays", "7"); err != nil {
+ t.Fatalf("the neighbouring activity keys must stay settable: %v", err)
+ }
+}
diff --git a/internal/config/validate.go b/internal/config/validate.go
index 1fe74d67..429e5cba 100644
--- a/internal/config/validate.go
+++ b/internal/config/validate.go
@@ -313,6 +313,9 @@ func ValidateFileConfig(fc *FileConfig) []error {
Message: fmt.Sprintf("must be > 0 (got %d)", *fc.Observability.Activity.RetentionDays),
})
}
+ if strings.TrimSpace(fc.Observability.Activity.StateDir) != "" {
+ errs = append(errs, fmt.Errorf("%s; remove the key, the file value is ignored", ActivityStateDirRefusal))
+ }
if fc.Sessions.Dashboard.IdleTimeoutSec != nil && *fc.Sessions.Dashboard.IdleTimeoutSec <= 0 {
errs = append(errs, ValidationError{
Field: "sessions.dashboard.idleTimeoutSec",
diff --git a/internal/config/validate_test.go b/internal/config/validate_test.go
index 46f156f3..f6dbafe1 100644
--- a/internal/config/validate_test.go
+++ b/internal/config/validate_test.go
@@ -2,6 +2,7 @@ package config
import (
"context"
+ "path/filepath"
"strings"
"sync"
"testing"
@@ -1192,3 +1193,39 @@ func TestValidateFileConfig_DefaultSolversAreKnown(t *testing.T) {
}
}
}
+
+// A file that already carries the key keeps loading — validation errors are non-fatal
+// diagnostics here — but it no longer does so in silence: the same reason the setter gives
+// is reported, so the operator learns the value is dead rather than discovering it never
+// took effect.
+func TestValidateFileConfigWarnsAboutActivityStateDirWithoutBreakingTheLoad(t *testing.T) {
+ root := t.TempDir()
+ body := `{"server":{"stateDir":` + quoteJSON(root) + `},
+ "observability":{"activity":{"stateDir":"/tmp/elsewhere"}}}`
+ writeConfigForGet(t, body)
+
+ fc, _, err := LoadFileConfig()
+ if err != nil {
+ t.Fatalf("LoadFileConfig() error = %v; a file carrying the key must still load", err)
+ }
+ found := ""
+ for _, e := range ValidateFileConfig(fc) {
+ if strings.Contains(e.Error(), "observability.activity.stateDir") {
+ found = e.Error()
+ }
+ }
+ if found == "" {
+ t.Fatal("validation said nothing about a file carrying observability.activity.stateDir")
+ }
+ if !strings.Contains(found, ActivityStateDirRefusal) {
+ t.Errorf("validation warning = %q, want it to carry the same reason %q", found, ActivityStateDirRefusal)
+ }
+
+ cfg, _, err := LoadConfig()
+ if err != nil {
+ t.Fatalf("LoadConfig() error = %v; the warning must not make an existing config unloadable", err)
+ }
+ if want := filepath.Join(root, "activity"); cfg.ActivityLogDir() != want {
+ t.Errorf("ActivityLogDir() = %q, want the derived %q", cfg.ActivityLogDir(), want)
+ }
+}