跳到主要内容

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

浏览文件

github_test.go

82 行 · 3.1 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package limatmpl
5
6import (
7 "strings"
8 "testing"
9
10 "gotest.tools/v3/assert"
11)
12
13func TestSymlinkTarget(t *testing.T) {
14 t.Run("target in the same directory", func(t *testing.T) {
15 got, err := symlinkTarget("dir/config.yaml", "other.yaml")
16 assert.NilError(t, err)
17 assert.Equal(t, got, "dir/other.yaml")
18 })
19 t.Run("target relative to the repo root", func(t *testing.T) {
20 got, err := symlinkTarget(".lima.yaml", "sub/x.yaml")
21 assert.NilError(t, err)
22 assert.Equal(t, got, "sub/x.yaml")
23 })
24 t.Run("parent that stays within the repo", func(t *testing.T) {
25 got, err := symlinkTarget("dir/config.yaml", "../top.yaml")
26 assert.NilError(t, err)
27 assert.Equal(t, got, "top.yaml")
28 })
29 t.Run("parent from the repo root is rejected", func(t *testing.T) {
30 _, err := symlinkTarget(".lima.yaml", "../evil.yaml")
31 assert.ErrorContains(t, err, "escapes the repository")
32 })
33 t.Run("traversal to another repo is rejected", func(t *testing.T) {
34 _, err := symlinkTarget("dir/config.yaml", "../../../../torvalds/linux/master/README")
35 assert.ErrorContains(t, err, "escapes the repository")
36 })
37}
38
39func TestEscapesRepo(t *testing.T) {
40 for _, tc := range []struct {
41 path string
42 want bool
43 }{
44 {"", false}, // github:ORG has no PATH, and path.Clean("") is "."
45 {"x.yaml", false},
46 {"dir/../x.yaml", false},
47 {"..", true},
48 {"../x.yaml", true},
49 {"dir/../../x.yaml", true},
50 } {
51 assert.Equal(t, escapesRepo(tc.path), tc.want, "path %#q", tc.path)
52 }
53}
54
55// A `..` in REPO or PATH reaches transformGitHubURL from a github: redirect body,
56// which validateGitHubRedirect's prefix check alone does not contain.
57func TestTransformGitHubURLContainment(t *testing.T) {
58 t.Run("path climbing out of the repo is rejected", func(t *testing.T) {
59 _, err := transformGitHubURL(t.Context(), "lima-vm/lima/../../../torvalds/linux/master/evil.yaml")
60 assert.ErrorContains(t, err, "escapes the repository")
61 })
62 t.Run("repo of .. is rejected", func(t *testing.T) {
63 _, err := transformGitHubURL(t.Context(), "lima-vm/../../torvalds/linux/master/evil.yaml")
64 assert.ErrorContains(t, err, "escapes the org")
65 })
66 t.Run("branch climbing out of the repo is rejected", func(t *testing.T) {
67 _, err := transformGitHubURL(t.Context(), "lima-vm/lima/README.md@../../torvalds/linux/master")
68 assert.ErrorContains(t, err, "escapes the repository")
69 })
70 t.Run("redirect body leaving the org is rejected on re-entry", func(t *testing.T) {
71 redirect, err := validateGitHubRedirect("github:lima-vm/../../torvalds/linux/master/evil.yaml", "lima-vm", "main", "https://example.com/x")
72 assert.NilError(t, err)
73 // TransformCustomURL feeds the locator back in as the github: URL's opaque part.
74 _, err = transformGitHubURL(t.Context(), strings.TrimPrefix(redirect, "github:"))
75 assert.ErrorContains(t, err, "escapes the org")
76 })
77 t.Run("parent that stays within the repo is allowed", func(t *testing.T) {
78 got, err := transformGitHubURL(t.Context(), "lima-vm/lima/docs/../README.md@main")
79 assert.NilError(t, err)
80 assert.Equal(t, got, "https://raw.githubusercontent.com/lima-vm/lima/main/README.md")
81 })
82}