Skip to content

Commit a180136

Browse files
committed
fix: use sentinel hash for empty Vary in cache key
This commit ensures a sentinel value is appended to the URL key when no Vary headers are present. This prevents collisions between the base URL key and the vary key, avoiding self-referential cache entries.
1 parent e30db2d commit a180136

2 files changed

Lines changed: 5 additions & 10 deletions

File tree

internal/normalization.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,11 @@ func (f VaryKeyerFunc) VaryKey(urlKey string, varyHeaders map[string]string) str
324324

325325
func NewVaryKeyer() VaryKeyer { return VaryKeyerFunc(makeVaryKey) }
326326

327+
const noVaryHash = "0"
328+
327329
func makeVaryKey(urlKey string, varyHeaders map[string]string) string {
328330
if len(varyHeaders) == 0 {
329-
return urlKey // No vary headers means no variation
331+
return urlKey + "#" + noVaryHash // No Vary headers, so no variations
330332
}
331333
varyHash := makeVaryHash(varyHeaders)
332334
return urlKey + "#" + strconv.FormatUint(varyHash, 10)

internal/normalization_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,12 @@ func Test_makeVaryKey(t *testing.T) {
101101
wantPrefix string
102102
wantHash func() string
103103
}{
104-
{
105-
name: "no vary headers (empty map)",
106-
urlKey: urlKey,
107-
vary: map[string]string{},
108-
wantPrefix: urlKey,
109-
wantHash: func() string { return "" },
110-
},
111104
{
112105
name: "no vary headers (nil map)",
113106
urlKey: urlKey,
114107
vary: nil,
115-
wantPrefix: urlKey,
116-
wantHash: func() string { return "" },
108+
wantPrefix: urlKey + "#",
109+
wantHash: func() string { return noVaryHash },
117110
},
118111
{
119112
name: "single header",

0 commit comments

Comments
 (0)