瀏覽代碼

fix(api): reject entry filters that PostgreSQL cannot accept

GET /v1/entries, /v1/feeds/{feedID}/entries and
/v1/categories/{categoryID}/entries passed timestamp, search and tags
values straight to the database query, so a request such as
?before=99999999999999 or ?search=%00 failed with a 500 error
("timestamp out of range", "invalid byte sequence for encoding UTF8").

Reject timestamps at or past PostgreSQL's finite timestamp range, and
search or tags values with NUL bytes or invalid UTF-8, with a 400 error
on every entry endpoint. The checks move from the /v1/entries/ids
validator into the shared filter parsing, so /v1/entries/ids behaves as
before. Non-numeric or negative timestamps are still ignored on
/v1/entries.
Fred 8 小時之前
父節點
當前提交
896d71ff43
共有 3 個文件被更改,包括 77 次插入 和 14 次删除
  1. 22 1
      internal/api/api_test.go
  2. 37 13
      internal/api/entry_query.go
  3. 18 0
      internal/api/entry_query_integration_test.go

+ 22 - 1
internal/api/api_test.go

@@ -11,6 +11,7 @@ import (
 	"reflect"
 	"runtime"
 	"slices"
+	"strconv"
 	"testing"
 
 	"miniflux.app/v2/internal/config"
@@ -159,7 +160,7 @@ func TestValidateEntryIDsFiltersRejectsZeroIDs(t *testing.T) {
 
 func TestValidateEntryIDsFiltersRejectsMalformedQueryStrings(t *testing.T) {
 	for _, query := range []string{
-		"feed_id=%zz", "status=unread;status=read", "search=%00", "tags=%00", "search=%ff", "tags=%ff",
+		"feed_id=%zz", "status=unread;status=read",
 	} {
 		t.Run(query, func(t *testing.T) {
 			r := httptest.NewRequest(http.MethodGet, "/v1/entries/ids?"+query, nil)
@@ -170,6 +171,26 @@ func TestValidateEntryIDsFiltersRejectsMalformedQueryStrings(t *testing.T) {
 	}
 }
 
+func TestParseEntryFiltersRejectsValuesPostgreSQLCannotAccept(t *testing.T) {
+	queries := []string{"search=%00", "search=%ff", "tags=%00", "tags=%ff", "tags=one&tags=%ff"}
+	for _, name := range []string{"before", "after", "published_before", "published_after", "changed_before", "changed_after"} {
+		queries = append(queries, name+"=9223372036854775807", name+"="+strconv.FormatInt(maxEntryFilterTimestamp, 10))
+	}
+	for _, query := range queries {
+		t.Run(query, func(t *testing.T) {
+			r := httptest.NewRequest(http.MethodGet, "/v1/entries?"+query, nil)
+			if _, err := parseEntryFilters(r, 0, 0); err == nil {
+				t.Fatal("Expected the value to be rejected")
+			}
+		})
+	}
+
+	r := httptest.NewRequest(http.MethodGet, "/v1/entries?before="+strconv.FormatInt(maxEntryFilterTimestamp-1, 10)+"&search=caf%C3%A9", nil)
+	if _, err := parseEntryFilters(r, 0, 0); err != nil {
+		t.Fatalf("Expected the last valid timestamp and UTF-8 search text to be accepted: %v", err)
+	}
+}
+
 func TestParseEntryFiltersPreservesEndpointCompatibility(t *testing.T) {
 	for _, test := range []struct {
 		query           string

+ 37 - 13
internal/api/entry_query.go

@@ -61,6 +61,31 @@ func parseEntryFilters(r *http.Request, feedID, categoryID int64) (entryFilters,
 		}
 	}
 
+	for _, timestamp := range []struct {
+		name  string
+		value int64
+	}{
+		{"before", filters.before},
+		{"after", filters.after},
+		{"published_before", filters.publishedBefore},
+		{"published_after", filters.publishedAfter},
+		{"changed_before", filters.changedBefore},
+		{"changed_after", filters.changedAfter},
+	} {
+		if timestamp.value >= maxEntryFilterTimestamp {
+			return entryFilters{}, fmt.Errorf("invalid %s parameter, timestamp is out of range", timestamp.name)
+		}
+	}
+
+	if err := validateEntryFilterText("search", filters.search); err != nil {
+		return entryFilters{}, err
+	}
+	for _, tag := range filters.tags {
+		if err := validateEntryFilterText("tags", tag); err != nil {
+			return entryFilters{}, err
+		}
+	}
+
 	if starred, err := strconv.ParseBool(r.URL.Query().Get("starred")); err == nil {
 		filters.starred = &starred
 	}
@@ -68,6 +93,17 @@ func parseEntryFilters(r *http.Request, feedID, categoryID int64) (entryFilters,
 	return filters, 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()
+
+// validateEntryFilterText rejects text that PostgreSQL cannot accept as a UTF-8 query parameter.
+func validateEntryFilterText(name, value string) error {
+	if !utf8.ValidString(value) || strings.ContainsRune(value, '\x00') {
+		return fmt.Errorf("invalid %s parameter, must be valid UTF-8 without null bytes", name)
+	}
+	return nil
+}
+
 func (filters entryFilters) apply(builder *storage.EntryQueryBuilder) {
 	builder.WithFeedID(filters.feedID).
 		WithCategoryID(filters.categoryID).
@@ -162,8 +198,7 @@ func validateEntryIDsFilters(r *http.Request) error {
 		}
 	}
 
-	// Reject timestamps outside PostgreSQL's finite timestamp range.
-	maxTimestamp := time.Date(294277, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()
+	// Timestamp range and text encoding checks shared by every entry endpoint are in parseEntryFilters.
 	for _, name := range []string{"before", "after", "published_before", "published_after", "changed_before", "changed_after"} {
 		if !values.Has(name) {
 			continue
@@ -172,9 +207,6 @@ func validateEntryIDsFilters(r *http.Request) error {
 		if err != nil || value < 0 {
 			return fmt.Errorf("invalid %s parameter, must be a non-negative integer", name)
 		}
-		if value >= maxTimestamp {
-			return fmt.Errorf("invalid %s parameter, timestamp is out of range", name)
-		}
 	}
 
 	for _, name := range []string{"starred", "globally_visible"} {
@@ -183,14 +215,6 @@ func validateEntryIDsFilters(r *http.Request) error {
 		}
 	}
 
-	for _, name := range []string{"search", "tags"} {
-		for _, value := range values[name] {
-			if !utf8.ValidString(value) || strings.ContainsRune(value, '\x00') {
-				return fmt.Errorf("invalid %s parameter, must be valid UTF-8 without null bytes", name)
-			}
-		}
-	}
-
 	for _, status := range values["status"] {
 		if strings.TrimSpace(status) == "" {
 			return validator.ValidateEntryStatus("")

+ 18 - 0
internal/api/entry_query_integration_test.go

@@ -407,6 +407,22 @@ func TestEntryQueryAPIRejectsInvalidParametersAfterAuthentication(t *testing.T)
 			}
 		})
 	}
+
+	// PostgreSQL rejects these values, so every entry endpoint must reject them before querying.
+	queries = []string{"search=%00", "search=%ff", "tags=%00", "tags=%ff"}
+	for _, name := range []string{"before", "after", "published_before", "published_after", "changed_before", "changed_after"} {
+		queries = append(queries, name+"=9223372036854775807", name+"="+strconv.FormatInt(maxEntryFilterTimestamp, 10))
+	}
+	for _, path := range []string{"/v1/entries", "/v1/feeds/11/entries", "/v1/categories/10/entries"} {
+		for _, query := range queries {
+			t.Run(path+"?"+query, func(t *testing.T) {
+				w := entryQueryAPIRequest(api, http.MethodGet, path+"?"+query, "")
+				if w.Code != http.StatusBadRequest {
+					t.Fatalf("Got status %d, want 400: %s", w.Code, w.Body.String())
+				}
+			})
+		}
+	}
 }
 
 func TestEntryQueryAPIRejectsForeignScopes(t *testing.T) {
@@ -476,6 +492,8 @@ func TestEntryQueryAPICompatibility(t *testing.T) {
 		{"/v1/entries?feed_id=invalid&before_entry_id=-1", []int64{2, 4, 5, 6, 3, 8, 1}},
 		{"/v1/entries?feed_id=0&category_id=0&before_entry_id=0&after_entry_id=0", []int64{2, 4, 5, 6, 3, 8, 1}},
 		{"/v1/entries?globally_visible=invalid", []int64{2, 3, 8, 1}},
+		{"/v1/entries?before=" + strconv.FormatInt(maxEntryFilterTimestamp-1, 10), []int64{2, 4, 5, 6, 3, 8, 1}},
+		{"/v1/entries?search=caf%C3%A9", nil},
 	} {
 		t.Run(test.path, func(t *testing.T) {
 			w := entryQueryAPIRequest(api, http.MethodGet, test.path, "")