256 lines
6.2 KiB
Go
256 lines
6.2 KiB
Go
package plugin
|
|
|
|
import (
|
|
"archive/zip"
|
|
"crypto/ed25519"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// PackOptions configures PackDirectory.
|
|
type PackOptions struct {
|
|
HostVersion string
|
|
Contract *UIContract
|
|
OutputPath string // optional; defaults to "<cwd>/<id>-<version>.zip"
|
|
SignKey ed25519.PrivateKey
|
|
}
|
|
|
|
// PackResult describes a freshly packed plugin archive ready for distribution
|
|
// or market submission.
|
|
type PackResult struct {
|
|
PluginID string `json:"plugin_id"`
|
|
Version string `json:"version"`
|
|
Tier Tier `json:"tier"`
|
|
OutputPath string `json:"output_path"`
|
|
SHA256 string `json:"sha256"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
FileCount int `json:"file_count"`
|
|
Signed bool `json:"signed"`
|
|
Report ScanReport `json:"report"`
|
|
}
|
|
|
|
// PackDirectory validates dir through the unified static security gate, generates
|
|
// deterministic SHA256SUMS (plus optional SHA256SUMS.sig), and writes a standard
|
|
// plugin .zip archive ready for local import or GitHub Release publishing.
|
|
func PackDirectory(dir string, opts PackOptions) (PackResult, error) {
|
|
absDir, err := filepath.Abs(dir)
|
|
if err != nil {
|
|
return PackResult{}, err
|
|
}
|
|
st, err := os.Stat(absDir)
|
|
if err != nil {
|
|
return PackResult{}, err
|
|
}
|
|
if !st.IsDir() {
|
|
return PackResult{}, fmt.Errorf("%q is not a directory", dir)
|
|
}
|
|
|
|
rawMan, err := os.ReadFile(filepath.Join(absDir, "plugin.json"))
|
|
if err != nil {
|
|
return PackResult{}, fmt.Errorf("read plugin.json: %w", err)
|
|
}
|
|
var m Manifest
|
|
if err := json.Unmarshal(rawMan, &m); err != nil {
|
|
return PackResult{}, fmt.Errorf("parse plugin.json: %w", err)
|
|
}
|
|
|
|
// Run static security gate before creating any output archive.
|
|
report := ScanDirectory(absDir, opts.HostVersion, ScanOptions{
|
|
Contract: opts.Contract,
|
|
RequireChecksums: false,
|
|
})
|
|
if report.HardBlocked {
|
|
var reasons []string
|
|
for _, f := range report.HardFindings {
|
|
if f.Target != "" {
|
|
reasons = append(reasons, fmt.Sprintf("[%s] %s: %s", f.Check, f.Target, f.Message))
|
|
} else {
|
|
reasons = append(reasons, fmt.Sprintf("[%s] %s", f.Check, f.Message))
|
|
}
|
|
}
|
|
return PackResult{
|
|
PluginID: m.ID,
|
|
Version: m.Version,
|
|
Tier: report.Tier,
|
|
Report: report,
|
|
}, fmt.Errorf("plugin directory blocked by static security gate: %s", strings.Join(reasons, "; "))
|
|
}
|
|
|
|
// Collect all regular files (excluding VCS metadata & old checksums/sigs).
|
|
type packFile struct {
|
|
rel string
|
|
full string
|
|
hash string
|
|
mode os.FileMode
|
|
}
|
|
var files []packFile
|
|
if err := filepath.Walk(absDir, func(p string, info os.FileInfo, wErr error) error {
|
|
if wErr != nil {
|
|
return wErr
|
|
}
|
|
rel, rErr := filepath.Rel(absDir, p)
|
|
if rErr != nil {
|
|
return rErr
|
|
}
|
|
if rel == "." {
|
|
return nil
|
|
}
|
|
slash := filepath.ToSlash(rel)
|
|
if info.IsDir() {
|
|
base := info.Name()
|
|
if base == ".git" || base == ".svn" || base == "__MACOSX" {
|
|
return filepath.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
return fmt.Errorf("symlinks are forbidden in plugin packages: %s", slash)
|
|
}
|
|
if slash == "SHA256SUMS" || slash == "SHA256SUMS.sig" || info.Name() == ".DS_Store" || info.Name() == "Thumbs.db" {
|
|
return nil
|
|
}
|
|
h, hErr := fileSHA256Hex(p)
|
|
if hErr != nil {
|
|
return hErr
|
|
}
|
|
files = append(files, packFile{
|
|
rel: slash,
|
|
full: p,
|
|
hash: h,
|
|
mode: info.Mode(),
|
|
})
|
|
return nil
|
|
}); err != nil {
|
|
return PackResult{}, err
|
|
}
|
|
sort.Slice(files, func(i, j int) bool { return files[i].rel < files[j].rel })
|
|
|
|
var sumsBuilder strings.Builder
|
|
for _, f := range files {
|
|
fmt.Fprintf(&sumsBuilder, "%s %s\n", f.hash, f.rel)
|
|
}
|
|
sumsBytes := []byte(sumsBuilder.String())
|
|
|
|
var sigBytes []byte
|
|
signed := false
|
|
if len(opts.SignKey) == ed25519.PrivateKeySize {
|
|
sig := ed25519.Sign(opts.SignKey, sumsBytes)
|
|
sigBytes = []byte(base64.StdEncoding.EncodeToString(sig) + "\n")
|
|
signed = true
|
|
}
|
|
|
|
outPath := strings.TrimSpace(opts.OutputPath)
|
|
if outPath == "" {
|
|
outPath = fmt.Sprintf("%s-%s.zip", m.ID, m.Version)
|
|
}
|
|
absOut, err := filepath.Abs(outPath)
|
|
if err != nil {
|
|
return PackResult{}, err
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(absOut), 0o755); err != nil {
|
|
return PackResult{}, err
|
|
}
|
|
|
|
tmpOut := absOut + ".tmp"
|
|
fOut, err := os.Create(tmpOut)
|
|
if err != nil {
|
|
return PackResult{}, err
|
|
}
|
|
zw := zip.NewWriter(fOut)
|
|
fixedTime := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
writeBytesEntry := func(name string, data []byte, mode os.FileMode) error {
|
|
hdr := &zip.FileHeader{
|
|
Name: name,
|
|
Method: zip.Deflate,
|
|
Modified: fixedTime,
|
|
}
|
|
hdr.SetMode(mode)
|
|
w, wErr := zw.CreateHeader(hdr)
|
|
if wErr != nil {
|
|
return wErr
|
|
}
|
|
_, wErr = w.Write(data)
|
|
return wErr
|
|
}
|
|
|
|
writeFail := func(cause error) (PackResult, error) {
|
|
_ = zw.Close()
|
|
_ = fOut.Close()
|
|
_ = os.Remove(tmpOut)
|
|
return PackResult{}, cause
|
|
}
|
|
|
|
for _, pf := range files {
|
|
hdr := &zip.FileHeader{
|
|
Name: pf.rel,
|
|
Method: zip.Deflate,
|
|
Modified: fixedTime,
|
|
}
|
|
hdr.SetMode(pf.mode)
|
|
w, wErr := zw.CreateHeader(hdr)
|
|
if wErr != nil {
|
|
return writeFail(wErr)
|
|
}
|
|
src, oErr := os.Open(pf.full)
|
|
if oErr != nil {
|
|
return writeFail(oErr)
|
|
}
|
|
_, cErr := io.Copy(w, src)
|
|
_ = src.Close()
|
|
if cErr != nil {
|
|
return writeFail(cErr)
|
|
}
|
|
}
|
|
|
|
if err := writeBytesEntry("SHA256SUMS", sumsBytes, 0o644); err != nil {
|
|
return writeFail(err)
|
|
}
|
|
if signed {
|
|
if err := writeBytesEntry("SHA256SUMS.sig", sigBytes, 0o644); err != nil {
|
|
return writeFail(err)
|
|
}
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
_ = fOut.Close()
|
|
_ = os.Remove(tmpOut)
|
|
return PackResult{}, err
|
|
}
|
|
if err := fOut.Close(); err != nil {
|
|
_ = os.Remove(tmpOut)
|
|
return PackResult{}, err
|
|
}
|
|
if err := os.Rename(tmpOut, absOut); err != nil {
|
|
_ = os.Remove(tmpOut)
|
|
return PackResult{}, err
|
|
}
|
|
|
|
zipHash, err := fileSHA256Hex(absOut)
|
|
if err != nil {
|
|
return PackResult{}, err
|
|
}
|
|
zipStat, err := os.Stat(absOut)
|
|
if err != nil {
|
|
return PackResult{}, err
|
|
}
|
|
|
|
return PackResult{
|
|
PluginID: m.ID,
|
|
Version: m.Version,
|
|
Tier: report.Tier,
|
|
OutputPath: absOut,
|
|
SHA256: zipHash,
|
|
SizeBytes: zipStat.Size(),
|
|
FileCount: len(files),
|
|
Signed: signed,
|
|
Report: report,
|
|
}, nil
|
|
}
|