brain_edit did its own I/O by path around notes.Edit: - the pre-edit snapshot used os.ReadFile, which hung on a FIFO note held open by a writer; - the rollback (when the provenance stamp fails) used os.WriteFile; - the edit lock "<note>.lock" was opened with O_CREATE by path, so a planted dangling .lock symlink made a successful edit create a file at the link's target, in the brain or outside it. Snapshot is brainfs ReadRegular, rollback WriteRegular, and the lock is opened and removed through new brainfs OpenLock/RemoveLock: clean path, real parent directories, an existing lock must be regular, and the opened handle must be the file at the path (SameFile). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
2.4 KiB
Go
62 lines
2.4 KiB
Go
package mcp_test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mathiasbq/hyperguild/ingestion/internal/mcp"
|
|
)
|
|
|
|
// hyperguild#192: brain_edit snapshotted the note with os.ReadFile by path,
|
|
// which blocks on a FIFO held open by a writer, and hung the call.
|
|
func TestBrainEditReturnsPromptlyOnAFIFONoteWithAWriter(t *testing.T) {
|
|
dir := newBrainRepo(t)
|
|
fifo := filepath.Join(dir, "wiki", "homelab", "facts", "f.md")
|
|
require.NoError(t, syscall.Mkfifo(fifo, 0o644))
|
|
w, err := os.OpenFile(fifo, os.O_RDWR, 0)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = w.Close() })
|
|
srv := mcp.NewServer(dir, nil, nil, nil)
|
|
|
|
done := make(chan map[string]any, 1)
|
|
go func() {
|
|
done <- call(t, srv, "brain_edit", map[string]any{"path": "wiki/homelab/facts/f.md", "old_str": "a", "new_str": "b"})
|
|
}()
|
|
select {
|
|
case resp := <-done:
|
|
assert.Contains(t, resp, "error", "a FIFO note is refused")
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("brain_edit hung on a FIFO note")
|
|
}
|
|
|
|
resp := call(t, srv, "brain_edit", map[string]any{"path": "wiki/homelab/facts/n.md", "old_str": "beta", "new_str": "BETA"})
|
|
require.NotContains(t, resp, "error", "positive control: a regular note is edited: %v", resp["error"])
|
|
}
|
|
|
|
// hyperguild#192: the edit lock "<note>.lock" was opened by path with
|
|
// O_CREATE, so a planted dangling .lock symlink made a successful edit create
|
|
// a file at the link's target, outside the brain or elsewhere in it.
|
|
func TestBrainEditDoesNotCreateAFileThroughADanglingLockSymlink(t *testing.T) {
|
|
for name, target := range map[string]func(t *testing.T, dir string) string{
|
|
"outside": func(t *testing.T, _ string) string { return filepath.Join(t.TempDir(), "created") },
|
|
"in-brain": func(t *testing.T, dir string) string { return filepath.Join(dir, "raw", "created") },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
dir := newBrainRepo(t)
|
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "raw"), 0o755))
|
|
tgt := target(t, dir)
|
|
require.NoError(t, os.Symlink(tgt, filepath.Join(dir, "wiki", "homelab", "facts", "n.md.lock")))
|
|
srv := mcp.NewServer(dir, nil, nil, nil)
|
|
_ = call(t, srv, "brain_edit", map[string]any{"path": "wiki/homelab/facts/n.md", "old_str": "beta", "new_str": "BETA"})
|
|
_, err := os.Lstat(tgt)
|
|
assert.True(t, os.IsNotExist(err), "a file was created at the lock symlink's target")
|
|
})
|
|
}
|
|
}
|