-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathrepository_path_test.go
More file actions
149 lines (141 loc) · 4.65 KB
/
Copy pathrepository_path_test.go
File metadata and controls
149 lines (141 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package github
import (
"context"
"testing"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidateRelativePath(t *testing.T) {
tests := []struct {
name string
value string
want string
wantErr string
}{
{name: "file", value: "docs/readme.md", want: "docs/readme.md"},
{name: "normalizes leading slash", value: "/docs/readme.md", want: "docs/readme.md"},
{name: "normalizes dot segment", value: "./.github/workflows/ci.yml", want: ".github/workflows/ci.yml"},
{name: "normalizes duplicate separator", value: ".github//workflows/ci.yml", want: ".github/workflows/ci.yml"},
{name: "empty", value: "", wantErr: "must not be empty"},
{name: "current directory", value: ".", wantErr: "must identify a file"},
{name: "double leading slash", value: "//.github/workflows/ci.yml", wantErr: "must be relative"},
{name: "parent traversal", value: "docs/../.github/workflows/ci.yml", wantErr: "parent directory traversal"},
{name: "leading traversal", value: "../.github/workflows/ci.yml", wantErr: "parent directory traversal"},
{name: "backslash traversal", value: `docs\..\.github\workflows\ci.yml`, wantErr: "forward slashes"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := validateRelativePath(tt.value)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestFileWriteWorkflowScopeChallenges(t *testing.T) {
tests := []struct {
name string
tool inventory.ServerTool
args map[string]any
want []string
}{
{
name: "create regular file",
tool: CreateOrUpdateFile(translations.NullTranslationHelper),
args: map[string]any{"path": "docs/readme.md"},
},
{
name: "create workflow",
tool: CreateOrUpdateFile(translations.NullTranslationHelper),
args: map[string]any{"path": ".github/workflows/ci.yml"},
want: []string{"repo", "workflow"},
},
{
name: "delete normalized workflow",
tool: DeleteFile(translations.NullTranslationHelper),
args: map[string]any{"path": "./.github/workflows/ci.yml"},
want: []string{"repo", "workflow"},
},
{
name: "reject traversal instead of resolving it",
tool: DeleteFile(translations.NullTranslationHelper),
args: map[string]any{"path": "docs/../.github/workflows/ci.yml"},
},
{
name: "push regular files",
tool: PushFiles(translations.NullTranslationHelper),
args: map[string]any{"files": []any{map[string]any{"path": "README.md"}}},
},
{
name: "push includes workflow",
tool: PushFiles(translations.NullTranslationHelper),
args: map[string]any{"files": []any{
map[string]any{"path": "README.md"},
map[string]any{"path": ".github/workflows/ci.yml"},
}},
want: []string{"workflow"},
},
{
name: "push validates entries after workflow",
tool: PushFiles(translations.NullTranslationHelper),
args: map[string]any{"files": []any{
map[string]any{"path": ".github/workflows/ci.yml"},
map[string]any{"path": "../unsafe.txt"},
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.NotNil(t, tt.tool.ScopeAccess.Challenge)
assert.Equal(t, tt.want, tt.tool.ScopeAccess.Challenge(tt.args, []string{"repo"}))
})
}
}
func TestFileWriteToolsRejectUnsafePathsBeforeAPICalls(t *testing.T) {
tests := []struct {
name string
tool inventory.ServerTool
args map[string]any
}{
{
name: "create or update",
tool: CreateOrUpdateFile(translations.NullTranslationHelper),
args: map[string]any{
"owner": "owner", "repo": "repo", "path": "../workflow.yml",
"content": "content", "message": "message", "branch": "main",
},
},
{
name: "delete",
tool: DeleteFile(translations.NullTranslationHelper),
args: map[string]any{
"owner": "owner", "repo": "repo", "path": "//.github/workflows/ci.yml",
"message": "message", "branch": "main",
},
},
{
name: "push",
tool: PushFiles(translations.NullTranslationHelper),
args: map[string]any{
"owner": "owner", "repo": "repo", "branch": "main", "message": "message",
"files": []any{map[string]any{"path": `..\workflow.yml`, "content": "content"}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
deps := BaseDeps{}
request := createMCPRequest(tt.args)
result, err := tt.tool.Handler(deps)(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)
require.True(t, result.IsError)
assert.Contains(t, getErrorResult(t, result).Text, "path")
})
}
}