fix: harden security resource lifecycles

This commit is contained in:
Andy Dixon
2026-07-29 12:37:02 +01:00
parent f0a1a0b8f2
commit b5c7528160
17 changed files with 460 additions and 76 deletions
@@ -3,6 +3,7 @@ package binsnoop
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
@@ -20,3 +21,27 @@ func TestReadFileLimitedRejectsOversizedFile(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}
func TestCarveUsesPrivatePermissions(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("POSIX permission bits are not enforced on Windows")
}
path := filepath.Join(t.TempDir(), "private", "carved.bin")
if err := Carve(path, []byte("secret"), 0, 6); err != nil {
t.Fatal(err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if got := info.Mode().Perm(); got != 0600 {
t.Fatalf("carved file permissions = %04o, want 0600", got)
}
dirInfo, err := os.Stat(filepath.Dir(path))
if err != nil {
t.Fatal(err)
}
if got := dirInfo.Mode().Perm(); got != 0700 {
t.Fatalf("carve directory permissions = %04o, want 0700", got)
}
}