| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/google/uuid" |
| 14 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 15 | ) |
| 16 | |
| 17 | func init() { |
| 18 | if os.Getenv("CAGE_TEST_WINDOWS_ATTACHMENT_RELAY") != "1" { |
| 19 | return |
| 20 | } |
| 21 | if len(os.Args) != 4 || os.Args[1] != "--relay-profile" || os.Args[3] != "attachments" { |
| 22 | os.Exit(2) |
| 23 | } |
| 24 | |
| 25 | var request map[string]string |
| 26 | line := make([]byte, 0, 4096) |
| 27 | for { |
| 28 | var b [1]byte |
| 29 | if _, err := io.ReadFull(os.Stdin, b[:]); err != nil { |
| 30 | os.Exit(2) |
| 31 | } |
| 32 | if b[0] == '\n' { |
| 33 | break |
| 34 | } |
| 35 | line = append(line, b[0]) |
| 36 | if len(line) > 4096 { |
| 37 | os.Exit(2) |
| 38 | } |
| 39 | } |
| 40 | if json.Unmarshal(line, &request) != nil || request["action"] != "save" { |
| 41 | os.Exit(2) |
| 42 | } |
| 43 | var size int64 |
| 44 | for { |
| 45 | var header [4]byte |
| 46 | if _, err := io.ReadFull(os.Stdin, header[:]); err != nil { |
| 47 | os.Exit(2) |
| 48 | } |
| 49 | n := binary.LittleEndian.Uint32(header[:]) |
| 50 | if n == 0 { |
| 51 | break |
| 52 | } |
| 53 | if n > 65536 { |
| 54 | os.Exit(2) |
| 55 | } |
| 56 | if _, err := io.CopyN(io.Discard, os.Stdin, int64(n)); err != nil { |
| 57 | os.Exit(2) |
| 58 | } |
| 59 | size += int64(n) |
| 60 | } |
| 61 | path := `C:\Users\Guest\Cage\Attachments\0123456789abcdef0123456789abcdef\` + request["name"] |
| 62 | if os.Getenv("CAGE_TEST_WINDOWS_ATTACHMENT_BAD_PATH") == "1" { |
| 63 | path = `C:\outside\` + request["name"] |
| 64 | } |
| 65 | reply := map[string]any{"ok": true, "scope": "attachments", "path": path} |
| 66 | if os.Getenv("CAGE_TEST_WINDOWS_ATTACHMENT_NO_SIZE") != "1" { |
| 67 | reply["size"] = size |
| 68 | } |
| 69 | _ = json.NewEncoder(os.Stdout).Encode(reply) |
| 70 | os.Exit(0) |
| 71 | } |
| 72 | |
| 73 | func TestWindowsDesktopUploadHandlerKeepsPathsGuestPrivate(t *testing.T) { |
| 74 | s := testServer(t) |
| 75 | binding := guestos.WindowsProfile{Version: 1, VMID: uuid.New(), GUIUserSID: "S-1-5-21-1-2-3-1001", GUIUser: "Guest", InstanceDirectory: filepath.Join(s.profile, "unused-vm"), PairingFile: filepath.Join(s.profile, "unused-pair"), RelayExecutable: os.Args[0]} |
| 76 | if err := guestos.BindWindowsProfile(s.profile, binding); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | s.windowsGuest = &binding |
| 80 | s.ready = true |
| 81 | t.Setenv("CAGE_TEST_WINDOWS_ATTACHMENT_RELAY", "1") |
| 82 | id := uuid.NewString() |
| 83 | for _, sizeKnown := range []bool{true, false} { |
| 84 | if !sizeKnown { |
| 85 | t.Setenv("CAGE_TEST_WINDOWS_ATTACHMENT_NO_SIZE", "1") |
| 86 | } |
| 87 | w := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/desktop/"+id+"?name=note.txt", []byte("abc")) |
| 88 | var response map[string]any |
| 89 | if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &response) != nil || response["ok"] != true || response["id"] != id || response["name"] != "note.txt" { |
| 90 | t.Fatalf("real desktop upload handler failed: %d %s", w.Code, w.Body.String()) |
| 91 | } |
| 92 | if response["path"] != nil || strings.Contains(w.Body.String(), "Attachments") { |
| 93 | t.Fatal("desktop upload exposed its native staging path") |
| 94 | } |
| 95 | _, hasSize := response["size"] |
| 96 | if hasSize != sizeKnown { |
| 97 | t.Fatal("desktop response invented or lost a native byte count") |
| 98 | } |
| 99 | if hasSize && response["size"] != float64(3) { |
| 100 | t.Fatal("desktop response changed actual received bytes") |
| 101 | } |
| 102 | } |
| 103 | result, err := s.windowsSaveAttachment(t.Context(), "note.txt", strings.NewReader("abc")) |
| 104 | if err != nil || !validWindowsAttachmentPath(result["path"].(string), "note.txt") { |
| 105 | t.Fatal("terminal/image native save lost its validated path") |
| 106 | } |
| 107 | t.Setenv("CAGE_TEST_WINDOWS_ATTACHMENT_BAD_PATH", "1") |
| 108 | if w := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/desktop/"+uuid.NewString()+"?name=note.txt", []byte("abc")); w.Code == http.StatusOK { |
| 109 | t.Fatal("path was hidden before validating native receipt") |
| 110 | } |
| 111 | } |