Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(handler): validate knowledge search pagination

PP1c5b6c10971f4
diff --git a/internal/handler/knowledge.go b/internal/handler/knowledge.go
index ae33084c8..7e4e4e8c0 100644
--- a/internal/handler/knowledge.go
+++ b/internal/handler/knowledge.go
@@ -2206,8 +2206,8 @@ func (h *KnowledgeHandler) UpdateImageInfo(c *gin.Context) {
// @Accept json
// @Produce json
// @Param keyword query string false "Keyword to search"
-// @Param offset query int false "Offset for pagination"
-// @Param limit query int false "Limit for pagination (default 20)"
+// @Param offset query int false "Offset for pagination (minimum 0)" minimum(0)
+// @Param limit query int false "Limit for pagination (default 20, maximum 100)" minimum(1) maximum(100)
// @Param file_types query string false "Comma-separated file extensions to filter (e.g., csv,xlsx)"
// @Param agent_id query string false "Shared agent ID (search within agent's KB scope)"
// @Param recent query bool false "Return recent files when keyword is empty"
@@ -2235,8 +2235,10 @@ func (h *KnowledgeHandler) SearchKnowledge(c *gin.Context) {
return
}
keyword = strings.TrimSpace(keyword)
- offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0"))
- limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
+ offset, limit, ok := parseOffsetPagination(c)
+ if !ok {
+ return
+ }
var fileTypes []string
if fileTypesStr := c.Query("file_types"); fileTypesStr != "" {
diff --git a/internal/handler/list_pagination.go b/internal/handler/list_pagination.go
index d574b7447..3e470db6b 100644
--- a/internal/handler/list_pagination.go
+++ b/internal/handler/list_pagination.go
@@ -13,6 +13,8 @@ import (
const (
defaultListPageSize = 20
maxListPageSize = 100
+ defaultSearchLimit = 20
+ maxSearchLimit = 100
)
// parseListPagination parses ?page and ?page_size for tenant-scoped list
@@ -40,3 +42,27 @@ func parseListPagination(c *gin.Context) (page, pageSize int, ok bool) {
}
return page, pageSize, true
}
+
+// parseOffsetPagination parses ?offset and ?limit for offset-based search
+// endpoints. Omitted keys default to offset=0 and limit=20.
+func parseOffsetPagination(c *gin.Context) (offset, limit int, ok bool) {
+ limit = defaultSearchLimit
+
+ if s := strings.TrimSpace(c.Query("offset")); s != "" {
+ parsedOffset, err := strconv.Atoi(s)
+ if err != nil || parsedOffset < 0 {
+ c.Error(apperrors.NewValidationError("offset must be a non-negative integer"))
+ return 0, 0, false
+ }
+ offset = parsedOffset
+ }
+ if s := strings.TrimSpace(c.Query("limit")); s != "" {
+ parsedLimit, err := strconv.Atoi(s)
+ if err != nil || parsedLimit < 1 || parsedLimit > maxSearchLimit {
+ c.Error(apperrors.NewValidationError(fmt.Sprintf("limit must be between 1 and %d", maxSearchLimit)))
+ return 0, 0, false
+ }
+ limit = parsedLimit
+ }
+ return offset, limit, true
+}
diff --git a/internal/handler/list_pagination_test.go b/internal/handler/list_pagination_test.go
new file mode 100644
index 000000000..4d0ffefac
--- /dev/null
+++ b/internal/handler/list_pagination_test.go
@@ -0,0 +1,77 @@
+package handler
+
+import (
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+)
+
+func TestParseOffsetPagination(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ tests := []struct {
+ name string
+ query string
+ wantOffset int
+ wantLimit int
+ wantOK bool
+ wantMessage string
+ }{
+ {name: "defaults", wantOffset: 0, wantLimit: 20, wantOK: true},
+ {name: "valid", query: "?offset=40&limit=50", wantOffset: 40, wantLimit: 50, wantOK: true},
+ {name: "invalid offset", query: "?offset=not-a-number", wantMessage: "offset must be a non-negative integer"},
+ {name: "negative offset", query: "?offset=-1", wantMessage: "offset must be a non-negative integer"},
+ {name: "invalid limit", query: "?limit=not-a-number", wantMessage: "limit must be between 1 and 100"},
+ {name: "zero limit", query: "?limit=0", wantMessage: "limit must be between 1 and 100"},
+ {name: "limit too large", query: "?limit=101", wantMessage: "limit must be between 1 and 100"},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest("GET", "/knowledge/search"+tt.query, nil)
+
+ offset, limit, ok := parseOffsetPagination(c)
+ if ok != tt.wantOK {
+ t.Fatalf("ok = %v, want %v", ok, tt.wantOK)
+ }
+ if offset != tt.wantOffset || limit != tt.wantLimit {
+ t.Fatalf("got offset=%d limit=%d, want offset=%d limit=%d", offset, limit, tt.wantOffset, tt.wantLimit)
+ }
+ if tt.wantOK {
+ if len(c.Errors) != 0 {
+ t.Fatalf("unexpected errors: %v", c.Errors)
+ }
+ return
+ }
+ if len(c.Errors) != 1 {
+ t.Fatalf("got %d errors, want 1", len(c.Errors))
+ }
+ if !strings.Contains(c.Errors[0].Error(), tt.wantMessage) {
+ t.Fatalf("error = %q, want message containing %q", c.Errors[0].Error(), tt.wantMessage)
+ }
+ })
+ }
+}
+
+func TestSearchKnowledgeRejectsInvalidPagination(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ for _, query := range []string{
+ "?keyword=test&offset=-1",
+ "?keyword=test&limit=101",
+ } {
+ t.Run(query, func(t *testing.T) {
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest("GET", "/knowledge/search"+query, nil)
+
+ (&KnowledgeHandler{}).SearchKnowledge(c)
+
+ if len(c.Errors) != 1 {
+ t.Fatalf("got %d errors, want 1", len(c.Errors))
+ }
+ })
+ }
+}