跳到主要内容

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

浏览文件

validate_unix_test.go

80 行 · 2.5 KB历史下载
1//go:build !windows
2
3// SPDX-FileCopyrightText: Copyright The Lima Authors
4// SPDX-License-Identifier: Apache-2.0
5
6package networks
7
8import (
9 "errors"
10 "os"
11 "testing"
12
13 "gotest.tools/v3/assert"
14)
15
16func TestValidateRejectsInjectableVarRun(t *testing.T) {
17 // findBaseDirectory() trims the components that don't exist yet, so the
18 // injected sudoers directive never reached the path check.
19 err := (&Config{Paths: Paths{
20 VarRun: "/private/var/run/lima\n%staff ALL=(root:wheel) NOPASSWD:NOSETENV: ALL",
21 }}).Validate()
22 assert.ErrorContains(t, err, "invalid component")
23
24 // A space in the same position injects an extra argument into the sudo command.
25 err = (&Config{Paths: Paths{
26 VarRun: "/private/var/run/lima --extra-root-flag",
27 }}).Validate()
28 assert.ErrorContains(t, err, "invalid component")
29
30 // A comma ends the command list entry, so "ALL" becomes a command of its own.
31 err = (&Config{Paths: Paths{
32 VarRun: "/private/var/run/lima,ALL",
33 }}).Validate()
34 assert.ErrorContains(t, err, "invalid component")
35}
36
37func TestValidateVarRunString(t *testing.T) {
38 for _, path := range []string{
39 "",
40 "/",
41 "/private/var/run/lima",
42 "/private/var/run/lima/",
43 "/private/etc/sudoers.d/lima",
44 "/opt/socket_vmnet/bin/socket_vmnet",
45 "/opt/homebrew/opt/socket_vmnet/bin/socket_vmnet",
46 } {
47 assert.NilError(t, validateVarRunString(path), path)
48 }
49 for _, path := range []string{
50 "private/var/run/lima",
51 "/private/var/run/lima ALL",
52 "/private/var/run/lima\tALL",
53 "/private/var/run/lima\nALL",
54 "/private/var/run/lima,ALL",
55 "/private/var/run/lima:ALL",
56 "/private/var/run/lima\\ ALL",
57 "/private/var/run/../../etc",
58 } {
59 assert.Assert(t, validateVarRunString(path) != nil, path)
60 }
61}
62
63// The stricter identifier rule is applied to varRun only. socketVMNet and sudoers
64// keep the whitespace check from master, so layouts with dotted components (which
65// identifiers.Validate rejects) must still pass validatePath's string checks.
66func TestValidatePathAllowsDottedComponents(t *testing.T) {
67 for _, path := range []string{
68 "/lima-nonexistent-test/foo/.local/bin/socket_vmnet",
69 "/lima-nonexistent-test/etc/sudoers.d/lima",
70 } {
71 // os.Lstat runs after the string checks and fails because the path does
72 // not exist; reaching that error means the dotted component passed the
73 // stricter identifier rule that would apply to varRun.
74 err := validatePath(path, false)
75 assert.Assert(t, errors.Is(err, os.ErrNotExist), "%s: %v", path, err)
76 }
77
78 err := validatePath("/private/var/run/lima ALL", false)
79 assert.ErrorContains(t, err, "contains whitespace")
80}