feat(plugin,cli): add static security scanner and lazy plugin market
- Add unified static security scanner and install/load gate (internal/plugin/scanner.go) blocking undeclared binaries, SVG active content, theme script escapes, and undeclared network calls while reporting L3 Sidecar privileges and SHA-256 - Add lazy plugin market client (internal/plugin/market.go) with remote index fetching (REIMS_GO_PLUGIN_MARKET_URL), memory/disk caching, embedded offline fallback, withdrawn-plugin blocking, compatibility preflight, and SHA-256 verification - Extend CLI with plugin search/update/install <id> and enhanced .zip/--json security doctor output; update backend locale catalog and plugin architecture docs
This commit is contained in:
1 parent
ceeb483cfb
commit
5a5977f93e
14 files changed
+2589
-164
No files matched your search
+237
-18
@@ -74,12 +74,84 @@ func openCLIPluginManager(cfg *config.Config, safeMode bool) (*plugin.Manager, *
|
||||
return mgr, router, nil
|
||||
}
|
||||
|
||||
func openCLIMarketClient(cfg *config.Config) *plugin.MarketClient {
|
||||
return plugin.NewMarketClient(plugin.MarketClientOptions{
|
||||
Layout: plugin.NewPluginsLayout(cfg.DataRoot),
|
||||
HostVersion: version.Version,
|
||||
})
|
||||
}
|
||||
|
||||
func cmdPlugin(cfg *config.Config, loc i18n.Locale, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("usage: reims-go plugin <list|install|link|enable|disable|remove|info|doctor|locale|run>")
|
||||
return fmt.Errorf("usage: reims-go plugin <list|search|install|update|link|enable|disable|remove|info|doctor|locale|run>")
|
||||
}
|
||||
ctx := context.Background()
|
||||
switch args[0] {
|
||||
case "search":
|
||||
fs := flag.NewFlagSet("plugin search", flag.ContinueOnError)
|
||||
kind := fs.String("kind", "", "Filter by plugin kind (theme, locale, driver, tool, template, runtime)")
|
||||
includeAlpha := fs.Bool("alpha", false, "Include alpha channel plugins")
|
||||
refresh := fs.Bool("refresh", false, "Bypass cache and refresh market index")
|
||||
asJSON := fs.Bool("json", false, "Output JSON")
|
||||
if err := fs.Parse(args[1:]); err != nil {
|
||||
return err
|
||||
}
|
||||
mgr, _, err := openCLIPluginManager(cfg, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer mgr.Close()
|
||||
mc := openCLIMarketClient(cfg)
|
||||
query := strings.TrimSpace(strings.Join(fs.Args(), " "))
|
||||
res := mc.Browse(ctx, mgr.Registry().List(), plugin.MarketBrowseOptions{
|
||||
Query: query,
|
||||
Kind: *kind,
|
||||
IncludeAlpha: *includeAlpha,
|
||||
ForceRefresh: *refresh,
|
||||
})
|
||||
if *asJSON {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(res)
|
||||
}
|
||||
if res.Stale && res.FetchError != "" {
|
||||
fmt.Fprintf(os.Stderr, "[warn] %s (%s)\n", i18n.T(loc, "plugin.market.stale_warn"), res.FetchError)
|
||||
}
|
||||
for _, w := range res.WithdrawnInstalled {
|
||||
fmt.Fprintf(os.Stderr, "[warn] %s: %s", w.ID, w.Reason.Get(string(loc)))
|
||||
if w.InsteadID != "" {
|
||||
fmt.Fprintf(os.Stderr, " -> %s", w.InsteadID)
|
||||
}
|
||||
fmt.Fprintln(os.Stderr)
|
||||
}
|
||||
if len(res.Items) == 0 {
|
||||
fmt.Println(i18n.T(loc, "plugin.market.empty"))
|
||||
return nil
|
||||
}
|
||||
for _, it := range res.Items {
|
||||
status := it.Entry.Channel
|
||||
switch {
|
||||
case it.Withdrawn != nil:
|
||||
status = "withdrawn"
|
||||
case !it.Compatible:
|
||||
status = "incompat"
|
||||
case it.UpdateAvailable:
|
||||
status = "update:" + it.InstalledVersion + "->" + it.Entry.Version
|
||||
case it.Installed:
|
||||
status = "installed"
|
||||
}
|
||||
kindLabel := it.Entry.Kind
|
||||
if kindLabel == "" {
|
||||
kindLabel = string(it.Entry.Tier)
|
||||
}
|
||||
if kindLabel == "" {
|
||||
kindLabel = "-"
|
||||
}
|
||||
fmt.Printf("%-34s %-10s %-8s %-9s %-18s %s\n",
|
||||
it.Entry.ID, it.Entry.Version, kindLabel, it.Entry.Trust, status, it.Entry.Name.Get(string(loc)))
|
||||
}
|
||||
return nil
|
||||
|
||||
case "list":
|
||||
fs := flag.NewFlagSet("plugin list", flag.ContinueOnError)
|
||||
asJSON := fs.Bool("json", false, "Output JSON")
|
||||
@@ -111,15 +183,39 @@ func cmdPlugin(cfg *config.Config, loc i18n.Locale, args []string) error {
|
||||
return nil
|
||||
|
||||
case "install":
|
||||
if len(args) < 2 {
|
||||
return fmt.Errorf("usage: reims-go plugin install <archive.zip>")
|
||||
fs := flag.NewFlagSet("plugin install", flag.ContinueOnError)
|
||||
includeAlpha := fs.Bool("alpha", false, "Allow alpha channel when installing by market plugin ID")
|
||||
refresh := fs.Bool("refresh", false, "Refresh market index before installing")
|
||||
if err := fs.Parse(args[1:]); err != nil {
|
||||
return err
|
||||
}
|
||||
if fs.NArg() < 1 {
|
||||
return fmt.Errorf("usage: reims-go plugin install [--alpha] [--refresh] <archive.zip|plugin-id>")
|
||||
}
|
||||
target := fs.Arg(0)
|
||||
mgr, _, err := openCLIPluginManager(cfg, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer mgr.Close()
|
||||
info, err := mgr.InstallLocalArchive(ctx, args[1], plugin.PackageInstallOptions{})
|
||||
|
||||
isLocalFile := false
|
||||
if st, statErr := os.Stat(target); statErr == nil && !st.IsDir() {
|
||||
isLocalFile = true
|
||||
} else if strings.HasSuffix(strings.ToLower(target), ".zip") || strings.ContainsAny(target, `/\`) {
|
||||
isLocalFile = true
|
||||
}
|
||||
|
||||
var info *plugin.PluginInfo
|
||||
if isLocalFile {
|
||||
info, err = mgr.InstallLocalArchive(ctx, target, plugin.PackageInstallOptions{})
|
||||
} else {
|
||||
mc := openCLIMarketClient(cfg)
|
||||
info, err = mc.InstallFromMarket(ctx, mgr, target, plugin.MarketInstallOptions{
|
||||
IncludeAlpha: *includeAlpha,
|
||||
ForceRefresh: *refresh,
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -129,6 +225,60 @@ func cmdPlugin(cfg *config.Config, loc i18n.Locale, args []string) error {
|
||||
}
|
||||
return nil
|
||||
|
||||
case "update":
|
||||
fs := flag.NewFlagSet("plugin update", flag.ContinueOnError)
|
||||
includeAlpha := fs.Bool("alpha", false, "Allow alpha channel updates")
|
||||
refresh := fs.Bool("refresh", true, "Refresh market index before checking updates")
|
||||
if err := fs.Parse(args[1:]); err != nil {
|
||||
return err
|
||||
}
|
||||
mgr, _, err := openCLIPluginManager(cfg, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer mgr.Close()
|
||||
mc := openCLIMarketClient(cfg)
|
||||
if fs.NArg() > 0 {
|
||||
id := fs.Arg(0)
|
||||
if _, ok := mgr.Registry().Info(id); !ok {
|
||||
return fmt.Errorf("%s", i18n.T(loc, "plugin.not_found", id))
|
||||
}
|
||||
info, uErr := mc.InstallFromMarket(ctx, mgr, id, plugin.MarketInstallOptions{
|
||||
IncludeAlpha: *includeAlpha,
|
||||
ForceRefresh: *refresh,
|
||||
})
|
||||
if uErr != nil {
|
||||
return uErr
|
||||
}
|
||||
fmt.Println(i18n.T(loc, "plugin.installed", info.ID, info.Version, info.Trust, info.Tier))
|
||||
if info.NeedsApproval {
|
||||
fmt.Println(i18n.T(loc, "plugin.needs_approval", info.ID))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
updates, browse := mc.CheckUpdates(ctx, mgr.Registry().List(), *includeAlpha, *refresh)
|
||||
if browse.Stale && browse.FetchError != "" {
|
||||
fmt.Fprintf(os.Stderr, "[warn] %s (%s)\n", i18n.T(loc, "plugin.market.stale_warn"), browse.FetchError)
|
||||
}
|
||||
if len(updates) == 0 {
|
||||
fmt.Println(i18n.T(loc, "plugin.market.up_to_date"))
|
||||
return nil
|
||||
}
|
||||
for _, up := range updates {
|
||||
info, uErr := mc.InstallFromMarket(ctx, mgr, up.Entry.ID, plugin.MarketInstallOptions{
|
||||
IncludeAlpha: *includeAlpha,
|
||||
ForceRefresh: false,
|
||||
})
|
||||
if uErr != nil {
|
||||
return fmt.Errorf("update %s: %w", up.Entry.ID, uErr)
|
||||
}
|
||||
fmt.Println(i18n.T(loc, "plugin.installed", info.ID, info.Version, info.Trust, info.Tier))
|
||||
if info.NeedsApproval {
|
||||
fmt.Println(i18n.T(loc, "plugin.needs_approval", info.ID))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
case "link":
|
||||
if len(args) < 2 {
|
||||
return fmt.Errorf("usage: reims-go plugin link <dir>")
|
||||
@@ -221,6 +371,7 @@ func cmdPlugin(cfg *config.Config, loc i18n.Locale, args []string) error {
|
||||
case "doctor":
|
||||
fs := flag.NewFlagSet("plugin doctor", flag.ContinueOnError)
|
||||
contractPath := fs.String("contract", "", "Optional path to ui-contract.json for CSS validation")
|
||||
asJSON := fs.Bool("json", false, "Output structured ScanReport JSON")
|
||||
if err := fs.Parse(args[1:]); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -236,15 +387,26 @@ func cmdPlugin(cfg *config.Config, loc i18n.Locale, args []string) error {
|
||||
}
|
||||
contract = &c
|
||||
}
|
||||
scanOpts := plugin.ScanOptions{Contract: contract}
|
||||
target := ""
|
||||
if fs.NArg() > 0 {
|
||||
target = fs.Arg(0)
|
||||
}
|
||||
// If target is an existing directory, validate it directly (developer workflow, §5.1).
|
||||
// Direct directory or .zip archive inspection (developer & market CI workflow, §5.1).
|
||||
if target != "" {
|
||||
if st, err := os.Stat(target); err == nil && st.IsDir() {
|
||||
findings := plugin.DoctorDirectory(target, version.Version, contract)
|
||||
return printDoctorFindings(loc, findings)
|
||||
if st, err := os.Stat(target); err == nil {
|
||||
if st.IsDir() {
|
||||
rep := plugin.ScanDirectory(target, version.Version, scanOpts)
|
||||
return printScanReports(loc, []plugin.ScanReport{rep}, *asJSON)
|
||||
}
|
||||
if strings.HasSuffix(strings.ToLower(target), ".zip") {
|
||||
layout := plugin.NewPluginsLayout(cfg.DataRoot)
|
||||
rep, sErr := plugin.ScanArchive(target, layout.TmpDir(), version.Version, scanOpts)
|
||||
if sErr != nil {
|
||||
return sErr
|
||||
}
|
||||
return printScanReports(loc, []plugin.ScanReport{rep}, *asJSON)
|
||||
}
|
||||
}
|
||||
}
|
||||
mgr, _, err := openCLIPluginManager(cfg, false)
|
||||
@@ -258,25 +420,46 @@ func cmdPlugin(cfg *config.Config, loc i18n.Locale, args []string) error {
|
||||
return fmt.Errorf("%s", i18n.T(loc, "plugin.target_not_found", target))
|
||||
}
|
||||
if info.InstallDir == "" {
|
||||
if *asJSON {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(plugin.ScanReport{
|
||||
PluginID: info.ID,
|
||||
Version: info.Version,
|
||||
Name: info.Name,
|
||||
Tier: info.Tier,
|
||||
Trust: info.Trust,
|
||||
Passed: true,
|
||||
Headline: plugin.LocalizedText{
|
||||
ZhCN: "内置插件状态正常",
|
||||
EnUS: "Built-in plugin is healthy",
|
||||
},
|
||||
})
|
||||
}
|
||||
fmt.Printf("[ok] %s@%s (%s, state=%s)\n", info.ID, info.Version, info.Source, info.State)
|
||||
return nil
|
||||
}
|
||||
return printDoctorFindings(loc, plugin.DoctorDirectory(info.InstallDir, version.Version, contract))
|
||||
rep := plugin.ScanDirectory(info.InstallDir, version.Version, scanOpts)
|
||||
return printScanReports(loc, []plugin.ScanReport{rep}, *asJSON)
|
||||
}
|
||||
// Audit all installed external plugins.
|
||||
var allFindings []plugin.DoctorFinding
|
||||
var reports []plugin.ScanReport
|
||||
for _, info := range mgr.Registry().List() {
|
||||
if info.InstallDir != "" {
|
||||
allFindings = append(allFindings, plugin.DoctorDirectory(info.InstallDir, version.Version, contract)...)
|
||||
} else {
|
||||
allFindings = append(allFindings, plugin.DoctorFinding{
|
||||
Severity: "ok",
|
||||
Check: info.ID,
|
||||
Message: fmt.Sprintf("%s@%s state=%s", info.ID, info.Version, info.State),
|
||||
})
|
||||
reports = append(reports, plugin.ScanDirectory(info.InstallDir, version.Version, scanOpts))
|
||||
}
|
||||
}
|
||||
return printDoctorFindings(loc, allFindings)
|
||||
if len(reports) == 0 {
|
||||
if *asJSON {
|
||||
fmt.Println("[]")
|
||||
return nil
|
||||
}
|
||||
for _, info := range mgr.Registry().List() {
|
||||
fmt.Printf("[ok] %s@%s (%s, state=%s)\n", info.ID, info.Version, info.Source, info.State)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return printScanReports(loc, reports, *asJSON)
|
||||
|
||||
case "locale":
|
||||
return cmdPluginLocale(loc, args[1:])
|
||||
@@ -311,6 +494,42 @@ func cmdPlugin(cfg *config.Config, loc i18n.Locale, args []string) error {
|
||||
}
|
||||
}
|
||||
|
||||
func printScanReports(loc i18n.Locale, reports []plugin.ScanReport, asJSON bool) error {
|
||||
hasErr := false
|
||||
for _, r := range reports {
|
||||
if r.HardBlocked {
|
||||
hasErr = true
|
||||
}
|
||||
}
|
||||
if asJSON {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
if len(reports) == 1 {
|
||||
if err := enc.Encode(reports[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := enc.Encode(reports); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if hasErr {
|
||||
return fmt.Errorf("%s", i18n.T(loc, "plugin.doctor.errors"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for i, r := range reports {
|
||||
if i > 0 {
|
||||
fmt.Println("---")
|
||||
}
|
||||
fmt.Println(r.SummaryText(string(loc)))
|
||||
}
|
||||
if hasErr {
|
||||
return fmt.Errorf("%s", i18n.T(loc, "plugin.doctor.errors"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printDoctorFindings(loc i18n.Locale, findings []plugin.DoctorFinding) error {
|
||||
hasErr := false
|
||||
for _, f := range findings {
|
||||
|
||||
Reference in new issue
Block a user