Browse Source

fix(api): keep route feed and category IDs when filtering entries

On GET /v1/feeds/{feedID}/entries and
/v1/categories/{categoryID}/entries, a feed_id or category_id query
parameter replaced the ID in the URL, and an explicit 0 removed the
scope: /v1/feeds/12/entries?feed_id=0 returned entries from every
feed, and /v1/feeds/16/entries?feed_id=11 returned feed 11's entries
without checking feed 16.

The ID in the URL now takes precedence over the query string. The
other ID still narrows the results, so
/v1/categories/10/entries?feed_id=13 keeps working, and /v1/entries is
unchanged.
Fred 7 hours ago
parent
commit
445137f3e2
3 changed files with 43 additions and 15 deletions
  1. 19 9
      internal/api/api_test.go
  2. 11 2
      internal/api/entry_query.go
  3. 13 4
      internal/api/entry_query_integration_test.go

+ 19 - 9
internal/api/api_test.go

@@ -5,6 +5,7 @@ package api // import "miniflux.app/v2/internal/api"
 
 import (
 	"encoding/json"
+	"fmt"
 	"net/http"
 	"net/http/httptest"
 	"net/url"
@@ -193,25 +194,34 @@ func TestParseEntryFiltersRejectsValuesPostgreSQLCannotAccept(t *testing.T) {
 
 func TestParseEntryFiltersPreservesEndpointCompatibility(t *testing.T) {
 	for _, test := range []struct {
+		routeFeedID     int64
+		routeCategoryID int64
 		query           string
 		feedID          int64
 		categoryID      int64
 		starred         *bool
 		globallyVisible bool
 	}{
-		{query: "", feedID: 11, categoryID: 10},
 		{query: "feed_id=12&category_id=20", feedID: 12, categoryID: 20},
-		{query: "feed_id=invalid&category_id=-1", feedID: 11, categoryID: 10},
+		{query: "feed_id=invalid&category_id=-1"},
 		{query: "feed_id=0&category_id=0"},
-		{query: "starred=1", feedID: 11, categoryID: 10, starred: new(true)},
-		{query: "starred=0", feedID: 11, categoryID: 10, starred: new(false)},
-		{query: "starred=invalid", feedID: 11, categoryID: 10},
-		{query: "globally_visible=invalid", feedID: 11, categoryID: 10, globallyVisible: true},
-		{query: "globally_visible=false", feedID: 11, categoryID: 10},
+		// Route IDs scope the endpoint: the query string cannot replace or clear them.
+		{routeFeedID: 11, query: "", feedID: 11},
+		{routeFeedID: 11, query: "feed_id=12", feedID: 11},
+		{routeFeedID: 11, query: "feed_id=0", feedID: 11},
+		{routeFeedID: 11, query: "category_id=20", feedID: 11, categoryID: 20},
+		{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},
+		{query: "starred=1", starred: new(true)},
+		{query: "starred=0", starred: new(false)},
+		{query: "starred=invalid"},
+		{query: "globally_visible=invalid", globallyVisible: true},
+		{query: "globally_visible=false"},
 	} {
-		t.Run(test.query, func(t *testing.T) {
+		t.Run(fmt.Sprintf("feed=%d/category=%d/%s", test.routeFeedID, test.routeCategoryID, test.query), func(t *testing.T) {
 			r := httptest.NewRequest(http.MethodGet, "/v1/entries?"+test.query, nil)
-			filters, err := parseEntryFilters(r, 11, 10)
+			filters, err := parseEntryFilters(r, test.routeFeedID, test.routeCategoryID)
 			if err != nil {
 				t.Fatal(err)
 			}

+ 11 - 2
internal/api/entry_query.go

@@ -37,10 +37,19 @@ type entryFilters struct {
 	search          string
 }
 
+// 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) {
+	if feedID == 0 {
+		feedID = request.QueryInt64Param(r, "feed_id", 0)
+	}
+	if categoryID == 0 {
+		categoryID = request.QueryInt64Param(r, "category_id", 0)
+	}
+
 	filters := entryFilters{
-		feedID:          request.QueryInt64Param(r, "feed_id", feedID),
-		categoryID:      request.QueryInt64Param(r, "category_id", categoryID),
+		feedID:          feedID,
+		categoryID:      categoryID,
 		statuses:        request.QueryStringParamList(r, "status"),
 		tags:            request.QueryStringParamList(r, "tags"),
 		globallyVisible: request.HasQueryParam(r, "globally_visible") && request.QueryBoolParam(r, "globally_visible", true),

+ 13 - 4
internal/api/entry_query_integration_test.go

@@ -441,10 +441,15 @@ func TestEntryQueryAPIRejectsForeignScopes(t *testing.T) {
 			})
 		}
 	}
-	for _, path := range []string{"/v1/feeds/16/entries", "/v1/categories/40/entries"} {
+	for _, path := range []string{
+		"/v1/feeds/16/entries", "/v1/categories/40/entries",
+		// 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",
+	} {
 		w := entryQueryAPIRequest(api, http.MethodGet, path, "")
 		if w.Code != http.StatusBadRequest {
-			t.Fatalf("Got status %d, want 400: %s", w.Code, w.Body.String())
+			t.Fatalf("%s: got status %d, want 400: %s", path, w.Code, w.Body.String())
 		}
 	}
 }
@@ -485,8 +490,12 @@ func TestEntryQueryAPICompatibility(t *testing.T) {
 		{"/v1/entries", []int64{2, 4, 5, 6, 3, 8, 1}},
 		{"/v1/feeds/11/entries", []int64{3, 8, 1}},
 		{"/v1/categories/10/entries", []int64{4, 3, 8, 1}},
-		{"/v1/feeds/12/entries?feed_id=11", []int64{3, 8, 1}},
-		{"/v1/categories/20/entries?category_id=10", []int64{4, 3, 8, 1}},
+		{"/v1/feeds/12/entries?feed_id=11", []int64{2}},
+		{"/v1/feeds/11/entries?feed_id=0", []int64{3, 8, 1}},
+		{"/v1/feeds/11/entries?category_id=10", []int64{3, 8, 1}},
+		{"/v1/categories/20/entries?category_id=10", []int64{2}},
+		{"/v1/categories/10/entries?category_id=0", []int64{4, 3, 8, 1}},
+		{"/v1/categories/10/entries?feed_id=13", []int64{4}},
 		{"/v1/entries?starred=1", []int64{2, 4}},
 		{"/v1/entries?starred=invalid", []int64{2, 4, 5, 6, 3, 8, 1}},
 		{"/v1/entries?feed_id=invalid&before_entry_id=-1", []int64{2, 4, 5, 6, 3, 8, 1}},