readExcerpt, ReadNote and ReadScoped now read through brainfs (os.Root, regular files only, type checked on the opened handle), so a symlink swapped in after a check is not followed and a FIFO does not hang the reader. ReadRegularMax bounds the read itself with io.LimitReader(max+1). notes.Target carries the brain root. Refs #157. Adversarial review round 2: PASS (round 1 BLOCKED: unbounded read, rule-9 test names). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Mathias <mthbqv@gmail.com>
35 lines
1.3 KiB
Go
35 lines
1.3 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)
|
|
|
|
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.AssertNoHang(t, p, "---\nx: y\n---\ninside", 1500*time.Millisecond, 6*time.Second, func() {
|
|
_ = readExcerpt(brain, "wiki/w/facts/r.md", 500)
|
|
})
|
|
}
|