From 2b4c0e362f11e3ef30c1125a42d18ddb304ac9f6 Mon Sep 17 00:00:00 2001 From: linus-liu-web <395141741@qq.com> Date: Sat, 17 Oct 2026 22:48:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(wiki):=20=E5=88=97=20wiki=20=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=20page=5Fsize=20=E8=A1=A5=E4=B8=8A=E7=95=8C?= =?UTF-8?q?=EF=BC=8C=E4=B8=8E=E5=85=84=E5=BC=9F=E8=B7=AF=E5=BE=84=E4=B8=80?= =?UTF-8?q?=E8=87=B4=20(#4407)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/handler/wiki_page.go | 14 +++ .../handler/wiki_page_list_pagesize_test.go | 119 ++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 internal/handler/wiki_page_list_pagesize_test.go diff --git a/internal/handler/wiki_page.go b/internal/handler/wiki_page.go index 6ecb8326..2d6a0d45 100644 --- a/internal/handler/wiki_page.go +++ b/internal/handler/wiki_page.go @@ -76,6 +76,14 @@ func getSlugParam(c *gin.Context) string { return strings.TrimSpace(slug) } +// wikiListMaxPageSize caps how many wiki pages one list request may return. +// The repository only floors page_size (no ceiling), so an unbounded value +// such as ?page_size=100000000 reaches the SQL LIMIT and pulls every page of +// the knowledge base — Content column included — into memory in a single +// request. 200 mirrors the cap ListRevisions applies to its own limit and +// stays above the largest batch the frontend fetches. +const wikiListMaxPageSize = 200 + // ListPages godoc // @Summary List wiki pages // @Description List wiki pages with optional filtering and pagination @@ -102,6 +110,12 @@ func (h *WikiPageHandler) ListPages(c *gin.Context) { page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20")) + if pageSize < 1 { + pageSize = 20 + } + if pageSize > wikiListMaxPageSize { + pageSize = wikiListMaxPageSize + } categoryPath := parseWikiCategoryPath(c.Query("category_path")) // folder_id is an exact placement filter. An explicitly-present but empty // value means "root" (folder_id = ''); an absent param means "no filter". diff --git a/internal/handler/wiki_page_list_pagesize_test.go b/internal/handler/wiki_page_list_pagesize_test.go new file mode 100644 index 00000000..e462797d --- /dev/null +++ b/internal/handler/wiki_page_list_pagesize_test.go @@ -0,0 +1,119 @@ +package handler + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/Tencent/WeKnora/internal/types/interfaces" +) + +// stubWikiPageService captures the list request the handler builds. Only +// ListPages is implemented; the embedded interface keeps every other method +// nil-panicky on purpose, so a test that unexpectedly reaches an un-stubbed +// method fails loudly instead of silently returning zero values. +type stubWikiPageService struct { + interfaces.WikiPageService + listPages func(ctx context.Context, req *types.WikiPageListRequest) (*types.WikiPageListResponse, error) +} + +func (s *stubWikiPageService) ListPages( + ctx context.Context, req *types.WikiPageListRequest, +) (*types.WikiPageListResponse, error) { + return s.listPages(ctx, req) +} + +// stubWikiKBService serves only the KB lookup validateWikiKB performs, and +// returns a wiki-enabled KB so the handler proceeds past validation. +type stubWikiKBService struct { + interfaces.KnowledgeBaseService +} + +func (s *stubWikiKBService) GetKnowledgeBaseByID(_ context.Context, id string) (*types.KnowledgeBase, error) { + return &types.KnowledgeBase{ + ID: id, + IndexingStrategy: types.IndexingStrategy{WikiEnabled: true}, + }, nil +} + +// newWikiListTestRouter mounts ListPages with just the tenant key the handler +// reads via c.GetUint64, without pulling in the production RBAC chain. +func newWikiListTestRouter(wikiSvc interfaces.WikiPageService) *gin.Engine { + gin.SetMode(gin.TestMode) + r := gin.New() + r.Use(func(c *gin.Context) { + c.Set(types.TenantIDContextKey.String(), uint64(1)) + c.Next() + }) + h := &WikiPageHandler{wikiService: wikiSvc, kbService: &stubWikiKBService{}} + r.GET("/knowledgebase/:kb_id/wiki/pages", h.ListPages) + return r +} + +// wikiListSizeUpperBound mirrors the sibling revision-list cap in this file +// (ListRevisions clamps its limit to the same value). It is pinned here as a +// literal instead of referencing the handler constant on purpose: this test +// must still compile — and fail on the assertion below — against the unfixed +// handler, which is what makes the regression guard meaningful. +const wikiListSizeUpperBound = 200 + +// TestWikiListPagesClampsPageSize pins the fix for the unbounded page_size on +// the wiki list endpoint. The repository floors page_size but has no ceiling, +// so before the fix ?page_size=100000000 reached the service untouched and the +// SQL LIMIT pulled every page of the KB — Content column included — into +// memory in a single request. The handler now clamps like its sibling list +// endpoints do. +func TestWikiListPagesClampsPageSize(t *testing.T) { + cases := []struct { + name string + query string + want int + }{ + {name: "absent falls back to default", query: "", want: 20}, + {name: "zero falls back to default", query: "?page_size=0", want: 20}, + {name: "negative falls back to default", query: "?page_size=-5", want: 20}, + {name: "in-range value is preserved", query: "?page_size=50", want: 50}, + {name: "cap is preserved", query: "?page_size=200", want: wikiListSizeUpperBound}, + {name: "oversized value is clamped", query: "?page_size=100000000", want: wikiListSizeUpperBound}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var got *types.WikiPageListRequest + wikiSvc := &stubWikiPageService{ + listPages: func( + _ context.Context, req *types.WikiPageListRequest, + ) (*types.WikiPageListResponse, error) { + got = req + return &types.WikiPageListResponse{PageSize: req.PageSize}, nil + }, + } + + w := httptest.NewRecorder() + req := httptest.NewRequest( + http.MethodGet, "/knowledgebase/kb1/wiki/pages"+tc.query, nil, + ) + newWikiListTestRouter(wikiSvc).ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", w.Code, w.Body.String()) + } + if got == nil { + t.Fatal("ListPages was not called") + } + if got.PageSize < 1 || got.PageSize > wikiListSizeUpperBound { + t.Fatalf( + "page_size must reach the service within [1, %d], got %d", + wikiListSizeUpperBound, got.PageSize, + ) + } + if got.PageSize != tc.want { + t.Fatalf("page_size = %d, want %d", got.PageSize, tc.want) + } + }) + } +}