1+ // Package bdcache provides a high-performance cache with S3-FIFO eviction and optional persistence.
12package bdcache
23
34import (
@@ -84,7 +85,7 @@ func (c *Cache[K, V]) warmup(ctx context.Context) {
8485
8586// Get retrieves a value from the cache.
8687// It first checks the memory cache, then falls back to persistence if available.
87- func (c * Cache [K , V ]) Get (ctx context.Context , key K ) (V , bool , error ) {
88+ func (c * Cache [K , V ]) Get (ctx context.Context , key K ) (value V , found bool , err error ) {
8889 // Check memory first
8990 if val , ok := c .memory .get (key ); ok {
9091 return val , true , nil
@@ -96,6 +97,13 @@ func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, bool, error) {
9697 return zero , false , nil
9798 }
9899
100+ // Validate key before accessing persistence (security: prevent path traversal)
101+ if err := c .persist .ValidateKey (key ); err != nil {
102+ slog .Warn ("invalid key for persistence" , "error" , err , "key" , key )
103+ var zero V
104+ return zero , false , nil
105+ }
106+
99107 // Check persistence
100108 val , expiry , found , err := c .persist .Load (ctx , key )
101109 if err != nil {
@@ -156,6 +164,11 @@ func (c *Cache[K, V]) Delete(ctx context.Context, key K) {
156164
157165 // Remove from persistence if available
158166 if c .persist != nil {
167+ // Validate key before accessing persistence (security: prevent path traversal)
168+ if err := c .persist .ValidateKey (key ); err != nil {
169+ slog .Warn ("invalid key for persistence delete" , "error" , err , "key" , key )
170+ return
171+ }
159172 if err := c .persist .Delete (ctx , key ); err != nil {
160173 // Log error but don't fail - graceful degradation
161174 slog .Warn ("persistence delete failed" , "error" , err , "key" , key )
0 commit comments