| 1 | |
| 2 | |
| 3 | package workbench |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "io" |
| 8 | "net" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "sync/atomic" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 19 | ) |
| 20 | |
| 21 | type disconnectedMacLoginResponse struct{ *httptest.ResponseRecorder } |
| 22 | |
| 23 | func (disconnectedMacLoginResponse) Write([]byte) (int, error) { return 0, io.ErrClosedPipe } |
| 24 | |
| 25 | func TestMacOSMaintenanceHTTPBoundaryAndRunnerUpgrade(t *testing.T) { |
| 26 | s := testServer(t) |
| 27 | profile, err := os.MkdirTemp("/tmp", "cage-maint-") |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | defer os.RemoveAll(profile) |
| 32 | s.profile = profile |
| 33 | s.macosGuest = &guestos.MacOSProfile{Instance: "mac", User: "trial"} |
| 34 | s.ready = true |
| 35 | if err := os.MkdirAll(s.instanceDir(), 0700); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | listener, err := net.Listen("unix", filepath.Join(s.instanceDir(), "cage-login.sock")) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | requests := make(chan guestos.MacLoginRequest, 4) |
| 43 | var legacy atomic.Bool |
| 44 | done := make(chan struct{}) |
| 45 | go func() { |
| 46 | defer close(done) |
| 47 | for { |
| 48 | connection, err := listener.Accept() |
| 49 | if err != nil { |
| 50 | return |
| 51 | } |
| 52 | var input guestos.MacLoginRequest |
| 53 | if json.NewDecoder(connection).Decode(&input) == nil { |
| 54 | requests <- input |
| 55 | response := guestos.MacLoginReply{MaintenanceVersion: 1, Session: "fixture-session"} |
| 56 | if legacy.Load() { |
| 57 | response = guestos.MacLoginReply{Closed: true} |
| 58 | } |
| 59 | _ = json.NewEncoder(connection).Encode(response) |
| 60 | } |
| 61 | _ = connection.Close() |
| 62 | } |
| 63 | }() |
| 64 | defer func() { listener.Close(); <-done }() |
| 65 | post := func(path, body string, trusted bool) *httptest.ResponseRecorder { |
| 66 | t.Helper() |
| 67 | request := httptest.NewRequest(http.MethodPost, s.origin+path, strings.NewReader(body)) |
| 68 | request.Header.Set("Origin", s.origin) |
| 69 | request.Header.Set("Content-Type", "application/json") |
| 70 | request.AddCookie(&http.Cookie{Name: s.cookieName(), Value: s.session}) |
| 71 | if trusted { |
| 72 | request.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 73 | } |
| 74 | response := httptest.NewRecorder() |
| 75 | s.handler().ServeHTTP(response, request) |
| 76 | return response |
| 77 | } |
| 78 | body := `{"action":"fill-password","maintenance":"projects","session":"fixture-session","geometry":"verified-window","password":"untrusted-input","initial_setup":true}` |
| 79 | if response := post("/api/macos/maintenance", body, false); response.Code != http.StatusForbidden { |
| 80 | t.Fatalf("untrusted maintenance accepted: %d", response.Code) |
| 81 | } |
| 82 | if response := post("/api/macos/maintenance", body, true); response.Code != http.StatusOK { |
| 83 | t.Fatalf("trusted maintenance failed: %d %s", response.Code, response.Body.String()) |
| 84 | } |
| 85 | input := <-requests |
| 86 | if input.Setup || input.Password != "" || input.Maintenance != "projects" || input.Geometry != "verified-window" || input.Action != "fill-password" { |
| 87 | t.Fatalf("maintenance boundary changed: action=%s purpose=%s setup=%v", input.Action, input.Maintenance, input.Setup) |
| 88 | } |
| 89 | for _, body := range []string{ |
| 90 | `{"action":"open","maintenance":"desktop"}`, |
| 91 | `{"action":"show-password","maintenance":"projects"}`, |
| 92 | `{"action":"finish","maintenance":"projects"}`, |
| 93 | } { |
| 94 | if response := post("/api/macos/maintenance", body, true); response.Code != http.StatusBadRequest { |
| 95 | t.Fatalf("invalid maintenance request returned %d", response.Code) |
| 96 | } |
| 97 | } |
| 98 | if response := post("/api/macos/first-setup", `{"action":"open"}`, true); response.Code != http.StatusGone { |
| 99 | t.Fatalf("completed initial setup was reopened: %d", response.Code) |
| 100 | } |
| 101 | legacy.Store(true) |
| 102 | response := post("/api/macos/maintenance", `{"action":"open","maintenance":"projects"}`, true) |
| 103 | <-requests |
| 104 | var result guestos.MacLoginReply |
| 105 | if response.Code != http.StatusOK || json.Unmarshal(response.Body.Bytes(), &result) != nil || result.Recovery != "environment-restart" || !result.Closed { |
| 106 | t.Fatalf("old runner did not request a safe restart: %d %s", response.Code, response.Body.String()) |
| 107 | } |
| 108 | legacy.Store(false) |
| 109 | if response := post("/api/macos/maintenance", `{"action":"open","maintenance":"projects"}`, true); response.Code != http.StatusOK { |
| 110 | t.Fatal("could not open the current maintenance runner") |
| 111 | } |
| 112 | <-requests |
| 113 | s.account.lock() |
| 114 | select { |
| 115 | case close := <-requests: |
| 116 | if close.Action != "close" || close.Session != "fixture-session" || close.Maintenance != "projects" { |
| 117 | t.Fatal("Cage lock did not revoke its maintenance session") |
| 118 | } |
| 119 | case <-time.After(3 * time.Second): |
| 120 | t.Fatal("Cage lock retained the maintenance view") |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | previousEpoch, _ := s.account.accessFor(true) |
| 126 | s.account.mu.Lock() |
| 127 | s.account.setKey(make([]byte, 32)) |
| 128 | s.account.mu.Unlock() |
| 129 | s.trackMacLogin(previousEpoch, |
| 130 | guestos.MacLoginRequest{Action: "open", Maintenance: "projects"}, |
| 131 | guestos.MacLoginReply{Session: "late-session"}) |
| 132 | select { |
| 133 | case close := <-requests: |
| 134 | if close.Action != "close" || close.Session != "late-session" { |
| 135 | t.Fatal("a late open response attached to the new account epoch") |
| 136 | } |
| 137 | case <-time.After(3 * time.Second): |
| 138 | t.Fatal("a late open response retained its cancelled maintenance view") |
| 139 | } |
| 140 | |
| 141 | request := httptest.NewRequest(http.MethodPost, s.origin+"/api/macos/maintenance", strings.NewReader(`{"action":"open","maintenance":"projects"}`)) |
| 142 | request.Header.Set("Origin", s.origin) |
| 143 | request.Header.Set("Content-Type", "application/json") |
| 144 | request.AddCookie(&http.Cookie{Name: s.cookieName(), Value: s.session}) |
| 145 | request.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 146 | s.handler().ServeHTTP(disconnectedMacLoginResponse{httptest.NewRecorder()}, request) |
| 147 | if open := <-requests; open.Action != "open" { |
| 148 | t.Fatal("disconnected response fixture did not open the maintenance view") |
| 149 | } |
| 150 | select { |
| 151 | case close := <-requests: |
| 152 | if close.Action != "close" || close.Session != "fixture-session" { |
| 153 | t.Fatal("a failed HTTP reply did not close its newly opened view") |
| 154 | } |
| 155 | case <-time.After(3 * time.Second): |
| 156 | t.Fatal("an undelivered HTTP response retained its maintenance view") |
| 157 | } |
| 158 | } |