Skip to content

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

fix: POST /record/stop and GET /record/status have no capability guard on the bridge mux, so screencast disabled still stops recordings and reports recording state (PIN-244)

Luigi Agostiaadd3566220d
diff --git a/internal/handlers/binary_export_test.go b/internal/handlers/binary_export_test.go
index 222e4d99..3604a1e8 100644
--- a/internal/handlers/binary_export_test.go
+++ b/internal/handlers/binary_export_test.go
@@ -271,7 +271,9 @@ func TestSnapshotFileBranchRoutesTheGeneratedNameThroughTheUniqueHelper(t *testi
// started writing into the package directory.
func TestARefusedRecordStopLeavesNoReservedFile(t *testing.T) {
stateDir := t.TempDir()
- h := New(&mockBridge{}, &config.RuntimeConfig{StateDir: stateDir}, nil, nil, nil)
+ // Screencast on: this pins the reserve-then-release path, which is only reachable
+ // once the capability guard has let the request through.
+ h := New(&mockBridge{}, &config.RuntimeConfig{StateDir: stateDir, AllowScreencast: true}, nil, nil, nil)
req := httptest.NewRequest("POST", "/record/stop", nil)
w := httptest.NewRecorder()
diff --git a/internal/handlers/capability_refusal_test.go b/internal/handlers/capability_refusal_test.go
index 5e152a62..fa1307c9 100644
--- a/internal/handlers/capability_refusal_test.go
+++ b/internal/handlers/capability_refusal_test.go
@@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"os"
+ "path/filepath"
"strings"
"testing"
@@ -407,3 +408,111 @@ func TestTheConfigWriteAloneDoesNotClearTheRefusal(t *testing.T) {
t.Fatalf("code = %q; the running config picked the edit up without a restart, so the remedy should stop naming one", code)
}
}
+
+// recordRoutes is the whole record family, driven together on purpose: all three are
+// CapScreencast in the catalogue, so the orchestrator front locks all three, and the
+// asymmetry this pins is that only start once carried the in-handler guard the bridge
+// mux relies on. Asserting them as a set is what stops a fourth route arriving guarded
+// on one front only.
+//
+// If this is ever widened into a guard-PRESENCE census over the package, it must follow
+// ensureCookiesEnabled and ensureStateExportEnabled as well: those two wrappers delegate
+// to writeCapabilityDisabled, so grepping for the owner alone reports the cookies and
+// state families — eleven routes — as unguarded when they are not.
+var recordRoutes = []struct {
+ method string
+ path string
+ body string
+}{
+ {http.MethodPost, "/record/start", `{}`},
+ {http.MethodPost, "/record/stop", `{}`},
+ {http.MethodGet, "/record/status", ``},
+}
+
+func recordMux(t *testing.T, allowScreencast bool, stateDir string) *http.ServeMux {
+ t.Helper()
+
+ h := New(&mockBridge{}, &config.RuntimeConfig{AllowScreencast: allowScreencast, StateDir: stateDir}, nil, nil, nil)
+ mux := http.NewServeMux()
+ h.RegisterRoutes(mux, func() {})
+ return mux
+}
+
+func serveRecord(mux *http.ServeMux, method, path, body string) *httptest.ResponseRecorder {
+ w := httptest.NewRecorder()
+ var r *http.Request
+ if body == "" {
+ r = httptest.NewRequest(method, path, nil)
+ } else {
+ r = httptest.NewRequest(method, path, strings.NewReader(body))
+ r.Header.Set("Content-Type", "application/json")
+ }
+ mux.ServeHTTP(w, r)
+ return w
+}
+
+// The bridge mux registers these three handlers directly, with no route-level lock, so an
+// in-handler guard is the only enforcement on that surface — and stop and status had none
+// while start refused. Wire change this pins: both move from 200/400 to 403 when
+// screencast is disabled.
+func TestEveryRecordRouteRefusesWithTheSharedScreencastCodeThroughTheMux(t *testing.T) {
+ mux := recordMux(t, false, t.TempDir())
+ screencast, _ := routes.Meta(routes.CapScreencast)
+
+ for _, route := range recordRoutes {
+ t.Run(route.method+" "+route.path, func(t *testing.T) {
+ w := serveRecord(mux, route.method, route.path, route.body)
+ _, code, details := decodeRefusal(t, route.method+" "+route.path, w)
+
+ if code != screencast.DisabledCode {
+ t.Errorf("code = %q, want %q — one capability must not answer two ways across its own routes", code, screencast.DisabledCode)
+ }
+ if got, _ := details["remedy"].(string); got != remedyFor(screencast.Setting) {
+ t.Errorf("remedy = %q, want the shared one", got)
+ }
+ })
+ }
+}
+
+// Distinct from TestARefusedRecordStopLeavesNoReservedFile, which pins the RELEASE path: a
+// stop refused for having no active recording reserves a name and gives it back. This one
+// pins that a capability-refused stop never reserves at all.
+//
+// Stop reserved a real output path BEFORE discovering there was no active recording, so a
+// disabled capability drove a create-then-remove in a server-controlled directory. The
+// guard has to sit above that work, which only the directory can show — a 403 alone is
+// also produced by a guard placed after the reservation.
+func TestAStopRefusedByTheCapabilityGuardNeverReservesAFile(t *testing.T) {
+ stateDir := t.TempDir()
+
+ if w := serveRecord(recordMux(t, false, stateDir), http.MethodPost, "/record/stop", `{}`); w.Code != http.StatusForbidden {
+ t.Fatalf("status = %d, want 403: %s", w.Code, w.Body.String())
+ }
+
+ entries, err := os.ReadDir(filepath.Join(stateDir, "recordings"))
+ if err == nil && len(entries) > 0 {
+ names := make([]string, 0, len(entries))
+ for _, entry := range entries {
+ names = append(names, entry.Name())
+ }
+ t.Errorf("a refused stop left %v in the recordings directory; the guard must sit above recordingsOutputPath, not after it", names)
+ }
+}
+
+// The positive control, without which a guard that refuses everything would pass every
+// assertion above. With screencast enabled none of the three may answer with the
+// capability refusal — what they answer instead is browser/state dependent and not this
+// test's business.
+func TestRecordRoutesAreNotRefusedWhenScreencastIsEnabled(t *testing.T) {
+ mux := recordMux(t, true, t.TempDir())
+ screencast, _ := routes.Meta(routes.CapScreencast)
+
+ for _, route := range recordRoutes {
+ t.Run(route.method+" "+route.path, func(t *testing.T) {
+ w := serveRecord(mux, route.method, route.path, route.body)
+ if strings.Contains(w.Body.String(), screencast.DisabledCode) {
+ t.Errorf("status = %d body = %s; screencast is enabled, so the capability guard must not fire", w.Code, w.Body.String())
+ }
+ })
+ }
+}
diff --git a/internal/handlers/record_handlers.go b/internal/handlers/record_handlers.go
index d24ee913..e6bdd018 100644
--- a/internal/handlers/record_handlers.go
+++ b/internal/handlers/record_handlers.go
@@ -104,6 +104,11 @@ func (h *Handlers) HandleRecordStart(w http.ResponseWriter, r *http.Request) {
// and the endpoint returns the path immediately. If discard is true, frames are
// dropped without encoding. Use /record/status to check encoding progress.
func (h *Handlers) HandleRecordStop(w http.ResponseWriter, r *http.Request) {
+ if !h.Config.AllowScreencast {
+ h.writeCapabilityDisabled(w, routes.CapScreencast)
+ return
+ }
+
var req struct {
Discard bool `json:"discard"`
}
@@ -171,6 +176,10 @@ func (h *Handlers) recordingsOutputPath() (string, error) {
// HandleRecordStatus returns the current recording status.
func (h *Handlers) HandleRecordStatus(w http.ResponseWriter, r *http.Request) {
+ if !h.Config.AllowScreencast {
+ h.writeCapabilityDisabled(w, routes.CapScreencast)
+ return
+ }
httpx.JSON(w, 200, h.recorder.status())
}
diff --git a/internal/handlers/record_test.go b/internal/handlers/record_test.go
index a1af2436..69ad446c 100644
--- a/internal/handlers/record_test.go
+++ b/internal/handlers/record_test.go
@@ -112,7 +112,7 @@ func TestRecorderStart_AlreadyRecording(t *testing.T) {
func TestHandleRecordStop_NoRecording(t *testing.T) {
// A real StateDir: the stop path reserves its output name on disk, so an empty
// StateDir would write into the package directory.
- cfg := &config.RuntimeConfig{StateDir: t.TempDir()}
+ cfg := &config.RuntimeConfig{StateDir: t.TempDir(), AllowScreencast: true}
h := New(&mockBridge{}, cfg, nil, nil, nil)
req := httptest.NewRequest("POST", "/record/stop", nil)
@@ -125,7 +125,7 @@ func TestHandleRecordStop_NoRecording(t *testing.T) {
}
func TestHandleRecordStatus_Inactive(t *testing.T) {
- cfg := &config.RuntimeConfig{}
+ cfg := &config.RuntimeConfig{AllowScreencast: true}
h := New(&mockBridge{}, cfg, nil, nil, nil)
req := httptest.NewRequest("GET", "/record/status", nil)