package plugins import ( "archive/zip" "encoding/json" "errors" "fmt" "io" "os" "path" "path/filepath" "strings" ) // archive limits mirror the upload checks on the cloud side. const ( maxArchiveEntries = 20000 maxArchiveBytes = 512 << 20 maxArchiveFile = 128 << 20 maxArchiveRatio = 100 ) // extractZip unpacks an archive into dest with zip-slip and zip-bomb guards. // Validation runs as a separate pass so a bomb is rejected before any bytes // are written to disk. func extractZip(src, dest string) error { info, err := os.Stat(src) if err != nil { return err } zr, err := zip.OpenReader(src) if err != nil { return fmt.Errorf("不是合法的 zip 包: %w", err) } defer zr.Close() if len(zr.File) > maxArchiveEntries { return errors.New("压缩包条目过多") } var total int64 names := make([]string, 0, len(zr.File)) for _, f := range zr.File { name, isDir, err := validateEntry(f) if err != nil { return err } names = append(names, name) if isDir { continue } total += int64(f.UncompressedSize64) if total > maxArchiveBytes { return errors.New("解压后体积超过限制") } } if info.Size() > 0 && float64(total)/float64(info.Size()) > maxArchiveRatio { return errors.New("压缩比异常,疑似 zip bomb") } for i, f := range zr.File { name := names[i] if f.FileInfo().IsDir() { if err := os.MkdirAll(filepath.Join(dest, name), 0o755); err != nil { return err } continue } target := filepath.Join(dest, name) if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil { return err } perm := os.FileMode(0o644) if f.Mode().Perm()&0o111 != 0 { perm = 0o755 } if err := writeZipEntry(f, target, perm); err != nil { return err } } return nil } func validateEntry(f *zip.File) (string, bool, error) { name := strings.ReplaceAll(f.Name, "\\", "/") if strings.HasPrefix(name, "__MACOSX/") || strings.HasSuffix(name, ".DS_Store") { return path.Clean(name), f.FileInfo().IsDir(), nil } clean := path.Clean(name) if strings.HasPrefix(clean, "/") || clean == ".." || strings.HasPrefix(clean, "../") || strings.Contains(clean, "/../") || strings.ContainsRune(clean, 0) { return "", false, fmt.Errorf("压缩包包含非法路径: %q", f.Name) } mode := f.Mode() if mode&os.ModeSymlink != 0 { return "", false, fmt.Errorf("压缩包不允许符号链接: %q", f.Name) } if f.FileInfo().IsDir() { return clean, true, nil } if !mode.IsRegular() { return "", false, fmt.Errorf("压缩包包含非普通文件: %q", f.Name) } if mode.Perm()&0o6000 != 0 { return "", false, fmt.Errorf("压缩包包含 setuid 文件: %q", f.Name) } if f.UncompressedSize64 > maxArchiveFile { return "", false, fmt.Errorf("单文件过大: %q", f.Name) } return clean, false, nil } func writeZipEntry(f *zip.File, target string, perm os.FileMode) error { rc, err := f.Open() if err != nil { return err } defer rc.Close() dst, err := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perm) if err != nil { return err } if _, err := io.Copy(dst, io.LimitReader(rc, maxArchiveFile+1)); err != nil { dst.Close() return err } return dst.Close() } // manifestFromZip returns the raw manifest bytes at the archive root. func manifestFromZip(src string) ([]byte, error) { zr, err := zip.OpenReader(src) if err != nil { return nil, err } defer zr.Close() for _, f := range zr.File { if path.Clean(strings.ReplaceAll(f.Name, "\\", "/")) == "manifest.json" { rc, err := f.Open() if err != nil { return nil, err } defer rc.Close() buf, err := io.ReadAll(io.LimitReader(rc, 256<<10+1)) if err != nil { return nil, err } if len(buf) > 256<<10 { return nil, errors.New("manifest 过大") } return buf, nil } } return nil, errors.New("压缩包根目录缺少 manifest.json") } // checkArchiveManifest verifies that the embedded manifest belongs to the same // plugin and version as the signed document. Byte equality is impossible: the // artifact hash covers the zip and the zip contains the manifest, so the signed // document is authoritative and only identity fields are compared. func checkArchiveManifest(src string, want *Manifest) error { raw, err := manifestFromZip(src) if err != nil { return err } var inner struct { Slug string `json:"slug"` Version string `json:"version"` } if err := json.Unmarshal(raw, &inner); err != nil { return errors.New("压缩包内的 manifest.json 不是合法 JSON") } if inner.Slug != want.Slug || inner.Version != want.Version { return errors.New("压缩包内的 manifest.json 与签名清单不一致") } return nil }