Skip to content

Commit 4616163

Browse files
anupam42claude
andcommitted
fix: include user key in search cache to isolate results per user
Previously the cache key only included cluster+query+limit, so two users with different RBAC permissions could receive each other's cached search results. Adding user.Key() scopes the cache per user. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 8bd3f77 commit 4616163

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

pkg/handlers/search_handler.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/zxh326/kite/pkg/common"
1616
"github.com/zxh326/kite/pkg/handlers/resources"
1717
"github.com/zxh326/kite/pkg/middleware"
18+
"github.com/zxh326/kite/pkg/model"
1819
"github.com/zxh326/kite/pkg/utils"
1920
"golang.org/x/sync/errgroup"
2021
"k8s.io/klog/v2"
@@ -52,8 +53,8 @@ func NewSearchHandler() *SearchHandler {
5253
}
5354
}
5455

55-
func (h *SearchHandler) createCacheKey(clusterName, query string, limit int) string {
56-
return fmt.Sprintf("search:%s:%d:%s", clusterName, limit, normalizeSearchQuery(query))
56+
func (h *SearchHandler) createCacheKey(clusterName, userKey, query string, limit int) string {
57+
return fmt.Sprintf("search:%s:%s:%d:%s", clusterName, userKey, limit, normalizeSearchQuery(query))
5758
}
5859

5960
func (h *SearchHandler) Search(c *gin.Context, query string, limit int) ([]common.SearchResult, error) {
@@ -119,7 +120,8 @@ func (h *SearchHandler) Search(c *gin.Context, query string, limit int) ([]commo
119120
// Only cache results when no failure (panic or error) occurred — avoids
120121
// caching incomplete results that would be served as valid 200 OK for the TTL.
121122
if !hadFailure.Load() {
122-
h.cache.Add(h.createCacheKey(getSearchClusterName(c), query, limit), allResults)
123+
user := c.MustGet("user").(model.User)
124+
h.cache.Add(h.createCacheKey(getSearchClusterName(c), user.Key(), query, limit), allResults)
123125
}
124126
return allResults, nil
125127
}
@@ -140,7 +142,8 @@ func (h *SearchHandler) GlobalSearch(c *gin.Context) {
140142
}
141143
limit = normalizeSearchLimit(limit)
142144

143-
cacheKey := h.createCacheKey(getSearchClusterName(c), query, limit)
145+
user := c.MustGet("user").(model.User)
146+
cacheKey := h.createCacheKey(getSearchClusterName(c), user.Key(), query, limit)
144147

145148
if cachedResults, found := h.cache.Get(cacheKey); found {
146149
response := SearchResponse{

pkg/handlers/search_handler_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/zxh326/kite/pkg/common"
1414
"github.com/zxh326/kite/pkg/handlers/resources"
1515
"github.com/zxh326/kite/pkg/middleware"
16+
"github.com/zxh326/kite/pkg/model"
1617
)
1718

1819
func TestNormalizeSearchQuery(t *testing.T) {
@@ -115,6 +116,7 @@ func TestGlobalSearchNegativeLimitDoesNotPanic(t *testing.T) {
115116
rec := httptest.NewRecorder()
116117
ctx, _ := gin.CreateTestContext(rec)
117118
ctx.Request = httptest.NewRequest(http.MethodGet, "/search?q=po&limit=-1", nil)
119+
ctx.Set("user", model.AnonymousUser)
118120

119121
handler := NewSearchHandler()
120122

@@ -188,6 +190,7 @@ func newSearchContext(t *testing.T, clusterName string) *gin.Context {
188190
if clusterName != "" {
189191
ctx.Set(middleware.ClusterNameKey, clusterName)
190192
}
193+
ctx.Set("user", model.AnonymousUser)
191194
return ctx
192195
}
193196

@@ -212,6 +215,7 @@ func performGlobalSearch(t *testing.T, handler *SearchHandler, clusterName, targ
212215
if clusterName != "" {
213216
ctx.Set(middleware.ClusterNameKey, clusterName)
214217
}
218+
ctx.Set("user", model.AnonymousUser)
215219

216220
handler.GlobalSearch(ctx)
217221

0 commit comments

Comments
 (0)