fix(chat): 消息历史与搜索的 limit 补上下界,limit=-1 不再取消 SQL LIMIT (#4409)

main
linus-liu-web 2026-10-17 22:48:10 +08:00 committed by GitHub
parent e6dc60be32
commit 5044ef69f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 182 additions and 4 deletions

View File

@ -19,6 +19,32 @@ import (
secutils "github.com/Tencent/WeKnora/internal/utils"
)
const (
// defaultMessagesPageSize is the page size used when a client omits the
// limit or sends a non-positive one.
defaultMessagesPageSize = 20
// maxMessagesPageSize bounds the client-supplied page size of the message
// read endpoints. Without an upper bound a single request can ask for an
// arbitrarily large page; the search path additionally multiplies the
// forwarded value for a bounded over-fetch.
maxMessagesPageSize = 200
)
// clampMessagesPageSize bounds a client-supplied page size to
// [1, maxMessagesPageSize], substituting defaultMessagesPageSize for
// non-positive input. Callers must clamp at the edge: the repository only
// guards `limit <= 0` upstream and GORM omits the LIMIT clause entirely for
// negative values, so an unclamped -1 reads the whole table.
func clampMessagesPageSize(limit int) int {
if limit < 1 {
return defaultMessagesPageSize
}
if limit > maxMessagesPageSize {
return maxMessagesPageSize
}
return limit
}
// MessageHandler handles HTTP requests related to messages within chat sessions
// It provides endpoints for loading and managing message history
type MessageHandler struct {
@ -134,7 +160,7 @@ func (h *MessageHandler) LoadMessages(c *gin.Context) {
// Get path parameters and query parameters
sessionID := secutils.SanitizeForLog(c.Param("session_id"))
limit := secutils.SanitizeForLog(c.DefaultQuery("limit", "20"))
limit := secutils.SanitizeForLog(c.DefaultQuery("limit", strconv.Itoa(defaultMessagesPageSize)))
beforeTimeStr := secutils.SanitizeForLog(c.DefaultQuery("before_time", ""))
logger.Infof(ctx, "Loading messages params, session ID: %s, limit: %s, before time: %s",
@ -147,12 +173,18 @@ func (h *MessageHandler) LoadMessages(c *gin.Context) {
return
}
// Parse limit parameter with fallback to default
// Parse limit parameter with fallback to default, then clamp it. A
// non-positive value must not reach the repository: GORM only emits a
// LIMIT clause for `limit >= 0`, so `limit=-1` would load the entire
// session history (and attach artifacts for every row) in one request.
limitInt, convErr := strconv.Atoi(limit)
if convErr != nil {
logger.Warnf(ctx, "Invalid limit value, using default value 20, input: %s", limit)
limitInt = 20
// Note: on out-of-range input Atoi returns the boundary value rather
// than zero, so the default has to be assigned explicitly here.
logger.Warnf(ctx, "Invalid limit value, using default value %d, input: %s", defaultMessagesPageSize, limit)
limitInt = defaultMessagesPageSize
}
limitInt = clampMessagesPageSize(limitInt)
// If no beforeTime is provided, retrieve the most recent messages
if beforeTimeStr == "" {
@ -313,6 +345,14 @@ func (h *MessageHandler) SearchMessages(c *gin.Context) {
return
}
// Clamp the requested page size before it reaches the service. The
// service later applies a bounded over-fetch (limit*3) to the keyword
// search so it has enough candidates to rank; that multiplier is
// intentional and is left untouched, so the SQL LIMIT is at most
// 3*maxMessagesPageSize. Without this clamp an unbounded request would
// inflate the query by the same factor.
request.Limit = clampMessagesPageSize(request.Limit)
params := &types.MessageSearchParams{
Query: request.Query,
Mode: types.MessageSearchMode(request.Mode),

View File

@ -0,0 +1,138 @@
package handler
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/Tencent/WeKnora/internal/middleware"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// messageLimitCap is the upper bound both message endpoints must enforce.
// Kept as a test-local literal (not a reference to the production constant)
// so the red-before/green-after pair works without the fix present.
const messageLimitCap = 200
// messageLimitDefault is the fallback applied when the requested limit is
// absent or non-positive.
const messageLimitDefault = 20
// searchParamsStub captures the params the search handler forwards downstream.
type searchParamsStub struct {
interfaces.MessageService
got chan *types.MessageSearchParams
}
func (s *searchParamsStub) SearchMessages(
_ context.Context, params *types.MessageSearchParams,
) (*types.MessageSearchResult, error) {
s.got <- params
return &types.MessageSearchResult{}, nil
}
// TestLoadMessagesClampsLimit covers GET /messages/:session_id/load on both
// branches (recent and before_time). A request without bounds used to reach
// the repository as-is; a negative limit cancels GORM's LIMIT clause
// (`limit >= 0` guard), returning the whole session history in one request.
func TestLoadMessagesClampsLimit(t *testing.T) {
cases := []struct {
name string
url string
want int
}{
{"non_numeric_falls_back_to_default", "/messages/s1/load?limit=abc", messageLimitDefault},
{"out_of_range_falls_back_to_default", "/messages/s1/load?limit=99999999999999999999", messageLimitDefault},
{"empty_falls_back_to_default", "/messages/s1/load?limit=", messageLimitDefault},
{"missing_falls_back_to_default", "/messages/s1/load", messageLimitDefault},
{"zero_falls_back_to_default", "/messages/s1/load?limit=0", messageLimitDefault},
{"negative_cannot_cancel_limit", "/messages/s1/load?limit=-1", messageLimitDefault},
{"large_negative_cannot_cancel_limit", "/messages/s1/load?limit=-1000000", messageLimitDefault},
{"huge_is_capped", "/messages/s1/load?limit=100000000", messageLimitCap},
{"one_above_cap_is_capped", "/messages/s1/load?limit=201", messageLimitCap},
{"at_cap_is_kept", "/messages/s1/load?limit=200", messageLimitCap},
{"in_range_is_kept", "/messages/s1/load?limit=7", 7},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := make(chan int, 1)
svc := &stubMessageService{
getRecent: func(_ context.Context, _ string, limit int) ([]*types.Message, error) {
got <- limit
return []*types.Message{}, nil
},
}
newMessageTestRouter(svc).ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet, tc.url, nil))
if limit := <-got; limit != tc.want {
t.Fatalf("load forwarded limit=%d; want %d", limit, tc.want)
}
})
}
}
// TestLoadMessagesClampsLimitBeforeTime pins the same clamp on the
// before_time branch, which forwards to a different service method.
func TestLoadMessagesClampsLimitBeforeTime(t *testing.T) {
got := make(chan int, 1)
svc := &stubMessageService{
getBeforeTime: func(_ context.Context, _ string, _ time.Time, limit int) ([]*types.Message, error) {
got <- limit
return []*types.Message{}, nil
},
}
newMessageTestRouter(svc).ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet,
"/messages/s1/load?limit=-1&before_time=2026-05-20T09:43:37Z", nil))
if limit := <-got; limit != messageLimitDefault {
t.Fatalf("load(before_time) forwarded limit=%d; want %d", limit, messageLimitDefault)
}
}
// TestSearchMessagesClampsLimitBeforeForwarding covers POST /messages/search.
// The service multiplies the forwarded limit (bounded over-fetch), so an
// unbounded request used to inflate the SQL LIMIT by the same factor.
func TestSearchMessagesClampsLimitBeforeForwarding(t *testing.T) {
cases := []struct {
name string
body string
want int
}{
{"missing_falls_back_to_default", `{"query":"x"}`, messageLimitDefault},
{"zero_falls_back_to_default", `{"query":"x","limit":0}`, messageLimitDefault},
{"negative_falls_back_to_default", `{"query":"x","limit":-5}`, messageLimitDefault},
{"huge_is_capped", `{"query":"x","limit":100000000}`, messageLimitCap},
{"one_above_cap_is_capped", `{"query":"x","limit":201}`, messageLimitCap},
{"at_cap_is_kept", `{"query":"x","limit":200}`, messageLimitCap},
{"in_range_is_kept", `{"query":"x","limit":5}`, 5},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := make(chan *types.MessageSearchParams, 1)
svc := &searchParamsStub{got: got}
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(middleware.ErrorHandler())
h := &MessageHandler{MessageService: svc}
r.POST("/messages/search", h.SearchMessages)
r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(
http.MethodPost, "/messages/search", bytes.NewBufferString(tc.body)))
if params := <-got; params.Limit != tc.want {
t.Fatalf("search forwarded limit=%d; want %d", params.Limit, tc.want)
}
})
}
}