feat(crypto): show findings in table output (#11395)

This commit is contained in:
Nikita Pivkin authored and GitHub committed 2026-10-28 13:17:13 +00:00
1 parent 948767f839
commit 4cdb2db63d
18 files changed
+715 -21

No files matched your search

+1
View File
@@ -24,6 +24,7 @@ $ trivy image --scanners vuln alpine:3.15
| Misconfiguration | ✓ |
| Secret | ✓ |
| License | ✓ |
| Crypto | ✓ |
By default, `Trivy` exits with code 0 even when security issues are detected.
Use the `--exit-code` option if you want to exit with a non-zero exit code.
+1
View File
@@ -18,6 +18,7 @@ Trivy supports the following formats:
| Misconfiguration | ✓ |
| Secret | ✓ |
| License | ✓ |
| Crypto | ✓ |
```bash
$ trivy image -f table golang:1.22.11-alpine3.20
@@ -41,7 +41,13 @@ trivy convert [flags] RESULT_JSON
-o, --output string output file name
--output-plugin-arg string [EXPERIMENTAL] output plugin arguments
--report string specify a report format for the output (allowed values: all,summary) (default "all")
--scanners strings List of scanners included when generating the json report. Used only for rendering the summary table. (allowed values: vuln,misconfig,secret,license)
--scanners strings List of scanners included when generating the json report. Used only for rendering the summary table.
Allowed values:
- vuln
- misconfig
- secret
- license
- crypto
-s, --severity strings severities of security issues to be displayed
Allowed values:
- UNKNOWN
+25 -5
View File
@@ -3,13 +3,12 @@
!!! warning "EXPERIMENTAL"
This feature might change without preserving backwards compatibility.
Trivy inventories the cryptographic material it finds in a container image and reports it as a CBOM (Cryptography Bill of Materials). The CBOM lists the certificates and keys themselves and the algorithms they use. The result is part of the CycloneDX report, next to the software components. In JSON output Trivy also reports [weak assets](#findings) found by built-in checks.
Trivy inventories the cryptographic material it finds in a container image and reports it as a CBOM (Cryptography Bill of Materials). The CBOM lists the certificates and keys themselves and the algorithms they use. The result is part of the CycloneDX report, next to the software components. In JSON and table output Trivy also reports [weak assets](#findings) found by built-in checks.
The scanner is off by default. Enable it with `--scanners crypto` together with `--format cyclonedx` or `--format json`.
The scanner is off by default. Enable it with `--scanners crypto` together with `--format cyclonedx`, `--format json` or `--format table`.
```shell
$ trivy image --scanners crypto --format cyclonedx --output result.cdx.json myimage:1.0.0
$ trivy image --scanners crypto --format json --output result.json myimage:1.0.0
```
Trivy rejects `--scanners crypto` with other report formats.
@@ -114,7 +113,7 @@ The parameters of an algorithm are stored in `algorithmProperties`. The key size
## Findings
Trivy runs built-in checks to find weak cryptographic assets and reports the findings in JSON output. Trivy does not determine whether any software uses these assets.
Trivy runs built-in checks to find weak cryptographic assets and reports the findings in JSON and table output. Trivy does not determine whether any software uses these assets.
| Rule ID | Title | Asset | Condition | Severity |
| --- | --- | --- | --- | --- |
@@ -167,7 +166,28 @@ Every entry of `CryptoAssets` in JSON output has an `AssetID`, and a finding ref
| `CRYPTO-0003` | `Curve`, `ClassicalSecurityLevel` |
| `CRYPTO-0004` | `NotAfter` |
Findings are evaluated only for JSON output, so they do not affect the exit code of CycloneDX output. `trivy convert` keeps the findings of a JSON report only when converting to JSON, and keeps the cryptographic assets for any format.
Table output shows the findings without the inventory. Identical rows are merged and the asset name ends with their number, such as `x2`. `Total` counts every finding, and JSON output tells the assets apart.
```
example:latest (crypto)
=======================
Total: 4 (UNKNOWN: 0, LOW: 0, MEDIUM: 1, HIGH: 3, CRITICAL: 0)
┌──────────┬─────────────┬───────────────────────────────────┬──────────────────────┬─────────────────────────────────────────────┐
│ Severity │ Rule ID │ Asset │ File │ Message │
├──────────┼─────────────┼───────────────────────────────────┼──────────────────────┼─────────────────────────────────────────────┤
│ HIGH │ CRYPTO-0002 │ RSA-1024 private key (PKCS#8) │ app/certs/legacy.key │ RSA key is 1024 bits, less than 2048 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0002/ │
│ │ ├───────────────────────────────────┼──────────────────────┤ │
│ │ │ RSA-1024 public key (PKIX) x2 │ app/certs/legacy.pem │ │
│ │ │ │ │ │
├──────────┼─────────────┼───────────────────────────────────┤ ├─────────────────────────────────────────────┤
│ MEDIUM │ CRYPTO-0004 │ CN=legacy.example.com certificate │ │ Certificate expired on 2024-03-01 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0004/ │
└──────────┴─────────────┴───────────────────────────────────┴──────────────────────┴─────────────────────────────────────────────┘
```
Findings are evaluated only for JSON and table output, so they do not affect the exit code of CycloneDX output. `trivy convert` keeps the findings of a JSON report only when converting to JSON or table, and keeps the cryptographic assets for any format.
A rule ID in an [ignore file](../configuration/filtering.md#by-finding-ids) suppresses its findings, and the `crypto` section of `.trivyignore.yaml` can limit that to some file paths. The assets of suppressed findings stay in the inventory. `--ignore-policy` and `--show-suppressed` do not apply to crypto findings.
+1 -1
View File
@@ -89,7 +89,7 @@ $ trivy image --scanners license [YOUR_IMAGE_NAME]
### Cryptographic assets
It is experimental and disabled by default.
It requires the CycloneDX or JSON format.
It requires the CycloneDX, JSON or table format.
See [here](../scanner/crypto.md) for the detail.
```shell
+1
View File
@@ -546,6 +546,7 @@ func NewConvertCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
Scanners: flag.ScannersFlag.Clone(),
}
scanFlagGroup.Scanners.Default = nil // disable default scanners
scanFlagGroup.Scanners.Values = append(scanFlagGroup.Scanners.Values, string(types.CryptoScanner))
scanFlagGroup.Scanners.Usage = "List of scanners included when generating the json report. Used only for rendering the summary table."
convertFlags := flag.Flags{
+3 -2
View File
@@ -41,10 +41,11 @@ func Run(ctx context.Context, opts flag.Options) (err error) {
}
compat(&r)
// Only JSON output shows crypto findings
if opts.Format != types.FormatJSON {
if !slices.Contains(types.CryptoFindingsFormats, opts.Format) {
r.Results.DropCryptoFindings()
}
if err = result.Filter(ctx, r, opts.FilterOpts()); err != nil {
return xerrors.Errorf("unable to filter results: %w", err)
}
+13
View File
@@ -20,6 +20,19 @@ type rule struct {
// match reports whether the asset fails the rule and returns the evidence.
// Unknown asset values never match.
match func(asset ftypes.CryptoAsset, now time.Time) (types.CryptoEvidence, bool)
// message describes the failed check by its evidence.
message func(evidence types.CryptoEvidence) string
}
// Message returns the text that describes why the finding was reported.
// It is empty for a finding of an unknown rule or a rule without a description.
func Message(finding types.CryptoFinding) string {
for _, r := range rules {
if r.id == finding.RuleID && r.message != nil {
return r.message(finding.Evidence)
}
}
return ""
}
// match is a finding together with the asset it refers to.
+59
View File
@@ -274,3 +274,62 @@ func TestEvaluate(t *testing.T) {
})
}
}
func TestMessage(t *testing.T) {
tests := []struct {
name string
finding types.CryptoFinding
want string
}{
{
name: "SHA-1 signature",
finding: types.CryptoFinding{
RuleID: "CRYPTO-0001",
Evidence: types.CryptoEvidence{SignatureAlgorithm: "RSA-PKCS1-1.5-SHA-1"},
},
want: "Certificate is signed with RSA-PKCS1-1.5-SHA-1",
},
{
name: "small RSA key",
finding: types.CryptoFinding{
RuleID: "CRYPTO-0002",
Evidence: types.CryptoEvidence{KeySize: 1024},
},
want: "RSA key is 1024 bits, less than 2048",
},
{
name: "weak curve",
finding: types.CryptoFinding{
RuleID: "CRYPTO-0003",
Evidence: types.CryptoEvidence{Curve: "P-192", ClassicalSecurityLevel: new(96)},
},
want: "Curve P-192 gives 96 bits of security, less than 112",
},
{
name: "weak curve without a name",
finding: types.CryptoFinding{
RuleID: "CRYPTO-0003",
Evidence: types.CryptoEvidence{ClassicalSecurityLevel: new(96)},
},
want: "Elliptic curve gives 96 bits of security, less than 112",
},
{
name: "expired certificate",
finding: types.CryptoFinding{
RuleID: "CRYPTO-0004",
Evidence: types.CryptoEvidence{NotAfter: time.Date(2024, 3, 1, 12, 0, 0, 0, time.UTC)},
},
want: "Certificate expired on 2024-03-01",
},
{
name: "unknown rule",
finding: types.CryptoFinding{RuleID: "CRYPTO-9999"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, check.Message(tt.finding))
})
}
}
+38 -2
View File
@@ -1,6 +1,7 @@
package check
import (
"fmt"
"slices"
"time"
@@ -17,6 +18,7 @@ var rules = []rule{
severity: dbTypes.SeverityMedium,
kind: ftypes.CryptoKindCertificate,
match: matchSHA1Signature,
message: sha1SignatureMessage,
},
{
id: "CRYPTO-0002",
@@ -24,6 +26,7 @@ var rules = []rule{
severity: dbTypes.SeverityHigh,
kind: ftypes.CryptoKindKey,
match: matchSmallRSAKey,
message: smallRSAKeyMessage,
},
{
id: "CRYPTO-0003",
@@ -31,6 +34,7 @@ var rules = []rule{
severity: dbTypes.SeverityHigh,
kind: ftypes.CryptoKindAlgorithm,
match: matchWeakCurve,
message: weakCurveMessage,
},
{
id: "CRYPTO-0004",
@@ -38,6 +42,7 @@ var rules = []rule{
severity: dbTypes.SeverityMedium,
kind: ftypes.CryptoKindCertificate,
match: matchExpiredCertificate,
message: expiredCertificateMessage,
},
}
@@ -48,6 +53,13 @@ const (
oidECPublicKey = "1.2.840.10045.2.1"
)
const (
// minRSAKeySize is the smallest RSA key size in bits that passes CRYPTO-0002.
minRSAKeySize = 2048
// minSecurityLevel is the smallest classical security level in bits that passes CRYPTO-0003.
minSecurityLevel = 112
)
// sha1SignatureOIDs are the signature algorithms that hash with SHA-1. RSASSA-PSS names its
// hash in parameters that are not collected, so it is left out.
var sha1SignatureOIDs = []string{
@@ -69,7 +81,7 @@ func matchSHA1Signature(asset ftypes.CryptoAsset, _ time.Time) (types.CryptoEvid
}
func matchSmallRSAKey(asset ftypes.CryptoAsset, _ time.Time) (types.CryptoEvidence, bool) {
if asset.Key == nil || asset.Key.Size <= 0 || asset.Key.Size >= 2048 {
if asset.Key == nil || asset.Key.Size <= 0 || asset.Key.Size >= minRSAKeySize {
return types.CryptoEvidence{}, false
}
algorithm, found := relatedAlgorithm(asset, ftypes.CryptoRelationshipUsedWith)
@@ -87,7 +99,7 @@ func matchWeakCurve(asset ftypes.CryptoAsset, _ time.Time) (types.CryptoEvidence
return types.CryptoEvidence{}, false
}
level := asset.Algorithm.ClassicalSecurityLevel
if level == nil || *level >= 112 {
if level == nil || *level >= minSecurityLevel {
return types.CryptoEvidence{}, false
}
// The curve is only evidence, so a parse error is ignored.
@@ -108,6 +120,30 @@ func matchExpiredCertificate(asset ftypes.CryptoAsset, now time.Time) (types.Cry
}, true
}
func sha1SignatureMessage(e types.CryptoEvidence) string {
return fmt.Sprintf("Certificate is signed with %s", e.SignatureAlgorithm)
}
func smallRSAKeyMessage(e types.CryptoEvidence) string {
return fmt.Sprintf("RSA key is %d bits, less than %d", e.KeySize, minRSAKeySize)
}
func weakCurveMessage(e types.CryptoEvidence) string {
curve := "Elliptic curve"
if e.Curve != "" {
curve = "Curve " + e.Curve
}
level := 0
if e.ClassicalSecurityLevel != nil {
level = *e.ClassicalSecurityLevel
}
return fmt.Sprintf("%s gives %d bits of security, less than %d", curve, level, minSecurityLevel)
}
func expiredCertificateMessage(e types.CryptoEvidence) string {
return "Certificate expired on " + e.NotAfter.UTC().Format("2006-01-02")
}
// relatedAlgorithm returns the identity of the algorithm the asset has the given
// relationship with.
func relatedAlgorithm(asset ftypes.CryptoAsset, relationship ftypes.CryptoRelationshipType) (ftypes.CryptoIdentity, bool) {
+6 -5
View File
@@ -449,6 +449,9 @@ type Options struct {
usedFlags []Flagger
}
// cryptoFormats are the formats that show cryptographic assets or their findings.
var cryptoFormats = slices.Concat([]types.Format{types.FormatCycloneDX}, types.CryptoFindingsFormats)
// Align takes consistency of options
func (o *Options) Align(f *Flags) error {
if scanFlagGroup, ok := findFlagGroup[*ScanFlagGroup](f); ok && scanFlagGroup.Scanners != nil {
@@ -488,9 +491,8 @@ func (o *Options) Align(f *Flags) error {
}
}
// Only JSON and CycloneDX output show cryptographic assets
if o.Scanners.Enabled(types.CryptoScanner) && o.Format != types.FormatCycloneDX && o.Format != types.FormatJSON {
return xerrors.Errorf("'--scanners crypto' can only be used with '--format cyclonedx' or '--format json'")
if o.Scanners.Enabled(types.CryptoScanner) && !slices.Contains(cryptoFormats, o.Format) {
return xerrors.Errorf("'--scanners crypto' can only be used with '--format cyclonedx', '--format json' or '--format table'")
}
// A remote SBOM replaces layer analysis, so cryptographic assets would not be collected
@@ -532,8 +534,7 @@ func (o *Options) ScanOpts() types.ScanOptions {
ScanRemovedPackages: o.ScanRemovedPkgs, // this is valid only for 'image' subcommand
LicenseCategories: o.LicenseCategories,
LicenseFull: o.LicenseFull,
// Only JSON output shows crypto findings
CryptoChecks: o.Format == types.FormatJSON,
CryptoChecks: slices.Contains(types.CryptoFindingsFormats, o.Format),
FilePatterns: o.FilePatterns,
IncludeDevDeps: o.IncludeDevDeps,
Distro: o.Distro,
+41 -1
View File
@@ -382,7 +382,14 @@ func TestOptions_Align(t *testing.T) {
ReportOptions: flag.ReportOptions{Format: types.FormatTable},
ScanOptions: flag.ScanOptions{Scanners: types.Scanners{types.CryptoScanner}},
},
wantErr: "'--scanners crypto' can only be used with '--format cyclonedx' or '--format json'",
},
{
name: "crypto scanner with SARIF",
opts: flag.Options{
ReportOptions: flag.ReportOptions{Format: types.FormatSarif},
ScanOptions: flag.ScanOptions{Scanners: types.Scanners{types.CryptoScanner}},
},
wantErr: "'--scanners crypto' can only be used with '--format cyclonedx', '--format json' or '--format table'",
},
{
name: "crypto scanner with SBOM sources",
@@ -425,3 +432,36 @@ func TestOptions_Align(t *testing.T) {
})
}
}
func TestOptions_ScanOpts_cryptoChecks(t *testing.T) {
tests := []struct {
name string
format types.Format
want bool
}{
{
name: "JSON",
format: types.FormatJSON,
want: true,
},
{
name: "table",
format: types.FormatTable,
want: true,
},
{
name: "CycloneDX",
format: types.FormatCycloneDX,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := flag.Options{
ReportOptions: flag.ReportOptions{Format: tt.format},
}
assert.Equal(t, tt.want, opts.ScanOpts().CryptoChecks)
})
}
}
+128
View File
@@ -0,0 +1,128 @@
package table
import (
"bytes"
"cmp"
"fmt"
"slices"
"strings"
"github.com/samber/lo"
"github.com/aquasecurity/tml"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy/pkg/crypto/check"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/types"
)
type cryptoRenderer struct {
w *bytes.Buffer
isTerminal bool
severities []dbTypes.Severity
}
func NewCryptoRenderer(buf *bytes.Buffer, isTerminal bool, severities []dbTypes.Severity) *cryptoRenderer {
if !isTerminal {
tml.DisableFormatting()
}
return &cryptoRenderer{
w: buf,
isTerminal: isTerminal,
severities: severities,
}
}
func (r *cryptoRenderer) Render(result types.Result) {
if len(result.CryptoFindings) == 0 {
return
}
assets := make(map[string]ftypes.CryptoAsset, len(result.CryptoAssets))
for _, asset := range result.CryptoAssets {
assets[asset.AssetID] = asset
}
findings := slices.Clone(result.CryptoFindings)
slices.SortStableFunc(findings, func(a, b types.CryptoFinding) int {
assetA, assetB := assets[a.AssetID], assets[b.AssetID]
return cmp.Or(
dbTypes.CompareSeverityString(a.Severity, b.Severity),
cmp.Compare(a.RuleID, b.RuleID),
cmp.Compare(assetA.FilePath, assetB.FilePath),
cmp.Compare(cryptoAssetLabel(assetA), cryptoAssetLabel(assetB)),
cmp.Compare(check.Message(a), check.Message(b)),
)
})
severityCount := lo.CountValuesBy(findings, func(f types.CryptoFinding) string {
return f.Severity
})
total, summaries := summarize(r.severities, severityCount)
RenderTarget(r.w, result.Target+" (crypto)", r.isTerminal)
fmt.Fprintf(r.w, "Total: %d (%s)\n\n", total, strings.Join(summaries, ", "))
tw := newTableWriter(r.w, r.isTerminal)
tw.SetHeaders("Severity", "Rule ID", "Asset", "File", "Message")
for _, row := range groupCryptoFindings(findings, assets) {
f := row.finding
severity := f.Severity
if r.isTerminal {
severity = ColorizeSeverity(f.Severity, f.Severity)
}
label := cryptoAssetLabel(row.asset)
if row.count > 1 {
label = fmt.Sprintf("%s x%d", label, row.count)
}
message := tml.Sprintf("%s\n<blue>%s</blue>", check.Message(f), cryptoRuleURL(f.RuleID))
tw.AddRow(severity, f.RuleID, label, row.asset.FilePath, message)
}
tw.Render()
}
// cryptoRow is a table row for findings that look the same.
type cryptoRow struct {
finding types.CryptoFinding
asset ftypes.CryptoAsset
count int
}
// groupCryptoFindings joins neighboring findings of different assets that show the same values in the table.
// The findings must be sorted.
func groupCryptoFindings(findings []types.CryptoFinding, assets map[string]ftypes.CryptoAsset) []cryptoRow {
var rows []cryptoRow
for _, f := range findings {
asset := assets[f.AssetID]
if n := len(rows); n > 0 {
prev := rows[n-1]
if prev.finding.Severity == f.Severity && prev.finding.RuleID == f.RuleID &&
prev.asset.FilePath == asset.FilePath && cryptoAssetLabel(prev.asset) == cryptoAssetLabel(asset) &&
check.Message(prev.finding) == check.Message(f) {
rows[n-1].count++
continue
}
}
rows = append(rows, cryptoRow{finding: f, asset: asset, count: 1})
}
return rows
}
func cryptoRuleURL(ruleID string) string {
return "https://avd.aquasec.com/crypto/" + strings.ToLower(ruleID) + "/"
}
func cryptoAssetLabel(a ftypes.CryptoAsset) string {
name := a.Name
switch a.Kind {
case ftypes.CryptoKindCertificate:
name += " certificate"
case ftypes.CryptoKindAlgorithm:
name += " algorithm"
}
if a.Format == "" {
return name
}
// The format keeps the same key in different containers from being merged into one cell.
return fmt.Sprintf("%s (%s)", name, a.Format)
}
+283
View File
@@ -0,0 +1,283 @@
package table_test
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/report/table"
"github.com/aquasecurity/trivy/pkg/types"
)
func TestCryptoRenderer(t *testing.T) {
tests := []struct {
name string
input types.Result
want string
}{
{
name: "findings",
input: types.Result{
Target: "example:latest",
Class: types.ClassCrypto,
CryptoAssets: []ftypes.CryptoAsset{
{
AssetID: "cert",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Kind: ftypes.CryptoKindCertificate, Name: "CN=legacy.example.com"},
FilePath: "app/certs/legacy.pem",
},
{
AssetID: "curve",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Kind: ftypes.CryptoKindAlgorithm, Name: "EC-P-192"},
FilePath: "app/certs/legacy.pem",
},
{
AssetID: "public",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Name: "RSA-1024 public key"},
FilePath: "app/certs/legacy.pem",
Format: ftypes.CryptoKeyFormatPKIX,
},
{
AssetID: "private",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Name: "RSA-1024 private key"},
FilePath: "app/certs/legacy.key",
Format: ftypes.CryptoKeyFormatPKCS8,
},
},
CryptoFindings: []types.CryptoFinding{
{
RuleID: "CRYPTO-0004",
Title: "Expired certificate",
Severity: "MEDIUM",
AssetID: "cert",
Evidence: types.CryptoEvidence{
NotAfter: time.Date(2024, 3, 1, 0, 0, 0, 0, time.UTC),
},
},
{
RuleID: "CRYPTO-0003",
Title: "Weak elliptic curve",
Severity: "HIGH",
AssetID: "curve",
Evidence: types.CryptoEvidence{Curve: "P-192", ClassicalSecurityLevel: new(96)},
},
{
RuleID: "CRYPTO-0002",
Title: "Small RSA key",
Severity: "HIGH",
AssetID: "public",
Evidence: types.CryptoEvidence{KeySize: 1024},
},
{
RuleID: "CRYPTO-0002",
Title: "Small RSA key",
Severity: "HIGH",
AssetID: "private",
Evidence: types.CryptoEvidence{KeySize: 1024},
},
},
},
want: `
example:latest (crypto)
=======================
Total: 4 (MEDIUM: 1, HIGH: 3)
┌──────────┬─────────────┬───────────────────────────────────┬──────────────────────┬──────────────────────────────────────────────────────┐
│ Severity │ Rule ID │ Asset │ File │ Message │
├──────────┼─────────────┼───────────────────────────────────┼──────────────────────┼──────────────────────────────────────────────────────┤
│ HIGH │ CRYPTO-0002 │ RSA-1024 private key (PKCS#8) │ app/certs/legacy.key │ RSA key is 1024 bits, less than 2048 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0002/ │
│ │ ├───────────────────────────────────┼──────────────────────┤ │
│ │ │ RSA-1024 public key (PKIX) │ app/certs/legacy.pem │ │
│ │ │ │ │ │
│ ├─────────────┼───────────────────────────────────┤ ├──────────────────────────────────────────────────────┤
│ │ CRYPTO-0003 │ EC-P-192 algorithm │ │ Curve P-192 gives 96 bits of security, less than 112 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0003/ │
├──────────┼─────────────┼───────────────────────────────────┤ ├──────────────────────────────────────────────────────┤
│ MEDIUM │ CRYPTO-0004 │ CN=legacy.example.com certificate │ │ Certificate expired on 2024-03-01 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0004/ │
└──────────┴─────────────┴───────────────────────────────────┴──────────────────────┴──────────────────────────────────────────────────────┘
`,
},
{
name: "group different assets that look the same",
input: types.Result{
Target: "example:latest",
Class: types.ClassCrypto,
CryptoAssets: []ftypes.CryptoAsset{
{
AssetID: "first",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Name: "RSA-1024 public key"},
FilePath: "app/certs/keys.pem",
Format: ftypes.CryptoKeyFormatPKIX,
},
{
AssetID: "second",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Name: "RSA-1024 public key"},
FilePath: "app/certs/keys.pem",
Format: ftypes.CryptoKeyFormatPKIX,
},
},
CryptoFindings: []types.CryptoFinding{
{
RuleID: "CRYPTO-0002",
Severity: "HIGH",
AssetID: "first",
Evidence: types.CryptoEvidence{KeySize: 1024},
},
{
RuleID: "CRYPTO-0002",
Severity: "HIGH",
AssetID: "second",
Evidence: types.CryptoEvidence{KeySize: 1024},
},
},
},
want: `
example:latest (crypto)
=======================
Total: 2 (MEDIUM: 0, HIGH: 2)
┌──────────┬─────────────┬───────────────────────────────┬────────────────────┬─────────────────────────────────────────────┐
│ Severity │ Rule ID │ Asset │ File │ Message │
├──────────┼─────────────┼───────────────────────────────┼────────────────────┼─────────────────────────────────────────────┤
│ HIGH │ CRYPTO-0002 │ RSA-1024 public key (PKIX) x2 │ app/certs/keys.pem │ RSA key is 1024 bits, less than 2048 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0002/ │
└──────────┴─────────────┴───────────────────────────────┴────────────────────┴─────────────────────────────────────────────┘
`,
},
{
name: "do not group keys of different formats",
input: types.Result{
Target: "example:latest",
Class: types.ClassCrypto,
CryptoAssets: []ftypes.CryptoAsset{
{
AssetID: "pkix",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Name: "RSA-1024 public key"},
FilePath: "app/certs/keys.pem",
Format: ftypes.CryptoKeyFormatPKIX,
},
{
AssetID: "pkcs1",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Name: "RSA-1024 public key"},
FilePath: "app/certs/keys.pem",
Format: ftypes.CryptoKeyFormatPKCS1,
},
},
CryptoFindings: []types.CryptoFinding{
{
RuleID: "CRYPTO-0002",
Severity: "HIGH",
AssetID: "pkix",
Evidence: types.CryptoEvidence{KeySize: 1024},
},
{
RuleID: "CRYPTO-0002",
Severity: "HIGH",
AssetID: "pkcs1",
Evidence: types.CryptoEvidence{KeySize: 1024},
},
},
},
want: `
example:latest (crypto)
=======================
Total: 2 (MEDIUM: 0, HIGH: 2)
┌──────────┬─────────────┬──────────────────────────────┬────────────────────┬─────────────────────────────────────────────┐
│ Severity │ Rule ID │ Asset │ File │ Message │
├──────────┼─────────────┼──────────────────────────────┼────────────────────┼─────────────────────────────────────────────┤
│ HIGH │ CRYPTO-0002 │ RSA-1024 public key (PKCS#1) │ app/certs/keys.pem │ RSA key is 1024 bits, less than 2048 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0002/ │
│ │ ├──────────────────────────────┤ │ │
│ │ │ RSA-1024 public key (PKIX) │ │ │
│ │ │ │ │ │
└──────────┴─────────────┴──────────────────────────────┴────────────────────┴─────────────────────────────────────────────┘
`,
},
{
name: "do not group certificates with different messages",
input: types.Result{
Target: "example:latest",
Class: types.ClassCrypto,
CryptoAssets: []ftypes.CryptoAsset{
{
AssetID: "old",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Kind: ftypes.CryptoKindCertificate, Name: "legacy.example.com"},
FilePath: "app/certs/chain.pem",
},
{
AssetID: "older",
CryptoAssetInfo: ftypes.CryptoAssetInfo{Kind: ftypes.CryptoKindCertificate, Name: "legacy.example.com"},
FilePath: "app/certs/chain.pem",
},
},
CryptoFindings: []types.CryptoFinding{
{
RuleID: "CRYPTO-0004",
Severity: "MEDIUM",
AssetID: "old",
Evidence: types.CryptoEvidence{
NotAfter: time.Date(2024, 3, 1, 0, 0, 0, 0, time.UTC),
},
},
{
RuleID: "CRYPTO-0004",
Severity: "MEDIUM",
AssetID: "older",
Evidence: types.CryptoEvidence{
NotAfter: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC),
},
},
},
},
want: `
example:latest (crypto)
=======================
Total: 2 (MEDIUM: 2, HIGH: 0)
┌──────────┬─────────────┬────────────────────────────────┬─────────────────────┬─────────────────────────────────────────────┐
│ Severity │ Rule ID │ Asset │ File │ Message │
├──────────┼─────────────┼────────────────────────────────┼─────────────────────┼─────────────────────────────────────────────┤
│ MEDIUM │ CRYPTO-0004 │ legacy.example.com certificate │ app/certs/chain.pem │ Certificate expired on 2023-03-01 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0004/ │
│ │ │ │ ├─────────────────────────────────────────────┤
│ │ │ │ │ Certificate expired on 2024-03-01 │
│ │ │ │ │ https://avd.aquasec.com/crypto/crypto-0004/ │
└──────────┴─────────────┴────────────────────────────────┴─────────────────────┴─────────────────────────────────────────────┘
`,
},
{
name: "no findings",
input: types.Result{
Target: "example:latest",
Class: types.ClassCrypto,
CryptoAssets: []ftypes.CryptoAsset{
{
CryptoAssetInfo: ftypes.CryptoAssetInfo{
Kind: ftypes.CryptoKindCertificate,
},
},
},
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
renderer := table.NewCryptoRenderer(buf, false, []dbTypes.Severity{
dbTypes.SeverityHigh,
dbTypes.SeverityMedium,
})
renderer.Render(tt.input)
assert.Equal(t, tt.want, buf.String())
})
}
}
+22 -4
View File
@@ -37,6 +37,8 @@ func NewScanner(scanner types.Scanner) Scanner {
return SecretScanner{}
case types.LicenseScanner:
return LicenseScanner{}
case types.CryptoScanner:
return CryptoScanner{}
}
return nil
}
@@ -115,6 +117,23 @@ func (s LicenseScanner) String() string {
return string(types.LicenseScanner)
}
type CryptoScanner struct{ scannerAlignment }
func (s CryptoScanner) Header() string {
return "Crypto"
}
func (s CryptoScanner) Count(result types.Result) int {
if result.Class == types.ClassCrypto {
return len(result.CryptoFindings)
}
return -1
}
func (s CryptoScanner) String() string {
return string(types.CryptoScanner)
}
type summaryRenderer struct {
w *bytes.Buffer
isTerminal bool
@@ -172,14 +191,12 @@ func (r *summaryRenderer) Render(report types.Report) {
t.SetAlignment(alignments...)
for _, result := range r.splitAggregatedPackages(report.Results) {
// The summary counts findings, and cryptographic assets are an inventory.
if result.Class == types.ClassCrypto {
continue
}
resultType := string(result.Type)
switch result.Class {
case types.ClassSecret:
resultType = "text"
case types.ClassCrypto:
resultType = "crypto"
case types.ClassLicense, types.ClassLicenseFile:
resultType = "-"
}
@@ -224,6 +241,7 @@ func (r *summaryRenderer) showEmptyResultsWarning() {
resultByFindings := []Scanner{
NewScanner(types.SecretScanner),
NewScanner(types.LicenseScanner),
NewScanner(types.CryptoScanner),
}
if scanners := lo.Intersect(resultByFiles, r.scanners); len(scanners) > 0 {
+77
View File
@@ -159,6 +159,27 @@ var (
},
},
}
crypto = types.Result{
Target: "example:latest",
Class: types.ClassCrypto,
CryptoFindings: []types.CryptoFinding{
{
RuleID: "CRYPTO-0002",
},
{
RuleID: "CRYPTO-0004",
},
},
}
cryptoNoFindings = types.Result{
Target: "example:latest",
Class: types.ClassCrypto,
CryptoAssets: []ftypes.CryptoAsset{
{
AssetID: "8c2f0e5a91d34b76",
},
},
}
secret = types.Result{
Target: "requirements.txt",
Class: types.ClassSecret,
@@ -328,6 +349,62 @@ Legend:
- '-': Not scanned
- '0': Clean (no security findings detected)
`,
},
{
name: "happy path no vulns + crypto",
scanners: []types.Scanner{
types.VulnerabilityScanner,
types.CryptoScanner,
},
report: types.Report{
Results: []types.Result{
noVuln,
crypto,
},
},
want: `
Report Summary
┌──────────────────┬────────┬─────────────────┬────────┐
│ Target │ Type │ Vulnerabilities │ Crypto │
├──────────────────┼────────┼─────────────────┼────────┤
│ requirements.txt │ pip │ 0 │ - │
├──────────────────┼────────┼─────────────────┼────────┤
│ example:latest │ crypto │ - │ 2 │
└──────────────────┴────────┴─────────────────┴────────┘
Legend:
- '-': Not scanned
- '0': Clean (no security findings detected)
`,
},
{
name: "happy path no vulns + crypto assets without findings",
scanners: []types.Scanner{
types.VulnerabilityScanner,
types.CryptoScanner,
},
report: types.Report{
Results: []types.Result{
noVuln,
cryptoNoFindings,
},
},
want: `
Report Summary
┌──────────────────┬────────┬─────────────────┬────────┐
│ Target │ Type │ Vulnerabilities │ Crypto │
├──────────────────┼────────┼─────────────────┼────────┤
│ requirements.txt │ pip │ 0 │ - │
├──────────────────┼────────┼─────────────────┼────────┤
│ example:latest │ crypto │ - │ 0 │
└──────────────────┴────────┴─────────────────┴────────┘
Legend:
- '-': Not scanned
- '0': Clean (no security findings detected)
`,
},
{
+5
View File
@@ -40,6 +40,7 @@ type Writer struct {
secretRenderer Renderer
pkgLicenseRenderer Renderer
fileLicenseRenderer Renderer
cryptoRenderer Renderer
options Options
}
@@ -80,6 +81,7 @@ func NewWriter(options Options) *Writer {
secretRenderer: NewSecretRenderer(buf, isTerminal, options.Severities),
pkgLicenseRenderer: NewPkgLicenseRenderer(buf, isTerminal, options.Severities),
fileLicenseRenderer: NewFileLicenseRenderer(buf, isTerminal, options.Severities),
cryptoRenderer: NewCryptoRenderer(buf, isTerminal, options.Severities),
options: options,
}
}
@@ -134,6 +136,9 @@ func (tw *Writer) render(result types.Result) {
// file license
case types.ClassLicenseFile:
tw.fileLicenseRenderer.Render(result)
// cryptographic asset
case types.ClassCrypto:
tw.cryptoRenderer.Render(result)
default:
return
}
+4
View File
@@ -129,6 +129,10 @@ var (
FormatSPDXJSON,
FormatGitHub,
}
CryptoFindingsFormats = []Format{
FormatJSON,
FormatTable,
}
SupportedCompliances = []string{
ComplianceK8sNsa10,
ComplianceK8sCIS123,