mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Overriding the audit gate with --force was per-invocation, so skills that legitimately quote attack strings (security scanners, red-team docs) had to be forced on every `update --all`. The only alternative, disabling the rule, was global and turned it off for every skill. Record the accepted findings (rule + file + matched text, no line number) in .metadata.json under `audit_accepted`, keyed by skill relPath so it survives entry rewrites and works for tracked repos. Later updates mark matching findings as acknowledged and exclude them from the block decision; any new finding, or the same rule matching different text, still blocks. Staged installs audit a temp directory, so InstallOptions gains an AuditAcceptRoot/AuditAcceptPath override pointing at the final destination. Refs #279
404 lines
14 KiB
Go
404 lines
14 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
|
|
acceptRoot, acceptPath := opts.auditAcceptTarget(destPath)
|
|
ApplyAcceptedFindings(acceptRoot, acceptPath, scanResult)
|
|
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.Acknowledged {
|
|
msg += " (accepted earlier)"
|
|
}
|
|
if f.Snippet != "" {
|
|
msg += fmt.Sprintf("\n %q", f.Snippet)
|
|
}
|
|
result.Warnings = append(result.Warnings, msg)
|
|
}
|
|
|
|
if scanResult.IsBlocked && !opts.AuditOverride {
|
|
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.AuditOverride {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings at/above block threshold (%s); proceeding due to --force", threshold))
|
|
result.Warnings = append(result.Warnings, recordAcceptedWarning(acceptRoot, acceptPath, scanResult, 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
|
|
acceptRoot, acceptPath := opts.auditAcceptTarget(repoPath)
|
|
ApplyAcceptedFindings(acceptRoot, acceptPath, scanResult)
|
|
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.Acknowledged {
|
|
msg += " (accepted earlier)"
|
|
}
|
|
if f.Snippet != "" {
|
|
msg += fmt.Sprintf("\n %q", f.Snippet)
|
|
}
|
|
result.Warnings = append(result.Warnings, msg)
|
|
}
|
|
|
|
if scanResult.IsBlocked && !opts.AuditOverride {
|
|
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.AuditOverride {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings at/above block threshold (%s); proceeding due to --force", threshold))
|
|
result.Warnings = append(result.Warnings, recordAcceptedWarning(acceptRoot, acceptPath, scanResult, 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(sourceDir, repoPath, beforeHash, threshold, projectRoot string, auditOverride bool) (*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)
|
|
}
|
|
ApplyAcceptedFindings(sourceDir, repoPath, scanResult)
|
|
// A scan that could not run stays fail-closed regardless of auditOverride:
|
|
// "I accept these findings" is not the same as "I could not be told any".
|
|
if scanResult.HasSeverityAtOrAbove(normalizedThreshold) && !auditOverride {
|
|
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 --force to override or --skip-audit to bypass scanning: %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
|
|
acceptRoot, acceptPath := opts.auditAcceptTarget(repoPath)
|
|
ApplyAcceptedFindings(acceptRoot, acceptPath, scanResult)
|
|
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.Acknowledged {
|
|
msg += " (accepted earlier)"
|
|
}
|
|
if f.Snippet != "" {
|
|
msg += fmt.Sprintf("\n %q", f.Snippet)
|
|
}
|
|
result.Warnings = append(result.Warnings, msg)
|
|
}
|
|
|
|
if scanResult.IsBlocked && !opts.AuditOverride {
|
|
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.AuditOverride {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("audit findings at/above block threshold (%s); proceeding due to --force", threshold))
|
|
result.Warnings = append(result.Warnings, recordAcceptedWarning(acceptRoot, acceptPath, scanResult, 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
|
|
}
|
|
|
|
// recordAcceptedWarning persists the findings a --force override just accepted
|
|
// and returns a warning line describing the outcome.
|
|
func recordAcceptedWarning(sourceDir, path string, res *audit.Result, threshold string) []string {
|
|
n, err := RecordAcceptedFindings(sourceDir, path, res, threshold)
|
|
if err != nil {
|
|
return []string{fmt.Sprintf("failed to record accepted findings: %v", err)}
|
|
}
|
|
if n == 0 {
|
|
return nil
|
|
}
|
|
return []string{fmt.Sprintf("recorded %d accepted finding(s); future updates won't block on them", n)}
|
|
}
|
|
|
|
func blockedFindingDetails(findings []audit.Finding, threshold string) []string {
|
|
var details []string
|
|
for _, f := range findings {
|
|
if f.Acknowledged {
|
|
continue
|
|
}
|
|
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
|