Skip to content

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

fix(api): batch POST /actions refuses stray/mistargeted query params before running, matching singular /action (PIN-409)

Luigi Agosti51bba5cba6fc
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index d244ecc2..36653a3b 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -284,13 +284,28 @@ func actionRequestJSONKeys() map[string]struct{} {
return keys
}
-// unknownQueryFields names every supplied parameter the request type does not declare, sorted
-// so the refusal reads the same on every run. Presence follows the decoder's own rule — a
-// non-empty value — so ?_= is absent rather than an unknown request.
-func unknownQueryFields(q url.Values) []string {
+// batchActionQueryKeys is the accepted query-parameter set for the batch POST
+// /actions endpoint. The batch takes its steps in the JSON body, not as per-action
+// query fields, so only the tab-context and browser-routing keys are meaningful
+// here — the same ones the routing layers read from the query. Every per-step
+// action field (kind, ref, selector, …) is a stray parameter on this route and is
+// refused, as is a mistargeted ?tab= or any typo. It is a subset of
+// actionQueryKeys, pinned by TestBatchActionQueryKeysAreActionParameters.
+var batchActionQueryKeys = map[string]struct{}{
+ "tabId": {},
+ "browser": {},
+ "owner": {},
+ "vocab": {},
+ "vocabTab": {},
+}
+
+// unknownQueryFields names every supplied parameter not in the known set, sorted
+// so the refusal reads the same on every run. Presence follows the decoder's own
+// rule — a non-empty value — so ?_= is absent rather than an unknown request.
+func unknownQueryFields(q url.Values, known map[string]struct{}) []string {
var unknown []string
for key := range q {
- if _, known := actionQueryKeys[key]; known {
+ if _, ok := known[key]; ok {
continue
}
if strings.TrimSpace(q.Get(key)) == "" {
@@ -360,7 +375,7 @@ func decodeActionRequest(w http.ResponseWriter, r *http.Request) (bridge.ActionR
httpx.Error(w, 400, fmt.Errorf("%s cannot be sent as query parameters and would be silently dropped; send this as POST /action with a JSON body", strings.Join(offenders, ", ")))
return bridge.ActionRequest{}, false
}
- if unknown := unknownQueryFields(q); len(unknown) > 0 {
+ if unknown := unknownQueryFields(q, actionQueryKeys); len(unknown) > 0 {
httpx.Error(w, 400, unknownQueryFieldsError(unknown))
return bridge.ActionRequest{}, false
}
@@ -760,6 +775,15 @@ func (h *Handlers) writeUnknownActionKind(w http.ResponseWriter, kind string) {
}
func (h *Handlers) HandleActions(w http.ResponseWriter, r *http.Request) {
+ // Refuse a stray or mistargeted query parameter before any step runs, matching
+ // the singular /action: the batch takes its steps and tab from the JSON body, so
+ // a ?tab= (or any unknown key) would be silently dropped and the batch would run
+ // on the current tab — a silent wrong-tab write.
+ if unknown := unknownQueryFields(r.URL.Query(), batchActionQueryKeys); len(unknown) > 0 {
+ httpx.Error(w, 400, unknownQueryFieldsError(unknown))
+ return
+ }
+
var req actionsRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil {
httpx.Error(w, 400, fmt.Errorf("decode: %w", err))
diff --git a/internal/handlers/actions_test.go b/internal/handlers/actions_test.go
index 659b363a..d75403f3 100644
--- a/internal/handlers/actions_test.go
+++ b/internal/handlers/actions_test.go
@@ -251,6 +251,90 @@ func TestHandleMacro_FollowsAutoSwitchedTab(t *testing.T) {
}
}
+// A stray or mistargeted query parameter (the common ?tab= mistake, or any unknown
+// key) must be refused 400 before any step runs, matching the singular /action —
+// otherwise the batch silently runs its writes on the current tab. A clean batch,
+// with no query or only routing keys, is unaffected.
+func TestHandleActions_RejectsStrayQueryParam(t *testing.T) {
+ body := `{"actions":[{"kind":"click"},{"kind":"type","text":"after"}]}`
+
+ t.Run("stray tab is refused before execution", func(t *testing.T) {
+ b := &autoSwitchActionBridge{}
+ h := New(b, &config.RuntimeConfig{ActionTimeout: time.Second}, nil, nil, nil)
+ req := httptest.NewRequest("POST", "/actions?tab=tab_fixture", bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+
+ h.HandleActions(w, req)
+
+ if w.Code != 400 {
+ t.Fatalf("expected 400 for a stray ?tab=, got %d: %s", w.Code, w.Body.String())
+ }
+ var resp map[string]string
+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
+ t.Fatalf("decode: %v", err)
+ }
+ if !strings.Contains(resp["error"], "tab") || !strings.Contains(resp["error"], "silently dropped") {
+ t.Errorf("error should name the offender and the drop: %q", resp["error"])
+ }
+ if len(b.actionTabs) != 0 {
+ t.Errorf("the batch executed %d steps despite the 400; no step may run on a rejected request: %v", len(b.actionTabs), b.actionTabs)
+ }
+ })
+
+ t.Run("unknown key is refused", func(t *testing.T) {
+ b := &autoSwitchActionBridge{}
+ h := New(b, &config.RuntimeConfig{ActionTimeout: time.Second}, nil, nil, nil)
+ req := httptest.NewRequest("POST", "/actions?bogusparam=1", bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+
+ h.HandleActions(w, req)
+
+ if w.Code != 400 {
+ t.Fatalf("expected 400 for an unknown query key, got %d: %s", w.Code, w.Body.String())
+ }
+ })
+
+ t.Run("no query executes normally", func(t *testing.T) {
+ b := &autoSwitchActionBridge{}
+ h := New(b, &config.RuntimeConfig{ActionTimeout: time.Second}, nil, nil, nil)
+ req := httptest.NewRequest("POST", "/actions", bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+
+ h.HandleActions(w, req)
+
+ if w.Code != 200 {
+ t.Fatalf("expected 200 for a clean batch, got %d: %s", w.Code, w.Body.String())
+ }
+ })
+
+ t.Run("routing key is accepted", func(t *testing.T) {
+ b := &autoSwitchActionBridge{}
+ h := New(b, &config.RuntimeConfig{ActionTimeout: time.Second}, nil, nil, nil)
+ req := httptest.NewRequest("POST", "/actions?browser=chrome", bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+
+ h.HandleActions(w, req)
+
+ if w.Code != 200 {
+ t.Fatalf("expected 200 for a batch with only a routing query key, got %d: %s", w.Code, w.Body.String())
+ }
+ })
+}
+
+// The batch accepted set must stay a subset of the action-family vocabulary, so a
+// key added here cannot silently permit something /action itself would refuse.
+func TestBatchActionQueryKeysAreActionParameters(t *testing.T) {
+ for key := range batchActionQueryKeys {
+ if _, ok := actionQueryKeys[key]; !ok {
+ t.Errorf("batchActionQueryKeys has %q, which is not an /action parameter; it would accept a key the singular route refuses", key)
+ }
+ }
+}
+
func TestHandleActions_ResponseIncludesRoute(t *testing.T) {
b := &autoSwitchActionBridge{}
h := New(b, &config.RuntimeConfig{ActionTimeout: time.Second}, nil, nil, nil)
@@ -987,7 +1071,7 @@ func TestActionQueryAcceptsEveryFieldTheRequestTypeDeclares(t *testing.T) {
if key == "" || key == "-" {
continue
}
- if unknown := unknownQueryFields(url.Values{key: []string{"1"}}); len(unknown) > 0 {
+ if unknown := unknownQueryFields(url.Values{key: []string{"1"}}, actionQueryKeys); len(unknown) > 0 {
t.Errorf("%q is declared by ActionRequest and the GET form calls it unknown; the allow-list is not derived from the type", key)
}
checked++
@@ -995,7 +1079,7 @@ func TestActionQueryAcceptsEveryFieldTheRequestTypeDeclares(t *testing.T) {
if checked == 0 {
t.Fatal("no field was checked; the guard is not reading the request type")
}
- if unknown := unknownQueryFields(url.Values{"modifers": []string{"8"}}); len(unknown) != 1 {
+ if unknown := unknownQueryFields(url.Values{"modifers": []string{"8"}}, actionQueryKeys); len(unknown) != 1 {
t.Fatalf("a key the type does not declare was not called unknown: %v — the allow-list accepts everything", unknown)
}
}