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

228 lines
6.3 KiB
Go

package audit
const (
sarifVersion = "2.1.0"
sarifSchema = "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json"
toolName = "skillshare"
toolInfoURI = "https://skillshare.runkids.cc/"
)
// --- SARIF 2.1.0 structs (unexported except SARIFOptions) ---
type sarifLog struct {
Schema string `json:"$schema"`
Version string `json:"version"`
Runs []sarifRun `json:"runs"`
}
type sarifRun struct {
Tool sarifTool `json:"tool"`
Results []sarifResult `json:"results"`
Properties map[string]any `json:"properties,omitempty"`
}
type sarifTool struct {
Driver sarifDriver `json:"driver"`
}
type sarifDriver struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
InformationURI string `json:"informationUri"`
Rules []sarifReportingDesc `json:"rules"`
}
type sarifReportingDesc struct {
ID string `json:"id"`
ShortDescription sarifMessage `json:"shortDescription"`
DefaultConfig sarifDefaultConf `json:"defaultConfiguration"`
Properties sarifRuleProps `json:"properties"`
}
type sarifDefaultConf struct {
Level string `json:"level"`
}
type sarifRuleProps struct {
SecuritySeverity float64 `json:"security-severity"`
}
type sarifResult struct {
RuleID string `json:"ruleId"`
RuleIndex int `json:"ruleIndex"`
Level string `json:"level"`
Message sarifMessage `json:"message"`
Locations []sarifLocation `json:"locations"`
Fingerprints map[string]string `json:"fingerprints,omitempty"`
Suppressions []sarifSuppression `json:"suppressions,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
}
// sarifSuppression records a finding accepted earlier with --force.
type sarifSuppression struct {
Kind string `json:"kind"`
Justification string `json:"justification,omitempty"`
}
type sarifMessage struct {
Text string `json:"text"`
}
type sarifLocation struct {
PhysicalLocation sarifPhysicalLoc `json:"physicalLocation"`
}
type sarifPhysicalLoc struct {
ArtifactLocation sarifArtifactLoc `json:"artifactLocation"`
Region *sarifRegion `json:"region,omitempty"`
}
type sarifArtifactLoc struct {
URI string `json:"uri"`
}
type sarifRegion struct {
StartLine int `json:"startLine"`
}
// --- Severity mapping ---
var severityToSARIF = map[string]struct {
level string
secSev float64
}{
SeverityCritical: {"error", 9.0},
SeverityHigh: {"error", 7.0},
SeverityMedium: {"warning", 4.0},
SeverityLow: {"note", 2.0},
SeverityInfo: {"note", 0.5},
}
// SARIFOptions controls optional fields in the SARIF output.
type SARIFOptions struct {
ToolVersion string
BaseURI string
}
// ToSARIF converts audit results into a SARIF 2.1.0 log.
// It deduplicates rules by pattern (first occurrence wins) and maps each
// Finding to a sarifResult with the correct ruleIndex.
func ToSARIF(results []*Result, opts SARIFOptions) *sarifLog {
ruleIndex := map[string]int{} // pattern → index in rules slice
var rules []sarifReportingDesc
var sarifResults []sarifResult
var coverage []map[string]any
for _, r := range results {
coverage = append(coverage, map[string]any{"resource": r.SkillName, "binaryFiles": r.BinaryFiles, "skippedFiles": r.SkippedFiles, "oversizedFiles": r.OversizedFiles})
for _, f := range r.Findings {
// Use RuleID when available, fall back to Pattern.
ruleID := f.RuleID
if ruleID == "" {
ruleID = f.Pattern
}
// Deduplicate rules by ruleID
idx, exists := ruleIndex[ruleID]
if !exists {
idx = len(rules)
ruleIndex[ruleID] = idx
mapping := severityToSARIF[f.Severity]
description := f.Message
switch ruleID {
case "executable-artifact":
description = "Executable artifact detected; runtime behavior has not been analyzed"
case "disguised-executable":
description = "Executable artifact detected in a disguised location; runtime behavior has not been analyzed"
case "python-bytecode":
description = "Python bytecode detected; runtime behavior has not been analyzed"
case "archive-artifact":
description = "Archive signature detected; contents have not been inspected"
case "uncertain-header":
description = "Uncertain executable or bytecode header detected"
}
rules = append(rules, sarifReportingDesc{
ID: ruleID,
ShortDescription: sarifMessage{Text: description},
DefaultConfig: sarifDefaultConf{Level: mapping.level},
Properties: sarifRuleProps{SecuritySeverity: mapping.secSev},
})
}
mapping := severityToSARIF[f.Severity]
sr := sarifResult{
RuleID: ruleID,
RuleIndex: idx,
Level: mapping.level,
Message: sarifMessage{Text: f.Message},
Locations: []sarifLocation{}, // non-nil for clean JSON
}
if f.Fingerprint != "" {
sr.Fingerprints = map[string]string{"skillshare/v1": f.Fingerprint}
}
if f.Acknowledged {
sr.Suppressions = []sarifSuppression{{Kind: "external", Justification: "accepted earlier with --force"}}
}
props := map[string]any{}
if f.Analyzer != "" {
props["analyzer"] = f.Analyzer
}
if f.Category != "" {
props["category"] = f.Category
}
if f.Confidence > 0 {
props["confidence"] = f.Confidence
}
if f.SeverityContext != "" {
props["severityContext"] = f.SeverityContext
}
if len(props) > 0 {
sr.Properties = props
}
if f.File != "" {
loc := sarifPhysicalLoc{
ArtifactLocation: sarifArtifactLoc{URI: f.File},
}
if f.Line > 0 {
loc.Region = &sarifRegion{StartLine: f.Line}
}
sr.Locations = []sarifLocation{{PhysicalLocation: loc}}
}
sarifResults = append(sarifResults, sr)
}
}
// Ensure non-nil slices for clean JSON output
if sarifResults == nil {
sarifResults = []sarifResult{}
}
if rules == nil {
rules = []sarifReportingDesc{}
}
return &sarifLog{
Schema: sarifSchema,
Version: sarifVersion,
Runs: []sarifRun{
{
Tool: sarifTool{
Driver: sarifDriver{
Name: toolName,
Version: opts.ToolVersion,
InformationURI: toolInfoURI,
Rules: rules,
},
},
Results: sarifResults,
Properties: map[string]any{"skillshareCoverage": coverage},
},
},
}
}