feat: add source-repository-link rule and severity-based risk floor (#39)

Supply-chain hardening Phase 1:
- Add source-repository-link-0 rule (HIGH) to detect [source repository](url) patterns
- Exclude source-repo labels from external-link-0 to prevent double-reporting
- Introduce risk floor: RiskLabel = max(score-label, severity-floor) so a
  single HIGH finding produces "high" risk instead of "low"
- Apply severity floor to CLI summary, server API summary, and per-result risk
This commit is contained in:
Willie
2026-02-23 04:18:01 +08:00
parent c59ae5aa19
commit a2a86c9e59
7 changed files with 286 additions and 4 deletions
+7 -1
View File
@@ -655,6 +655,7 @@ func summarizeAuditResults(total int, results []*audit.Result, threshold string)
}
maxRisk := 0
maxSeverity := ""
for _, r := range results {
c, h, m, l, i := r.CountBySeverityAll()
summary.Critical += c
@@ -683,9 +684,14 @@ func summarizeAuditResults(total int, results []*audit.Result, threshold string)
if r.RiskScore > maxRisk {
maxRisk = r.RiskScore
}
if rs := r.MaxSeverity(); rs != "" {
if maxSeverity == "" || audit.SeverityRank(rs) < audit.SeverityRank(maxSeverity) {
maxSeverity = rs
}
}
}
summary.RiskScore = maxRisk
summary.RiskLabel = audit.RiskLabelFromScore(maxRisk)
summary.RiskLabel = audit.RiskLabelFromScoreAndMaxSeverity(maxRisk, maxSeverity)
return summary
}
+46 -1
View File
@@ -53,7 +53,7 @@ type Result struct {
func (r *Result) updateRisk() {
r.RiskScore = CalculateRiskScore(r.Findings)
r.RiskLabel = RiskLabelFromScore(r.RiskScore)
r.RiskLabel = RiskLabelFromScoreAndMaxSeverity(r.RiskScore, r.MaxSeverity())
}
// HasCritical returns true if any finding is CRITICAL severity.
@@ -148,6 +148,51 @@ func RiskLabelFromScore(score int) string {
}
}
// 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
}
// ScanSkill scans all scannable files in a skill directory using global rules.
func ScanSkill(skillPath string) (*Result, error) {
disabled := disabledIDsGlobal()
+104
View File
@@ -279,6 +279,110 @@ func TestScanSkill_ExternalLinkDetected(t *testing.T) {
}
}
func TestScanSkill_SourceRepoLink_HIGH(t *testing.T) {
dir := t.TempDir()
skillDir := filepath.Join(dir, "repo-skill")
os.MkdirAll(skillDir, 0755)
os.WriteFile(filepath.Join(skillDir, "SKILL.md"),
[]byte("# Skill\n\n[source repository](https://github.com/org/repo)\n"), 0644)
result, err := ScanSkill(skillDir)
if err != nil {
t.Fatal(err)
}
var foundSourceRepo, foundExternal bool
for _, f := range result.Findings {
if f.Pattern == "source-repository-link" && f.Severity == "HIGH" {
foundSourceRepo = true
if f.Line != 3 {
t.Errorf("expected line 3, got %d", f.Line)
}
}
if f.Pattern == "external-link" {
foundExternal = true
}
}
if !foundSourceRepo {
t.Errorf("expected HIGH source-repository-link finding, got: %+v", result.Findings)
}
if foundExternal {
t.Error("source repository link should NOT also trigger external-link (overlap exclusion)")
}
// Risk label should be "high" due to severity floor, not "low" from score alone
if result.RiskLabel != "high" {
t.Errorf("expected risk label 'high', got %q (score=%d)", result.RiskLabel, result.RiskScore)
}
}
func TestScanSkill_SourceRepoLink_LocalNotTriggered(t *testing.T) {
dir := t.TempDir()
skillDir := filepath.Join(dir, "local-skill")
os.MkdirAll(skillDir, 0755)
os.WriteFile(filepath.Join(skillDir, "guide.md"), []byte("# Guide"), 0644)
os.WriteFile(filepath.Join(skillDir, "SKILL.md"),
[]byte("# Skill\n\n[source repository](guide.md)\n"), 0644)
result, err := ScanSkill(skillDir)
if err != nil {
t.Fatal(err)
}
for _, f := range result.Findings {
if f.Pattern == "source-repository-link" {
t.Errorf("local link should NOT trigger source-repository-link: %+v", f)
}
}
}
func TestScanSkill_DocumentationLink_OnlyExternal(t *testing.T) {
dir := t.TempDir()
skillDir := filepath.Join(dir, "doc-skill")
os.MkdirAll(skillDir, 0755)
os.WriteFile(filepath.Join(skillDir, "SKILL.md"),
[]byte("# Skill\n\n[documentation](https://docs.example.com)\n"), 0644)
result, err := ScanSkill(skillDir)
if err != nil {
t.Fatal(err)
}
var foundExternal bool
for _, f := range result.Findings {
if f.Pattern == "external-link" {
foundExternal = true
}
if f.Pattern == "source-repository-link" {
t.Errorf("documentation link should NOT trigger source-repository-link: %+v", f)
}
}
if !foundExternal {
t.Error("documentation link should trigger external-link")
}
}
func TestScanSkill_SourceRepoLink_ShortLabel(t *testing.T) {
dir := t.TempDir()
skillDir := filepath.Join(dir, "short-skill")
os.MkdirAll(skillDir, 0755)
os.WriteFile(filepath.Join(skillDir, "SKILL.md"),
[]byte("# Skill\n\n[source repo](https://github.com/org/repo)\n"), 0644)
result, err := ScanSkill(skillDir)
if err != nil {
t.Fatal(err)
}
var found bool
for _, f := range result.Findings {
if f.Pattern == "source-repository-link" && f.Severity == "HIGH" {
found = true
}
}
if !found {
t.Errorf("expected HIGH source-repository-link for 'source repo' label, got: %+v", result.Findings)
}
}
func TestScanSkill_ExternalLinkLocalhostSkipped(t *testing.T) {
dir := t.TempDir()
skillDir := filepath.Join(dir, "my-skill")
+35
View File
@@ -79,6 +79,41 @@ func TestRiskScoreAndLabel(t *testing.T) {
}
}
func TestRiskLabelFromScoreAndMaxSeverity(t *testing.T) {
t.Parallel()
tests := []struct {
name string
score int
maxSev string
wantLabel string
}{
// Single HIGH finding: score=15 → "low" by score, but floor is "high"
{"single HIGH floor", 15, SeverityHigh, "high"},
// Single CRITICAL: score=25 → "low" by score, floor is "critical"
{"single CRITICAL floor", 25, SeverityCritical, "critical"},
// Score already above floor: many findings push score high
{"score exceeds floor", 80, SeverityHigh, "critical"},
// MEDIUM floor: score=8 → "low" by score, floor is "medium"
{"single MEDIUM floor", 8, SeverityMedium, "medium"},
// LOW floor: score=3 → "low" by score, floor is "low"
{"single LOW floor", 3, SeverityLow, "low"},
// INFO: no floor, stays "low"
{"INFO no floor", 1, SeverityInfo, "low"},
// Clean: no findings
{"clean no findings", 0, "", "clean"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := RiskLabelFromScoreAndMaxSeverity(tt.score, tt.maxSev)
if got != tt.wantLabel {
t.Errorf("RiskLabelFromScoreAndMaxSeverity(%d, %q) = %q, want %q",
tt.score, tt.maxSev, got, tt.wantLabel)
}
})
}
}
func TestScanFileWithRules_Boundaries(t *testing.T) {
t.Parallel()
+8 -1
View File
@@ -161,13 +161,20 @@ rules:
regex: '(?i)(curl|wget|invoke-webrequest|iwr)\s+http://'
exclude: '(?i)http://(localhost|127\.0\.0\.1|0\.0\.0\.0)'
# ── HIGH: source repository link in markdown ──
- id: source-repository-link-0
severity: HIGH
pattern: source-repository-link
message: "Source repository link detected — may be used for supply-chain redirects"
regex: '(?i)\[source\s+repo(sitory)?\]\(https?://[^)]+\)'
# ── LOW: external links in markdown ──
- id: external-link-0
severity: LOW
pattern: external-link
message: "External URL in markdown link"
regex: '\]\(https?://[^)]+\)'
exclude: '(?i)\]\(https?://(localhost|127\.0\.0\.1|0\.0\.0\.0)[:/)]'
exclude: '(?i)(\]\(https?://(localhost|127\.0\.0\.1|0\.0\.0\.0)[:/)]|\[source\s+repo(sitory)?\]\(https?://)'
# ── INFO: risky shell chaining patterns (advisory) ──
- id: shell-chain-0
+7 -1
View File
@@ -104,6 +104,7 @@ func (s *Server) handleAuditAll(w http.ResponseWriter, r *http.Request) {
infoSkills := make([]string, 0)
scanErrors := 0
maxRisk := 0
maxSeverity := ""
// Phase 1: parallel scan with bounded workers.
projectRoot := s.projectRoot
@@ -151,6 +152,11 @@ func (s *Server) handleAuditAll(w http.ResponseWriter, r *http.Request) {
if result.RiskScore > maxRisk {
maxRisk = result.RiskScore
}
if ms := result.MaxSeverity(); ms != "" {
if maxSeverity == "" || audit.SeverityRank(ms) < audit.SeverityRank(maxSeverity) {
maxSeverity = ms
}
}
}
status := "ok"
@@ -166,7 +172,7 @@ func (s *Server) handleAuditAll(w http.ResponseWriter, r *http.Request) {
summary.Info = infoCount
summary.ScanErrors = scanErrors
summary.RiskScore = maxRisk
summary.RiskLabel = audit.RiskLabelFromScore(maxRisk)
summary.RiskLabel = audit.RiskLabelFromScoreAndMaxSeverity(maxRisk, maxSeverity)
args := map[string]any{
"scope": "all",
+79
View File
@@ -674,6 +674,85 @@ func TestAudit_AllUnresolved(t *testing.T) {
result.AssertAnyOutputContains(t, "no skills matched")
}
func TestAudit_SourceRepoLink_HIGH(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("repo-skill", map[string]string{
"SKILL.md": "---\nname: repo-skill\n---\n# Skill\n\n[source repository](https://github.com/org/repo)\n",
})
sb.WriteConfig(`source: ` + sb.SourcePath + "\ntargets: {}\n")
// source-repository-link is HIGH; default threshold is CRITICAL → warning, not failed
result := sb.RunCLI("audit", "repo-skill")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "Source repository link detected")
// Risk label should be "high" (severity floor), not "low" (score-only)
result.AssertAnyOutputContains(t, "Risk: HIGH")
}
func TestAudit_SourceRepoLink_JSON_RiskLabel(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("repo-json", map[string]string{
"SKILL.md": "---\nname: repo-json\n---\n# Skill\n\n[source repository](https://github.com/org/repo)\n",
})
sb.WriteConfig(`source: ` + sb.SourcePath + "\ntargets: {}\n")
result := sb.RunCLI("audit", "repo-json", "--json")
result.AssertSuccess(t)
var payload struct {
Results []struct {
RiskLabel string `json:"riskLabel"`
RiskScore int `json:"riskScore"`
Findings []struct {
Pattern string `json:"pattern"`
Severity string `json:"severity"`
} `json:"findings"`
} `json:"results"`
Summary struct {
RiskLabel string `json:"riskLabel"`
High int `json:"high"`
} `json:"summary"`
}
if err := json.Unmarshal([]byte(strings.TrimSpace(result.Stdout)), &payload); err != nil {
t.Fatalf("JSON parse error: %v\nstdout=%s", err, result.Stdout)
}
if len(payload.Results) != 1 {
t.Fatalf("expected 1 result, got %d", len(payload.Results))
}
r := payload.Results[0]
// Should have source-repository-link HIGH, NOT external-link
var hasSourceRepo bool
for _, f := range r.Findings {
if f.Pattern == "source-repository-link" && f.Severity == "HIGH" {
hasSourceRepo = true
}
if f.Pattern == "external-link" {
t.Error("source repo link should not also trigger external-link")
}
}
if !hasSourceRepo {
t.Errorf("expected source-repository-link finding, got: %+v", r.Findings)
}
// Risk label: result level
if r.RiskLabel != "high" {
t.Errorf("result riskLabel = %q, want 'high'", r.RiskLabel)
}
// Risk label: summary level (severity floor consistency)
if payload.Summary.RiskLabel != "high" {
t.Errorf("summary riskLabel = %q, want 'high'", payload.Summary.RiskLabel)
}
if payload.Summary.High != 1 {
t.Errorf("summary high = %d, want 1", payload.Summary.High)
}
}
func TestAudit_GroupJSON(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()