Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(report): export modified findings in JSON #7383

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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