Also copy value in Get() to prevent caller mutations

Returning the stored slice directly allows callers to mutate the stored
data, which defeats the purpose of the defensive copying in Set().

Since this package doesn't have access to gofiber/utils, we manually
copy the slice using make() and copy().

This completes the fix by ensuring stored data cannot be corrupted
either on input (Set) or accessed mutably on output (Get).
This commit is contained in:
Jason McNeil
2025-10-31 07:26:53 -03:00
parent 3b32d861ca
commit 4e90d76c39

View File

@@ -53,7 +53,10 @@ func (s *Storage) Get(key string) ([]byte, error) {
return nil, nil
}
return v.data, nil
// Return a copy to prevent callers from mutating stored data
valCopy := make([]byte, len(v.data))
copy(valCopy, v.data)
return valCopy, nil
}
// GetWithContext gets value by key (dummy context support)