Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(knowledge): cap the hybrid-search body at 1 MiB (#4731)
yetugeee49d73560f9
diff --git a/internal/handler/hybrid_search_body_limit_test.go b/internal/handler/hybrid_search_body_limit_test.gonew file mode 100644index 000000000..d68f0521a--- /dev/null+++ b/internal/handler/hybrid_search_body_limit_test.go@@ -0,0 +1,66 @@+package handler++import (+ "net/http"+ "strings"+ "testing"+)++// The search body carries a query, filters and an optional precomputed+// `query_embedding`. HybridSearch is reachable by any Viewer of the knowledge+// base, so an uncapped read lets a low-privilege caller allocate an arbitrary+// body per request; the Logger middleware bounds only its own log line.+func TestHybridSearchRejectsAnOversizedBody(t *testing.T) {+ svc := &hybridSearchTestService{}+ body := `{"query_text":"` + strings.Repeat("a", hybridSearchJSONMaxBytes) + `"}`++ response := performHybridSearchRequest(svc, body)++ if response.Code != http.StatusBadRequest {+ t.Fatalf("expected 400 for an oversized body, got %d body=%s", response.Code, response.Body.String())+ }+ if svc.searchCalls != 0 {+ t.Fatalf("an oversized body must not reach the search service, got %d calls", svc.searchCalls)+ }+ if !strings.Contains(response.Body.String(), "too large") {+ t.Fatalf("expected the size error, got body=%s", response.Body.String())+ }+}++// The cap must accept the widest realistic vector: an 8192-dimension float32+// array is roughly 160 KiB of JSON, well inside 1 MiB.+func TestHybridSearchAcceptsAWideVectorBody(t *testing.T) {+ embedding := make([]string, 8192)+ for i := range embedding {+ embedding[i] = "0.5"+ }+ body := `{"query_embedding":[` + strings.Join(embedding, ",") + `],"disable_keywords_match":true}`+ if len(body) >= hybridSearchJSONMaxBytes {+ t.Fatalf("test fixture is %d bytes, over the %d cap", len(body), hybridSearchJSONMaxBytes)+ }++ svc := &hybridSearchTestService{}+ response := performHybridSearchRequest(svc, body)++ if response.Code != http.StatusOK {+ t.Fatalf("expected 200 for a wide vector, got %d body=%s", response.Code, response.Body.String())+ }+ if len(svc.searchParams.QueryEmbedding) != 8192 {+ t.Fatalf("embedding length = %d, want 8192", len(svc.searchParams.QueryEmbedding))+ }+}++// A malformed but small body must still report as invalid parameters, not as a+// size limit: the cap changes only what is rejected for size.+func TestHybridSearchKeepsTheParseErrorForMalformedBodies(t *testing.T) {+ svc := &hybridSearchTestService{}++ response := performHybridSearchRequest(svc, `{"query_text":`)++ if response.Code != http.StatusBadRequest {+ t.Fatalf("expected 400, got %d", response.Code)+ }+ if strings.Contains(response.Body.String(), "too large") {+ t.Fatalf("a syntax error must not be reported as a size limit, got %s", response.Body.String())+ }+}diff --git a/internal/handler/knowledgebase.go b/internal/handler/knowledgebase.goindex e31919cd8..e6e58a344 100644--- a/internal/handler/knowledgebase.go+++ b/internal/handler/knowledgebase.go@@ -383,8 +383,13 @@ func (h *KnowledgeBaseHandler) HybridSearch(c *gin.Context) {}// Parse request body+ limitJSONBody(c, hybridSearchJSONMaxBytes)var req types.SearchParamsif err := c.ShouldBindJSON(&req); err != nil {+ if isRequestBodyTooLarge(err) {+ _ = c.Error(apperrors.NewBadRequestError("request body is too large"))+ return+ }logger.Error(ctx, "Failed to parse request parameters", err)c.Error(apperrors.NewBadRequestError("Invalid request parameters").WithDetails(err.Error()))returndiff --git a/internal/handler/upload_limit.go b/internal/handler/upload_limit.goindex 9840ebcf4..e83ba59a5 100644--- a/internal/handler/upload_limit.go+++ b/internal/handler/upload_limit.go@@ -29,6 +29,17 @@ const skillSourceJSONMaxBytes = 64 << 10// memory as the process will surrender.const knowledgeBaseJSONMaxBytes = 1 << 20+// hybridSearchJSONMaxBytes caps a hybrid-search body.+//+// The body is a query, filters and an optional precomputed `query_embedding`.+// That vector is the only large field: an 8192-dimension float32 array is about+// 160 KiB of JSON, so 1 MiB leaves room for the widest embedding model plus a+// generous filter list while still bounding what one request can allocate.+// HybridSearch is reachable by any Viewer of the knowledge base, so the cap+// applies to a much broader audience than the knowledge-base lifecycle routes,+// which need manage_kbs.+const hybridSearchJSONMaxBytes = 1 << 20+// limitUploadBody caps the request body before anything parses it.//// It has to run before FormFile rather than after: ParseMultipartForm buffers