aboutsummaryrefslogtreecommitdiff
path: root/internal/crud/crud_test.go
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-08-20 14:03:30 +0530
committerishaan <contact@ishaan.cc>2026-08-20 14:03:30 +0530
commit7caaf4c96d4f657d9193baf48e9745c9f68181c9 (patch)
treeabcdb288540f465b1e39a9c6fa4b6bf94cefa65e /internal/crud/crud_test.go
parentc7b546fb94687a6cbe9bab9db041d15104c5111d (diff)
fix: wipe temp file before removal to prevent leak
Diffstat (limited to 'internal/crud/crud_test.go')
-rw-r--r--internal/crud/crud_test.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/internal/crud/crud_test.go b/internal/crud/crud_test.go
index d0ccf13..f9e5f47 100644
--- a/internal/crud/crud_test.go
+++ b/internal/crud/crud_test.go
@@ -232,6 +232,45 @@ func TestEditNoteSSH_NotFound(t *testing.T) {
assert.Error(t, err)
}
+func TestEditNoteSSH_NoTempFileLeak(t *testing.T) {
+ pub, priv, err := ed25519.GenerateKey(rand.Reader)
+ require.NoError(t, err)
+ sshPub, err := ssh.NewPublicKey(pub)
+ require.NoError(t, err)
+ defaultPub := ssh.MarshalAuthorizedKey(sshPub)
+
+ dir := t.TempDir()
+ output := filepath.Join(dir, "test.eno")
+
+ old := setScannerInput("y\nn\n")
+ err = NewNoteSSH(output, defaultPub)
+ restoreScanner(old)
+ require.NoError(t, err)
+
+ block, err := ssh.MarshalPrivateKey(priv, "")
+ require.NoError(t, err)
+ var pemBuf bytes.Buffer
+ require.NoError(t, pem.Encode(&pemBuf, block))
+ privKeyPath := filepath.Join(dir, "id_ed25519")
+ require.NoError(t, os.WriteFile(privKeyPath, pemBuf.Bytes(), 0600))
+
+ tempDirs := []string{"/dev/shm", os.TempDir()}
+ countTempFiles := func() int {
+ n := 0
+ for _, d := range tempDirs {
+ matches, _ := filepath.Glob(filepath.Join(d, "eno-*"))
+ n += len(matches)
+ }
+ return n
+ }
+
+ before := countTempFiles()
+ err = EditNoteSSH(output, privKeyPath, "true")
+ require.NoError(t, err)
+
+ assert.Equal(t, before, countTempFiles(), "temp file should be wiped and removed, not leaked")
+}
+
func TestEditNoteSSH_WrongAuthMode(t *testing.T) {
dir := t.TempDir()
output := filepath.Join(dir, "test.eno")