Skip to content

Commit fb21d65

Browse files
patrislavclaude
andcommitted
fix(encryption): disable the DEK cache below a one-second TTL
expirable.LRU runs its eviction ticker at TTL/100. Under 100ns that panics outright; between 100ns and a millisecond it silently spins the cleanup loop against the mutex every get and put takes, which is the worse of the two. A bare integer literal is a legal time.Duration, so `TTL: 600` compiles, reads as ten minutes, and lands in that range. Warn and run uncached instead. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 7923314 commit fb21d65

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎encryption/cache.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ type CacheConfig struct {
1414
// MaxSize is the maximum number of DEKs to cache.
1515
MaxSize int
1616
// TTL is how long a cached DEK lives, counted from when it is stored.
17+
// Values below minCacheTTL disable the cache.
1718
TTL time.Duration
1819
}
1920

21+
// minCacheTTL keeps expirable.LRU's eviction ticker, which runs at TTL/100,
22+
// above 10ms. Below 100ns it panics outright, and a bare integer literal is a
23+
// legal time.Duration, so a TTL meant as seconds lands in that range.
24+
const minCacheTTL = time.Second
25+
2026
type dekCache struct {
2127
lru *expirable.LRU[string, []byte]
2228
group singleflight.Group

‎encryption/cache_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
package encryption
22

33
import (
4+
"log/slog"
45
"sync"
56
"testing"
67
"time"
78

89
"github.com/stretchr/testify/require"
910
)
1011

12+
func TestWithCache_DisablesCacheBelowMinTTL(t *testing.T) {
13+
quiet := slog.New(slog.DiscardHandler)
14+
15+
for _, cfg := range []CacheConfig{
16+
{MaxSize: 10, TTL: 0},
17+
{MaxSize: 0, TTL: time.Minute},
18+
{MaxSize: 10, TTL: 50}, // would panic expirable.LRU
19+
{MaxSize: 10, TTL: 600}, // reads as seconds, means nanoseconds
20+
} {
21+
p := NewPool(nil, nil, nil, nil, quiet, WithCache(cfg))
22+
require.Nil(t, p.cache, "cache should be disabled for %+v", cfg)
23+
}
24+
25+
p := NewPool(nil, nil, nil, nil, quiet, WithCache(CacheConfig{MaxSize: 10, TTL: minCacheTTL}))
26+
require.NotNil(t, p.cache)
27+
}
28+
1129
func TestDEKCache_GetPut(t *testing.T) {
1230
c := newDEKCache(10, time.Minute)
1331
dek := []byte("0123456789abcdef0123456789abcdef")

‎encryption/pool.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ type PoolOption func(*Pool)
5656
// and starts a background eviction goroutine that runs until the process exits.
5757
func WithCache(cfg CacheConfig) PoolOption {
5858
return func(p *Pool) {
59-
if cfg.MaxSize > 0 && cfg.TTL > 0 {
60-
p.cache = newDEKCache(cfg.MaxSize, cfg.TTL)
59+
if cfg.MaxSize <= 0 || cfg.TTL <= 0 {
60+
return
6161
}
62+
if cfg.TTL < minCacheTTL {
63+
p.logger.Warn("DEK cache disabled: TTL below minimum", "ttl", cfg.TTL, "minimum", minCacheTTL)
64+
return
65+
}
66+
p.cache = newDEKCache(cfg.MaxSize, cfg.TTL)
6267
}
6368
}
6469

0 commit comments

Comments
 (0)