High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
refactor: derive guards-down and recommended-defaults capability lists from the canonical table and delete the unwired UpdateSensitiveEndpoints (PIN-200)
Luigi Agostibcf998860ceb
diff --git a/internal/cli/report/security_defaults.go b/internal/cli/report/security_defaults.goindex 91ee9e5d..0b324a89 100644--- a/internal/cli/report/security_defaults.go+++ b/internal/cli/report/security_defaults.go@@ -1,10 +1,30 @@package reportimport (+ "sort"+"github.com/pinchtab/pinchtab/internal/config""github.com/pinchtab/pinchtab/internal/config/workflow"+ "github.com/pinchtab/pinchtab/internal/routes")+// capabilityDisableLines is the one owner of the "disable capability X" recommendation+// lines, derived from the canonical capability table rather than restated: a ninth+// capability appears here — and in the sensitive-endpoints recommendation that reuses+// these lines — with no edit. Sorted so the report output is stable.+func capabilityDisableLines() []string {+ lines := make([]string, 0)+ for cap := range routes.CapabilityEndpoints() {+ meta, ok := routes.Meta(cap)+ if !ok {+ continue+ }+ lines = append(lines, meta.Setting+" = false")+ }+ sort.Strings(lines)+ return lines+}+func ApplyRecommendedSecurityDefaults(fc *config.FileConfig) {workflow.ApplyRecommendedSecurityDefaults(fc)}@@ -23,14 +43,10 @@ func restoreSecurityDefaults() (string, bool, error) {func RecommendedSecurityDefaultLines(cfg *config.RuntimeConfig) []string {posture := AssessSecurityPosture(cfg)- ordered := []string{- "server.bind = 127.0.0.1",- "security.allowEvaluate = false",- "security.allowMacro = false",- "security.allowScreencast = false",- "security.allowDownload = false",- "security.allowUpload = false",- "security.allowNetworkIntercept = false",+ capabilityLines := capabilityDisableLines()+ ordered := []string{"server.bind = 127.0.0.1"}+ ordered = append(ordered, capabilityLines...)+ ordered = append(ordered,"security.attach.enabled = false","security.attach.allowHosts = 127.0.0.1,localhost,::1","security.attach.allowSchemes = ws,wss",@@ -39,7 +55,7 @@ func RecommendedSecurityDefaultLines(cfg *config.RuntimeConfig) []string {"security.idpi.strictMode = true","security.idpi.scanContent = true","security.idpi.wrapContent = true",- }+ )needed := make(map[string]bool, len(ordered))for _, check := range posture.Checks {@@ -50,14 +66,7 @@ func RecommendedSecurityDefaultLines(cfg *config.RuntimeConfig) []string {case "bind_loopback":needed["server.bind = 127.0.0.1"] = truecase "sensitive_endpoints_disabled":- for _, line := range []string{- "security.allowEvaluate = false",- "security.allowMacro = false",- "security.allowScreencast = false",- "security.allowDownload = false",- "security.allowUpload = false",- "security.allowNetworkIntercept = false",- } {+ for _, line := range capabilityLines {needed[line] = true}case "attach_disabled", "attach_local_only":diff --git a/internal/cli/report/security_defaults_test.go b/internal/cli/report/security_defaults_test.gonew file mode 100644index 00000000..fc282fb0--- /dev/null+++ b/internal/cli/report/security_defaults_test.go@@ -0,0 +1,82 @@+package report++import (+ "os"+ "strings"+ "testing"++ "github.com/pinchtab/pinchtab/internal/routes"+)++// The recommended-defaults report used to hand-list its capability lines and had drifted+// from the canonical table, omitting cookies and stateExport, so it under-reported which+// capabilities a hardened posture turns off. The lines now derive from routes, so this+// census walks CapabilityEndpoints() and requires the report to name every gated capability+// with exactly the config path routes.Meta owns — an endpoint added without a table entry+// fails here rather than dropping out of the report silently.+func TestRecommendedDefaultsNameEveryCapability(t *testing.T) {+ gated := routes.CapabilityEndpoints()+ if len(gated) < 2 {+ t.Fatalf("only %d gated capabilities; this census would pass vacuously", len(gated))+ }++ have := map[string]bool{}+ for _, line := range capabilityDisableLines() {+ have[line] = true+ }++ for cap := range gated {+ meta, ok := routes.Meta(cap)+ if !ok {+ t.Errorf("capability %q gates routes but routes.Meta does not describe it, so the report cannot derive its line", cap)+ continue+ }+ want := meta.Setting + " = false"+ if !have[want] {+ t.Errorf("the recommended-defaults report omits %q for capability %q; it under-reports which capabilities are off", want, cap)+ }+ }+}++// No synthesis: every line the report emits must be a real capability's owned setting, not a+// fabricated path. Pairs with the coverage census so the guard cannot be satisfied by naming+// plausible-looking settings that no capability actually has.+func TestRecommendedDefaultsEmitNoFabricatedSetting(t *testing.T) {+ valid := map[string]bool{}+ for cap := range routes.CapabilityEndpoints() {+ if meta, ok := routes.Meta(cap); ok {+ valid[meta.Setting+" = false"] = true+ }+ }+ for _, line := range capabilityDisableLines() {+ if !valid[line] {+ t.Errorf("report emits %q, which no gated capability's routes.Meta owns", line)+ }+ }+}++// The block of capability lines appeared twice in this file — once in the ordered list and+// once in the sensitive-endpoints recommendation — and the two had already diverged. They now+// share capabilityDisableLines, so no capability disable line may be written as a literal:+// finding one means a hand-listed copy has returned.+func TestNoHandListedCapabilityDisableLineRemains(t *testing.T) {+ src, err := os.ReadFile("security_defaults.go")+ if err != nil {+ t.Fatalf("read source: %v", err)+ }+ text := string(src)+ found := 0+ for cap := range routes.CapabilityEndpoints() {+ meta, ok := routes.Meta(cap)+ if !ok {+ continue+ }+ if strings.Contains(text, `"`+meta.Setting+" = false"+`"`) {+ t.Errorf("security_defaults.go hand-lists %q as a literal; it must come from capabilityDisableLines so a ninth capability is covered by construction", meta.Setting+" = false")+ }+ found+++ }+ if found < 2 {+ t.Fatalf("checked only %d capabilities; this guard would prove little", found)+ }+}diff --git a/internal/config/workflow/security.go b/internal/config/workflow/security.goindex 57cbd44d..e5669d95 100644--- a/internal/config/workflow/security.go+++ b/internal/config/workflow/security.go@@ -5,10 +5,10 @@ import ("fmt""os""reflect"- "slices""strings""github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/routes")func ApplyRecommendedSecurityDefaults(fc *config.FileConfig) {@@ -44,39 +44,6 @@ func RestoreSecurityDefaults() (string, bool, error) {return configPath, true, nil}-func UpdateSensitiveEndpoints(value string) (*config.RuntimeConfig, bool, error) {- change, err := prepareChange(func(fc *config.FileConfig) error {- selected := map[string]bool{}- for _, item := range splitCommaList(value) {- selected[item] = true- }- for endpoint, path := range map[string]string{- "evaluate": "security.allowEvaluate",- "macro": "security.allowMacro",- "screencast": "security.allowScreencast",- "download": "security.allowDownload",- "cookies": "security.allowCookies",- "upload": "security.allowUpload",- "networkIntercept": "security.allowNetworkIntercept",- } {- if err := config.SetConfigValue(fc, path, fmt.Sprintf("%t", selected[endpoint])); err != nil {- return fmt.Errorf("set %s: %w", endpoint, err)- }- }- return nil- })- if err != nil {- return nil, false, err- }- if len(change.ValidationErrors) > 0 {- return nil, false, change.ValidationErrors[0]- }- if err := SavePreparedChange(change); err != nil {- return nil, false, err- }- return config.Load(), true, nil-}-func UpdateContentGuard(mode string) (*config.RuntimeConfig, bool, error) {change, err := prepareChange(func(fc *config.FileConfig) error {scan := mode == "both" || mode == "scan"@@ -106,6 +73,13 @@ func UpdateContentGuard(mode string) (*config.RuntimeConfig, bool, error) {return config.Load(), true, nil}+// guardsDownExcludedCapability is the one capability the guards-down preset does not+// bulk-enable: stateExport writes cookies and browser storage to disk, and enabling+// disk export as a side effect of "turn the guards off for local dev" is a surprise+// no other capability carries. The census in this package's tests holds this exclusion+// so a ninth capability is enabled by default while this one stays a deliberate opt-out.+const guardsDownExcludedCapability = routes.CapStateExport+// BuildGuardsDownConfig mutates fc in memory to apply the guards-down preset.// It does not persist anything. Returns whether fc was modified.func BuildGuardsDownConfig(fc *config.FileConfig) (bool, error) {@@ -131,18 +105,24 @@ func BuildGuardsDownConfig(fc *config.FileConfig) (bool, error) {}}+ for cap := range routes.CapabilityEndpoints() {+ if cap == guardsDownExcludedCapability {+ continue+ }+ meta, ok := routes.Meta(cap)+ if !ok {+ return false, fmt.Errorf("capability %q gates routes but routes.Meta does not describe it", cap)+ }+ if err := config.SetConfigValue(fc, meta.Setting, "true"); err != nil {+ return false, fmt.Errorf("set %s: %w", meta.Setting, err)+ }+ }+for _, item := range []struct {path stringvalue string}{{path: "server.bind", value: "127.0.0.1"},- {path: "security.allowEvaluate", value: "true"},- {path: "security.allowMacro", value: "true"},- {path: "security.allowScreencast", value: "true"},- {path: "security.allowDownload", value: "true"},- {path: "security.allowCookies", value: "true"},- {path: "security.allowUpload", value: "true"},- {path: "security.allowNetworkIntercept", value: "true"},{path: "security.attach.enabled", value: "true"},{path: "security.attach.allowHosts", value: "127.0.0.1,localhost,::1"},{path: "security.attach.allowSchemes", value: "ws,wss"},@@ -193,19 +173,6 @@ func formatFileConfigJSON(fc *config.FileConfig) (string, error) {return string(data), nil}-func splitCommaList(value string) []string {- parts := strings.Split(value, ",")- items := make([]string, 0, len(parts))- for _, part := range parts {- trimmed := strings.TrimSpace(strings.ToLower(part))- if trimmed != "" {- items = append(items, trimmed)- }- }- slices.Sort(items)- return slices.Compact(items)-}-type securityDefaultsState struct {Bind stringToken stringdiff --git a/internal/config/workflow/security_test.go b/internal/config/workflow/security_test.goindex faf830df..d4e7b1d0 100644--- a/internal/config/workflow/security_test.go+++ b/internal/config/workflow/security_test.go@@ -10,8 +10,54 @@ import (_ "github.com/pinchtab/pinchtab/internal/browsers/cloak"_ "github.com/pinchtab/pinchtab/internal/browsers/ghostchrome""github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/routes")+// The guards-down preset used to hand-list its capability toggles and had silently drifted+// from the canonical table — omitting stateExport. It now derives from routes, so this census+// walks CapabilityEndpoints() and drives the real preset: every gated capability must be+// enabled EXCEPT the one recorded exclusion, and each must resolve through routes.Meta rather+// than a synthesised path. A capability that gates routes but routes.Meta cannot describe+// (an endpoint added without a table entry) fails here rather than being silently skipped.+func TestGuardsDownEnablesEveryCapabilityExceptTheRecordedExclusion(t *testing.T) {+ gated := routes.CapabilityEndpoints()+ if len(gated) < 2 {+ t.Fatalf("only %d gated capabilities; this census would pass vacuously", len(gated))+ }++ fc := config.DefaultFileConfig()+ if _, err := BuildGuardsDownConfig(&fc); err != nil {+ t.Fatalf("BuildGuardsDownConfig() error = %v", err)+ }++ excludedSeen := false+ for cap := range gated {+ meta, ok := routes.Meta(cap)+ if !ok {+ t.Errorf("capability %q gates routes but routes.Meta does not describe it, so guards-down cannot derive its setting", cap)+ continue+ }+ got, err := config.GetConfigValue(&fc, meta.Setting)+ if err != nil {+ t.Errorf("reading %s for capability %q: %v", meta.Setting, cap, err)+ continue+ }+ if cap == guardsDownExcludedCapability {+ excludedSeen = true+ if got == "true" {+ t.Errorf("guards-down enabled the recorded-exclusion capability %q (%s); disk state export must stay a deliberate opt-out, not a side effect of turning guards off", cap, meta.Setting)+ }+ continue+ }+ if got != "true" {+ t.Errorf("guards-down left capability %q (%s) at %q, not enabled; a capability it omits is silently kept off", cap, meta.Setting, got)+ }+ }+ if !excludedSeen {+ t.Fatalf("the recorded exclusion %q is not among the gated capabilities; the exclusion is stale", guardsDownExcludedCapability)+ }+}+func TestApplyGuardsDownPreset(t *testing.T) {configPath := filepath.Join(t.TempDir(), "pinchtab", "config.json")t.Setenv("PINCHTAB_CONFIG", configPath)