-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathfs.go
More file actions
267 lines (229 loc) · 6.33 KB
/
fs.go
File metadata and controls
267 lines (229 loc) · 6.33 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package decoder
import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
)
var (
ErrNotDir = errors.New("not a directory")
)
type FSPluginDecoder struct {
PluginDecoder
PluginDecoderHelper
// root directory of the plugin
root string
fs fs.FS
}
func NewFSPluginDecoder(root string) (*FSPluginDecoder, error) {
decoder := &FSPluginDecoder{
root: root,
}
err := decoder.Open()
if err != nil {
return nil, err
}
// read the manifest file
if _, err := decoder.Manifest(); err != nil {
return nil, err
}
return decoder, nil
}
func (d *FSPluginDecoder) Open() error {
d.fs = os.DirFS(d.root)
// try to stat the root directory
s, err := os.Stat(d.root)
if err != nil {
return err
}
if !s.IsDir() {
return ErrNotDir
}
return nil
}
func (d *FSPluginDecoder) Walk(fn func(filename string, dir string) error) error {
// read .difyignore file
ignorePatterns := []gitignore.Pattern{}
// Try .difyignore first, fallback to .gitignore if not found
ignoreBytes, err := d.ReadFile(".difyignore")
if err != nil {
ignoreBytes, err = d.ReadFile(".gitignore")
}
if err == nil {
ignoreLines := strings.Split(string(ignoreBytes), "\n")
for _, line := range ignoreLines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
ignorePatterns = append(ignorePatterns, gitignore.ParsePattern(line, nil))
}
}
return filepath.WalkDir(d.root, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
// get relative path from root
relPath, err := filepath.Rel(d.root, path)
if err != nil {
return err
}
// skip root directory
if relPath == "." {
return nil
}
// check if path matches any ignore pattern
pathParts := strings.Split(relPath, string(filepath.Separator))
for _, pattern := range ignorePatterns {
if result := pattern.Match(pathParts, info.IsDir()); result == gitignore.Exclude {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
}
// get directory path relative to root
dir := filepath.Dir(relPath)
if dir == "." {
dir = ""
}
// skip if the path is a directory
if info.IsDir() {
return nil
}
return fn(info.Name(), dir)
})
}
func (d *FSPluginDecoder) Close() error {
return nil
}
// secureResolvePath securely resolves a path relative to a root directory.
//
// This function prevents path traversal attacks by validating that the resolved
// path stays within the root directory. It handles both forward slashes and
// OS-specific path separators, making it safe for cross-platform use.
//
// Parameters:
// - root: The base directory path that acts as a security boundary
// - name: A relative path (potentially with forward slashes) to resolve
//
// Returns:
// - The absolute, resolved path if it stays within root
// - An error if the path attempts to escape the root directory
//
// Security: This prevents attacks like "../../../etc/passwd" by computing
// the relative path from root to the target and rejecting any path that
// starts with ".." (indicating an escape attempt).
//
// Algorithm:
// 1. Join root with name, converting forward slashes to OS format
// 2. Clean the joined path to resolve any "." or ".." segments
// 3. Convert both root and target to absolute paths
// 4. Compute the relative path from root to target
// 5. If relative path starts with "..", reject as path traversal
//
// Example:
// root="/app/plugins", name="config/settings.yaml" -> "/app/plugins/config/settings.yaml"
// root="/app/plugins", name="../../../etc/passwd" -> error (path traversal)
func secureResolvePath(root, name string) (string, error) {
p := filepath.Join(root, filepath.FromSlash(name))
clean := filepath.Clean(p)
rootAbs, err := filepath.Abs(root)
if err != nil {
return "", err
}
cleanAbs, err := filepath.Abs(clean)
if err != nil {
return "", err
}
rel, err := filepath.Rel(rootAbs, cleanAbs)
if err != nil {
return "", err
}
if rel == "." {
return cleanAbs, nil
}
if strings.HasPrefix(rel, "..") {
return "", os.ErrPermission
}
return cleanAbs, nil
}
func (d *FSPluginDecoder) Stat(filename string) (fs.FileInfo, error) {
abs, err := secureResolvePath(d.root, filename)
if err != nil {
return nil, err
}
return os.Stat(abs)
}
func (d *FSPluginDecoder) ReadFile(filename string) ([]byte, error) {
abs, err := secureResolvePath(d.root, filename)
if err != nil {
return nil, err
}
return os.ReadFile(abs)
}
func (d *FSPluginDecoder) ReadDir(dirname string) ([]string, error) {
var files []string
err := filepath.WalkDir(
filepath.Join(d.root, dirname),
func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
relPath, err := filepath.Rel(d.root, path)
if err != nil {
return err
}
files = append(files, relPath)
}
return nil
},
)
if err != nil {
return nil, err
}
return files, nil
}
func (d *FSPluginDecoder) FileReader(filename string) (io.ReadCloser, error) {
abs, err := secureResolvePath(d.root, filename)
if err != nil {
return nil, err
}
return os.Open(abs)
}
func (d *FSPluginDecoder) Signature() (string, error) {
return "", nil
}
func (d *FSPluginDecoder) CreateTime() (int64, error) {
return 0, nil
}
func (d *FSPluginDecoder) Verification() (*Verification, error) {
return nil, nil
}
func (d *FSPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
return d.PluginDecoderHelper.Manifest(d)
}
func (d *FSPluginDecoder) Assets() (map[string][]byte, error) {
// use filepath.Separator as the separator to make it os-independent
return d.PluginDecoderHelper.Assets(d, string(filepath.Separator))
}
func (d *FSPluginDecoder) Checksum() (string, error) {
return d.PluginDecoderHelper.Checksum(d)
}
func (d *FSPluginDecoder) UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error) {
return d.PluginDecoderHelper.UniqueIdentity(d)
}
func (d *FSPluginDecoder) CheckAssetsValid() error {
return d.PluginDecoderHelper.CheckAssetsValid(d)
}
func (d *FSPluginDecoder) Verified() bool {
return d.PluginDecoderHelper.verified(d)
}
func (d *FSPluginDecoder) AvailableI18nReadme() (map[string]string, error) {
return d.PluginDecoderHelper.AvailableI18nReadme(d, string(filepath.Separator))
}