os.Root follows an intermediate directory symlink that stays inside the brain. A dir symlink inside wiki/ pointing elsewhere in the brain let brain_update and brain_tunnel rewrite an existing .md there (e.g. under raw/), and session_log followed a symlinked sessions dir. - WriteRegular, ReadRegular and ReadRegularMax require every directory above the file to be a real directory (realDir). Reads too: brain_tunnel read the redirected target, then rewrote the source before the target's write was refused; and brain_read/brain_get could read another part of the brain through such a path. - session.Append refuses a sessions dir that is not a real directory. - A directory swapped in after the check remains the #167 residual. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
4.2 KiB
Go
105 lines
4.2 KiB
Go
package session
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// session_log passed the caller's session_id straight into
|
|
// filepath.Join(sessionsDir, id+".jsonl") and opened it O_CREATE|O_APPEND:
|
|
// path segments in the id created or appended files anywhere the pod could
|
|
// write. The id is now a plain name, and the open does not follow a final
|
|
// symlink.
|
|
func TestAppendRefusesSessionIDsThatAreNotPlainNames(t *testing.T) {
|
|
root := t.TempDir()
|
|
dir := filepath.Join(root, "sessions")
|
|
for _, id := range []string{"../x", "a/b", "/abs", ".", "..", ".hidden", "", "a\\b", "a\x00b"} {
|
|
assert.Error(t, Append(dir, id, Entry{SessionID: id}), "%q", id)
|
|
}
|
|
ents, _ := os.ReadDir(root)
|
|
for _, e := range ents {
|
|
assert.Equal(t, "sessions", e.Name(), "nothing created beside the sessions dir")
|
|
}
|
|
for _, id := range []string{"1557b2e1-bf0d-5c4f-bbe3-0754d087e356", "_routing", "2026-05-05-infra.plan", "0"} {
|
|
require.NoError(t, Append(dir, id, Entry{SessionID: id}), "positive control: %q", id)
|
|
}
|
|
}
|
|
|
|
func TestAppendDoesNotWriteThroughASymlinkedLog(t *testing.T) {
|
|
root := t.TempDir()
|
|
dir := filepath.Join(root, "sessions")
|
|
require.NoError(t, os.MkdirAll(dir, 0o755))
|
|
victim := filepath.Join(root, "victim.md")
|
|
require.NoError(t, os.WriteFile(victim, []byte("untouched\n"), 0o644))
|
|
require.NoError(t, os.Symlink(filepath.Join("..", "victim.md"), filepath.Join(dir, "s.jsonl")))
|
|
assert.Error(t, Append(dir, "s", Entry{SessionID: "s"}))
|
|
got, _ := os.ReadFile(victim)
|
|
assert.Equal(t, "untouched\n", string(got))
|
|
}
|
|
|
|
func TestReadRefusesSessionIDsThatAreNotPlainNames(t *testing.T) {
|
|
root := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(root, "x.jsonl"), []byte(`{"session_id":"x"}`+"\n"), 0o644))
|
|
_, err := Read(filepath.Join(root, "sessions"), "../x")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// A name that starts plainly and then climbs out with ".." is exactly what the
|
|
// ban on "/" in the id's tail stops: filepath.Join cleans "a/../../x" to a
|
|
// path beside the sessions dir without needing sessions/a to exist. The
|
|
// assertion is on the refusal itself (ErrInvalidID), not on any error.
|
|
func TestAppendRefusesAnIDThatClimbsOutAfterAPlainPrefix(t *testing.T) {
|
|
root := t.TempDir()
|
|
dir := filepath.Join(root, "sessions")
|
|
require.NoError(t, os.MkdirAll(dir, 0o755))
|
|
for _, id := range []string{"a/../../escape", "a/../../../escape", "a/../b"} {
|
|
err := Append(dir, id, Entry{SessionID: id})
|
|
assert.ErrorIs(t, err, ErrInvalidID, "%q", id)
|
|
}
|
|
_, err := os.Lstat(filepath.Join(root, "escape.jsonl"))
|
|
assert.True(t, errors.Is(err, os.ErrNotExist), "nothing created beside the sessions dir")
|
|
_, err = os.Lstat(filepath.Join(dir, "b.jsonl"))
|
|
assert.True(t, errors.Is(err, os.ErrNotExist), "no in-dir alias created either")
|
|
}
|
|
|
|
// Append does no Lstat type check, so O_NONBLOCK is the only thing that keeps
|
|
// a FIFO named <id>.jsonl with no reader from blocking session_log forever.
|
|
func TestAppendReturnsPromptlyOnAFIFOLogWithNoReader(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "sessions")
|
|
require.NoError(t, os.MkdirAll(dir, 0o755))
|
|
fifo := filepath.Join(dir, "s.jsonl")
|
|
require.NoError(t, syscall.Mkfifo(fifo, 0o644))
|
|
done := make(chan error, 1)
|
|
go func() { done <- Append(dir, "s", Entry{SessionID: "s"}) }()
|
|
select {
|
|
case err := <-done:
|
|
assert.Error(t, err, "a FIFO with no reader is refused, not written")
|
|
case <-time.After(3 * time.Second):
|
|
// Unblock the stuck open so the goroutine and TempDir cleanup finish.
|
|
if r, err := os.OpenFile(fifo, os.O_RDONLY|syscall.O_NONBLOCK, 0); err == nil {
|
|
defer r.Close() //nolint:errcheck
|
|
}
|
|
<-done
|
|
t.Fatal("Append blocked on a FIFO log with no reader")
|
|
}
|
|
}
|
|
|
|
// hyperguild#173: session_log followed a symlinked sessions dir.
|
|
func TestAppendRefusesASymlinkedSessionsDir(t *testing.T) {
|
|
root := t.TempDir()
|
|
elsewhere := filepath.Join(root, "elsewhere")
|
|
require.NoError(t, os.MkdirAll(elsewhere, 0o755))
|
|
dir := filepath.Join(root, "sessions")
|
|
require.NoError(t, os.Symlink("elsewhere", dir))
|
|
assert.Error(t, Append(dir, "s", Entry{SessionID: "s"}))
|
|
ents, _ := os.ReadDir(elsewhere)
|
|
assert.Empty(t, ents, "a log was created through the symlinked dir")
|
|
}
|