Files
skillshare/internal/install/audit_accept.go
T
Willie f52ae74c5f feat(audit): remember findings accepted via --force across updates
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
2026-09-17 10:24:18 +08:00

85 lines
2.1 KiB
Go

package install
import (
"path/filepath"
"sort"
"strings"
"skillshare/internal/audit"
)
// acceptedKeyPath returns the .metadata.json key under which accepted findings
// for the resource at path are stored. Mirrors WriteMetaToStore's path logic.
func acceptedKeyPath(sourceDir, path string) (string, string, bool) {
if sourceDir == "" {
sourceDir = filepath.Dir(path)
}
rel, err := filepath.Rel(sourceDir, path)
if err != nil || rel == "." || strings.HasPrefix(rel, "..") {
return "", "", false
}
return sourceDir, filepath.ToSlash(rel), true
}
// ApplyAcceptedFindings marks findings previously accepted via --force for the
// resource at path so they no longer block. No-op when nothing was recorded.
func ApplyAcceptedFindings(sourceDir, path string, res *audit.Result) int {
if res == nil {
return 0
}
dir, rel, ok := acceptedKeyPath(sourceDir, path)
if !ok {
return 0
}
store, err := loadMetadataFile(dir)
if err != nil {
return 0
}
return res.MarkAcknowledged(store.AuditAccepted[rel])
}
// RecordAcceptedFindings persists the keys of unacknowledged findings at/above
// threshold — the ones the user just overrode — merged with earlier records.
// Returns the number of newly recorded keys.
func RecordAcceptedFindings(sourceDir, path string, res *audit.Result, threshold string) (int, error) {
if res == nil {
return 0, nil
}
keys := res.AcceptKeysAtOrAbove(threshold)
if len(keys) == 0 {
return 0, nil
}
dir, rel, ok := acceptedKeyPath(sourceDir, path)
if !ok {
return 0, nil
}
store, err := LoadMetadata(dir)
if err != nil {
return 0, err
}
if store.AuditAccepted == nil {
store.AuditAccepted = make(map[string][]string)
}
existing := make(map[string]bool, len(store.AuditAccepted[rel]))
for _, k := range store.AuditAccepted[rel] {
existing[k] = true
}
added := 0
for _, k := range keys {
if !existing[k] {
existing[k] = true
added++
}
}
if added == 0 {
return 0, nil
}
merged := make([]string, 0, len(existing))
for k := range existing {
merged = append(merged, k)
}
sort.Strings(merged)
store.AuditAccepted[rel] = merged
return added, store.Save(dir)
}