os.Root refuses links that leave the brain on its own, so the slice 1-3 callers' outside-target tests stayed green when a caller read without brainfs's Lstat/SameFile checks (PR #168 round-2 review). Adds a static in-brain link (wiki/w/facts/inlink.md -> ../../../raw/s.md) and an AssertNoLeakFrom swap race with an in-brain target for search.Query, hydrate, vectorstore.Sync, readExcerpt and api.ReadNote. Flip-verified: replacing each caller's bfs.ReadRegular with an os.OpenRoot(brainDir).ReadFile turns every one red on the new assertions (Query, hydrate, Sync, readExcerpt, ReadNote). Also from that review: rename TestReadRegularNever... to ...DoesNotReturn... (rule 9: a sampled race), and AssertNoLeakFrom's failure message names the symlink target instead of 'outside file'. Closes #171 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
43 lines
1.8 KiB
Go
43 lines
1.8 KiB
Go
package mcp
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mathiasbq/hyperguild/ingestion/internal/brainfs/brainfstest"
|
|
)
|
|
|
|
// hyperguild#157 slice 2: readExcerpt (brain_context) read by path, so a
|
|
// symlink in the Syncthing-shared brain put an outside file in context, and
|
|
// a FIFO hung it. It now reads through brainfs.
|
|
func TestReadExcerptRefusesSymlinksAndSurvivesSwapRaces(t *testing.T) {
|
|
brain := t.TempDir()
|
|
facts := filepath.Join(brain, "wiki", "w", "facts")
|
|
require.NoError(t, os.MkdirAll(facts, 0o755))
|
|
require.NoError(t, os.Symlink(brainfstest.Outside(t), filepath.Join(facts, "leak.md")))
|
|
assert.NotContains(t, readExcerpt(brain, "wiki/w/facts/leak.md", 500), brainfstest.Sentinel)
|
|
// A link to a file elsewhere in the brain: os.Root allows it, only the
|
|
// regular-file check refuses it (hyperguild#171).
|
|
inBrain := brainfstest.WriteSentinelAt(t, filepath.Join(brain, "raw", "s.md"))
|
|
require.NoError(t, os.Symlink(filepath.Join("..", "..", "..", "raw", "s.md"), filepath.Join(facts, "inlink.md")))
|
|
assert.NotContains(t, readExcerpt(brain, "wiki/w/facts/inlink.md", 500), brainfstest.Sentinel)
|
|
|
|
p := filepath.Join(facts, "r.md")
|
|
require.NoError(t, os.WriteFile(p, []byte("---\nx: y\n---\ninside"), 0o644))
|
|
assert.Equal(t, "inside", readExcerpt(brain, "wiki/w/facts/r.md", 500))
|
|
brainfstest.AssertNoLeak(t, p, "---\nx: y\n---\ninside", 1500*time.Millisecond, func() string {
|
|
return readExcerpt(brain, "wiki/w/facts/r.md", 500)
|
|
})
|
|
brainfstest.AssertNoLeakFrom(t, p, "---\nx: y\n---\ninside", inBrain, 1500*time.Millisecond, func() string {
|
|
return readExcerpt(brain, "wiki/w/facts/r.md", 500)
|
|
})
|
|
brainfstest.AssertNoHang(t, p, "---\nx: y\n---\ninside", 1500*time.Millisecond, 6*time.Second, func() {
|
|
_ = readExcerpt(brain, "wiki/w/facts/r.md", 500)
|
|
})
|
|
}
|