Răsfoiți Sursa

fix(api): only validate the scope ID the route does not set

Address review feedback on the feed and category entry endpoints:

- Parse the raw query with url.ParseQuery and check every value, so
  malformed pairs such as feed_id=%zz or feed_id=-1; and repeated
  parameters such as feed_id=&feed_id=-1 are rejected.
- Only check the parameter that narrows the results: category_id on the
  feed endpoint and feed_id on the category endpoint. The route ID
  always wins, so its query value is still ignored.
- Use a store backed by a failing database driver in the handler unit
  tests, and move the integration cases into their own test.
Kuri 4 zile în urmă
părinte
comite
1bd23a2065

+ 42 - 9
internal/api/api_test.go

@@ -4,7 +4,10 @@
 package api // import "miniflux.app/v2/internal/api"
 
 import (
+	"database/sql"
+	"database/sql/driver"
 	"encoding/json"
+	"errors"
 	"fmt"
 	"net/http"
 	"net/http/httptest"
@@ -16,6 +19,7 @@ import (
 	"testing"
 
 	"miniflux.app/v2/internal/config"
+	"miniflux.app/v2/internal/storage"
 	"miniflux.app/v2/internal/version"
 )
 
@@ -214,6 +218,11 @@ func TestParseEntryFiltersPreservesEndpointCompatibility(t *testing.T) {
 		{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},
+		// The route's own ID always wins, so an invalid query value for it is ignored.
+		{routeFeedID: 11, query: "feed_id=-1", feedID: 11},
+		{routeFeedID: 11, query: "feed_id=invalid", feedID: 11},
+		{routeCategoryID: 10, query: "category_id=-1", categoryID: 10},
+		{routeCategoryID: 10, query: "category_id=invalid", categoryID: 10},
 		{query: "starred=1", starred: new(true)},
 		{query: "starred=0", starred: new(false)},
 		{query: "starred=invalid"},
@@ -249,17 +258,19 @@ func TestParseEntryFiltersRejectsInvalidRouteScopedQueryIDs(t *testing.T) {
 	for _, route := range []struct {
 		name               string
 		feedID, categoryID int64
+		param              string
 	}{
-		{"feed", 11, 0},
-		{"category", 0, 10},
+		{"feed", 11, 0, "category_id"},
+		{"category", 0, 10, "feed_id"},
 	} {
 		for _, query := range []string{
-			"feed_id=-1",
-			"category_id=-1",
-			"feed_id=invalid",
-			"category_id=invalid",
-			"feed_id=9223372036854775808",
-			"category_id=9223372036854775808",
+			route.param + "=-1",
+			route.param + "=invalid",
+			route.param + "=9223372036854775808",
+			route.param + "=%zz",
+			route.param + "=-1;",
+			route.param + "=&" + route.param + "=-1",
+			route.param + "=1&" + route.param + "=invalid",
 		} {
 			t.Run(route.name+"/"+query, func(t *testing.T) {
 				r := httptest.NewRequest(http.MethodGet, "/v1/entries?"+query, nil)
@@ -271,6 +282,28 @@ func TestParseEntryFiltersRejectsInvalidRouteScopedQueryIDs(t *testing.T) {
 	}
 }
 
+// failingDriver is a database/sql driver whose connections always fail, so a handler
+// that reaches the store returns an error instead of crashing on a nil store.
+type failingDriver struct{}
+
+func (failingDriver) Open(string) (driver.Conn, error) {
+	return nil, errors.New("no database in unit tests")
+}
+
+func init() {
+	sql.Register("api-unit-test-failing", failingDriver{})
+}
+
+func newFailingStoreHandler(t *testing.T) *handler {
+	t.Helper()
+	db, err := sql.Open("api-unit-test-failing", "")
+	if err != nil {
+		t.Fatal(err)
+	}
+	t.Cleanup(func() { db.Close() })
+	return &handler{store: storage.NewStorage(db)}
+}
+
 func TestRouteScopedEntryHandlersRejectInvalidScopeQueryIDs(t *testing.T) {
 	tests := []struct {
 		name       string
@@ -300,7 +333,7 @@ func TestRouteScopedEntryHandlersRejectInvalidScopeQueryIDs(t *testing.T) {
 			}
 			w := httptest.NewRecorder()
 
-			test.handle(&handler{}, w, r)
+			test.handle(newFailingStoreHandler(t), w, r)
 
 			if w.Code != http.StatusBadRequest {
 				t.Fatalf("Unexpected status code: got %d, want %d", w.Code, http.StatusBadRequest)

+ 20 - 11
internal/api/entry_query.go

@@ -40,13 +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.
+	// Feed and category endpoints reject an invalid value for the scope ID that the route does not set,
+	// since that value narrows the results. The route's own ID always wins, so its query value is not checked.
 	// /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
-			}
+		name := "feed_id"
+		if feedID != 0 {
+			name = "category_id"
+		}
+		if err := validateEntryScopeQueryID(r, name); err != nil {
+			return entryFilters{}, err
 		}
 	}
 
@@ -112,14 +115,20 @@ 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.
+// validateEntryScopeQueryID rejects a query string that cannot be parsed,
+// and any value of the parameter that is neither empty nor a non-negative integer.
 func validateEntryScopeQueryID(r *http.Request, name string) error {
-	value := r.URL.Query().Get(name)
-	if value == "" {
-		return nil
+	values, err := url.ParseQuery(r.URL.RawQuery)
+	if err != nil {
+		return fmt.Errorf("invalid query parameters: %w", err)
 	}
-	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)
+	for _, value := range values[name] {
+		if value == "" {
+			continue
+		}
+		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
 }

+ 21 - 3
internal/api/entry_query_integration_test.go

@@ -446,9 +446,6 @@ 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 {
@@ -457,6 +454,25 @@ func TestEntryQueryAPIRejectsForeignScopes(t *testing.T) {
 	}
 }
 
+func TestEntryQueryAPIRejectsInvalidRouteScopedQueryIDs(t *testing.T) {
+	api, _ := newEntryQueryAPI(t)
+	for _, path := range []string{
+		"/v1/feeds/11/entries?category_id=-1",
+		"/v1/feeds/11/entries?category_id=invalid",
+		"/v1/feeds/11/entries?category_id=%zz",
+		"/v1/categories/10/entries?feed_id=-1",
+		"/v1/categories/10/entries?feed_id=invalid",
+		"/v1/categories/10/entries?feed_id=&feed_id=-1",
+	} {
+		t.Run(path, func(t *testing.T) {
+			w := entryQueryAPIRequest(api, http.MethodGet, path, "")
+			if w.Code != http.StatusBadRequest {
+				t.Fatalf("Got status %d, want 400: %s", w.Code, w.Body.String())
+			}
+		})
+	}
+}
+
 func TestEntryQueryAPICompatibility(t *testing.T) {
 	api, _ := newEntryQueryAPI(t)
 	for _, test := range []struct {
@@ -495,6 +511,8 @@ func TestEntryQueryAPICompatibility(t *testing.T) {
 		{"/v1/categories/10/entries", []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?feed_id=-1", []int64{3, 8, 1}},
+		{"/v1/categories/10/entries?category_id=invalid", []int64{4, 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}},