zip.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package decoder
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/fs"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  15. "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
  16. )
  17. type ZipPluginDecoder struct {
  18. PluginDecoder
  19. PluginDecoderHelper
  20. reader *zip.Reader
  21. err error
  22. sig string
  23. createTime int64
  24. thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig
  25. }
  26. type ThirdPartySignatureVerificationConfig struct {
  27. Enabled bool
  28. PublicKeyPaths []string
  29. }
  30. func newZipPluginDecoder(binary []byte, thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig) (*ZipPluginDecoder, error) {
  31. reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
  32. if err != nil {
  33. return nil, errors.New(strings.ReplaceAll(err.Error(), "zip", "difypkg"))
  34. }
  35. decoder := &ZipPluginDecoder{
  36. reader: reader,
  37. err: err,
  38. thirdPartySignatureVerificationConfig: thirdPartySignatureVerificationConfig,
  39. }
  40. err = decoder.Open()
  41. if err != nil {
  42. return nil, err
  43. }
  44. if _, err := decoder.Manifest(); err != nil {
  45. return nil, err
  46. }
  47. return decoder, nil
  48. }
  49. // NewZipPluginDecoder is a helper function to create ZipPluginDecoder
  50. func NewZipPluginDecoder(binary []byte) (*ZipPluginDecoder, error) {
  51. return newZipPluginDecoder(binary, nil)
  52. }
  53. // NewZipPluginDecoderWithThirdPartySignatureVerificationConfig is a helper function
  54. // to create a ZipPluginDecoder with a third party signature verification
  55. func NewZipPluginDecoderWithThirdPartySignatureVerificationConfig(binary []byte, thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig) (*ZipPluginDecoder, error) {
  56. return newZipPluginDecoder(binary, thirdPartySignatureVerificationConfig)
  57. }
  58. // NewZipPluginDecoderWithSizeLimit is a helper function to create a ZipPluginDecoder with a size limit
  59. // It checks the total uncompressed size of the plugin package and returns an error if it exceeds the max size
  60. func NewZipPluginDecoderWithSizeLimit(binary []byte, maxSize int64) (*ZipPluginDecoder, error) {
  61. reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
  62. if err != nil {
  63. return nil, errors.New(strings.ReplaceAll(err.Error(), "zip", "difypkg"))
  64. }
  65. totalSize := int64(0)
  66. for _, file := range reader.File {
  67. totalSize += int64(file.UncompressedSize64)
  68. if totalSize > maxSize {
  69. return nil, errors.New(
  70. "plugin package size is too large, please ensure the uncompressed size is less than " +
  71. strconv.FormatInt(maxSize, 10) + " bytes",
  72. )
  73. }
  74. }
  75. return newZipPluginDecoder(binary, nil)
  76. }
  77. func (z *ZipPluginDecoder) Stat(filename string) (fs.FileInfo, error) {
  78. f, err := z.reader.Open(filename)
  79. if err != nil {
  80. return nil, err
  81. }
  82. defer f.Close()
  83. return f.Stat()
  84. }
  85. func (z *ZipPluginDecoder) Open() error {
  86. if z.reader == nil {
  87. return z.err
  88. }
  89. return nil
  90. }
  91. func (z *ZipPluginDecoder) Walk(fn func(filename string, dir string) error) error {
  92. if z.reader == nil {
  93. return z.err
  94. }
  95. for _, file := range z.reader.File {
  96. // split the path into directory and filename
  97. dir, filename := path.Split(file.Name)
  98. if err := fn(filename, dir); err != nil {
  99. return err
  100. }
  101. }
  102. return nil
  103. }
  104. func (z *ZipPluginDecoder) Close() error {
  105. return nil
  106. }
  107. func (z *ZipPluginDecoder) ReadFile(filename string) ([]byte, error) {
  108. if z.reader == nil {
  109. return nil, z.err
  110. }
  111. file, err := z.reader.Open(filename)
  112. if err != nil {
  113. return nil, err
  114. }
  115. defer file.Close()
  116. data := new(bytes.Buffer)
  117. _, err = data.ReadFrom(file)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return data.Bytes(), nil
  122. }
  123. func (z *ZipPluginDecoder) ReadDir(dirname string) ([]string, error) {
  124. if z.reader == nil {
  125. return nil, z.err
  126. }
  127. files := make([]string, 0)
  128. dirNameWithSlash := strings.TrimSuffix(dirname, "/") + "/"
  129. for _, file := range z.reader.File {
  130. if strings.HasPrefix(file.Name, dirNameWithSlash) {
  131. files = append(files, file.Name)
  132. }
  133. }
  134. return files, nil
  135. }
  136. func (z *ZipPluginDecoder) FileReader(filename string) (io.ReadCloser, error) {
  137. return z.reader.Open(filename)
  138. }
  139. func (z *ZipPluginDecoder) decode() error {
  140. if z.reader == nil {
  141. return z.err
  142. }
  143. signatureData, err := parser.UnmarshalJson[struct {
  144. Signature string `json:"signature"`
  145. Time int64 `json:"time"`
  146. }](z.reader.Comment)
  147. if err != nil {
  148. return err
  149. }
  150. pluginSig := signatureData.Signature
  151. pluginTime := signatureData.Time
  152. z.sig = pluginSig
  153. z.createTime = pluginTime
  154. return nil
  155. }
  156. func (z *ZipPluginDecoder) Signature() (string, error) {
  157. if z.sig != "" {
  158. return z.sig, nil
  159. }
  160. if z.reader == nil {
  161. return "", z.err
  162. }
  163. err := z.decode()
  164. if err != nil {
  165. return "", err
  166. }
  167. return z.sig, nil
  168. }
  169. func (z *ZipPluginDecoder) CreateTime() (int64, error) {
  170. if z.createTime != 0 {
  171. return z.createTime, nil
  172. }
  173. if z.reader == nil {
  174. return 0, z.err
  175. }
  176. err := z.decode()
  177. if err != nil {
  178. return 0, err
  179. }
  180. return z.createTime, nil
  181. }
  182. func (z *ZipPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
  183. return z.PluginDecoderHelper.Manifest(z)
  184. }
  185. func (z *ZipPluginDecoder) Assets() (map[string][]byte, error) {
  186. return z.PluginDecoderHelper.Assets(z)
  187. }
  188. func (z *ZipPluginDecoder) Checksum() (string, error) {
  189. return z.PluginDecoderHelper.Checksum(z)
  190. }
  191. func (z *ZipPluginDecoder) UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error) {
  192. return z.PluginDecoderHelper.UniqueIdentity(z)
  193. }
  194. func (z *ZipPluginDecoder) ExtractTo(dst string) error {
  195. // copy to working directory
  196. if err := z.Walk(func(filename, dir string) error {
  197. workingPath := path.Join(dst, dir)
  198. // check if directory exists
  199. if err := os.MkdirAll(workingPath, 0755); err != nil {
  200. return err
  201. }
  202. bytes, err := z.ReadFile(filepath.Join(dir, filename))
  203. if err != nil {
  204. return err
  205. }
  206. filename = filepath.Join(workingPath, filename)
  207. // copy file
  208. if err := os.WriteFile(filename, bytes, 0644); err != nil {
  209. return err
  210. }
  211. return nil
  212. }); err != nil {
  213. // if error, delete the working directory
  214. os.RemoveAll(dst)
  215. return errors.Join(fmt.Errorf("copy plugin to working directory error: %v", err), err)
  216. }
  217. return nil
  218. }
  219. func (z *ZipPluginDecoder) CheckAssetsValid() error {
  220. return z.PluginDecoderHelper.CheckAssetsValid(z)
  221. }