package builtin import ( "errors" "os" "path/filepath" "testing" ) func TestResolveReadableFollowsSymlinks(t *testing.T) { root := t.TempDir() ws := filepath.Join(root, "ws") outside := filepath.Join(root, "secret") for _, d := range []string{ws, filepath.Join(ws, "sub")} { if err := os.Mkdir(d, 0o755); err != nil { t.Fatal(err) } } for _, f := range []string{outside, filepath.Join(ws, "sub", "ok.txt")} { if err := os.WriteFile(f, []byte("x"), 0o600); err != nil { t.Fatal(err) } } links := map[string]string{"out": outside, "in": filepath.Join(ws, "sub", "ok.txt"), "outdir": root} for name, target := range links { if err := os.Symlink(target, filepath.Join(ws, name)); err != nil { t.Fatal(err) } } linkedWS := filepath.Join(root, "linkedws") if err := os.Symlink(ws, linkedWS); err != nil { t.Fatal(err) } tests := []struct { name, cwd, path string allowed bool }{ {"plain file", ws, "sub/ok.txt", true}, {"link inside workspace", ws, "in", true}, {"link to file outside", ws, "out", false}, {"link to dir outside", ws, "outdir/secret", false}, {"symlinked workspace", linkedWS, "sub/ok.txt", true}, {"lexical escape", ws, "../secret", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { _, err := ResolveReadable(tt.cwd, tt.path) if tt.allowed && err != nil { t.Fatalf("want allowed, got %v", err) } if !tt.allowed && !errors.Is(err, ErrPathNotAllowed) { t.Fatalf("want ErrPathNotAllowed, got %v", err) } }) } }