// Package plugins installs and supervises third-party plugins published on the // official catalog. Every artifact must carry both a developer signature and a // platform signature; the panel pins the platform key on first use. package plugins import ( "encoding/json" "errors" "fmt" "path" "regexp" "strings" "github.com/miaopanel/miaopanel/internal/store" ) // Manifest mirrors the signed document served by the cloud. Keep the JSON tags // in sync with server/internal/pluginpkg/manifest.go. type Manifest struct { Schema int `json:"schema"` API int `json:"api"` Slug string `json:"slug"` Name string `json:"name"` Version string `json:"version"` Channel string `json:"channel"` Summary string `json:"summary"` DescriptionMD string `json:"description_md"` Category string `json:"category"` License string `json:"license"` Homepage string `json:"homepage"` Author Author `json:"author"` Panel PanelRange `json:"panel"` Platforms []Platform `json:"platforms"` Permissions []string `json:"permissions"` Requires Requires `json:"requires"` Runtime Runtime `json:"runtime"` Install Install `json:"install"` UI json.RawMessage `json:"ui,omitempty"` Artifact Artifact `json:"artifact"` ChangelogMD string `json:"changelog_md"` DevSig string `json:"dev_sig"` } // Author identifies the signing key. type Author struct { Name string `json:"name"` Email string `json:"email"` Pubkey string `json:"pubkey"` } // PanelRange constrains compatible panel versions. type PanelRange struct { Min string `json:"min"` Max string `json:"max"` } // Platform is one supported architecture. type Platform struct { OS string `json:"os"` Arch string `json:"arch"` } // Requires declares prerequisites. type Requires struct { Commands []string `json:"commands"` Services []string `json:"services"` DiskMB int `json:"disk_mb"` } // Runtime describes how the plugin runs. type Runtime struct { Type string `json:"type"` Service *ServiceSpec `json:"service,omitempty"` } // ServiceSpec configures the systemd unit. type ServiceSpec struct { Exec string `json:"exec"` Args []string `json:"args"` RunAs string `json:"run_as"` Restart string `json:"restart"` MemoryMaxMB int `json:"memory_max_mb"` Health string `json:"health"` } // Install points at lifecycle hooks inside the archive. type Install struct { Script string `json:"script"` Uninstall string `json:"uninstall"` Upgrade string `json:"upgrade"` Timeout int `json:"timeout"` } // Artifact pins the archive. type Artifact struct { File string `json:"file"` SHA256 string `json:"sha256"` Size int64 `json:"size"` } var ( slugRe = regexp.MustCompile(`^[a-z][a-z0-9-]{2,40}$`) permRe = regexp.MustCompile(`^(files|service|exec|nginx|net|docker):[A-Za-z0-9:/._@-]{1,120}$`) hexRe = regexp.MustCompile(`^[a-f0-9]{64}$`) safeRelRe = regexp.MustCompile(`^[A-Za-z0-9._/-]{1,160}$`) ) // Parse validates the parts a compromised catalog could abuse. The cloud // already enforces the full rule set; the panel repeats the security relevant // checks because it is the process that runs as root. func Parse(raw []byte) (*Manifest, error) { if len(raw) == 0 || len(raw) > 256<<10 { return nil, errors.New("manifest 大小不合法") } var m Manifest if err := json.Unmarshal(raw, &m); err != nil { return nil, fmt.Errorf("manifest 不是合法 JSON: %w", err) } if m.Schema != 1 || m.API != 1 { return nil, errors.New("manifest schema/api 版本不受支持") } if !slugRe.MatchString(m.Slug) { return nil, errors.New("slug 不合法") } if m.Name == "" || len(m.Name) > 64 { return nil, errors.New("插件名称不合法") } if m.Version == "" || len(m.Version) > 64 { return nil, errors.New("版本号不合法") } switch m.Runtime.Type { case "script", "static", "service": default: return nil, errors.New("runtime.type 不合法") } if m.Runtime.Type == "service" { if m.Runtime.Service == nil || !safeRelative(m.Runtime.Service.Exec) { return nil, errors.New("service 型插件必须提供合法的 exec") } } if !safeRelative(m.Install.Script) { return nil, errors.New("install.script 不合法") } if m.Install.Timeout == 0 { m.Install.Timeout = 900 } if m.Install.Timeout < 60 || m.Install.Timeout > 3600 { return nil, errors.New("install.timeout 超出范围") } if !hexRe.MatchString(strings.ToLower(m.Artifact.SHA256)) || m.Artifact.Size <= 0 { return nil, errors.New("artifact 校验信息不合法") } m.Artifact.SHA256 = strings.ToLower(m.Artifact.SHA256) if len(m.Permissions) > 32 { return nil, errors.New("权限声明过多") } for _, p := range m.Permissions { if !permRe.MatchString(p) { return nil, fmt.Errorf("权限声明不合法: %s", p) } if strings.HasPrefix(p, "files:") { target := strings.TrimPrefix(p, "files:") if !strings.HasPrefix(target, "/") || strings.Contains(target, "..") { return nil, fmt.Errorf("files 权限必须是安全的绝对路径: %s", p) } if guardedPath(target) { return nil, fmt.Errorf("files 权限不允许指向 %s", target) } } } return &m, nil } func guardedPath(p string) bool { clean := path.Clean(p) for _, root := range []string{"/", "/etc", "/usr", "/bin", "/sbin", "/boot", "/dev", "/proc", "/sys", "/lib", "/lib64", "/www/server/panel"} { if clean == root || strings.HasPrefix(clean, root+"/") { return true } } return false } func safeRelative(p string) bool { if p == "" || strings.HasPrefix(p, "/") || !safeRelRe.MatchString(p) { return false } clean := path.Clean(p) return clean != "." && clean != ".." && !strings.HasPrefix(clean, "../") } // Permission is a UI friendly view of one declared capability. type Permission struct { Raw string `json:"raw"` Kind string `json:"kind"` Value string `json:"value"` Risk string `json:"risk"` Label string `json:"label"` } // ParsePermissions classifies the strings for the install confirmation dialog. func ParsePermissions(list []string) []Permission { out := make([]Permission, 0, len(list)) for _, raw := range list { kind, value, _ := strings.Cut(raw, ":") p := Permission{Raw: raw, Kind: kind, Value: value, Risk: "medium"} switch kind { case "files": p.Risk, p.Label = "high", "读写目录" case "service": p.Risk, p.Label = "high", "管理系统服务" case "exec": p.Risk, p.Label = "high", "执行外部命令" case "docker": p.Risk, p.Label = "high", "管理 Docker" case "nginx": p.Risk, p.Label = "medium", "写入 nginx 配置" case "net": p.Risk, p.Label = "medium", "监听套接字" default: p.Label = kind } out = append(out, p) } return out } // InstalledView joins a catalog entry with local state. type InstalledView struct { store.Plugin Available string `json:"available"` Update bool `json:"update"` Revoked bool `json:"revoked"` Reason string `json:"reason"` }