Просмотр исходного кода

fix(api): reject invalid scope IDs on feed and category entry endpoints

On GET /v1/feeds/{feedID}/entries and /v1/categories/{categoryID}/entries,
an invalid feed_id or category_id query parameter was silently ignored,
so /v1/categories/10/entries?feed_id=-1 returned every entry in the
category instead of reporting the bad filter.

These endpoints now return 400 Bad Request when feed_id or category_id
is not a non-negative integer. Empty values are still ignored, and
/v1/entries keeps ignoring invalid values for compatibility with
existing clients.
Kuri 5 дней назад
Родитель
Сommit
d64e4aaa3d

+ 65 - 0
internal/api/api_test.go

@@ -213,6 +213,7 @@ func TestParseEntryFiltersPreservesEndpointCompatibility(t *testing.T) {
 		{routeCategoryID: 10, query: "category_id=20", categoryID: 10},
 		{routeCategoryID: 10, query: "category_id=0", categoryID: 10},
 		{routeCategoryID: 10, query: "feed_id=12", feedID: 12, categoryID: 10},
+		{routeFeedID: 11, query: "feed_id=&category_id=", feedID: 11},
 		{query: "starred=1", starred: new(true)},
 		{query: "starred=0", starred: new(false)},
 		{query: "starred=invalid"},
@@ -244,6 +245,70 @@ func TestParseEntryFiltersPreservesEndpointCompatibility(t *testing.T) {
 	}
 }
 
+func TestParseEntryFiltersRejectsInvalidRouteScopedQueryIDs(t *testing.T) {
+	for _, route := range []struct {
+		name               string
+		feedID, categoryID int64
+	}{
+		{"feed", 11, 0},
+		{"category", 0, 10},
+	} {
+		for _, query := range []string{
+			"feed_id=-1",
+			"category_id=-1",
+			"feed_id=invalid",
+			"category_id=invalid",
+			"feed_id=9223372036854775808",
+			"category_id=9223372036854775808",
+		} {
+			t.Run(route.name+"/"+query, func(t *testing.T) {
+				r := httptest.NewRequest(http.MethodGet, "/v1/entries?"+query, nil)
+				if _, err := parseEntryFilters(r, route.feedID, route.categoryID); err == nil {
+					t.Fatal("Expected invalid scope query ID to be rejected")
+				}
+			})
+		}
+	}
+}
+
+func TestRouteScopedEntryHandlersRejectInvalidScopeQueryIDs(t *testing.T) {
+	tests := []struct {
+		name       string
+		path       string
+		pathValues map[string]string
+		handle     func(*handler, http.ResponseWriter, *http.Request)
+	}{
+		{
+			name:       "feed route",
+			path:       "/v1/feeds/11/entries?category_id=-1",
+			pathValues: map[string]string{"feedID": "11"},
+			handle:     (*handler).getFeedEntriesHandler,
+		},
+		{
+			name:       "category route",
+			path:       "/v1/categories/10/entries?feed_id=invalid",
+			pathValues: map[string]string{"categoryID": "10"},
+			handle:     (*handler).getCategoryEntriesHandler,
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			r := httptest.NewRequest(http.MethodGet, test.path, nil)
+			for name, value := range test.pathValues {
+				r.SetPathValue(name, value)
+			}
+			w := httptest.NewRecorder()
+
+			test.handle(&handler{}, w, r)
+
+			if w.Code != http.StatusBadRequest {
+				t.Fatalf("Unexpected status code: got %d, want %d", w.Code, http.StatusBadRequest)
+			}
+		})
+	}
+}
+
 func TestParseEntryIDsParamsDefaults(t *testing.T) {
 	r := httptest.NewRequest(http.MethodGet, "/v1/entries/ids", nil)
 	limit, offset := parseEntryIDsParams(r)

+ 22 - 0
internal/api/entry_query.go

@@ -40,6 +40,16 @@ type entryFilters struct {
 // parseEntryFilters reads the entry filters shared by the entry endpoints.
 // A non-zero feedID or categoryID comes from the route and takes precedence over the query string.
 func parseEntryFilters(r *http.Request, feedID, categoryID int64) (entryFilters, error) {
+	// Feed and category endpoints reject an invalid feed_id or category_id instead of ignoring it.
+	// /v1/entries keeps ignoring invalid values for compatibility with existing clients.
+	if feedID != 0 || categoryID != 0 {
+		for _, name := range []string{"feed_id", "category_id"} {
+			if err := validateEntryScopeQueryID(r, name); err != nil {
+				return entryFilters{}, err
+			}
+		}
+	}
+
 	if feedID == 0 {
 		feedID = request.QueryInt64Param(r, "feed_id", 0)
 	}
@@ -102,6 +112,18 @@ func parseEntryFilters(r *http.Request, feedID, categoryID int64) (entryFilters,
 	return filters, nil
 }
 
+// validateEntryScopeQueryID rejects a non-empty query value that is not a non-negative integer.
+func validateEntryScopeQueryID(r *http.Request, name string) error {
+	value := r.URL.Query().Get(name)
+	if value == "" {
+		return nil
+	}
+	if id, err := strconv.ParseInt(value, 10, 64); err != nil || id < 0 {
+		return fmt.Errorf("invalid %s parameter, must be a non-negative integer", name)
+	}
+	return nil
+}
+
 // maxEntryFilterTimestamp is the first Unix timestamp past PostgreSQL's finite timestamp range.
 var maxEntryFilterTimestamp = time.Date(294277, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()
 

+ 3 - 0
internal/api/entry_query_integration_test.go

@@ -446,6 +446,9 @@ func TestEntryQueryAPIRejectsForeignScopes(t *testing.T) {
 		// The query string cannot replace or clear a foreign route scope.
 		"/v1/feeds/16/entries?feed_id=11", "/v1/feeds/16/entries?feed_id=0",
 		"/v1/categories/40/entries?category_id=10", "/v1/categories/40/entries?category_id=0",
+		// Feed and category endpoints reject invalid scope IDs in the query string.
+		"/v1/feeds/11/entries?feed_id=-1", "/v1/feeds/11/entries?category_id=invalid",
+		"/v1/categories/10/entries?category_id=invalid", "/v1/categories/10/entries?feed_id=-1",
 	} {
 		recorder := entryQueryAPIRequest(api, http.MethodGet, path, "")
 		if recorder.Code != http.StatusBadRequest {