mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
Extend install/update with agent-aware --json output and apply the same audit block-threshold gate skills already use. Install: - install --json: imply --force; imply --all only when neither --skill nor --agent is set (so explicit selectors still work) - project-mode install now routes through cmdInstallProjectParsed so --json suppresses UI and emits a pure-JSON payload - agent installs propagate SourceDir so metadata lands in the agents root, not the install subdir - validation error lists --agent / --kind / --branch explicitly Update agents: - update agents supports --force, --skip-audit, --audit-threshold - tracked agent repos + metadata-backed single-file agents now run the same audit gate as skills; blocks roll back from a temp stage - JSON output returns the final per-agent status (updated/skipped/failed/up_to_date/...) by merging check results with batch update outcomes Audit: - auditInstalledAgent shares one helper with auditInstalledSkill (scan + cleanup callbacks); single-file agent scans now use audit.ScanFile / ScanFileForProject instead of staging the file into a temp directory - UpdateAgentFromDiscovery stages to temp, audits, then renames atomically (rename already overwrites on POSIX) Refactors pulled in while reworking these paths: - extract jsonUISuppressor helper in json_output.go and use it from install/update/update_agents; collapses 4 copies of the suppress/restore closure dance and the per-error failJSON block - drop relPath plumbing from installAgentFromDiscoveryInternal now that ScanFile doesn't need a staged relative path - list.go render functions now use theme.ANSI() so NO_COLOR is respected in plain-text output Tests: - add TestInstallAgentFromDiscovery_HighFindingBlocked (unit) - add TestUpdate_Agents_HighFindingBlockedByThreshold (integration) - add TestInstall_Project_JSON_Agent_PureJSON, TestUpdate_Agents_JSON_ReportsFinalStatus - fix TestList_SKILLSHARE_THEME_Light / TestList_NO_COLOR to write a config before invoking list - fix TestTargetList_NoTUI_FallsBackToPlainText assertion to match the new structured Mode: merge output - fix TestDoctor_AllGood_PassesAll by pinning SKILLSHARE_THEME=dark so the no-TTY theme fallback warning doesn't fire - add list_tui_content_test.go covering light/dark glamour base - list detail pane now picks LightStyleConfig on light terminals Docs: - update install.md, update.md, understand/agents.md for the new flags, JSON shapes, and agents update workflow
367 lines
12 KiB
Go
367 lines
12 KiB
Go
package install
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"skillshare/internal/audit"
|
|
)
|
|
|
|
// auditInstalledResource runs the audit gate shared by skill and agent installs.
|
|
// scan performs the actual scan; cleanupOnBlock removes the installed artifact
|
|
// when findings at/above threshold fire and --force is not set.
|
|
func auditInstalledResource(
|
|
destPath string,
|
|
result *InstallResult,
|
|
opts InstallOptions,
|
|
scan func() (*audit.Result, error),
|
|
cleanupOnBlock func() error,
|
|
) error {
|
|
if opts.SkipAudit {
|
|
result.AuditSkipped = true
|
|
result.AuditThreshold = opts.AuditThreshold
|
|
result.Warnings = append(result.Warnings, "audit skipped (--skip-audit)")
|
|
return nil
|
|
}
|
|
|
|
threshold, err := audit.NormalizeThreshold(opts.AuditThreshold)
|
|
if err != nil {
|
|
threshold = audit.DefaultThreshold()
|
|
}
|
|
result.AuditThreshold = threshold
|
|
|
|
scanResult, err := scan()
|
|
if err != nil {
|
|
// Non-fatal: warn but don't block
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("audit scan error: %v", err))
|
|
return nil
|
|
}
|
|
result.AuditRiskScore = scanResult.RiskScore
|
|
result.AuditRiskLabel = scanResult.RiskLabel
|
|
if result.AuditRiskLabel == "" && len(scanResult.Findings) == 0 {
|
|
result.AuditRiskLabel = "CLEAN"
|
|
}
|
|
scanResult.Threshold = threshold
|
|
scanResult.IsBlocked = scanResult.HasSeverityAtOrAbove(threshold)
|
|
|
|
if len(scanResult.Findings) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for _, f := range scanResult.Findings {
|
|
msg := fmt.Sprintf("audit %s: %s (%s:%d)", f.Severity, f.Message, f.File, f.Line)
|
|
if f.Snippet != "" {
|
|
msg += fmt.Sprintf("\n %q", f.Snippet)
|
|
}
|
|
result.Warnings = append(result.Warnings, msg)
|
|
}
|
|
|
|
if scanResult.IsBlocked && !opts.Force {
|
|
details := blockedFindingDetails(scanResult.Findings, threshold)
|
|
if cleanupErr := cleanupOnBlock(); cleanupErr != nil {
|
|
return fmt.Errorf(
|
|
"security audit failed — findings at/above %s detected:\n%s\n\nAutomatic cleanup failed for %s: %v\nManual removal is required: %w",
|
|
threshold,
|
|
strings.Join(details, "\n"),
|
|
destPath,
|
|
cleanupErr,
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
return fmt.Errorf(
|
|
"security audit failed — findings at/above %s detected:\n%s\n\nUse --force to override or --skip-audit to bypass scanning: %w",
|
|
threshold,
|
|
strings.Join(details, "\n"),
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
|
|
if scanResult.IsBlocked && opts.Force {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings at/above block threshold (%s); proceeding due to --force", threshold))
|
|
} else if !scanResult.IsBlocked {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings detected, but none at/above block threshold (%s)", threshold))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func auditInstalledSkill(destPath string, result *InstallResult, opts InstallOptions) error {
|
|
scan := func() (*audit.Result, error) {
|
|
if opts.AuditProjectRoot != "" {
|
|
return audit.ScanSkillForProject(destPath, opts.AuditProjectRoot)
|
|
}
|
|
return audit.ScanSkill(destPath)
|
|
}
|
|
cleanup := func() error { return removeAll(destPath) }
|
|
return auditInstalledResource(destPath, result, opts, scan, cleanup)
|
|
}
|
|
|
|
func auditInstalledAgent(destFile string, result *InstallResult, opts InstallOptions) error {
|
|
scan := func() (*audit.Result, error) {
|
|
if opts.AuditProjectRoot != "" {
|
|
return audit.ScanFileForProject(destFile, opts.AuditProjectRoot)
|
|
}
|
|
return audit.ScanFile(destFile)
|
|
}
|
|
// Agents are single .md files. After removing the file, walk up any
|
|
// newly-empty parent directories (e.g. into-subdir) so a blocked install
|
|
// leaves nothing behind.
|
|
cleanup := func() error {
|
|
if err := removeAll(destFile); err != nil {
|
|
return err
|
|
}
|
|
cleanEmptyInstallParents(destFile, opts.SourceDir)
|
|
return nil
|
|
}
|
|
return auditInstalledResource(destFile, result, opts, scan, cleanup)
|
|
}
|
|
|
|
func cleanEmptyInstallParents(path, stopAt string) {
|
|
stopAt = filepath.Clean(stopAt)
|
|
dir := filepath.Dir(filepath.Clean(path))
|
|
for dir != stopAt && dir != "." && dir != "/" {
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil || len(entries) > 0 {
|
|
break
|
|
}
|
|
_ = os.Remove(dir)
|
|
dir = filepath.Dir(dir)
|
|
}
|
|
}
|
|
|
|
// auditTrackedRepo scans an entire tracked repo directory for security threats.
|
|
// It blocks installation when findings are at or above configured threshold
|
|
// unless force is enabled. On block, the repo directory is removed.
|
|
func auditTrackedRepo(repoPath string, result *TrackedRepoResult, opts InstallOptions) error {
|
|
if opts.SkipAudit {
|
|
result.AuditSkipped = true
|
|
result.AuditThreshold = opts.AuditThreshold
|
|
result.Warnings = append(result.Warnings, "audit skipped (--skip-audit)")
|
|
return nil
|
|
}
|
|
|
|
threshold, err := audit.NormalizeThreshold(opts.AuditThreshold)
|
|
if err != nil {
|
|
threshold = audit.DefaultThreshold()
|
|
}
|
|
result.AuditThreshold = threshold
|
|
|
|
var scanResult *audit.Result
|
|
if opts.AuditProjectRoot != "" {
|
|
scanResult, err = audit.ScanSkillForProject(repoPath, opts.AuditProjectRoot)
|
|
} else {
|
|
scanResult, err = audit.ScanSkill(repoPath)
|
|
}
|
|
if err != nil {
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("audit scan error: %v", err))
|
|
return nil
|
|
}
|
|
result.AuditRiskScore = scanResult.RiskScore
|
|
result.AuditRiskLabel = scanResult.RiskLabel
|
|
if result.AuditRiskLabel == "" && len(scanResult.Findings) == 0 {
|
|
result.AuditRiskLabel = "CLEAN"
|
|
}
|
|
scanResult.Threshold = threshold
|
|
scanResult.IsBlocked = scanResult.HasSeverityAtOrAbove(threshold)
|
|
|
|
if len(scanResult.Findings) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for _, f := range scanResult.Findings {
|
|
msg := fmt.Sprintf("audit %s: %s (%s:%d)", f.Severity, f.Message, f.File, f.Line)
|
|
if f.Snippet != "" {
|
|
msg += fmt.Sprintf("\n %q", f.Snippet)
|
|
}
|
|
result.Warnings = append(result.Warnings, msg)
|
|
}
|
|
|
|
if scanResult.IsBlocked && !opts.Force {
|
|
details := blockedFindingDetails(scanResult.Findings, threshold)
|
|
if removeErr := removeAll(repoPath); removeErr != nil {
|
|
return fmt.Errorf(
|
|
"security audit failed — findings at/above %s detected in tracked repository:\n%s\n\nAutomatic cleanup failed for %s: %v\nManual removal is required: %w",
|
|
threshold,
|
|
strings.Join(details, "\n"),
|
|
repoPath,
|
|
removeErr,
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
return fmt.Errorf(
|
|
"security audit failed — findings at/above %s detected in tracked repository:\n%s\n\nUse --force to override or --skip-audit to bypass scanning: %w",
|
|
threshold,
|
|
strings.Join(details, "\n"),
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
|
|
if scanResult.IsBlocked && opts.Force {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings at/above block threshold (%s); proceeding due to --force", threshold))
|
|
} else if !scanResult.IsBlocked {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings detected, but none at/above block threshold (%s)", threshold))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// auditGateFailClosed scans a repo after git pull and rolls back on scan
|
|
// error or findings at/above threshold. Used by handleUpdate for non-tracked
|
|
// skill updates where fail-closed is the only behaviour.
|
|
func auditGateFailClosed(repoPath, beforeHash, threshold, projectRoot string) (*audit.Result, error) {
|
|
if beforeHash == "" {
|
|
return nil, fmt.Errorf(
|
|
"post-update audit failed — rollback commit unavailable, update aborted and repository state is unknown: %w",
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
|
|
normalizedThreshold, err := audit.NormalizeThreshold(threshold)
|
|
if err != nil {
|
|
normalizedThreshold = audit.DefaultThreshold()
|
|
}
|
|
|
|
var scanResult *audit.Result
|
|
var scanErr error
|
|
if projectRoot != "" {
|
|
scanResult, scanErr = audit.ScanSkillForProject(repoPath, projectRoot)
|
|
} else {
|
|
scanResult, scanErr = audit.ScanSkill(repoPath)
|
|
}
|
|
if scanErr != nil {
|
|
if resetErr := gitResetHard(repoPath, beforeHash); resetErr != nil {
|
|
return nil, fmt.Errorf("post-update audit failed: %v; WARNING: rollback also failed: %v — malicious content may remain: %w", scanErr, resetErr, audit.ErrBlocked)
|
|
}
|
|
return nil, fmt.Errorf("post-update audit failed: %v — rolled back (use --skip-audit to bypass): %w", scanErr, audit.ErrBlocked)
|
|
}
|
|
if scanResult.HasSeverityAtOrAbove(normalizedThreshold) {
|
|
details := blockedFindingDetails(scanResult.Findings, normalizedThreshold)
|
|
if resetErr := gitResetHard(repoPath, beforeHash); resetErr != nil {
|
|
return nil, fmt.Errorf("post-update audit found findings at/above %s; WARNING: rollback also failed: %v — malicious content may remain: %w", normalizedThreshold, resetErr, audit.ErrBlocked)
|
|
}
|
|
return nil, fmt.Errorf(
|
|
"post-update audit failed — findings at/above %s detected (rolled back to %s):\n%s\n\nUse --skip-audit to bypass: %w",
|
|
normalizedThreshold,
|
|
shortHash(beforeHash),
|
|
strings.Join(details, "\n"),
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
return scanResult, nil
|
|
}
|
|
|
|
// auditTrackedRepoUpdate scans an updated tracked repo for security threats.
|
|
// Unlike auditTrackedRepo (used for fresh installs), on block it rolls back
|
|
// via git reset --hard to preserve the repo and its history.
|
|
func auditTrackedRepoUpdate(repoPath, beforeHash string, result *TrackedRepoResult, opts InstallOptions) error {
|
|
if opts.SkipAudit {
|
|
result.AuditSkipped = true
|
|
result.AuditThreshold = opts.AuditThreshold
|
|
result.Warnings = append(result.Warnings, "audit skipped (--skip-audit)")
|
|
return nil
|
|
}
|
|
|
|
threshold, err := audit.NormalizeThreshold(opts.AuditThreshold)
|
|
if err != nil {
|
|
threshold = audit.DefaultThreshold()
|
|
}
|
|
result.AuditThreshold = threshold
|
|
|
|
var scanResult *audit.Result
|
|
if opts.AuditProjectRoot != "" {
|
|
scanResult, err = audit.ScanSkillForProject(repoPath, opts.AuditProjectRoot)
|
|
} else {
|
|
scanResult, err = audit.ScanSkill(repoPath)
|
|
}
|
|
if err != nil {
|
|
if beforeHash == "" {
|
|
return fmt.Errorf(
|
|
"security audit failed: %v — rollback commit unavailable, update aborted and repository state is unknown: %w",
|
|
err,
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
if resetErr := gitResetHard(repoPath, beforeHash); resetErr != nil {
|
|
return fmt.Errorf("security audit failed: %v; WARNING: rollback also failed: %v — malicious content may remain: %w",
|
|
err, resetErr, audit.ErrBlocked)
|
|
}
|
|
return fmt.Errorf("security audit failed: %v — rolled back (use --skip-audit to bypass): %w",
|
|
err, audit.ErrBlocked)
|
|
}
|
|
result.AuditRiskScore = scanResult.RiskScore
|
|
result.AuditRiskLabel = scanResult.RiskLabel
|
|
if result.AuditRiskLabel == "" && len(scanResult.Findings) == 0 {
|
|
result.AuditRiskLabel = "CLEAN"
|
|
}
|
|
scanResult.Threshold = threshold
|
|
scanResult.IsBlocked = scanResult.HasSeverityAtOrAbove(threshold)
|
|
|
|
if len(scanResult.Findings) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for _, f := range scanResult.Findings {
|
|
msg := fmt.Sprintf("audit %s: %s (%s:%d)", f.Severity, f.Message, f.File, f.Line)
|
|
if f.Snippet != "" {
|
|
msg += fmt.Sprintf("\n %q", f.Snippet)
|
|
}
|
|
result.Warnings = append(result.Warnings, msg)
|
|
}
|
|
|
|
if scanResult.IsBlocked && !opts.Force {
|
|
if beforeHash == "" {
|
|
return fmt.Errorf(
|
|
"security audit found findings at/above %s in tracked repository — rollback commit unavailable, update aborted and repository state is unknown: %w",
|
|
threshold,
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
|
|
// Rollback via git reset to preserve the repo
|
|
if resetErr := gitResetHard(repoPath, beforeHash); resetErr != nil {
|
|
return fmt.Errorf("security audit found findings at/above %s in tracked repository — WARNING: rollback also failed: %v — malicious content may remain: %w",
|
|
threshold, resetErr, audit.ErrBlocked)
|
|
}
|
|
details := blockedFindingDetails(scanResult.Findings, threshold)
|
|
return fmt.Errorf(
|
|
"security audit failed — findings at/above %s detected in tracked repository (rolled back to %s):\n%s\n\nUse --force to override or --skip-audit to bypass scanning: %w",
|
|
threshold,
|
|
shortHash(beforeHash),
|
|
strings.Join(details, "\n"),
|
|
audit.ErrBlocked,
|
|
)
|
|
}
|
|
|
|
if scanResult.IsBlocked && opts.Force {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings at/above block threshold (%s); proceeding due to --force", threshold))
|
|
} else if !scanResult.IsBlocked {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings detected, but none at/above block threshold (%s)", threshold))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func blockedFindingDetails(findings []audit.Finding, threshold string) []string {
|
|
var details []string
|
|
for _, f := range findings {
|
|
if audit.SeverityRank(f.Severity) <= audit.SeverityRank(threshold) {
|
|
detail := fmt.Sprintf(" %s: %s (%s:%d)", f.Severity, f.Message, f.File, f.Line)
|
|
if f.Snippet != "" {
|
|
detail += fmt.Sprintf("\n %q", f.Snippet)
|
|
}
|
|
details = append(details, detail)
|
|
}
|
|
}
|
|
return details
|
|
}
|
|
|
|
// isGitInstalled checks if git command is available
|