From df640158008bfae465650ff5b2800fc6682a7331 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Mon, 2 Nov 2026 22:09:06 -0700 Subject: [PATCH] Carry each detector's finding kinds in the JSON report report.detectors now lists every kind with its title, SARIF level and CVSS-style score, so an importer can rank a finding by its kind without keeping its own table that goes stale with each new detector. Co-Authored-By: Claude Opus 5.5 --- internal/report/report.go | 28 +++++++++++++++++++----- internal/report/report_test.go | 40 +++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/internal/report/report.go b/internal/report/report.go index 2e4bd22..60a9301 100644 --- a/internal/report/report.go +++ b/internal/report/report.go @@ -19,11 +19,23 @@ import ( // DetectorInfo is what the report says about each detector that ran. type DetectorInfo struct { - ID string `json:"id"` - Title string `json:"title"` - CVEs []string `json:"cves,omitempty"` - Advisories []string `json:"advisories,omitempty"` - Data string `json:"data,omitempty"` + ID string `json:"id"` + Title string `json:"title"` + CVEs []string `json:"cves,omitempty"` + Advisories []string `json:"advisories,omitempty"` + Data string `json:"data,omitempty"` + Kinds []KindInfo `json:"kinds,omitempty"` +} + +// KindInfo is one type of finding a detector reports, so a reader can rank a +// finding by its kind without a table of its own. +type KindInfo struct { + ID string `json:"id"` + Title string `json:"title"` + // Level is the SARIF level: error, warning or note. + Level string `json:"level"` + // Severity is the CVSS-style score ("9.2"), when the kind has one. + Severity string `json:"severity,omitempty"` } // Report is the whole run. @@ -42,7 +54,11 @@ func New(tool, version string, dets []engine.Detector) *Report { for _, d := range dets { m := d.Manifest() r.manifests = append(r.manifests, m) - r.Detectors = append(r.Detectors, DetectorInfo{ID: m.ID, Title: m.Title, CVEs: m.CVEs, Advisories: m.Advisories, Data: m.Data}) + info := DetectorInfo{ID: m.ID, Title: m.Title, CVEs: m.CVEs, Advisories: m.Advisories, Data: m.Data} + for _, k := range m.Kinds { + info.Kinds = append(info.Kinds, KindInfo{ID: k.ID, Title: k.Title, Level: k.Level, Severity: k.SecuritySeverity}) + } + r.Detectors = append(r.Detectors, info) } return r } diff --git a/internal/report/report_test.go b/internal/report/report_test.go index 47fb82e..23a1865 100644 --- a/internal/report/report_test.go +++ b/internal/report/report_test.go @@ -1,6 +1,13 @@ package report -import "testing" +import ( + "bytes" + "encoding/json" + "reflect" + "testing" + + "github.com/DefectDojo/earlylight/internal/engine" +) func TestSafe(t *testing.T) { for _, s := range []string{"\x1b[31mred", "a\nfake line", "\x9b31m", "bad\xffutf8", "tab\there"} { @@ -14,3 +21,34 @@ func TestSafe(t *testing.T) { } } } + +type kindsDetector struct{} + +func (kindsDetector) Manifest() engine.Manifest { + return engine.Manifest{ID: "t-kinds", Kinds: []engine.Kind{ + {ID: "lib", Title: "Library", Level: "error", SecuritySeverity: "9.2"}, + {ID: "pin", Title: "Pin not checked", Level: "note"}, + }} +} +func (kindsDetector) Load() error { return nil } +func (kindsDetector) NewScan(engine.TargetKind) engine.Scan { return nil } + +// Importers rank findings by kind. The JSON report carries each kind's level +// and score so they don't need a table that goes stale with every detector. +func TestJSONCarriesKinds(t *testing.T) { + r := New("earlylight", "test", []engine.Detector{kindsDetector{}}) + var b bytes.Buffer + if err := JSON(&b, r); err != nil { + t.Fatal(err) + } + var got struct { + Detectors []DetectorInfo `json:"detectors"` + } + if err := json.Unmarshal(b.Bytes(), &got); err != nil { + t.Fatal(err) + } + want := []KindInfo{{ID: "lib", Title: "Library", Level: "error", Severity: "9.2"}, {ID: "pin", Title: "Pin not checked", Level: "note"}} + if len(got.Detectors) != 1 || !reflect.DeepEqual(got.Detectors[0].Kinds, want) { + t.Errorf("kinds = %+v, want %+v", got.Detectors, want) + } +}