forked from langgenius/dify-plugin-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.go
More file actions
340 lines (271 loc) · 7.7 KB
/
zip.go
File metadata and controls
340 lines (271 loc) · 7.7 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package decoder
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
"github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/consts"
"github.com/langgenius/dify-plugin-daemon/pkg/utils/parser"
"github.com/langgenius/dify-plugin-daemon/pkg/utils/system"
)
type ZipPluginDecoder struct {
PluginDecoder
PluginDecoderHelper
reader *zip.Reader
err error
sig string
createTime int64
verification *Verification
thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig
}
type ThirdPartySignatureVerificationConfig struct {
Enabled bool
PublicKeyPaths []string
}
func newZipPluginDecoder(
binary []byte,
thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig,
) (*ZipPluginDecoder, error) {
reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
if err != nil {
return nil, errors.New(strings.ReplaceAll(err.Error(), "zip", "difypkg"))
}
decoder := &ZipPluginDecoder{
reader: reader,
err: err,
thirdPartySignatureVerificationConfig: thirdPartySignatureVerificationConfig,
}
err = decoder.Open()
if err != nil {
return nil, err
}
if _, err := decoder.Manifest(); err != nil {
return nil, err
}
return decoder, nil
}
// NewZipPluginDecoder is a helper function to create ZipPluginDecoder
func NewZipPluginDecoder(binary []byte) (*ZipPluginDecoder, error) {
return newZipPluginDecoder(binary, nil)
}
// NewZipPluginDecoderWithThirdPartySignatureVerificationConfig is a helper function
// to create a ZipPluginDecoder with a third party signature verification
func NewZipPluginDecoderWithThirdPartySignatureVerificationConfig(
binary []byte,
thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig,
) (*ZipPluginDecoder, error) {
return newZipPluginDecoder(binary, thirdPartySignatureVerificationConfig)
}
// NewZipPluginDecoderWithSizeLimit is a helper function to create a ZipPluginDecoder with a size limit
// It checks the total uncompressed size of the plugin package and returns an error if it exceeds the max size
func NewZipPluginDecoderWithSizeLimit(binary []byte, maxSize int64) (*ZipPluginDecoder, error) {
reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
if err != nil {
return nil, errors.New(strings.ReplaceAll(err.Error(), "zip", "difypkg"))
}
totalSize := int64(0)
for _, file := range reader.File {
totalSize += int64(file.UncompressedSize64)
if totalSize > maxSize {
return nil, errors.New(
"plugin package size is too large, please ensure the uncompressed size is less than " +
strconv.FormatInt(maxSize, 10) + " bytes",
)
}
}
return newZipPluginDecoder(binary, nil)
}
func (z *ZipPluginDecoder) Stat(filename string) (fs.FileInfo, error) {
f, err := z.reader.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return f.Stat()
}
func (z *ZipPluginDecoder) Open() error {
if z.reader == nil {
return z.err
}
return nil
}
func (z *ZipPluginDecoder) Walk(fn func(filename string, dir string) error) error {
if z.reader == nil {
return z.err
}
for _, file := range z.reader.File {
// split the path into directory and filename
dir, filename := path.Split(file.Name)
if err := fn(filename, dir); err != nil {
return err
}
}
return nil
}
func (z *ZipPluginDecoder) Close() error {
return nil
}
func (z *ZipPluginDecoder) ReadFile(filename string) ([]byte, error) {
if z.reader == nil {
return nil, z.err
}
file, err := z.reader.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
data := new(bytes.Buffer)
_, err = data.ReadFrom(file)
if err != nil {
return nil, err
}
return data.Bytes(), nil
}
func (z *ZipPluginDecoder) ReadDir(dirname string) ([]string, error) {
if z.reader == nil {
return nil, z.err
}
files := make([]string, 0)
dirNameWithSlash := strings.TrimSuffix(dirname, "/") + "/"
for _, file := range z.reader.File {
if strings.HasPrefix(file.Name, dirNameWithSlash) {
files = append(files, file.Name)
}
}
return files, nil
}
func (z *ZipPluginDecoder) FileReader(filename string) (io.ReadCloser, error) {
return z.reader.Open(filename)
}
func (z *ZipPluginDecoder) decode() error {
if z.reader == nil {
return z.err
}
signatureData, err := parser.UnmarshalJson[struct {
Signature string `json:"signature"`
Time int64 `json:"time"`
}](z.reader.Comment)
if err != nil {
return err
}
pluginSig := signatureData.Signature
pluginTime := signatureData.Time
var verification *Verification
// try to read the verification file
verificationData, err := z.ReadFile(consts.VERIFICATION_FILE)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
// if the verification file is not found, set the verification to nil
verification = nil
} else {
// unmarshal the verification data
verificationData, err := parser.UnmarshalJsonBytes[Verification](verificationData)
if err != nil {
return err
}
verification = &verificationData
}
z.sig = pluginSig
z.createTime = pluginTime
z.verification = verification
return nil
}
func (z *ZipPluginDecoder) Signature() (string, error) {
if z.sig != "" {
return z.sig, nil
}
if z.reader == nil {
return "", z.err
}
err := z.decode()
if err != nil {
return "", err
}
return z.sig, nil
}
func (z *ZipPluginDecoder) CreateTime() (int64, error) {
if z.createTime != 0 {
return z.createTime, nil
}
if z.reader == nil {
return 0, z.err
}
err := z.decode()
if err != nil {
return 0, err
}
return z.createTime, nil
}
func (z *ZipPluginDecoder) Verification() (*Verification, error) {
if !z.Verified() {
return nil, errors.New("plugin is not verified")
}
if z.verification != nil {
return z.verification, nil
}
err := z.decode()
if err != nil {
return nil, err
}
// if the plugin is verified but the verification is nil
// it's historical reason that all plugins are signed by langgenius
return nil, nil
}
func (z *ZipPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
return z.PluginDecoderHelper.Manifest(z)
}
func (z *ZipPluginDecoder) Assets() (map[string][]byte, error) {
// FIXES: https://github.com/langgenius/dify-plugin-daemon/issues/166
// zip file is os-independent, `/` is the separator
return z.PluginDecoderHelper.Assets(z, "/")
}
func (z *ZipPluginDecoder) Checksum() (string, error) {
return z.PluginDecoderHelper.Checksum(z)
}
func (z *ZipPluginDecoder) UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error) {
return z.PluginDecoderHelper.UniqueIdentity(z)
}
func (z *ZipPluginDecoder) ExtractTo(dst string) error {
// copy to working directory
if err := z.Walk(func(filename, dir string) error {
workingPath := path.Join(dst, dir)
// check if directory exists
if err := os.MkdirAll(workingPath, 0755); err != nil {
return err
}
bytes, err := z.ReadFile(system.GetZipReadPath(dir, filename))
if err != nil {
return err
}
filename = filepath.Join(workingPath, filename)
// copy file
if err := os.WriteFile(filename, bytes, 0644); err != nil {
return err
}
return nil
}); err != nil {
// if error, delete the working directory
os.RemoveAll(dst)
return errors.Join(fmt.Errorf("copy plugin to working directory error: %v", err), err)
}
return nil
}
func (z *ZipPluginDecoder) CheckAssetsValid() error {
return z.PluginDecoderHelper.CheckAssetsValid(z)
}
func (z *ZipPluginDecoder) Verified() bool {
return z.PluginDecoderHelper.verified(z)
}
func (z *ZipPluginDecoder) AvailableI18nReadme() (map[string]string, error) {
return z.PluginDecoderHelper.AvailableI18nReadme(z, "/")
}