Review round 2 on PR #190: swallowing root.Remove's error in removeSpecial left every test green (the false 'removed' alert also named the file). The test now asserts no message says 'removed'; red under that flip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
4.0 KiB
Go
105 lines
4.0 KiB
Go
package symlinkguard
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// hyperguild#179: the tripwire looked only for symlinks, so a FIFO (or
|
|
// socket) in the brain passed it. A FIFO at classification.yaml then made
|
|
// startup exit; at a note path it could hang readers that skip the handle
|
|
// check.
|
|
func TestScanReportsFIFOsAndSocketsAsSpecialFiles(t *testing.T) {
|
|
brain := t.TempDir()
|
|
require.NoError(t, os.MkdirAll(filepath.Join(brain, "wiki", "w"), 0o755))
|
|
require.NoError(t, syscall.Mkfifo(filepath.Join(brain, "classification.yaml"), 0o644))
|
|
require.NoError(t, syscall.Mkfifo(filepath.Join(brain, "wiki", "w", "f.md"), 0o644))
|
|
l, err := net.Listen("unix", filepath.Join(brain, "s.sock"))
|
|
require.NoError(t, err)
|
|
defer func() { _ = l.Close() }()
|
|
require.NoError(t, os.WriteFile(filepath.Join(brain, "wiki", "w", "n.md"), []byte("n"), 0o644))
|
|
|
|
r, err := Scan(brain)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, []string{"classification.yaml", "s.sock", "wiki/w/f.md"}, r.Special)
|
|
assert.False(t, r.Clean())
|
|
assert.Contains(t, r.String(), "special file")
|
|
}
|
|
|
|
// Watch removes a FIFO as it removes symlinks, and leaves a regular file
|
|
// (which Scan never reports) alone.
|
|
func TestWatchRemovesAFIFOAndLeavesAnUnreportedRegularFile(t *testing.T) {
|
|
brain := t.TempDir()
|
|
fifo := filepath.Join(brain, "f.md")
|
|
require.NoError(t, syscall.Mkfifo(fifo, 0o644))
|
|
regular := filepath.Join(brain, "n.md")
|
|
require.NoError(t, os.WriteFile(regular, []byte("n"), 0o644))
|
|
n := &recNotifier{}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go Watch(ctx, brain, 10*time.Millisecond, n)
|
|
require.Eventually(t, func() bool {
|
|
_, err := os.Lstat(fifo)
|
|
return os.IsNotExist(err)
|
|
}, 2*time.Second, 10*time.Millisecond, "the FIFO is removed")
|
|
require.Eventually(t, func() bool { return n.count() >= 1 }, 2*time.Second, 10*time.Millisecond)
|
|
b, err := os.ReadFile(regular)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "n", string(b))
|
|
n.mu.Lock()
|
|
defer n.mu.Unlock()
|
|
assert.Contains(t, n.msgs[0], "f.md")
|
|
}
|
|
|
|
// removeSpecial re-checks the type: a path that is a regular file or a
|
|
// directory at the re-check is not removed. (A swap between the re-check and
|
|
// the unlink can still remove one; Linux has no conditional unlink. The same
|
|
// window exists in removeSymlinks.)
|
|
func TestRemoveSpecialRefusesRegularFilesAndDirectories(t *testing.T) {
|
|
brain := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(brain, "n.md"), []byte("n"), 0o644))
|
|
require.NoError(t, os.MkdirAll(filepath.Join(brain, "d"), 0o755))
|
|
require.NoError(t, syscall.Mkfifo(filepath.Join(brain, "f.md"), 0o644))
|
|
removed, failed := removeSpecial(brain, []string{"n.md", "d", "f.md"})
|
|
assert.Equal(t, []string{"f.md"}, removed)
|
|
assert.Equal(t, []string{"n.md", "d"}, failed)
|
|
_, err := os.Stat(filepath.Join(brain, "n.md"))
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
// A special file Watch cannot remove (its directory is read-only) stays
|
|
// reported: the persistent-finding alert names it.
|
|
func TestWatchReportsASpecialFileItCannotRemove(t *testing.T) {
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("root ignores directory permissions")
|
|
}
|
|
brain := t.TempDir()
|
|
ro := filepath.Join(brain, "ro")
|
|
require.NoError(t, os.MkdirAll(ro, 0o755))
|
|
require.NoError(t, syscall.Mkfifo(filepath.Join(ro, "f.md"), 0o644))
|
|
require.NoError(t, os.Chmod(ro, 0o555))
|
|
t.Cleanup(func() { _ = os.Chmod(ro, 0o755) })
|
|
n := &recNotifier{}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go Watch(ctx, brain, 10*time.Millisecond, n)
|
|
require.Eventually(t, func() bool { return n.count() >= 1 }, 2*time.Second, 10*time.Millisecond)
|
|
_, err := os.Lstat(filepath.Join(ro, "f.md"))
|
|
require.NoError(t, err, "the FIFO could not be removed")
|
|
n.mu.Lock()
|
|
defer n.mu.Unlock()
|
|
assert.Contains(t, n.msgs[0], "ro/f.md")
|
|
assert.Contains(t, n.msgs[0], "special file")
|
|
for _, m := range n.msgs {
|
|
assert.NotContains(t, m, "removed", "a file it could not remove was reported as removed")
|
|
}
|
|
}
|