Skip to content

Commit a3aacaa

Browse files
committed
sec(age): zero out cached credentials
Signed-off-by: Caio Rocha de Oliveira <caiorocoli@gmail.com>
1 parent 9b7e8c4 commit a3aacaa

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

age/keysource.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,21 @@ func getUserConfigDir() (string, error) {
399399
return os.UserConfigDir()
400400
}
401401

402+
// ClearFileStreamCache wipes the cached stream secrets from memory by overwriting
403+
// the byte slices with zeros before deleting them from the map.
404+
// This is critical for security to prevent keys from lingering in RAM.
405+
func ClearFileStreamCache() {
406+
fileStreamCache.Range(func(key, value interface{}) bool {
407+
if byte, ok := value.([]byte); ok {
408+
for i := range byte {
409+
byte[i] = 0
410+
}
411+
}
412+
fileStreamCache.Delete(key)
413+
return true
414+
})
415+
}
416+
402417
// reads a file from the given path, if it is a stream (e.g., /dev/fd/* or /proc/*)
403418
// it caches the content in memory to avoid issues with multiple reads from the same stream.
404419
func readStreamSafe(path string) ([]byte, error) {

cmd/sops/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func warnMoreThanOnePositionalArgument(c *cli.Context) {
7575
}
7676

7777
func main() {
78+
defer age.ClearFileStreamCache()
79+
7880
cli.VersionPrinter = version.PrintVersion
7981
app := cli.NewApp()
8082

0 commit comments

Comments
 (0)