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

fix(fever): ignore mark requests with an invalid ID

Mark requests with an invalid ID returned an empty body, and mark=group
without a numeric ID marked every item as read. Return the authenticated
response without changes instead. Marking all items as read now needs an
explicit id=0.
Fred 8 часов назад
Родитель
Сommit
e5deaac69c
2 измененных файлов с 45 добавлено и 3 удалено
  1. 9 3
      internal/fever/handler.go
  2. 36 0
      internal/fever/mark_integration_test.go

+ 9 - 3
internal/fever/handler.go

@@ -407,6 +407,8 @@ func (h *feverHandler) handleWriteItems(w http.ResponseWriter, r *http.Request)
 
 	entryID := request.FormInt64Value(r, "id")
 	if entryID <= 0 {
+		// Fever has no error response: an invalid item ID changes nothing.
+		response.JSON(w, r, newBaseResponse())
 		return
 	}
 
@@ -499,6 +501,8 @@ func (h *feverHandler) handleWriteFeeds(w http.ResponseWriter, r *http.Request)
 	)
 
 	if feedID <= 0 {
+		// Fever has no error response: an invalid feed ID changes nothing.
+		response.JSON(w, r, newBaseResponse())
 		return
 	}
 
@@ -518,13 +522,15 @@ before=? where ? is replaced with the Unix timestamp of the the local client’s
 */
 func (h *feverHandler) handleWriteGroups(w http.ResponseWriter, r *http.Request) {
 	userID := request.UserID(r)
-	groupID := request.FormInt64Value(r, "id")
 
-	if groupID < 0 {
+	// Group 0 marks every item as read, so it must be requested explicitly:
+	// a missing or invalid group ID changes nothing, as Fever has no error response.
+	groupID, err := strconv.ParseInt(r.FormValue("id"), 10, 64)
+	if err != nil || groupID < 0 {
+		response.JSON(w, r, newBaseResponse())
 		return
 	}
 
-	var err error
 	beforeTimestamp := request.FormInt64Value(r, "before")
 	before := time.Unix(beforeTimestamp, 0)
 

+ 36 - 0
internal/fever/mark_integration_test.go

@@ -119,6 +119,42 @@ func TestMarkAllItems(t *testing.T) {
 	requireUnreadEntries(t, user)
 }
 
+func TestMarkOperationsIgnoreInvalidIDs(t *testing.T) {
+	t.Parallel()
+
+	now := strconv.FormatInt(time.Now().Unix(), 10)
+	scenarios := []struct {
+		name   string
+		params url.Values
+	}{
+		{"item without ID", url.Values{"mark": {"item"}, "as": {"read"}}},
+		{"item with ID 0", url.Values{"mark": {"item"}, "as": {"read"}, "id": {"0"}}},
+		{"item with a negative ID", url.Values{"mark": {"item"}, "as": {"read"}, "id": {"-1"}}},
+		{"item with an invalid ID", url.Values{"mark": {"item"}, "as": {"read"}, "id": {"invalid"}}},
+		{"feed without ID", url.Values{"mark": {"feed"}, "as": {"read"}, "before": {now}}},
+		{"feed with ID 0", url.Values{"mark": {"feed"}, "as": {"read"}, "id": {"0"}, "before": {now}}},
+		{"feed with a negative ID", url.Values{"mark": {"feed"}, "as": {"read"}, "id": {"-1"}, "before": {now}}},
+		{"feed with an invalid ID", url.Values{"mark": {"feed"}, "as": {"read"}, "id": {"invalid"}, "before": {now}}},
+		{"group without ID", url.Values{"mark": {"group"}, "as": {"read"}, "before": {now}}},
+		{"group with a negative ID", url.Values{"mark": {"group"}, "as": {"read"}, "id": {"-1"}, "before": {now}}},
+		{"group with an invalid ID", url.Values{"mark": {"group"}, "as": {"read"}, "id": {"invalid"}, "before": {now}}},
+	}
+
+	for _, scenario := range scenarios {
+		t.Run(scenario.name, func(t *testing.T) {
+			t.Parallel()
+
+			user := newTestUser(t)
+			_, entries := subscribeToFixture(t, user)
+
+			// Fever has no error response: the request gets the authenticated response and changes nothing.
+			var result baseResult
+			user.fever.postJSON(t, scenario.params, &result)
+			requireUnreadEntries(t, user, entryIDs(entries)...)
+		})
+	}
+}
+
 // markItem sends a mark=item request with the given sender and requires the authenticated response.
 // An empty as is left out of the request.
 func markItem(t *testing.T, send func(*testing.T, url.Values, any), entryID int64, as string) {