Files
skillshare/internal/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

58 lines
1.6 KiB
Go

package audit
import (
"crypto/sha256"
"encoding/hex"
"strings"
)
// AcceptKey returns a stable key for acknowledging a finding across updates.
// Unlike Fingerprint it omits the line number, so an accepted finding survives
// edits elsewhere in the file. A different rule, file, or matched text yields
// a new key and blocks again.
func AcceptKey(f Finding) string {
raw := strings.Join([]string{
f.RuleID, f.Pattern, f.Analyzer, f.File, normalizeSnippet(f.Snippet),
}, "|")
h := sha256.Sum256([]byte(strings.ToLower(raw)))
return hex.EncodeToString(h[:])
}
// MarkAcknowledged flags findings whose AcceptKey is in keys. Acknowledged
// findings stay in the result for display but no longer count toward blocking.
// Returns the number of findings marked.
func (r *Result) MarkAcknowledged(keys []string) int {
if r == nil || len(keys) == 0 {
return 0
}
set := make(map[string]bool, len(keys))
for _, k := range keys {
set[k] = true
}
n := 0
for i := range r.Findings {
if set[AcceptKey(r.Findings[i])] {
r.Findings[i].Acknowledged = true
n++
}
}
return n
}
// AcceptKeysAtOrAbove returns keys for unacknowledged findings at or above
// threshold — the set a --force override accepts.
func (r *Result) AcceptKeysAtOrAbove(threshold string) []string {
normalized, err := NormalizeThreshold(threshold)
if err != nil {
normalized = DefaultThreshold()
}
cutoff := SeverityRank(normalized)
var keys []string
for _, f := range r.Findings {
if !f.Acknowledged && SeverityRank(f.Severity) <= cutoff {
keys = append(keys, AcceptKey(f))
}
}
return keys
}