From e580465745ce2fd204292d941ce7d4b67336bcb4 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 08:55:57 -0500 Subject: [PATCH] fix(mcpx): reject invalid /v1/interactions score bounds with 400 (#2223) A non-numeric minScore/maxScore was silently dropped (200, unfiltered list); NaN/Inf parsed successfully and reached SQLite as NULL (200, empty list). Both read as real answers on the feedback-review surface. The handler now 400s naming the parameter on a parse error or non-finite value; finite bounds are unchanged. The same silent-drop shape in limit/offset gets the same 400 rule (triage pin, task 3372). Handler tests cover abc/NaN/Inf/+Inf/-Inf/1e999 on both bounds, bad limit/offset, and the finite control cases. --- pkgs/mcpx/internal/daemon/routes_feedback.go | 51 +++++--- .../internal/daemon/routes_feedback_test.go | 112 ++++++++++++++++++ 2 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 pkgs/mcpx/internal/daemon/routes_feedback_test.go diff --git a/pkgs/mcpx/internal/daemon/routes_feedback.go b/pkgs/mcpx/internal/daemon/routes_feedback.go index 602d2c7562..63d336e431 100644 --- a/pkgs/mcpx/internal/daemon/routes_feedback.go +++ b/pkgs/mcpx/internal/daemon/routes_feedback.go @@ -3,6 +3,7 @@ package daemon import ( "encoding/json" "fmt" + "math" "net/http" "strconv" "strings" @@ -92,25 +93,47 @@ func (s *Server) handleInteractionsList(w http.ResponseWriter, r *http.Request) hasFb := hasFbStr == "1" || strings.ToLower(hasFbStr) == "true" filter.HasFeedback = &hasFb } - if minStr := q.Get("minScore"); minStr != "" { - if val, err := strconv.ParseFloat(minStr, 64); err == nil { - filter.MinScore = &val + // Invalid query values are rejected with 400 naming the parameter + // (#2223): silently dropping a bound made a typo'd minScore return the + // unfiltered list with 200, and a NaN/Inf bound (which ParseFloat + // accepts) reached SQLite as NULL and matched nothing — both read as + // real answers. The same silent-drop shape is fixed for limit/offset. + parseScoreBound := func(name string) (*float64, bool) { + raw := q.Get(name) + if raw == "" { + return nil, true } - } - if maxStr := q.Get("maxScore"); maxStr != "" { - if val, err := strconv.ParseFloat(maxStr, 64); err == nil { - filter.MaxScore = &val + val, err := strconv.ParseFloat(raw, 64) + if err != nil || math.IsNaN(val) || math.IsInf(val, 0) { + writeErr(w, http.StatusBadRequest, fmt.Errorf("invalid %s %q: must be a finite number", name, raw)) + return nil, false } + return &val, true } - if limitStr := q.Get("limit"); limitStr != "" { - if l, err := strconv.Atoi(limitStr); err == nil { - filter.Limit = l + parseIntParam := func(name string) (int, bool) { + raw := q.Get(name) + if raw == "" { + return 0, true } - } - if offsetStr := q.Get("offset"); offsetStr != "" { - if o, err := strconv.Atoi(offsetStr); err == nil { - filter.Offset = o + n, err := strconv.Atoi(raw) + if err != nil { + writeErr(w, http.StatusBadRequest, fmt.Errorf("invalid %s %q: must be an integer", name, raw)) + return 0, false } + return n, true + } + var ok bool + if filter.MinScore, ok = parseScoreBound("minScore"); !ok { + return + } + if filter.MaxScore, ok = parseScoreBound("maxScore"); !ok { + return + } + if filter.Limit, ok = parseIntParam("limit"); !ok { + return + } + if filter.Offset, ok = parseIntParam("offset"); !ok { + return } list, err := fbStore.List(filter) diff --git a/pkgs/mcpx/internal/daemon/routes_feedback_test.go b/pkgs/mcpx/internal/daemon/routes_feedback_test.go new file mode 100644 index 0000000000..f5647220bd --- /dev/null +++ b/pkgs/mcpx/internal/daemon/routes_feedback_test.go @@ -0,0 +1,112 @@ +package daemon + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/dezren39/mcpx/internal/logstore" +) + +// setupInteractionsListServer builds a Server backed by a real feedback +// store seeded with three interactions scored 0.2 / 0.5 / 0.9 (#2223). +func setupInteractionsListServer(t *testing.T) *Server { + t.Helper() + fs, err := logstore.NewFeedbackStore(filepath.Join(t.TempDir(), "feedback.db")) + if err != nil { + t.Fatalf("NewFeedbackStore: %v", err) + } + t.Cleanup(func() { _ = fs.Close() }) + for i, score := range []float64{0.2, 0.5, 0.9} { + traceID := fmt.Sprintf("trc-2223-%d", i) + rec := logstore.InteractionRecord{ + TraceID: traceID, + Timestamp: time.Now(), + Source: "proxy", + Input: "find bad responses", + Output: "an answer", + } + if err := fs.RecordInteraction(rec); err != nil { + t.Fatalf("RecordInteraction: %v", err) + } + if err := fs.ProvideFeedback(logstore.FeedbackParams{TraceID: traceID, Score: score}); err != nil { + t.Fatalf("ProvideFeedback: %v", err) + } + } + return &Server{reg: &Registry{feedbackStore: fs}} +} + +func getInteractions(t *testing.T, s *Server, query string) *httptest.ResponseRecorder { + t.Helper() + req := httptest.NewRequest(http.MethodGet, "/v1/interactions?"+query, nil) + rec := httptest.NewRecorder() + s.handleInteractionsList(rec, req) + return rec +} + +// TestInteractionsListRejectsInvalidScoreBounds pins #2223: a non-numeric, +// NaN, infinite, or out-of-range minScore/maxScore must be a 400 naming the +// parameter — never a silently unfiltered (or silently empty) 200. +func TestInteractionsListRejectsInvalidScoreBounds(t *testing.T) { + s := setupInteractionsListServer(t) + for _, param := range []string{"minScore", "maxScore"} { + for _, bad := range []string{"abc", "NaN", "Inf", "+Inf", "-Inf", "1e999"} { + rec := getInteractions(t, s, param+"="+bad) + if rec.Code != http.StatusBadRequest { + t.Errorf("%s=%s: status = %d, want 400 (body %s)", param, bad, rec.Code, rec.Body.String()) + continue + } + if !strings.Contains(rec.Body.String(), param) { + t.Errorf("%s=%s: 400 body %q does not name the parameter", param, bad, rec.Body.String()) + } + } + } +} + +// TestInteractionsListRejectsInvalidLimitOffset pins the same 400 rule for +// this handler's limit/offset, which had the same silent-drop shape. +func TestInteractionsListRejectsInvalidLimitOffset(t *testing.T) { + s := setupInteractionsListServer(t) + for _, query := range []string{"limit=abc", "offset=1.5"} { + rec := getInteractions(t, s, query) + if rec.Code != http.StatusBadRequest { + t.Errorf("%s: status = %d, want 400 (body %s)", query, rec.Code, rec.Body.String()) + } + } +} + +// TestInteractionsListFiniteScoreBoundsUnchanged is the control: valid +// finite bounds behave exactly as before the 400 rule existed. +func TestInteractionsListFiniteScoreBoundsUnchanged(t *testing.T) { + s := setupInteractionsListServer(t) + cases := []struct { + query string + want int + }{ + {"", 3}, + {"minScore=0.4", 2}, + {"maxScore=0.4", 1}, + {"minScore=0.4&maxScore=0.6", 1}, + {"limit=2", 2}, + {"limit=2&offset=2", 1}, + } + for _, tc := range cases { + rec := getInteractions(t, s, tc.query) + if rec.Code != http.StatusOK { + t.Errorf("%q: status = %d, want 200 (body %s)", tc.query, rec.Code, rec.Body.String()) + continue + } + var list []logstore.InteractionRecord + if err := json.NewDecoder(rec.Body).Decode(&list); err != nil { + t.Fatalf("%q: decode list: %v", tc.query, err) + } + if len(list) != tc.want { + t.Errorf("%q: got %d rows, want %d", tc.query, len(list), tc.want) + } + } +}