跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

windows_host_manager_e2e_windows_test.go

138 行 · 5.9 KB历史下载
1//go:build windows
2
3package guestos
4
5import (
6 "bufio"
7 "context"
8 "encoding/json"
9 "os"
10 "strings"
11 "testing"
12 "time"
13
14 "github.com/Microsoft/go-winio"
15 "github.com/google/uuid"
16 "golang.org/x/sys/windows"
17)
18
19// This test only reads the already registered VM. It must run in the ordinary
20// desktop user's token so direct Hyper-V administrator access cannot mask a
21// broken manager boundary.
22func TestWindowsHostManagerOrdinaryRead(t *testing.T) {
23 idText := os.Getenv("CAGE_TEST_WINDOWS_MANAGER_VM")
24 if idText == "" {
25 t.Skip("requires the installed fixed-VM manager and ordinary host session")
26 }
27 id, err := uuid.Parse(idText)
28 if err != nil {
29 t.Fatal("invalid test VM")
30 }
31 token := windows.GetCurrentProcessToken()
32 if token.IsElevated() {
33 t.Fatal("this acceptance test must use the ordinary desktop token")
34 }
35 admins, _ := windows.StringToSid("S-1-5-32-578")
36 member, err := token.IsMember(admins)
37 if err != nil || member {
38 t.Fatal("ordinary host must not receive Hyper-V Administrators membership")
39 }
40 binding, err := LoadWindowsHostManagerBinding(WindowsHostManagerBindingPath(id))
41 if err != nil {
42 t.Fatal(err)
43 }
44 user, err := token.GetTokenUser()
45 if err != nil || user.User.Sid.String() != binding.HostSID {
46 t.Fatal("acceptance token differs from protected manager host identity")
47 }
48 for _, path := range []string{WindowsHostManagerBindingPath(id), WindowsHostManagerExecutablePath(id)} {
49 name, _ := windows.UTF16PtrFromString(path)
50 handle, err := windows.CreateFile(name, windows.FILE_WRITE_DATA|windows.WRITE_DAC, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0)
51 if err == nil {
52 windows.CloseHandle(handle)
53 t.Fatal("ordinary caller can alter manager code or authority")
54 }
55 if err != windows.ERROR_ACCESS_DENIED {
56 t.Fatal("manager write protection was not proven by access denial", err)
57 }
58 }
59 scm, err := windows.OpenSCManager(nil, nil, windows.SC_MANAGER_CONNECT)
60 if err != nil {
61 t.Fatal(err)
62 }
63 defer windows.CloseServiceHandle(scm)
64 serviceName, _ := windows.UTF16PtrFromString(WindowsHostManagerServiceName(id))
65 service, err := windows.OpenService(scm, serviceName, windows.SERVICE_QUERY_CONFIG|windows.SERVICE_QUERY_STATUS|windows.SERVICE_START)
66 if err != nil {
67 t.Fatal("ordinary host cannot query or restore its fixed helper", err)
68 }
69 windows.CloseServiceHandle(service)
70 service, err = windows.OpenService(scm, serviceName, windows.SERVICE_CHANGE_CONFIG|windows.WRITE_DAC)
71 if err == nil {
72 windows.CloseServiceHandle(service)
73 t.Fatal("ordinary host can replace its SYSTEM service configuration")
74 }
75 if err != windows.ERROR_ACCESS_DENIED {
76 t.Fatal("service configuration write denial was not proven", err)
77 }
78 ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
79 defer cancel()
80 var inspected HyperVInspection
81 if err := windowsHostManagerCall(ctx, id, "inspect", 0, 0, &inspected); err != nil {
82 t.Fatal(err)
83 }
84 if inspected.Lookup != "found" || inspected.VMID != id.String() || inspected.VM == nil || inspected.VM.NetworkAdapterCount != 0 || inspected.VM.Generation != 2 || !inspected.VM.SecureBoot || !inspected.VM.VirtualTPM {
85 t.Fatal("manager did not return the registered offline-network VM layout")
86 }
87 public, err := InspectHyperV(ctx, id)
88 if err != nil || public.Lookup != "found" || public.VMID != id.String() || public.VM == nil || public.VM.State != inspected.VM.State {
89 t.Fatal("ordinary public inspection did not reach the authenticated fixed-VM manager", err)
90 }
91 var resources WindowsVMResourceState
92 if err := windowsHostManagerCall(ctx, id, "resources", 0, 0, &resources); err != nil {
93 t.Fatal(err)
94 }
95 if resources.State != inspected.VM.State || resources.CPUs <= 0 || resources.MemoryStartup < 4<<30 {
96 t.Fatal("manager did not return actual fixed-VM resources")
97 }
98 t.Logf("ordinary token accepted; VM state=%s, no NIC, CPU=%d, memory=%d GiB", inspected.VM.State, resources.CPUs, resources.MemoryStartup>>30)
99
100 request := func(t *testing.T, body string, expected string) {
101 t.Helper()
102 conn, err := winio.DialPipeAccessImpLevel(ctx, `\\.\pipe\Cage.HyperV.`+id.String(), 0x120003, winio.PipeImpLevelIdentification)
103 if err != nil {
104 t.Fatal(err)
105 }
106 defer conn.Close()
107 _ = conn.SetDeadline(time.Now().Add(10 * time.Second))
108 if err := verifyWindowsHostManagerPeer(conn, id); err != nil {
109 t.Fatal(err)
110 }
111 if _, err := conn.Write([]byte(body)); err != nil {
112 t.Fatal(err)
113 }
114 line, err := bufio.NewReader(conn).ReadBytes('\n')
115 if err != nil {
116 t.Fatal(err)
117 }
118 var response windowsHostManagerResponse
119 if json.Unmarshal(line, &response) != nil || response.OK || response.Error != expected || response.VMID != id || response.Version != windowsHostManagerVersion || len(response.Result) != 0 {
120 t.Fatalf("unsafe or unexpected rejection: %s", line)
121 }
122 }
123 for _, item := range []struct{ name, body, code string }{
124 {"wrong_vm", `{"version":1,"vm_id":"` + uuid.NewString() + `","operation":"inspect"}` + "\n", "invalid_request"},
125 {"unknown_operation", `{"version":1,"vm_id":"` + id.String() + `","operation":"execute"}` + "\n", "invalid_request"},
126 {"unknown_field", `{"version":1,"vm_id":"` + id.String() + `","operation":"inspect","command":"noop"}` + "\n", "invalid_request"},
127 {"resource_bounds", `{"version":1,"vm_id":"` + id.String() + `","operation":"resources","memory_gib":1025,"cpus":1}` + "\n", "invalid_request"},
128 {"oversize", strings.Repeat("x", windowsHostManagerWireLimit+1) + "\n", "invalid_request"},
129 {"extra_json", `{"version":1,"vm_id":"` + id.String() + `","operation":"inspect"} {}` + "\n", "invalid_request"},
130 } {
131 t.Run(item.name, func(t *testing.T) { request(t, item.body, item.code) })
132 }
133 // Invalid clients must not poison later reads or stop the existing VM.
134 var after HyperVInspection
135 if err := windowsHostManagerCall(ctx, id, "inspect", 0, 0, &after); err != nil || after.VM == nil || after.VM.State != inspected.VM.State {
136 t.Fatal("rejected requests damaged the manager or VM state", err)
137 }
138}