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") } }