High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: capability refusals say 'endpoint' for a feature label, so /storage cites stateExport PIN-031
Luigi Agosti3abbe17cd33f
diff --git a/internal/config/editor_get.go b/internal/config/editor_get.goindex f46196ed..87cfe264 100644--- a/internal/config/editor_get.go+++ b/internal/config/editor_get.go@@ -330,6 +330,8 @@ func getSecurityField(s *SecurityConfig, field string) (string, error) {return formatBoolPtr(s.AllowDownload), nilcase "allowCookies":return formatBoolPtr(s.AllowCookies), nil+ case "allowStateExport":+ return formatBoolPtr(s.AllowStateExport), nilcase "allowNetworkIntercept":return formatBoolPtr(s.AllowNetworkIntercept), nilcase "allowFileScheme":diff --git a/internal/config/editor_get_test.go b/internal/config/editor_get_test.goindex 895b3992..2d434905 100644--- a/internal/config/editor_get_test.go+++ b/internal/config/editor_get_test.go@@ -49,6 +49,7 @@ func TestGetConfigValue_RoundTrip(t *testing.T) {{"security.allowScreencast", "on", "true"},{"security.allowDownload", "off", "false"},{"security.allowCookies", "yes", "true"},+ {"security.allowStateExport", "true", "true"},{"security.downloadAllowedDomains", "pinchtab.com,*.pinchtab.com", "pinchtab.com,*.pinchtab.com"},{"security.downloadMaxBytes", "33554432", "33554432"},{"security.allowUpload", "yes", "true"},@@ -114,6 +115,7 @@ func TestGetConfigValue_NilPointerReturnsEmpty(t *testing.T) {"security.allowScreencast","security.allowDownload","security.allowCookies",+ "security.allowStateExport","security.downloadMaxBytes","security.allowUpload","security.enableActionGuards",diff --git a/internal/config/editor_set.go b/internal/config/editor_set.goindex e5606b1a..f8716d83 100644--- a/internal/config/editor_set.go+++ b/internal/config/editor_set.go@@ -529,6 +529,8 @@ func setSecurityField(s *SecurityConfig, field, value string) error {s.AllowDownload = &bcase "allowCookies":s.AllowCookies = &b+ case "allowStateExport":+ s.AllowStateExport = &bcase "allowUpload":s.AllowUpload = &bcase "allowNetworkIntercept":diff --git a/internal/config/editor_set_test.go b/internal/config/editor_set_test.goindex 08743e97..f89dcaa5 100644--- a/internal/config/editor_set_test.go+++ b/internal/config/editor_set_test.go@@ -158,6 +158,7 @@ func TestSetConfigValue_SecurityFields(t *testing.T) {{"security.allowScreencast", "false", func(fc *FileConfig) bool { return *fc.Security.AllowScreencast == false }, false},{"security.allowDownload", "on", func(fc *FileConfig) bool { return *fc.Security.AllowDownload == true }, false},{"security.allowCookies", "yes", func(fc *FileConfig) bool { return *fc.Security.AllowCookies == true }, false},+ {"security.allowStateExport", "true", func(fc *FileConfig) bool { return *fc.Security.AllowStateExport == true }, false},{"security.downloadAllowedDomains", "pinchtab.com, *.pinchtab.com", func(fc *FileConfig) bool {return len(fc.Security.DownloadAllowedDomains) == 2 &&fc.Security.DownloadAllowedDomains[0] == "pinchtab.com" &&diff --git a/internal/handlers/capability_refusal_test.go b/internal/handlers/capability_refusal_test.gonew file mode 100644index 00000000..be348fe4--- /dev/null+++ b/internal/handlers/capability_refusal_test.go@@ -0,0 +1,87 @@+package handlers++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/pinchtab/pinchtab/internal/config"+)++func capabilityRefusal(t *testing.T, method, path string, call func(*Handlers, http.ResponseWriter, *http.Request)) (string, string) {+ t.Helper()+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)+ w := httptest.NewRecorder()+ call(h, w, httptest.NewRequest(method, path, nil))++ if w.Code != http.StatusForbidden {+ t.Fatalf("%s %s: status = %d, want 403: %s", method, path, w.Code, w.Body.String())+ }+ var resp struct {+ Error string `json:"error"`+ Code string `json:"code"`+ Details map[string]any `json:"details"`+ }+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatalf("decode: %v (body=%s)", err, w.Body.String())+ }+ if setting, _ := resp.Details["setting"].(string); setting == "" {+ t.Fatalf("%s %s: refusal dropped details.setting: %v", method, path, resp.Details)+ }+ return resp.Error, resp.Code+}++// /storage is gated by the stateExport capability, so the old wording sent the+// reader looking for a "stateExport endpoint" that does not exist. /cookies is+// the shape whose label already matched, and must still read correctly.+func TestCapabilityRefusalNamesTheCapabilityAndKeepsTheCode(t *testing.T) {+ tests := []struct {+ name string+ method string+ path string+ call func(*Handlers, http.ResponseWriter, *http.Request)+ capability string+ setting string+ wantCode string+ }{+ {+ name: "storage is gated by another endpoint's capability",+ method: "GET",+ path: "/storage",+ call: func(h *Handlers, w http.ResponseWriter, r *http.Request) { h.HandleStorage(w, r) },+ capability: "stateExport",+ setting: "security.allowStateExport",+ wantCode: "state_export_disabled",+ },+ {+ name: "cookies label already matched its endpoint",+ method: "GET",+ path: "/cookies",+ call: func(h *Handlers, w http.ResponseWriter, r *http.Request) { h.HandleGetCookies(w, r) },+ capability: "cookies",+ setting: "security.allowCookies",+ wantCode: "cookies_disabled",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ message, code := capabilityRefusal(t, tt.method, tt.path, tt.call)++ if code != tt.wantCode {+ t.Fatalf("code = %q, want %q", code, tt.wantCode)+ }+ if strings.Contains(message, tt.capability+" endpoint") {+ t.Fatalf("message calls the capability an endpoint: %q", message)+ }+ if !strings.Contains(message, tt.capability+" capability") {+ t.Fatalf("message does not name the required capability: %q", message)+ }+ if !strings.Contains(message, tt.setting) {+ t.Fatalf("message does not name the setting to change: %q", message)+ }+ })+ }+}diff --git a/internal/httpx/endpointlock.go b/internal/httpx/endpointlock.goindex 08519662..7e1a97c4 100644--- a/internal/httpx/endpointlock.go+++ b/internal/httpx/endpointlock.go@@ -5,15 +5,18 @@ import ("net/http")-// DisabledEndpointMessage returns a consistent message for locked endpoint families.-func DisabledEndpointMessage(feature, setting string) string {- return fmt.Sprintf("%s endpoint is disabled; enable %s in config to use this endpoint", feature, setting)+// DisabledEndpointMessage returns a consistent message for locked endpoint+// families. capability names the gated capability, which is not always the+// endpoint the caller asked for — /storage is gated by stateExport — so the+// sentence names the requirement instead of calling the label an endpoint.+func DisabledEndpointMessage(capability, setting string) string {+ return fmt.Sprintf("this endpoint requires the %s capability; enable %s in config to use it", capability, setting)}-// DisabledEndpointHandler returns a handler that reports a feature gate lock.-func DisabledEndpointHandler(feature, setting, code string) http.HandlerFunc {+// DisabledEndpointHandler returns a handler that reports a capability gate lock.+func DisabledEndpointHandler(capability, setting, code string) http.HandlerFunc {return func(w http.ResponseWriter, _ *http.Request) {- ErrorCode(w, http.StatusForbidden, code, DisabledEndpointMessage(feature, setting), false, map[string]any{+ ErrorCode(w, http.StatusForbidden, code, DisabledEndpointMessage(capability, setting), false, map[string]any{"setting": setting,"hint": fmt.Sprintf("Enable %s to use this feature.", setting),"remedy": fmt.Sprintf("pinchtab config set %s true", setting),diff --git a/internal/httpx/endpointlock_test.go b/internal/httpx/endpointlock_test.goindex a846376d..242f979d 100644--- a/internal/httpx/endpointlock_test.go+++ b/internal/httpx/endpointlock_test.go@@ -4,6 +4,7 @@ import ("encoding/json""net/http""net/http/httptest"+ "strings""testing")@@ -44,3 +45,65 @@ func TestDisabledEndpointHandlerIncludesHintAndRemedy(t *testing.T) {t.Fatalf("remedy = %q, want config set command", remedy)}}++// The label is the capability, not the endpoint: /storage is gated by+// stateExport, and /record/* by the screencast setting. Calling either label an+// "endpoint" sends the reader looking for a route that does not exist.+func TestDisabledEndpointMessageNamesTheCapabilityNotAnEndpoint(t *testing.T) {+ tests := []struct {+ name string+ capability string+ setting string+ }{+ {"capability named after another endpoint", "stateExport", "security.allowStateExport"},+ {"capability matching its own endpoint", "cookies", "security.allowCookies"},+ {"feature gated by a differently named setting", "recording", "security.allowScreencast"},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ msg := DisabledEndpointMessage(tt.capability, tt.setting)++ if strings.Contains(msg, tt.capability+" endpoint") {+ t.Fatalf("message calls the capability an endpoint: %q", msg)+ }+ if !strings.Contains(msg, tt.capability+" capability") {+ t.Fatalf("message does not name the required capability: %q", msg)+ }+ if !strings.Contains(msg, tt.setting) {+ t.Fatalf("message does not name the setting to change: %q", msg)+ }+ })+ }+}++func TestDisabledEndpointHandlerKeepsSettingHintAndRemedy(t *testing.T) {+ handler := DisabledEndpointHandler("stateExport", "security.allowStateExport", "state_export_disabled")++ w := httptest.NewRecorder()+ r, _ := http.NewRequest("GET", "/storage", nil)+ handler(w, r)++ var resp struct {+ Error string `json:"error"`+ Code string `json:"code"`+ Details map[string]any `json:"details"`+ }+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatalf("decode body: %v", err)+ }+ if resp.Code != "state_export_disabled" {+ t.Fatalf("code = %q, want state_export_disabled", resp.Code)+ }+ if strings.Contains(resp.Error, "stateExport endpoint") {+ t.Fatalf("error still describes a stateExport endpoint: %q", resp.Error)+ }+ for key, want := range map[string]string{+ "setting": "security.allowStateExport",+ "hint": "Enable security.allowStateExport to use this feature.",+ "remedy": "pinchtab config set security.allowStateExport true",+ } {+ if got, _ := resp.Details[key].(string); got != want {+ t.Fatalf("details[%q] = %q, want %q", key, got, want)+ }+ }+}