Files
skillshare/internal/audit/audit_result.go
T
Willie 9dbcd925d8 feat(audit): honor findings accepted with --force via --respect-accepted (#872)
* feat(audit): honor findings accepted with --force via --respect-accepted

install and update --force record the findings a user accepted in
audit_accepted, but audit ignored that record. A skill installed with
--force therefore kept failing `skillshare audit` in CI with no way to
let the accepted findings pass (#869).

audit --respect-accepted marks those findings as acknowledged. They stay
in every output (JSON acknowledged: true, a text/TUI note, "(accepted
earlier)" in Markdown, SARIF external suppressions), but they are counted
in summary.accepted instead of their severity and no longer fail the
audit. Without the flag nothing changes, so existing CI gates keep their
behavior. A changed file or snippet produces a new key and blocks again.

Tracked repositories record acceptances at the repo root with paths
relative to it, so ApplyAcceptedFindings walks up from each skill to
match them. The install, update and edit gates use the same function and
now honor repo-wide acceptances for skills inside tracked repos too.

The dashboard always honors accepted findings: the Audit page mutes them
and counts them apart, the skill Audit tab collapses them, and the
Dashboard attention card no longer lists a skill whose only blocking
findings were accepted. The CLI JSON summary and the server summary share
Result.CountOpenBySeverity so both count accepted findings the same way.
The oplog records the accepted count and the log views show it.

* fix(audit): match acceptances when a single file is scanned

A single-file scan names its findings by the file's base name, but the
ancestor lookup used the file's whole path as the prefix, so it hashed
SKILL.md/SKILL.md instead of the recorded SKILL.md. Auditing a skill file
directly with --respect-accepted, and auditing agents inside a tracked
repository in the CLI or the dashboard, still failed on unchanged
accepted findings. For a file target the prefix is now the file's own
directory.
2026-11-12 11:15:44 +08:00

201 lines
5.5 KiB
Go

package audit
var riskWeights = map[string]int{
SeverityCritical: 25,
SeverityHigh: 15,
SeverityMedium: 8,
SeverityLow: 3,
SeverityInfo: 1,
}
// Result holds all findings for a single skill.
type Result struct {
SkillName string `json:"skillName"`
Kind string `json:"kind,omitempty"` // "skill" or "agent" — set by caller
Findings []Finding `json:"findings"`
RiskScore int `json:"riskScore"`
RiskLabel string `json:"riskLabel"` // "clean", "low", "medium", "high", "critical"
Threshold string `json:"threshold,omitempty"`
IsBlocked bool `json:"isBlocked,omitempty"`
ScanTarget string `json:"scanTarget,omitempty"`
TotalBytes int64 `json:"totalBytes"`
AuditableBytes int64 `json:"auditableBytes"`
Analyzability float64 `json:"analyzability"` // AuditableBytes / TotalBytes (1.0 when TotalBytes == 0)
BinaryFiles int `json:"binaryFiles"` // Opaque binary headers, including recognized archives.
SkippedFiles int `json:"skippedFiles"` // Files excluded from full text analysis (includes binaries and oversized files).
OversizedFiles int `json:"oversizedFiles"`
TierProfile TierProfile `json:"tierProfile"`
}
func (r *Result) updateRisk() {
r.RiskScore = CalculateRiskScore(r.Findings)
r.RiskLabel = RiskLabelFromScoreAndMaxSeverity(r.RiskScore, r.MaxSeverity())
}
// HasCritical returns true if any finding is CRITICAL severity.
func (r *Result) HasCritical() bool {
return r.HasSeverityAtOrAbove(SeverityCritical)
}
// HasHigh returns true if any finding is HIGH or above.
func (r *Result) HasHigh() bool {
return r.HasSeverityAtOrAbove(SeverityHigh)
}
// HasSeverityAtOrAbove returns true if any finding severity is at or above threshold.
func (r *Result) HasSeverityAtOrAbove(threshold string) bool {
normalized, err := NormalizeThreshold(threshold)
if err != nil {
normalized = DefaultThreshold()
}
cutoff := SeverityRank(normalized)
for _, f := range r.Findings {
if f.Acknowledged {
continue
}
if SeverityRank(f.Severity) <= cutoff {
return true
}
}
return false
}
// MaxSeverity returns the highest severity found, or "" if no findings.
func (r *Result) MaxSeverity() string {
max := ""
maxRank := 999
for _, f := range r.Findings {
rank := SeverityRank(f.Severity)
if rank < maxRank {
max = f.Severity
maxRank = rank
}
}
return max
}
// CountByCategory returns the count of findings per Category.
// Only categories with count > 0 are included.
func (r *Result) CountByCategory() map[string]int {
m := make(map[string]int)
for _, f := range r.Findings {
if f.Category != "" {
m[f.Category]++
}
}
return m
}
// CountBySeverity returns the count of findings at CRITICAL/HIGH/MEDIUM severities.
func (r *Result) CountBySeverity() (critical, high, medium int) {
critical, high, medium, _, _ = r.CountBySeverityAll()
return
}
// CountBySeverityAll returns the count of findings at each severity level.
func (r *Result) CountBySeverityAll() (critical, high, medium, low, info int) {
critical, high, medium, low, info, _ = r.countSeverities(false)
return
}
// CountOpenBySeverity is CountBySeverityAll for the findings that still count:
// those accepted earlier with --force are counted as accepted instead.
func (r *Result) CountOpenBySeverity() (critical, high, medium, low, info, accepted int) {
return r.countSeverities(true)
}
func (r *Result) countSeverities(skipAccepted bool) (critical, high, medium, low, info, accepted int) {
for _, f := range r.Findings {
if skipAccepted && f.Acknowledged {
accepted++
continue
}
switch f.Severity {
case SeverityCritical:
critical++
case SeverityHigh:
high++
case SeverityMedium:
medium++
case SeverityLow:
low++
case SeverityInfo:
info++
}
}
return
}
// CalculateRiskScore converts findings into a normalized 0-100 risk score.
func CalculateRiskScore(findings []Finding) int {
score := 0
for _, f := range findings {
score += riskWeights[f.Severity]
}
if score > 100 {
return 100
}
return score
}
// RiskLabelFromScore maps risk score into one of: clean/low/medium/high/critical.
func RiskLabelFromScore(score int) string {
switch {
case score <= 0:
return "clean"
case score <= 25:
return "low"
case score <= 50:
return "medium"
case score <= 75:
return "high"
default:
return "critical"
}
}
// riskLabelRanks maps risk labels to numeric ranks (lower = more severe).
var riskLabelRanks = map[string]int{
"critical": 0,
"high": 1,
"medium": 2,
"low": 3,
"clean": 4,
}
// riskLabelRank returns the numeric rank for a risk label (lower = more severe).
func riskLabelRank(label string) int {
if r, ok := riskLabelRanks[label]; ok {
return r
}
return 999
}
// riskFloorFromSeverity returns the minimum risk label implied by a severity.
func riskFloorFromSeverity(severity string) string {
switch severity {
case SeverityCritical:
return "critical"
case SeverityHigh:
return "high"
case SeverityMedium:
return "medium"
case SeverityLow:
return "low"
default:
return "clean"
}
}
// RiskLabelFromScoreAndMaxSeverity computes the risk label as the higher of
// the score-based label and the severity floor. This ensures a single HIGH
// finding is never reported as "low" risk.
func RiskLabelFromScoreAndMaxSeverity(score int, maxSeverity string) string {
scoreLabel := RiskLabelFromScore(score)
floor := riskFloorFromSeverity(maxSeverity)
if riskLabelRank(floor) < riskLabelRank(scoreLabel) {
return floor
}
return scoreLabel
}