Vectrum
Sign In
200 lines · 6.1 KB · Go
package tools

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

// A skill lives outside the working root. The read tools reach into it; the
// write tools must not.
func TestExtraRootsAreReadableNotWritable(t *testing.T) {
	root := t.TempDir()
	skill := t.TempDir()
	ref := filepath.Join(skill, "references.md")
	if err := os.WriteFile(ref, []byte("conventions\n"), 0o644); err != nil {
		t.Fatal(err)
	}

	read := NewRead(root, nil, skill)
	out, err := run(t, read, map[string]any{"path": ref})
	if err != nil {
		t.Fatalf("read_file refused an extra root: %v", err)
	}
	if !strings.Contains(out, "conventions") {
		t.Errorf("read_file returned %q", out)
	}

	write := NewWrite(root, nil)
	if _, err := run(t, write, map[string]any{
		"path": ref, "content": "rewritten",
	}); err == nil {
		t.Error("write_file was allowed into a skill directory")
	}
}

func TestExtraRootsDoNotWidenTheJail(t *testing.T) {
	root := t.TempDir()
	skill := t.TempDir()
	outside := filepath.Join(t.TempDir(), "secret.txt")
	if err := os.WriteFile(outside, []byte("secret\n"), 0o644); err != nil {
		t.Fatal(err)
	}

	read := NewRead(root, nil, skill)
	if _, err := run(t, read, map[string]any{"path": outside}); err == nil {
		t.Error("a path under neither the root nor an extra root was allowed")
	}
}

func TestExtraRootSymlinkCannotStepSideways(t *testing.T) {
	root := t.TempDir()
	skill := t.TempDir()
	outside := t.TempDir()
	if err := os.WriteFile(filepath.Join(outside, "secret.txt"), []byte("secret\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	link := filepath.Join(skill, "escape")
	if err := os.Symlink(outside, link); err != nil {
		t.Skipf("symlinks unavailable: %v", err)
	}

	read := NewRead(root, nil, skill)
	if _, err := run(t, read, map[string]any{
		"path": filepath.Join(link, "secret.txt"),
	}); err == nil {
		t.Error("a symlink out of a skill directory was followed")
	}
}

func TestGlobAndGrepAcceptExtraRoots(t *testing.T) {
	root := t.TempDir()
	skill := t.TempDir()
	if err := os.WriteFile(filepath.Join(skill, "notes.md"), []byte("needle\n"), 0o644); err != nil {
		t.Fatal(err)
	}

	g := NewGlob(root, skill)
	if _, err := run(t, g, map[string]any{
		"pattern": "*.md", "path": skill,
	}); err != nil {
		t.Errorf("glob refused an extra root: %v", err)
	}

	gr := NewGrep(root, skill)
	out, err := run(t, gr, map[string]any{
		"pattern": "needle", "path": skill,
	})
	if err != nil {
		t.Errorf("grep refused an extra root: %v", err)
	}
	if !strings.Contains(out, "needle") {
		t.Errorf("grep returned %q", out)
	}
}

func TestJail(t *testing.T) {
	root := t.TempDir()
	writeFile(t, root, "inside/file.go", "package inside\n")

	outside := t.TempDir()
	secret := writeFile(t, outside, "secret.env", "TOKEN=hunter2\n")

	// A symlink inside the root pointing out of it is the interesting case:
	// the path looks fine until it is resolved.
	link := filepath.Join(root, "escape")
	if err := os.Symlink(outside, link); err != nil {
		t.Skipf("symlinks unavailable: %v", err)
	}

	tests := []struct {
		name    string
		path    string
		wantErr bool
	}{
		{name: "relative inside", path: "inside/file.go"},
		{name: "absolute inside", path: filepath.Join(root, "inside/file.go")},
		{name: "a file that does not exist yet", path: "inside/new.go"},
		{name: "nested path that does not exist yet", path: "a/b/c/new.go"},
		{name: "dot-dot escape", path: "../escaped.go", wantErr: true},
		{name: "buried dot-dot escape", path: "inside/../../escaped.go", wantErr: true},
		{name: "absolute path outside", path: secret, wantErr: true},
		{name: "through a symlink pointing outside", path: "escape/secret.env", wantErr: true},
		{name: "the root itself", path: "."},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			got, err := resolve(root, tc.path)
			if tc.wantErr {
				if err == nil {
					t.Fatalf("resolve(%q) = %q, want a refusal", tc.path, got)
				}
				if !strings.Contains(err.Error(), "outside the working root") {
					t.Errorf("error = %v, want it to explain the jail", err)
				}
				return
			}
			if err != nil {
				t.Fatalf("resolve(%q): %v", tc.path, err)
			}
		})
	}
}

func TestToolsRefuseToEscapeTheRoot(t *testing.T) {
	root := t.TempDir()
	outside := t.TempDir()
	secret := writeFile(t, outside, "secret.env", "TOKEN=hunter2\n")
	track := newTracker()

	tools := []struct {
		name string
		tool Tool
		args any
	}{
		{name: "read_file", tool: NewRead(root, track), args: ReadArgs{Path: secret}},
		{name: "edit_file", tool: NewEdit(root, track), args: EditArgs{Path: secret, OldStr: "TOKEN", NewStr: "X"}},
		{name: "write_file", tool: NewWrite(root, track), args: WriteArgs{Path: secret, Content: "x"}},
		{name: "glob", tool: NewGlob(root), args: GlobArgs{Pattern: "*.env", Path: outside}},
		{name: "grep", tool: NewGrep(root), args: GrepArgs{Pattern: "TOKEN", Path: outside}},
	}
	for _, tc := range tools {
		t.Run(tc.name, func(t *testing.T) {
			if _, err := run(t, tc.tool, tc.args); err == nil {
				t.Fatal("want a refusal for a path outside the root")
			}
		})
	}
	if got, _ := os.ReadFile(secret); string(got) != "TOKEN=hunter2\n" {
		t.Error("the file outside the root was modified")
	}
}

// A model that follows a skill's own wording and asks for "references/note.md"
// gets a miss, because relative paths anchor to the working root. The error is
// the only place it can learn that skill directories exist at all.
func TestReadNotFoundNamesSkillDirsWhenThereAreSome(t *testing.T) {
	root := t.TempDir()
	skill := t.TempDir()

	withSkills := NewRead(root, nil, skill)
	_, err := run(t, withSkills, map[string]any{"path": "references/note.md"})
	if err == nil {
		t.Fatal("reading a missing file succeeded")
	}
	if !strings.Contains(err.Error(), skill) {
		t.Errorf("error does not name the skill directory, so the model cannot recover:\n%v", err)
	}

	// Without extra roots there are no skill directories to mention, and the
	// message must stay exactly as terse as it has always been.
	plain := NewRead(root, nil)
	_, err = run(t, plain, map[string]any{"path": "references/note.md"})
	if err == nil {
		t.Fatal("reading a missing file succeeded")
	}
	if got, want := err.Error(), "no such file: references/note.md"; got != want {
		t.Errorf("error without skills changed\n got: %s\nwant: %s", got, want)
	}
}