miaopanel
1// Package plugins installs and supervises third-party plugins published on the
2// official catalog. Every artifact must carry both a developer signature and a
3// platform signature; the panel pins the platform key on first use.
4package plugins
5
6import (
7 "encoding/json"
8 "errors"
9 "fmt"
10 "path"
11 "regexp"
12 "strings"
13
14 "github.com/miaopanel/miaopanel/internal/store"
15)
16
17// Manifest mirrors the signed document served by the cloud. Keep the JSON tags
18// in sync with server/internal/pluginpkg/manifest.go.
19type Manifest struct {
20 Schema int `json:"schema"`
21 API int `json:"api"`
22 Slug string `json:"slug"`
23 Name string `json:"name"`
24 Version string `json:"version"`
25 Channel string `json:"channel"`
26 Summary string `json:"summary"`
27 DescriptionMD string `json:"description_md"`
28 Category string `json:"category"`
29 License string `json:"license"`
30 Homepage string `json:"homepage"`
31 Author Author `json:"author"`
32 Panel PanelRange `json:"panel"`
33 Platforms []Platform `json:"platforms"`
34 Permissions []string `json:"permissions"`
35 Requires Requires `json:"requires"`
36 Runtime Runtime `json:"runtime"`
37 Install Install `json:"install"`
38 UI json.RawMessage `json:"ui,omitempty"`
39 Artifact Artifact `json:"artifact"`
40 ChangelogMD string `json:"changelog_md"`
41 DevSig string `json:"dev_sig"`
42}
43
44// Author identifies the signing key.
45type Author struct {
46 Name string `json:"name"`
47 Email string `json:"email"`
48 Pubkey string `json:"pubkey"`
49}
50
51// PanelRange constrains compatible panel versions.
52type PanelRange struct {
53 Min string `json:"min"`
54 Max string `json:"max"`
55}
56
57// Platform is one supported architecture.
58type Platform struct {
59 OS string `json:"os"`
60 Arch string `json:"arch"`
61}
62
63// Requires declares prerequisites.
64type Requires struct {
65 Commands []string `json:"commands"`
66 Services []string `json:"services"`
67 DiskMB int `json:"disk_mb"`
68}
69
70// Runtime describes how the plugin runs.
71type Runtime struct {
72 Type string `json:"type"`
73 Service *ServiceSpec `json:"service,omitempty"`
74}
75
76// ServiceSpec configures the systemd unit.
77type ServiceSpec struct {
78 Exec string `json:"exec"`
79 Args []string `json:"args"`
80 RunAs string `json:"run_as"`
81 Restart string `json:"restart"`
82 MemoryMaxMB int `json:"memory_max_mb"`
83 Health string `json:"health"`
84}
85
86// Install points at lifecycle hooks inside the archive.
87type Install struct {
88 Script string `json:"script"`
89 Uninstall string `json:"uninstall"`
90 Upgrade string `json:"upgrade"`
91 Timeout int `json:"timeout"`
92}
93
94// Artifact pins the archive.
95type Artifact struct {
96 File string `json:"file"`
97 SHA256 string `json:"sha256"`
98 Size int64 `json:"size"`
99}
100
101var (
102 slugRe = regexp.MustCompile(`^[a-z][a-z0-9-]{2,40}$`)
103 permRe = regexp.MustCompile(`^(files|service|exec|nginx|net|docker):[A-Za-z0-9:/._@-]{1,120}$`)
104 hexRe = regexp.MustCompile(`^[a-f0-9]{64}$`)
105 safeRelRe = regexp.MustCompile(`^[A-Za-z0-9._/-]{1,160}$`)
106)
107
108// Parse validates the parts a compromised catalog could abuse. The cloud
109// already enforces the full rule set; the panel repeats the security relevant
110// checks because it is the process that runs as root.
111func Parse(raw []byte) (*Manifest, error) {
112 if len(raw) == 0 || len(raw) > 256<<10 {
113 return nil, errors.New("manifest 大小不合法")
114 }
115 var m Manifest
116 if err := json.Unmarshal(raw, &m); err != nil {
117 return nil, fmt.Errorf("manifest 不是合法 JSON: %w", err)
118 }
119 if m.Schema != 1 || m.API != 1 {
120 return nil, errors.New("manifest schema/api 版本不受支持")
121 }
122 if !slugRe.MatchString(m.Slug) {
123 return nil, errors.New("slug 不合法")
124 }
125 if m.Name == "" || len(m.Name) > 64 {
126 return nil, errors.New("插件名称不合法")
127 }
128 if m.Version == "" || len(m.Version) > 64 {
129 return nil, errors.New("版本号不合法")
130 }
131 switch m.Runtime.Type {
132 case "script", "static", "service":
133 default:
134 return nil, errors.New("runtime.type 不合法")
135 }
136 if m.Runtime.Type == "service" {
137 if m.Runtime.Service == nil || !safeRelative(m.Runtime.Service.Exec) {
138 return nil, errors.New("service 型插件必须提供合法的 exec")
139 }
140 }
141 if !safeRelative(m.Install.Script) {
142 return nil, errors.New("install.script 不合法")
143 }
144 if m.Install.Timeout == 0 {
145 m.Install.Timeout = 900
146 }
147 if m.Install.Timeout < 60 || m.Install.Timeout > 3600 {
148 return nil, errors.New("install.timeout 超出范围")
149 }
150 if !hexRe.MatchString(strings.ToLower(m.Artifact.SHA256)) || m.Artifact.Size <= 0 {
151 return nil, errors.New("artifact 校验信息不合法")
152 }
153 m.Artifact.SHA256 = strings.ToLower(m.Artifact.SHA256)
154 if len(m.Permissions) > 32 {
155 return nil, errors.New("权限声明过多")
156 }
157 for _, p := range m.Permissions {
158 if !permRe.MatchString(p) {
159 return nil, fmt.Errorf("权限声明不合法: %s", p)
160 }
161 if strings.HasPrefix(p, "files:") {
162 target := strings.TrimPrefix(p, "files:")
163 if !strings.HasPrefix(target, "/") || strings.Contains(target, "..") {
164 return nil, fmt.Errorf("files 权限必须是安全的绝对路径: %s", p)
165 }
166 if guardedPath(target) {
167 return nil, fmt.Errorf("files 权限不允许指向 %s", target)
168 }
169 }
170 }
171 return &m, nil
172}
173
174func guardedPath(p string) bool {
175 clean := path.Clean(p)
176 for _, root := range []string{"/", "/etc", "/usr", "/bin", "/sbin", "/boot",
177 "/dev", "/proc", "/sys", "/lib", "/lib64", "/www/server/panel"} {
178 if clean == root || strings.HasPrefix(clean, root+"/") {
179 return true
180 }
181 }
182 return false
183}
184
185func safeRelative(p string) bool {
186 if p == "" || strings.HasPrefix(p, "/") || !safeRelRe.MatchString(p) {
187 return false
188 }
189 clean := path.Clean(p)
190 return clean != "." && clean != ".." && !strings.HasPrefix(clean, "../")
191}
192
193// Permission is a UI friendly view of one declared capability.
194type Permission struct {
195 Raw string `json:"raw"`
196 Kind string `json:"kind"`
197 Value string `json:"value"`
198 Risk string `json:"risk"`
199 Label string `json:"label"`
200}
201
202// ParsePermissions classifies the strings for the install confirmation dialog.
203func ParsePermissions(list []string) []Permission {
204 out := make([]Permission, 0, len(list))
205 for _, raw := range list {
206 kind, value, _ := strings.Cut(raw, ":")
207 p := Permission{Raw: raw, Kind: kind, Value: value, Risk: "medium"}
208 switch kind {
209 case "files":
210 p.Risk, p.Label = "high", "读写目录"
211 case "service":
212 p.Risk, p.Label = "high", "管理系统服务"
213 case "exec":
214 p.Risk, p.Label = "high", "执行外部命令"
215 case "docker":
216 p.Risk, p.Label = "high", "管理 Docker"
217 case "nginx":
218 p.Risk, p.Label = "medium", "写入 nginx 配置"
219 case "net":
220 p.Risk, p.Label = "medium", "监听套接字"
221 default:
222 p.Label = kind
223 }
224 out = append(out, p)
225 }
226 return out
227}
228
229// InstalledView joins a catalog entry with local state.
230type InstalledView struct {
231 store.Plugin
232 Available string `json:"available"`
233 Update bool `json:"update"`
234 Revoked bool `json:"revoked"`
235 Reason string `json:"reason"`
236}