跳到主要内容

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

浏览文件

private_windows.go

90 行 · 2.7 KB历史下载
1package host
2
3import (
4 "errors"
5 "os"
6 "unsafe"
7
8 "golang.org/x/sys/windows"
9)
10
11func privateSID() (*windows.SID, error) {
12 u, err := windows.GetCurrentProcessToken().GetTokenUser()
13 if err != nil {
14 return nil, err
15 }
16 return u.User.Sid, nil
17}
18
19func protectPrivate(path string, dir bool) error {
20 sid, err := privateSID()
21 if err != nil {
22 return err
23 }
24 flags := ""
25 if dir {
26 flags = "OICI"
27 }
28 sd, err := windows.SecurityDescriptorFromString("D:P(A;" + flags + ";FA;;;" + sid.String() + ")(A;" + flags + ";FA;;;SY)")
29 if err != nil {
30 return err
31 }
32 acl, _, err := sd.DACL()
33 if err != nil {
34 return err
35 }
36 // An owner may replace its DACL even when the parent granted only Modify.
37 // Requesting WRITE_OWNER on that path would deny a normal user's install.
38 current, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION)
39 if err != nil {
40 return err
41 }
42 owner, _, err := current.Owner()
43 if err != nil {
44 return err
45 }
46 information := windows.SECURITY_INFORMATION(windows.DACL_SECURITY_INFORMATION | windows.PROTECTED_DACL_SECURITY_INFORMATION)
47 var newOwner *windows.SID
48 if owner == nil || !windows.EqualSid(owner, sid) {
49 // An elevated process can create objects owned by Administrators.
50 // Transfer them to the user so both normal and elevated launches work.
51 information |= windows.OWNER_SECURITY_INFORMATION
52 newOwner = sid
53 }
54 return windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, information, newOwner, nil, acl, nil)
55}
56
57func checkPrivate(path string, _ os.FileInfo) error {
58 sid, err := privateSID()
59 if err != nil {
60 return err
61 }
62 sd, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION)
63 if err != nil {
64 return err
65 }
66 owner, _, err := sd.Owner()
67 if err != nil || owner == nil || !windows.EqualSid(owner, sid) {
68 return errors.New("private record is not owned by the current user")
69 }
70 acl, _, err := sd.DACL()
71 if err != nil || acl == nil || acl.AceCount == 0 {
72 return errors.New("private record has no usable access restriction")
73 }
74 for i := uint32(0); i < uint32(acl.AceCount); i++ {
75 var ace *windows.ACCESS_ALLOWED_ACE
76 if err := windows.GetAce(acl, i, &ace); err != nil {
77 return err
78 }
79 // Only simple allow ACEs from our protected DACL are accepted. Unknown
80 // ACE formats must not accidentally grant access that this check misses.
81 if ace.Header.AceType != windows.ACCESS_ALLOWED_ACE_TYPE {
82 return errors.New("private record has an unsupported access rule")
83 }
84 principal := (*windows.SID)(unsafe.Pointer(&ace.SidStart))
85 if !windows.EqualSid(principal, sid) && !principal.IsWellKnown(windows.WinLocalSystemSid) {
86 return errors.New("private record is accessible to other users")
87 }
88 }
89 return nil
90}