Kaynağa Gözat

fix(googlereader): reject item IDs with trailing characters

fmt.Sscanf stopped at the first non-hexadecimal character and read at
most 16 digits, so IDs like tag:.../0000000000000001garbage were
accepted and longer IDs were truncated. Parse item IDs with
strconv.ParseUint, which also rejects signs and spaces.
Fred 8 saat önce
ebeveyn
işleme
e97a7bbe8b

+ 1 - 0
internal/googlereader/edit_tag_integration_test.go

@@ -92,6 +92,7 @@ func TestEditTagRejectsInvalidRequests(t *testing.T) {
 	}{
 	}{
 		{"missing item ID", url.Values{"a": {readStream}}},
 		{"missing item ID", url.Values{"a": {readStream}}},
 		{"invalid item ID", url.Values{"i": {"invalid"}, "a": {readStream}}},
 		{"invalid item ID", url.Values{"i": {"invalid"}, "a": {readStream}}},
+		{"item ID with trailing characters", url.Values{"i": {longItemID(entries[0].ID) + "garbage"}, "a": {readStream}}},
 		{"no tags", url.Values{"i": {itemID}}},
 		{"no tags", url.Values{"i": {itemID}}},
 		{"invalid tag", url.Values{"i": {itemID}, "a": {"invalid"}}},
 		{"invalid tag", url.Values{"i": {itemID}, "a": {"invalid"}}},
 		{"invalid removed tag", url.Values{"i": {itemID}, "r": {"invalid"}}},
 		{"invalid removed tag", url.Values{"i": {itemID}, "r": {"invalid"}}},

+ 12 - 27
internal/googlereader/item.go

@@ -25,41 +25,26 @@ func convertEntryIDToLongFormItemID(entryID int64) string {
 // NetNewsWire uses this format: "tag:google.com,2005:reader/item/2f2" (hexadecimal string with prefix and no padding)
 // NetNewsWire uses this format: "tag:google.com,2005:reader/item/2f2" (hexadecimal string with prefix and no padding)
 // Reeder uses this format: "000000000000048c" (hexadecimal string without prefix and padding)
 // Reeder uses this format: "000000000000048c" (hexadecimal string without prefix and padding)
 // Liferea uses this format: "12345" (decimal string)
 // Liferea uses this format: "12345" (decimal string)
+// A 16-character ID without prefix is read as hexadecimal, since decimal IDs only reach 16 digits at 10^15.
 // It returns the parsed ID as a int64 and an error if parsing fails.
 // It returns the parsed ID as a int64 and an error if parsing fails.
 func parseItemID(itemIDValue string) (int64, error) {
 func parseItemID(itemIDValue string) (int64, error) {
-	var itemID int64
-	if strings.HasPrefix(itemIDValue, ItemIDPrefix) {
-		n, err := fmt.Sscanf(itemIDValue, ItemIDFormat, &itemID)
-		if err != nil {
-			return 0, fmt.Errorf("failed to parse hexadecimal item ID %s: %w", itemIDValue, err)
-		}
-		if n != 1 {
-			return 0, fmt.Errorf("failed to parse hexadecimal item ID %s: expected 1 value, got %d", itemIDValue, n)
-		}
-		if itemID <= 0 {
-			return 0, fmt.Errorf("failed to parse hexadecimal item ID %s: item ID must be positive", itemIDValue)
-		}
-		return itemID, nil
-	}
-
-	if len(itemIDValue) == 16 {
-		if n, err := fmt.Sscanf(itemIDValue, "%016x", &itemID); err == nil && n == 1 {
-			if itemID > 0 {
-				return itemID, nil
-			}
-			return 0, fmt.Errorf("failed to parse hexadecimal item ID %s: item ID must be positive", itemIDValue)
-		}
+	value, base, format := itemIDValue, 10, "decimal"
+	if hexID, found := strings.CutPrefix(itemIDValue, ItemIDPrefix); found {
+		value, base, format = hexID, 16, "hexadecimal"
+	} else if len(itemIDValue) == 16 {
+		base, format = 16, "hexadecimal"
 	}
 	}
 
 
-	itemID, err := strconv.ParseInt(itemIDValue, 10, 64)
+	// ParseUint rejects signs, spaces and trailing characters, and a bit size of 63 keeps the ID within int64.
+	itemID, err := strconv.ParseUint(value, base, 63)
 	if err != nil {
 	if err != nil {
-		return 0, fmt.Errorf("failed to parse decimal item ID %s: %w", itemIDValue, err)
+		return 0, fmt.Errorf("failed to parse %s item ID %s: %w", format, itemIDValue, err)
 	}
 	}
-	if itemID <= 0 {
-		return 0, fmt.Errorf("failed to parse decimal item ID %s: item ID must be positive", itemIDValue)
+	if itemID == 0 {
+		return 0, fmt.Errorf("failed to parse %s item ID %s: item ID must be positive", format, itemIDValue)
 	}
 	}
 
 
-	return itemID, nil
+	return int64(itemID), nil
 }
 }
 
 
 func parseItemIDsFromRequest(r *http.Request) ([]int64, error) {
 func parseItemIDsFromRequest(r *http.Request) ([]int64, error) {

+ 32 - 0
internal/googlereader/item_test.go

@@ -103,6 +103,38 @@ func TestParseItemID(t *testing.T) {
 	}
 	}
 }
 }
 
 
+func TestParseItemIDParsesEveryDigit(t *testing.T) {
+	result, err := parseItemID("tag:google.com,2005:reader/item/00000000000000010")
+	if err != nil {
+		t.Fatalf("unexpected error: %v", err)
+	}
+	if result != 16 {
+		t.Errorf("expected 16, got %d", result)
+	}
+}
+
+func TestParseItemIDRejectsMalformedIDs(t *testing.T) {
+	for _, itemID := range []string{
+		"tag:google.com,2005:reader/item/0000000000000001garbage",
+		"tag:google.com,2005:reader/item/2f2xyz",
+		"tag:google.com,2005:reader/item/ 2f2",
+		"tag:google.com,2005:reader/item/+2f2",
+		"tag:google.com,2005:reader/item/0x2f2",
+		"tag:google.com,2005:reader/item/",
+		"tag:google.com,2005:reader/item/8000000000000000",
+		"000000000000048g",
+		"00000000000001-5",
+		"+12345",
+		"12345garbage",
+	} {
+		t.Run(itemID, func(t *testing.T) {
+			if result, err := parseItemID(itemID); err == nil {
+				t.Fatalf("expected error for malformed item ID %q, got %d", itemID, result)
+			}
+		})
+	}
+}
+
 func TestParseItemIDRejectsNonPositiveIDs(t *testing.T) {
 func TestParseItemIDRejectsNonPositiveIDs(t *testing.T) {
 	for _, itemID := range []string{
 	for _, itemID := range []string{
 		"0",
 		"0",