| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/google/uuid" |
| 14 | "github.com/sylvinus/agent-vm/internal/cage" |
| 15 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 16 | ) |
| 17 | |
| 18 | func TestMacOSAttachmentPathRejectsOtherUsersAndEscapes(t *testing.T) { |
| 19 | s := &Server{macosGuest: &guestos.MacOSProfile{User: "cage"}} |
| 20 | root := "/Users/cage/.local/share/cage/attachments/files/" + uuid.NewString() + "/" + uuid.NewString() + "/" |
| 21 | if !s.validMacOSAttachmentPath(root+"附件.txt", "附件.txt") { |
| 22 | t.Fatal("bound native attachment rejected") |
| 23 | } |
| 24 | for _, value := range []string{strings.Replace(root, "/cage/", "/another/", 1) + "附件.txt", root + "../附件.txt", root + "other.txt", "/Users/cage/.ssh/id_ed25519"} { |
| 25 | if s.validMacOSAttachmentPath(value, "附件.txt") { |
| 26 | t.Fatalf("unbound attachment accepted: %q", value) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestMacOSTerminalUsesSharedAttachmentReceiver(t *testing.T) { |
| 32 | if runtime.GOOS == "windows" { |
| 33 | t.Skip("shared macOS/Linux guest receiver requires POSIX UnixStreamServer") |
| 34 | } |
| 35 | if _, err := exec.LookPath("uv"); err != nil { |
| 36 | t.Skip("uv is required for the shared Python receiver") |
| 37 | } |
| 38 | home := t.TempDir() |
| 39 | contents := []byte("native attachment 中文\x00\x01") |
| 40 | var previous string |
| 41 | for attempt := 0; attempt < 2; attempt++ { |
| 42 | cmd := exec.Command("uv", "run", "--no-project", "python", "-I", "-c", cage.DesktopFileReceiver(), "--save-file", "file.bin") |
| 43 | cmd.Env = append(os.Environ(), "HOME="+home, "USERPROFILE="+home) |
| 44 | cmd.Stdin = bytes.NewReader(contents) |
| 45 | output, err := cmd.Output() |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | var saved struct { |
| 50 | OK bool `json:"ok"` |
| 51 | Path string `json:"path"` |
| 52 | } |
| 53 | if json.Unmarshal(output, &saved) != nil || !saved.OK || !strings.HasPrefix(saved.Path, filepath.Join(home, ".local", "share", "cage", "attachments", "files")+string(os.PathSeparator)) { |
| 54 | t.Fatalf("receiver returned invalid attachment: %s", output) |
| 55 | } |
| 56 | actual, err := os.ReadFile(saved.Path) |
| 57 | if err != nil || !bytes.Equal(actual, contents) || saved.Path == previous { |
| 58 | t.Fatal("attachment bytes changed or duplicate name replaced a prior attachment") |
| 59 | } |
| 60 | previous = saved.Path |
| 61 | } |
| 62 | } |