High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
test: the route-level capability refusal is unguarded end to end, so a setting drifting into a broken remedy reds nothing on the path that serves it (PIN-198)
Luigi Agosti0655b1551527
diff --git a/internal/strategy/routes.go b/internal/strategy/routes.goindex 5d1240ca..158b9e38 100644--- a/internal/strategy/routes.go+++ b/internal/strategy/routes.go@@ -7,15 +7,6 @@ import ("github.com/pinchtab/pinchtab/internal/routes")-// capabilitySetting returns the label, config key and disabled code a refused route-// answers with, from routes.Meta — the one owner. This front previously restated the whole-// table, which is how the same route came to answer two different codes depending on which-// front a client reached; the handler layer reads Meta, so this must too.-//-// The fallback only covers a capability the catalogue gates without describing, which-// CapabilityEndpoints cannot currently produce. It is deliberately not the old synthesis:-// that derived "security.allow"+cap and cap+"_disabled", which is wrong for every-// camelCase capability — the reason each one needed an explicit case in the first place.func capabilitySetting(cap routes.Capability) (feature, setting, code string) {if meta, ok := routes.Meta(cap); ok {return meta.Label, meta.Setting, meta.DisabledCode@@ -23,12 +14,6 @@ func capabilitySetting(cap routes.Capability) (feature, setting, code string) {return string(cap), "", ""}-// RegisterShorthandRoutes registers all shorthand proxy routes on the mux,-// binding them to the given handler. It also registers capability-gated-// routes (evaluate, download, upload, screencast, macro) using the-// orchestrator's security settings.-//-// Routes are sourced from the shared routes.Core() catalogue.func RegisterShorthandRoutes(mux *http.ServeMux, orch *orchestrator.Orchestrator, handler http.HandlerFunc) {wrapped := orch.WrapShorthand(handler)diff --git a/internal/strategy/simple/simple_test.go b/internal/strategy/simple/simple_test.goindex a652382e..bcc2e2ea 100644--- a/internal/strategy/simple/simple_test.go+++ b/internal/strategy/simple/simple_test.go@@ -6,13 +6,16 @@ import ("io""net/http""net/http/httptest"+ "reflect""strings""sync""testing""github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/httpx""github.com/pinchtab/pinchtab/internal/orchestrator""github.com/pinchtab/pinchtab/internal/proxy"+ "github.com/pinchtab/pinchtab/internal/routes")type mockRunner struct {@@ -191,35 +194,85 @@ func TestStrategy_HandleTabs_NoInstances(t *testing.T) {}}-func TestStrategy_RegisterRoutes_LocksSensitiveShorthandRoutes(t *testing.T) {+func lockedMux(t *testing.T) *http.ServeMux {+ t.Helper()orch := orchestrator.NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})orch.ApplyRuntimeConfig(&config.RuntimeConfig{})s := &Strategy{orch: orch}mux := http.NewServeMux()s.RegisterRoutes(mux)+ return mux+}- tests := []struct {- method string- path string- body string- setting string- }{- {method: "POST", path: "/evaluate", body: `{"expression":"1+1"}`, setting: "security.allowEvaluate"},- {method: "GET", path: "/cookies", setting: "security.allowCookies"},- {method: "DELETE", path: "/cookies", setting: "security.allowCookies"},+func refuse(t *testing.T, mux *http.ServeMux, ep routes.Endpoint) map[string]any {+ t.Helper()+ req := httptest.NewRequest(ep.Method, ep.Path, strings.NewReader("{}"))+ rec := httptest.NewRecorder()+ mux.ServeHTTP(rec, req)++ if rec.Code != http.StatusForbidden {+ t.Fatalf("%s: got %d, want 403: %s", ep.Route(), rec.Code, rec.Body.String())+ }+ var body map[string]any+ if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {+ t.Fatalf("%s: refusal is not JSON: %v: %s", ep.Route(), err, rec.Body.String())}+ return body+}- for _, tt := range tests {- req := httptest.NewRequest(tt.method, tt.path, strings.NewReader(tt.body))- rec := httptest.NewRecorder()- mux.ServeHTTP(rec, req)+func TestStrategy_RegisterRoutes_RefusalsComeFromTheCapabilityOwner(t *testing.T) {+ gated := routes.CapabilityEndpoints()+ if len(gated) == 0 {+ t.Fatal("no capability-gated endpoints; this guard would pass vacuously")+ }+ mux := lockedMux(t)++ seen := 0+ for cap, eps := range gated {+ meta, ok := routes.Meta(cap)+ if !ok {+ t.Errorf("capability %q gates routes but routes.Meta does not describe it", cap)+ continue+ }+ want := map[string]any{+ "code": meta.DisabledCode,+ "error": httpx.DisabledEndpointMessage(meta.Label, meta.Setting),+ "details": httpx.DisabledEndpointDetails(meta.Setting),+ }+ if len(eps) == 0 {+ t.Errorf("capability %q yields no endpoints, so it is uncovered here", cap)+ }+ for _, ep := range eps {+ got := refuse(t, mux, ep)+ delete(got, "retryable")+ if !reflect.DeepEqual(got, want) {+ t.Errorf("%s refusal = %#v, want %#v", ep.Route(), got, want)+ }+ seen+++ }+ }+ if seen < len(gated) {+ t.Fatalf("checked %d endpoints across %d capabilities; every capability must contribute at least one", seen, len(gated))+ }+}- if rec.Code != 403 {- t.Fatalf("%s %s expected 403, got %d: %s", tt.method, tt.path, rec.Code, rec.Body.String())+func TestStrategy_RegisterRoutes_RefusedSettingIsASettableConfigKey(t *testing.T) {+ gated := routes.CapabilityEndpoints()+ if len(gated) == 0 {+ t.Fatal("no capability-gated endpoints; this guard would pass vacuously")+ }+ mux := lockedMux(t)++ for cap, eps := range gated {+ details, _ := refuse(t, mux, eps[0])["details"].(map[string]any)+ setting, _ := details["setting"].(string)+ if setting == "" {+ t.Errorf("capability %q refuses without naming a setting, so its remedy cannot be followed", cap)+ continue}- if !strings.Contains(rec.Body.String(), tt.setting) {- t.Fatalf("%s %s expected lock response to mention %s, got %s", tt.method, tt.path, tt.setting, rec.Body.String())+ if err := config.SetConfigValue(&config.FileConfig{}, setting, "true"); err != nil {+ t.Errorf("capability %q refuses citing %q, which the config editor rejects: %v", cap, setting, err)}}}