| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "image" |
| 8 | "image/color" |
| 9 | "image/png" |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 16 | ) |
| 17 | |
| 18 | func windowsBrowserRequestForTest(s *Server, method, path string, body []byte) *httptest.ResponseRecorder { |
| 19 | r := httptest.NewRequest(method, s.origin+path, bytes.NewReader(body)) |
| 20 | r.Header.Set("Origin", s.origin) |
| 21 | r.Header.Set("Content-Type", "application/json") |
| 22 | r.AddCookie(&http.Cookie{Name: s.cookieName(), Value: s.session}) |
| 23 | r.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 24 | w := httptest.NewRecorder() |
| 25 | s.handler().ServeHTTP(w, r) |
| 26 | return w |
| 27 | } |
| 28 | |
| 29 | func TestWindowsBrowserImageHTTPUsesBoundedNativePNGChunks(t *testing.T) { |
| 30 | s := testServer(t) |
| 31 | s.windowsGuest, s.ready = &guestos.WindowsProfile{}, true |
| 32 | s.windowsBrowserControl = fixtureWindowsBrowserClient(t) |
| 33 | img := image.NewRGBA(image.Rect(0, 0, 512, 512)) |
| 34 | var state uint32 = 0x7450133 |
| 35 | for y := 0; y < 512; y++ { |
| 36 | for x := 0; x < 512; x++ { |
| 37 | state = state*1664525 + 1013904223 |
| 38 | img.SetRGBA(x, y, color.RGBA{byte(state), byte(state >> 8), byte(state >> 16), 255}) |
| 39 | } |
| 40 | } |
| 41 | var data bytes.Buffer |
| 42 | if err := png.Encode(&data, img); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if data.Len() < 128<<10 { |
| 46 | t.Fatal("PNG fixture did not exercise bounded relay chunks") |
| 47 | } |
| 48 | body, _ := json.Marshal(map[string]any{"target": "browser", "id": strings.Repeat("a", 32), "png": base64.StdEncoding.EncodeToString(data.Bytes())}) |
| 49 | w := windowsBrowserRequestForTest(s, "POST", "/api/input/image", body) |
| 50 | if w.Code != 200 { |
| 51 | t.Fatal("Windows PNG seam failed", w.Code) |
| 52 | } |
| 53 | if s.browserControl != nil { |
| 54 | t.Fatal("PNG used Linux transport") |
| 55 | } |
| 56 | bad, _ := json.Marshal(map[string]any{"target": "browser", "id": strings.Repeat("a", 32), "png": "not png"}) |
| 57 | if w := windowsBrowserRequestForTest(s, "POST", "/api/input/image", bad); w.Code == 200 { |
| 58 | t.Fatal("invalid PNG accepted") |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestNativeBrowserTextHTTPPreservesTwoMiBAndRequiresPreparation(t *testing.T) { |
| 63 | s := testServer(t) |
| 64 | s.windowsGuest, s.ready = &guestos.WindowsProfile{}, true |
| 65 | s.windowsBrowserControl = fixtureWindowsBrowserClient(t) |
| 66 | id := strings.Repeat("a", 32) |
| 67 | prepared := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/paste/start", []byte(`{"target":"browser","id":"`+id+`"}`)) |
| 68 | var binding struct { |
| 69 | Transfer string `json:"transfer"` |
| 70 | } |
| 71 | if prepared.Code != http.StatusOK || json.Unmarshal(prepared.Body.Bytes(), &binding) != nil || !validInputToken(binding.Transfer) { |
| 72 | t.Fatalf("clipboard preparation failed: %d %s", prepared.Code, prepared.Body.String()) |
| 73 | } |
| 74 | text := strings.Repeat("中", (2<<20)/3) + "ab" |
| 75 | if len(text) != 2<<20 { |
| 76 | t.Fatal("fixture is not at the UTF-8 byte limit") |
| 77 | } |
| 78 | body, _ := json.Marshal(map[string]any{"target": "browser", "id": id, "text": text, "transfer": binding.Transfer}) |
| 79 | response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/text", body) |
| 80 | if response.Code != http.StatusOK { |
| 81 | t.Fatalf("bounded text paste failed: %d %s", response.Code, response.Body.String()) |
| 82 | } |
| 83 | body, _ = json.Marshal(map[string]any{"target": "browser", "id": id, "text": text + "c", "transfer": binding.Transfer}) |
| 84 | if response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/text", body); response.Code != http.StatusBadRequest { |
| 85 | t.Fatal("oversized text was accepted") |
| 86 | } |
| 87 | body, _ = json.Marshal(map[string]any{"target": "browser", "id": id, "text": "missing binding"}) |
| 88 | if response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/text", body); response.Code != http.StatusConflict { |
| 89 | t.Fatal("unprepared text was accepted") |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestNativeBrowserClipboardHTTPReadsBoundPNGAndLargeText(t *testing.T) { |
| 94 | s := testServer(t) |
| 95 | s.macosGuest, s.ready = &guestos.MacOSProfile{}, true |
| 96 | s.windowsBrowserControl = fixtureWindowsBrowserClient(t) |
| 97 | for _, id := range []string{"11111111-1111-4111-8111-111111111111", "22222222-2222-4222-8222-222222222222"} { |
| 98 | input := browserClipboardInput{ID: id, Tab: strings.Repeat("a", 32), Context: strings.Repeat("b", 32), Document: strings.Repeat("c", 32)} |
| 99 | body, _ := json.Marshal(input) |
| 100 | response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/browser/clipboard", body) |
| 101 | var result map[string]string |
| 102 | if response.Code != http.StatusOK || json.Unmarshal(response.Body.Bytes(), &result) != nil { |
| 103 | t.Fatalf("clipboard reference failed: %d %s", response.Code, response.Body.String()) |
| 104 | } |
| 105 | if strings.HasPrefix(id, "11111111") { |
| 106 | if result["png"] != testPNG(t) { |
| 107 | t.Fatal("PNG reference changed its bytes") |
| 108 | } |
| 109 | } else if result["text"] != strings.Repeat("中", (2<<20)/3)+"ab" { |
| 110 | t.Fatal("large UTF-8 clipboard text changed") |
| 111 | } |
| 112 | input.Context = strings.Repeat("d", 32) |
| 113 | body, _ = json.Marshal(input) |
| 114 | if response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/browser/clipboard", body); response.Code == http.StatusOK { |
| 115 | t.Fatal("superseded copy context was accepted") |
| 116 | } |
| 117 | input.Context = "" |
| 118 | body, _ = json.Marshal(input) |
| 119 | if response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/browser/clipboard", body); response.Code != http.StatusBadRequest { |
| 120 | t.Fatal("unbound copy reference was accepted") |
| 121 | } |
| 122 | } |
| 123 | if s.browserControl != nil { |
| 124 | t.Fatal("private clipboard used Linux transport") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestWindowsBrowserExtensionHTTPUsesNativeChunkedPreview(t *testing.T) { |
| 129 | s := testServer(t) |
| 130 | s.windowsGuest, s.ready = &guestos.WindowsProfile{}, true |
| 131 | s.windowsBrowserControl = fixtureWindowsBrowserClient(t) |
| 132 | body := bytes.Repeat([]byte{0xab}, 131089) |
| 133 | w := windowsBrowserRequestForTest(s, http.MethodPost, "/api/browser/extensions/upload", body) |
| 134 | var preview struct { |
| 135 | Session string `json:"session"` |
| 136 | Size int `json:"size"` |
| 137 | } |
| 138 | if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &preview) != nil || !browserFileID.MatchString(preview.Session) || preview.Size != len(body) { |
| 139 | t.Fatal("Windows extension upload did not return its preview", w.Code) |
| 140 | } |
| 141 | if s.browserControl != nil { |
| 142 | t.Fatal("Windows extension upload created the Linux browser transport") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestWindowsVaultStoreAndBrowserRoutesRemainAuthorized(t *testing.T) { |
| 147 | s := testServer(t) |
| 148 | s.account = newTestAccount(t) |
| 149 | s.windowsGuest, s.ready = &guestos.WindowsProfile{}, true |
| 150 | s.windowsBrowserControl = fixtureWindowsBrowserClient(t) |
| 151 | save := []byte(`{"action":"save","entry":{"origin":"https://mail.example.com","username":"fixture-user","password":"fixture-only-secret"}}`) |
| 152 | if w := windowsBrowserRequestForTest(s, "POST", "/api/vault", save); w.Code != 200 { |
| 153 | t.Fatal("Windows vault save refused", w.Code) |
| 154 | } |
| 155 | w := windowsBrowserRequestForTest(s, "GET", "/api/vault", nil) |
| 156 | if w.Code != 200 || strings.Contains(w.Body.String(), "fixture-only-secret") { |
| 157 | t.Fatal("Windows vault list refused or exposed a password", w.Code) |
| 158 | } |
| 159 | w = windowsBrowserRequestForTest(s, "POST", "/api/vault/browser", []byte(`{"action":"status","tab":"tab"}`)) |
| 160 | var state vaultBrowserState |
| 161 | if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &state) != nil || !state.Available { |
| 162 | t.Fatal("Windows vault browser seam unavailable", w.Code) |
| 163 | } |
| 164 | if s.browserControl != nil { |
| 165 | t.Fatal("Windows vault created a Linux browser transport") |
| 166 | } |
| 167 | if w := request(s, "GET", "/api/vault", s.origin, "", true); w.Code != 403 { |
| 168 | t.Fatal("Windows vault admitted a viewer without native trust", w.Code) |
| 169 | } |
| 170 | s.account.lock() |
| 171 | if w := windowsBrowserRequestForTest(s, "GET", "/api/vault", nil); w.Code != http.StatusLocked { |
| 172 | t.Fatal("locked Windows vault admitted", w.Code) |
| 173 | } |
| 174 | for _, path := range []string{"/api/vault/export", "/api/vault/browser/extra"} { |
| 175 | if windowsWorkbenchRoute(path) { |
| 176 | t.Fatal("unregistered vault route admitted") |
| 177 | } |
| 178 | } |
| 179 | if !windowsWorkbenchRoute("/api/client/browser-extensions") || windowsWorkbenchRoute("/api/client/browser-extensions/extra") { |
| 180 | t.Fatal("host extension import route boundary changed") |
| 181 | } |
| 182 | } |