fix: add pagination limits and validation
- Enforce maximum pagination limit of 1000 items per request - Prevent negative offset values in pagination - Apply limits to both /api/ebooks and /api/media-items endpoints - Protect against DoS attacks from large limit values Fixes security issue: No maximum pagination limit
This commit is contained in:
@@ -15,6 +15,10 @@ import (
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
maxPaginationLimit = 1000
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
db *database.Queries
|
||||
scanner *services.EbookScanner
|
||||
@@ -120,12 +124,19 @@ func (h *Handler) ListEbooks(c echo.Context) error {
|
||||
if limitStr != "" {
|
||||
if l, err := strconv.Atoi(limitStr); err == nil {
|
||||
limit = int32(l)
|
||||
// Enforce maximum limit
|
||||
if limit > maxPaginationLimit {
|
||||
limit = maxPaginationLimit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
offset := int32(0)
|
||||
if offsetStr != "" {
|
||||
if o, err := strconv.Atoi(offsetStr); err == nil {
|
||||
if o < 0 {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "offset cannot be negative"})
|
||||
}
|
||||
offset = int32(o)
|
||||
}
|
||||
}
|
||||
@@ -590,6 +601,15 @@ func (h *Handler) ListMediaItems(c echo.Context) error {
|
||||
limit = 50
|
||||
}
|
||||
|
||||
// Enforce maximum limit
|
||||
if limit > maxPaginationLimit {
|
||||
limit = maxPaginationLimit
|
||||
}
|
||||
|
||||
if offset < 0 {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "offset cannot be negative"})
|
||||
}
|
||||
|
||||
if libraryID != "" {
|
||||
libUUID, err := uuid.Parse(libraryID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user