Skip to content

Commit

Permalink
feat(report): export modified findings in JSON (#7383)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 authored Aug 29, 2024
1 parent 4c6e8ca commit 7aea79d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/docs/configuration/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ You can filter the results by
To show the suppressed results, use the `--show-suppressed` flag.
!!! note
This flag is currently available only in the table format.
It's exported as `ExperimentalModifiedFindings` in the JSON output.
```bash
$ trivy image --vex debian11.csaf.vex --ignorefile .trivyignore.yaml --show-suppressed debian:11
Expand Down
11 changes: 9 additions & 2 deletions pkg/report/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (

// JSONWriter implements result Writer
type JSONWriter struct {
Output io.Writer
ListAllPkgs bool
Output io.Writer
ListAllPkgs bool
ShowSuppressed bool
}

// Write writes the results in JSON format
Expand All @@ -26,6 +27,12 @@ func (jw JSONWriter) Write(_ context.Context, report types.Report) error {
report.Results[i].Packages = nil
}
}
if !jw.ShowSuppressed {
// Delete suppressed findings
for i := range report.Results {
report.Results[i].ModifiedFindings = nil
}
}
report.Results = lo.Filter(report.Results, func(r types.Result, _ int) bool {
return r.Target != "" || !r.IsEmpty()
})
Expand Down
5 changes: 3 additions & 2 deletions pkg/report/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func Write(ctx context.Context, report types.Report, option flag.Options) (err e
}
case types.FormatJSON:
writer = &JSONWriter{
Output: output,
ListAllPkgs: option.ListAllPkgs,
Output: output,
ListAllPkgs: option.ListAllPkgs,
ShowSuppressed: option.ShowSuppressed,
}
case types.FormatGitHub:
writer = &github.Writer{
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ type Result struct {

// ModifiedFindings holds a list of findings that have been modified from their original state.
// This can include vulnerabilities that have been marked as ignored, not affected, or have had
// their severity adjusted. It is currently available only in the table format.
ModifiedFindings []ModifiedFinding `json:"-"`
// their severity adjusted. It's still in an experimental stage and may change in the future.
ModifiedFindings []ModifiedFinding `json:"ExperimentalModifiedFindings,omitempty"`
}

func (r *Result) IsEmpty() bool {
Expand Down

0 comments on commit 7aea79d

Please sign in to comment.