仰望星辰工作室

miaopanel

miaopanel/ internal/plugins/archive.go 4.6 KB · 176 行 原始文件
1package plugins
2
3import (
4 "archive/zip"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "os"
10 "path"
11 "path/filepath"
12 "strings"
13)
14
15// archive limits mirror the upload checks on the cloud side.
16const (
17 maxArchiveEntries = 20000
18 maxArchiveBytes = 512 << 20
19 maxArchiveFile = 128 << 20
20 maxArchiveRatio = 100
21)
22
23// extractZip unpacks an archive into dest with zip-slip and zip-bomb guards.
24// Validation runs as a separate pass so a bomb is rejected before any bytes
25// are written to disk.
26func extractZip(src, dest string) error {
27 info, err := os.Stat(src)
28 if err != nil {
29 return err
30 }
31 zr, err := zip.OpenReader(src)
32 if err != nil {
33 return fmt.Errorf("不是合法的 zip 包: %w", err)
34 }
35 defer zr.Close()
36 if len(zr.File) > maxArchiveEntries {
37 return errors.New("压缩包条目过多")
38 }
39
40 var total int64
41 names := make([]string, 0, len(zr.File))
42 for _, f := range zr.File {
43 name, isDir, err := validateEntry(f)
44 if err != nil {
45 return err
46 }
47 names = append(names, name)
48 if isDir {
49 continue
50 }
51 total += int64(f.UncompressedSize64)
52 if total > maxArchiveBytes {
53 return errors.New("解压后体积超过限制")
54 }
55 }
56 if info.Size() > 0 && float64(total)/float64(info.Size()) > maxArchiveRatio {
57 return errors.New("压缩比异常,疑似 zip bomb")
58 }
59
60 for i, f := range zr.File {
61 name := names[i]
62 if f.FileInfo().IsDir() {
63 if err := os.MkdirAll(filepath.Join(dest, name), 0o755); err != nil {
64 return err
65 }
66 continue
67 }
68 target := filepath.Join(dest, name)
69 if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
70 return err
71 }
72 perm := os.FileMode(0o644)
73 if f.Mode().Perm()&0o111 != 0 {
74 perm = 0o755
75 }
76 if err := writeZipEntry(f, target, perm); err != nil {
77 return err
78 }
79 }
80 return nil
81}
82
83func validateEntry(f *zip.File) (string, bool, error) {
84 name := strings.ReplaceAll(f.Name, "\\", "/")
85 if strings.HasPrefix(name, "__MACOSX/") || strings.HasSuffix(name, ".DS_Store") {
86 return path.Clean(name), f.FileInfo().IsDir(), nil
87 }
88 clean := path.Clean(name)
89 if strings.HasPrefix(clean, "/") || clean == ".." || strings.HasPrefix(clean, "../") ||
90 strings.Contains(clean, "/../") || strings.ContainsRune(clean, 0) {
91 return "", false, fmt.Errorf("压缩包包含非法路径: %q", f.Name)
92 }
93 mode := f.Mode()
94 if mode&os.ModeSymlink != 0 {
95 return "", false, fmt.Errorf("压缩包不允许符号链接: %q", f.Name)
96 }
97 if f.FileInfo().IsDir() {
98 return clean, true, nil
99 }
100 if !mode.IsRegular() {
101 return "", false, fmt.Errorf("压缩包包含非普通文件: %q", f.Name)
102 }
103 if mode.Perm()&0o6000 != 0 {
104 return "", false, fmt.Errorf("压缩包包含 setuid 文件: %q", f.Name)
105 }
106 if f.UncompressedSize64 > maxArchiveFile {
107 return "", false, fmt.Errorf("单文件过大: %q", f.Name)
108 }
109 return clean, false, nil
110}
111
112func writeZipEntry(f *zip.File, target string, perm os.FileMode) error {
113 rc, err := f.Open()
114 if err != nil {
115 return err
116 }
117 defer rc.Close()
118 dst, err := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perm)
119 if err != nil {
120 return err
121 }
122 if _, err := io.Copy(dst, io.LimitReader(rc, maxArchiveFile+1)); err != nil {
123 dst.Close()
124 return err
125 }
126 return dst.Close()
127}
128
129// manifestFromZip returns the raw manifest bytes at the archive root.
130func manifestFromZip(src string) ([]byte, error) {
131 zr, err := zip.OpenReader(src)
132 if err != nil {
133 return nil, err
134 }
135 defer zr.Close()
136 for _, f := range zr.File {
137 if path.Clean(strings.ReplaceAll(f.Name, "\\", "/")) == "manifest.json" {
138 rc, err := f.Open()
139 if err != nil {
140 return nil, err
141 }
142 defer rc.Close()
143 buf, err := io.ReadAll(io.LimitReader(rc, 256<<10+1))
144 if err != nil {
145 return nil, err
146 }
147 if len(buf) > 256<<10 {
148 return nil, errors.New("manifest 过大")
149 }
150 return buf, nil
151 }
152 }
153 return nil, errors.New("压缩包根目录缺少 manifest.json")
154}
155
156// checkArchiveManifest verifies that the embedded manifest belongs to the same
157// plugin and version as the signed document. Byte equality is impossible: the
158// artifact hash covers the zip and the zip contains the manifest, so the signed
159// document is authoritative and only identity fields are compared.
160func checkArchiveManifest(src string, want *Manifest) error {
161 raw, err := manifestFromZip(src)
162 if err != nil {
163 return err
164 }
165 var inner struct {
166 Slug string `json:"slug"`
167 Version string `json:"version"`
168 }
169 if err := json.Unmarshal(raw, &inner); err != nil {
170 return errors.New("压缩包内的 manifest.json 不是合法 JSON")
171 }
172 if inner.Slug != want.Slug || inner.Version != want.Version {
173 return errors.New("压缩包内的 manifest.json 与签名清单不一致")
174 }
175 return nil
176}