plugin_package.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package service
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "context"
  6. "crypto/ed25519"
  7. "crypto/sha256"
  8. "encoding/base64"
  9. "encoding/hex"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "os"
  15. "path/filepath"
  16. "strings"
  17. "github.com/Wei-Shaw/sub2api/internal/config"
  18. )
  19. const (
  20. pluginManifestFilename = "manifest.json"
  21. pluginSignatureFilename = "signature.json"
  22. pluginArchiveMaxFiles = 512
  23. builtInOpenAITransportPluginID = "local.sub2api.openai-transport"
  24. builtInOpenAITransportPublisherKeyID = "sub2api-openai-transport-v1"
  25. builtInOpenAITransportPublisherKeyBase64 = "MqzSXAoG0iVR5kKWrC+mqcCeExkrT6zAr2WpQ4sA+yc="
  26. )
  27. type PluginPackageInstaller struct {
  28. cfg *config.Config
  29. hostInfo PluginHostInfo
  30. rootDir string
  31. }
  32. func NewPluginPackageInstaller(cfg *config.Config, hostInfo PluginHostInfo) *PluginPackageInstaller {
  33. return &PluginPackageInstaller{
  34. cfg: cfg,
  35. hostInfo: hostInfo,
  36. rootDir: resolvePluginRootDir(cfg),
  37. }
  38. }
  39. func resolvePluginRootDir(cfg *config.Config) string {
  40. if cfg != nil && strings.TrimSpace(cfg.Plugins.DataDir) != "" {
  41. return filepath.Clean(cfg.Plugins.DataDir)
  42. }
  43. base := strings.TrimSpace(os.Getenv("DATA_DIR"))
  44. if base == "" {
  45. base = "./data"
  46. }
  47. return filepath.Join(base, "plugins")
  48. }
  49. func (i *PluginPackageInstaller) RootDir() string {
  50. return i.rootDir
  51. }
  52. func (i *PluginPackageInstaller) Install(ctx context.Context, reader io.Reader, installedBy *int64) (*PluginInstallation, error) {
  53. if i == nil || i.cfg == nil {
  54. return nil, errors.New("插件安装器未配置")
  55. }
  56. if err := ctx.Err(); err != nil {
  57. return nil, err
  58. }
  59. stagingDir := filepath.Join(i.rootDir, "staging")
  60. packagesDir := filepath.Join(i.rootDir, "packages")
  61. installedDir := filepath.Join(i.rootDir, "installed")
  62. for _, dir := range []string{stagingDir, packagesDir, installedDir} {
  63. if err := os.MkdirAll(dir, 0o700); err != nil {
  64. return nil, fmt.Errorf("创建插件目录: %w", err)
  65. }
  66. }
  67. tempFile, err := os.CreateTemp(stagingDir, "upload-*.s2plugin")
  68. if err != nil {
  69. return nil, fmt.Errorf("创建插件上传临时文件: %w", err)
  70. }
  71. tempPath := tempFile.Name()
  72. committed := false
  73. defer func() {
  74. _ = tempFile.Close()
  75. if !committed {
  76. _ = os.Remove(tempPath)
  77. }
  78. }()
  79. hasher := sha256.New()
  80. limit := i.cfg.Plugins.MaxUploadBytes
  81. written, err := io.Copy(io.MultiWriter(tempFile, hasher), io.LimitReader(reader, limit+1))
  82. if err != nil {
  83. return nil, fmt.Errorf("读取插件包: %w", err)
  84. }
  85. if written > limit {
  86. return nil, fmt.Errorf("插件包超过 %d 字节限制", limit)
  87. }
  88. if err := tempFile.Sync(); err != nil {
  89. return nil, fmt.Errorf("同步插件包: %w", err)
  90. }
  91. if err := tempFile.Close(); err != nil {
  92. return nil, fmt.Errorf("关闭插件包: %w", err)
  93. }
  94. artifactSHA := hex.EncodeToString(hasher.Sum(nil))
  95. archive, err := zip.OpenReader(tempPath)
  96. if err != nil {
  97. return nil, fmt.Errorf("插件包不是有效的 ZIP: %w", err)
  98. }
  99. archiveClosed := false
  100. closeArchive := func() {
  101. if !archiveClosed {
  102. archiveClosed = true
  103. _ = archive.Close()
  104. }
  105. }
  106. defer closeArchive()
  107. manifest, _, signatureStatus, err := i.inspectArchive(&archive.Reader)
  108. if err != nil {
  109. return nil, err
  110. }
  111. compatibility := EvaluatePluginCompatibility(manifest, i.hostInfo)
  112. initialState := PluginStateDisabled
  113. if !compatibility.Compatible {
  114. initialState = PluginStateIncompatible
  115. }
  116. installParent := filepath.Join(installedDir, manifest.ID)
  117. if err := os.MkdirAll(installParent, 0o700); err != nil {
  118. return nil, fmt.Errorf("创建插件安装父目录: %w", err)
  119. }
  120. extractPath, err := os.MkdirTemp(installParent, ".install-*")
  121. if err != nil {
  122. return nil, fmt.Errorf("创建插件安装临时目录: %w", err)
  123. }
  124. installNonce := strings.TrimPrefix(filepath.Base(extractPath), ".install-")
  125. installPath := filepath.Join(installParent, manifest.Version+"-"+artifactSHA[:12]+"-"+installNonce)
  126. extracted := false
  127. defer func() {
  128. if !extracted {
  129. _ = os.RemoveAll(extractPath)
  130. }
  131. }()
  132. if err := i.extractArchive(ctx, &archive.Reader, manifest, extractPath); err != nil {
  133. return nil, err
  134. }
  135. // Windows 不允许重命名仍被打开的文件,提交前先释放 ZIP 读取器。
  136. closeArchive()
  137. if err := os.Rename(extractPath, installPath); err != nil {
  138. return nil, fmt.Errorf("提交插件安装目录: %w", err)
  139. }
  140. extracted = true
  141. artifactPath := filepath.Join(packagesDir, manifest.ID+"-"+manifest.Version+"-"+artifactSHA[:12]+"-"+installNonce+".s2plugin")
  142. if err := os.Rename(tempPath, artifactPath); err != nil {
  143. _ = os.RemoveAll(installPath)
  144. return nil, fmt.Errorf("保存插件包: %w", err)
  145. }
  146. committed = true
  147. artifactData, err := os.ReadFile(artifactPath)
  148. if err != nil {
  149. _ = os.Remove(artifactPath)
  150. _ = os.RemoveAll(installPath)
  151. return nil, fmt.Errorf("读取已保存插件包: %w", err)
  152. }
  153. runtimeEntry := manifest.Runtimes[manifest.RuntimeKey()]
  154. return &PluginInstallation{
  155. PluginKey: manifest.ID,
  156. Name: manifest.Name,
  157. Version: manifest.Version,
  158. Description: manifest.Description,
  159. Author: manifest.Author,
  160. Manifest: manifest,
  161. ArtifactData: artifactData,
  162. ArtifactPath: artifactPath,
  163. InstallPath: installPath,
  164. BinaryPath: filepath.Join(installPath, filepath.FromSlash(runtimeEntry.Path)),
  165. BinarySHA256: manifest.Files[runtimeEntry.Path],
  166. SignatureStatus: signatureStatus,
  167. State: initialState,
  168. InstalledBy: installedBy,
  169. Compatibility: compatibility,
  170. }, nil
  171. }
  172. func (i *PluginPackageInstaller) inspectArchive(archive *zip.Reader) (PluginManifest, []byte, string, error) {
  173. if len(archive.File) == 0 || len(archive.File) > pluginArchiveMaxFiles {
  174. return PluginManifest{}, nil, "", errors.New("插件包文件数量无效")
  175. }
  176. entries := make(map[string]*zip.File, len(archive.File))
  177. var total uint64
  178. for _, file := range archive.File {
  179. if file.FileInfo().IsDir() {
  180. if _, err := normalizePluginArchivePath(strings.TrimSuffix(file.Name, "/")); err != nil {
  181. return PluginManifest{}, nil, "", err
  182. }
  183. continue
  184. }
  185. name, err := normalizePluginArchivePath(file.Name)
  186. if err != nil {
  187. return PluginManifest{}, nil, "", err
  188. }
  189. if _, exists := entries[name]; exists {
  190. return PluginManifest{}, nil, "", fmt.Errorf("插件包包含重复路径: %s", name)
  191. }
  192. if file.Mode()&os.ModeSymlink != 0 {
  193. return PluginManifest{}, nil, "", fmt.Errorf("插件包不允许符号链接: %s", name)
  194. }
  195. total += file.UncompressedSize64
  196. if total > uint64(i.cfg.Plugins.MaxUncompressedBytes) {
  197. return PluginManifest{}, nil, "", errors.New("插件包解压后体积超过限制")
  198. }
  199. entries[name] = file
  200. }
  201. manifestFile := entries[pluginManifestFilename]
  202. if manifestFile == nil {
  203. return PluginManifest{}, nil, "", errors.New("插件包缺少 manifest.json")
  204. }
  205. manifestRaw, err := readPluginZipFile(manifestFile, 2*1024*1024)
  206. if err != nil {
  207. return PluginManifest{}, nil, "", fmt.Errorf("读取插件清单: %w", err)
  208. }
  209. var manifest PluginManifest
  210. decoder := json.NewDecoder(bytes.NewReader(manifestRaw))
  211. decoder.DisallowUnknownFields()
  212. if err := decoder.Decode(&manifest); err != nil {
  213. return PluginManifest{}, nil, "", fmt.Errorf("解析插件清单: %w", err)
  214. }
  215. if err := decoder.Decode(&struct{}{}); err != io.EOF {
  216. return PluginManifest{}, nil, "", errors.New("插件清单只能包含一个 JSON 对象")
  217. }
  218. if err := manifest.Validate(); err != nil {
  219. return PluginManifest{}, nil, "", err
  220. }
  221. for path := range entries {
  222. if path == pluginManifestFilename || path == pluginSignatureFilename {
  223. continue
  224. }
  225. if _, declared := manifest.Files[path]; !declared {
  226. return PluginManifest{}, nil, "", fmt.Errorf("插件包包含未声明文件: %s", path)
  227. }
  228. }
  229. for path := range manifest.Files {
  230. if entries[path] == nil {
  231. return PluginManifest{}, nil, "", fmt.Errorf("插件包缺少已声明文件: %s", path)
  232. }
  233. }
  234. signatureStatus, err := i.verifySignature(entries[pluginSignatureFilename], manifestRaw, manifest.ID)
  235. if err != nil {
  236. return PluginManifest{}, nil, "", err
  237. }
  238. return manifest, manifestRaw, signatureStatus, nil
  239. }
  240. func (i *PluginPackageInstaller) verifySignature(file *zip.File, manifestRaw []byte, pluginID string) (string, error) {
  241. if file == nil {
  242. if i.cfg.Plugins.AllowUnsigned {
  243. return PluginSignatureUnsigned, nil
  244. }
  245. return "", errors.New("生产配置不允许安装未签名插件")
  246. }
  247. raw, err := readPluginZipFile(file, 64*1024)
  248. if err != nil {
  249. return "", fmt.Errorf("读取插件签名: %w", err)
  250. }
  251. var signature PluginSignature
  252. if err := json.Unmarshal(raw, &signature); err != nil {
  253. return "", fmt.Errorf("解析插件签名: %w", err)
  254. }
  255. if signature.Algorithm != "ed25519" || strings.TrimSpace(signature.KeyID) == "" {
  256. return "", errors.New("插件签名算法或密钥 ID 无效")
  257. }
  258. encodedKey := trustedPluginPublisherKey(i.cfg, signature.KeyID, pluginID)
  259. if encodedKey == "" {
  260. return "", fmt.Errorf("插件发布者密钥不受信任: %s", signature.KeyID)
  261. }
  262. publicKey, err := base64.StdEncoding.DecodeString(encodedKey)
  263. if err != nil || len(publicKey) != ed25519.PublicKeySize {
  264. return "", fmt.Errorf("受信任发布者密钥无效: %s", signature.KeyID)
  265. }
  266. signatureBytes, err := base64.StdEncoding.DecodeString(signature.Signature)
  267. if err != nil || !ed25519.Verify(ed25519.PublicKey(publicKey), manifestRaw, signatureBytes) {
  268. return "", errors.New("插件签名校验失败")
  269. }
  270. return PluginSignatureTrusted, nil
  271. }
  272. func trustedPluginPublisherKey(cfg *config.Config, keyID, pluginID string) string {
  273. // 内置公钥是官方私有插件的固定信任根,不允许被部署配置覆盖。
  274. if keyID == builtInOpenAITransportPublisherKeyID {
  275. if pluginID != builtInOpenAITransportPluginID {
  276. return ""
  277. }
  278. return builtInOpenAITransportPublisherKeyBase64
  279. }
  280. if cfg == nil {
  281. return ""
  282. }
  283. return strings.TrimSpace(cfg.Plugins.TrustedPublishers[keyID])
  284. }
  285. func (i *PluginPackageInstaller) extractArchive(ctx context.Context, archive *zip.Reader, manifest PluginManifest, target string) error {
  286. var extractedBytes int64
  287. extractLimit := i.cfg.Plugins.MaxUncompressedBytes
  288. for path, expectedHash := range manifest.Files {
  289. if err := ctx.Err(); err != nil {
  290. return err
  291. }
  292. var source *zip.File
  293. for _, file := range archive.File {
  294. if strings.ReplaceAll(file.Name, "\\", "/") == path {
  295. source = file
  296. break
  297. }
  298. }
  299. if source == nil {
  300. return fmt.Errorf("缺少插件文件: %s", path)
  301. }
  302. destination, err := safePluginJoin(target, path)
  303. if err != nil {
  304. return err
  305. }
  306. if err := os.MkdirAll(filepath.Dir(destination), 0o700); err != nil {
  307. return fmt.Errorf("创建插件文件目录: %w", err)
  308. }
  309. input, err := source.Open()
  310. if err != nil {
  311. return fmt.Errorf("打开插件文件 %s: %w", path, err)
  312. }
  313. hasher := sha256.New()
  314. mode := os.FileMode(0o600)
  315. if path == manifest.Runtimes[manifest.RuntimeKey()].Path {
  316. mode = 0o700
  317. }
  318. output, err := os.OpenFile(destination, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode)
  319. if err != nil {
  320. _ = input.Close()
  321. return fmt.Errorf("创建插件文件 %s: %w", path, err)
  322. }
  323. remaining := extractLimit - extractedBytes
  324. if remaining < 0 {
  325. remaining = 0
  326. }
  327. copied, copyErr := io.Copy(io.MultiWriter(output, hasher), io.LimitReader(input, remaining+1))
  328. extractedBytes += copied
  329. closeOutErr := output.Close()
  330. closeInErr := input.Close()
  331. if copyErr != nil || closeOutErr != nil || closeInErr != nil {
  332. return fmt.Errorf("解压插件文件 %s 失败", path)
  333. }
  334. if extractedBytes > extractLimit {
  335. return errors.New("插件包实际解压体积超过限制")
  336. }
  337. if actual := hex.EncodeToString(hasher.Sum(nil)); actual != expectedHash {
  338. return fmt.Errorf("插件文件哈希不匹配: %s", path)
  339. }
  340. }
  341. return nil
  342. }
  343. func normalizePluginArchivePath(name string) (string, error) {
  344. normalized := strings.ReplaceAll(strings.TrimSpace(name), "\\", "/")
  345. if normalized == "" || strings.HasPrefix(normalized, "/") || strings.Contains(normalized, "\x00") {
  346. return "", errors.New("插件包包含无效路径")
  347. }
  348. cleaned := filepath.ToSlash(filepath.Clean(normalized))
  349. if cleaned == "." || cleaned == ".." || strings.HasPrefix(cleaned, "../") || cleaned != normalized {
  350. return "", fmt.Errorf("插件包包含不安全路径: %s", name)
  351. }
  352. return cleaned, nil
  353. }
  354. func safePluginJoin(root, relative string) (string, error) {
  355. destination := filepath.Join(root, filepath.FromSlash(relative))
  356. rel, err := filepath.Rel(root, destination)
  357. if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
  358. return "", fmt.Errorf("插件路径越界: %s", relative)
  359. }
  360. return destination, nil
  361. }
  362. func readPluginZipFile(file *zip.File, limit int64) ([]byte, error) {
  363. reader, err := file.Open()
  364. if err != nil {
  365. return nil, err
  366. }
  367. defer func() { _ = reader.Close() }()
  368. data, err := io.ReadAll(io.LimitReader(reader, limit+1))
  369. if err != nil {
  370. return nil, err
  371. }
  372. if int64(len(data)) > limit {
  373. return nil, errors.New("插件文件超过读取限制")
  374. }
  375. return data, nil
  376. }