High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(api): POST /action refuses stray/mistargeted query params before running, matching GET /action and the batch route (PIN-416)
Luigi Agosti91b3f9c03b5f
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.goindex f00f196e..4ada485f 100644--- a/internal/handlers/actions.go+++ b/internal/handlers/actions.go@@ -298,6 +298,19 @@ var batchActionQueryKeys = map[string]struct{}{"owner": {},}+// postActionQueryKeys is the accepted query-parameter set for POST /action. The body+// is authoritative, so the handler reads nothing else from the query except owner+// (resolveOwner) and browser — MCP's routedPathWithBody appends ?browser= to every+// POST /action and the multi-instance router reads it to pick the instance, so+// refusing it would 400 a browser-targeted MCP action. Every other query key would be+// silently dropped — a mistargeted ?tab=/?tabId= running on the current tab, a typo —+// so it is refused with the same message the batch route uses. Pinned as a subset of+// actionQueryKeys by TestPostActionQueryKeysAreActionParameters.+var postActionQueryKeys = map[string]struct{}{+ "owner": {},+ "browser": {},+}+// 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.@@ -451,6 +464,15 @@ func decodeActionRequest(w http.ResponseWriter, r *http.Request) (bridge.ActionR}return req, true}+ // POST takes its parameters from the JSON body, so a query parameter here is+ // silently dropped — a mistargeted ?tab=/?tabId= would run on the current tab, the+ // same wrong-tab-write hazard the batch route refuses. Refuse any stray key before+ // the action runs, matching the batch route's message and its owner/browser+ // exceptions (browser is what MCP appends and the multi-instance router reads).+ if unknown := unknownQueryFields(r.URL.Query(), postActionQueryKeys); len(unknown) > 0 {+ httpx.Error(w, 400, unknownQueryFieldsError(unknown))+ return bridge.ActionRequest{}, false+ }if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil {httpx.Error(w, 400, fmt.Errorf("decode: %w", err))return bridge.ActionRequest{}, falsediff --git a/internal/handlers/actions_test.go b/internal/handlers/actions_test.goindex ccdeebe7..a6d8a6c6 100644--- a/internal/handlers/actions_test.go+++ b/internal/handlers/actions_test.go@@ -358,6 +358,76 @@ func TestHandleActions_RejectsStrayQueryParam(t *testing.T) {})}+// PIN-416: POST /action reads its parameters from the JSON body, so a stray query+// parameter is silently dropped — the same wrong-tab-write hazard the batch route+// refuses. It must be refused 400 before the action runs, while the two keys that are+// legitimately sent on the query (owner, and browser which MCP appends) still pass.+func TestHandleAction_POST_RejectsStrayQueryParam(t *testing.T) {+ body := `{"kind":"click","nodeId":42,"tabId":"tab1"}`++ postAction := func(t *testing.T, query string) *httptest.ResponseRecorder {+ t.Helper()+ h := New(&mockBridge{}, &config.RuntimeConfig{ActionTimeout: time.Second}, nil, nil, nil)+ url := "/action"+ if query != "" {+ url += "?" + query+ }+ req := httptest.NewRequest("POST", url, bytes.NewReader([]byte(body)))+ req.Header.Set("Content-Type", "application/json")+ w := httptest.NewRecorder()+ h.HandleAction(w, req)+ return w+ }++ for _, tc := range []struct{ name, query string }{+ {"stray tab", "tab=tab_fixture"},+ {"mistargeted tabId", "tabId=tab_other"},+ {"unknown key", "bogusparam=1"},+ } {+ t.Run(tc.name+" is refused", func(t *testing.T) {+ w := postAction(t, tc.query)+ if w.Code != 400 {+ t.Fatalf("expected 400 for ?%s, got %d: %s", tc.query, w.Code, w.Body.String())+ }+ var resp map[string]string+ if json.Unmarshal(w.Body.Bytes(), &resp) == nil && !strings.Contains(resp["error"], "silently dropped") {+ t.Errorf("error should carry the drop guidance: %q", resp["error"])+ }+ })+ }++ t.Run("a clean POST is unaffected", func(t *testing.T) {+ if w := postAction(t, ""); w.Code == 400 {+ t.Fatalf("a clean POST /action was refused: %s", w.Body.String())+ }+ })++ // browser (MCP's routedPathWithBody appends ?browser=) and owner (resolveOwner)+ // are the keys legitimately carried on a POST query; refusing them would 400 a+ // browser-targeted MCP action, so they must pass the guard.+ for _, tc := range []struct{ name, query string }{+ {"browser (MCP appends this)", "browser=cloak"},+ {"owner", "owner=agent"},+ } {+ t.Run(tc.name+" is accepted", func(t *testing.T) {+ if w := postAction(t, tc.query); w.Code == 400 {+ t.Fatalf("?%s must not be refused on POST /action: %s", tc.query, w.Body.String())+ }+ })+ }+}++// The POST accepted set must stay a subset of the action-family vocabulary, so a key+// added here cannot silently permit something the query allow-list would name as not+// a parameter of /action.+func TestPostActionQueryKeysAreActionParameters(t *testing.T) {+ for key := range postActionQueryKeys {+ if _, ok := actionQueryKeys[key]; !ok {+ t.Errorf("postActionQueryKeys has %q, which is not an /action parameter", key)+ }+ }+}+// 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) {