trivy: bug(template): incorrect JSON marshaling for some fields
Problem
There are cases when a custom template doesn’t covert some result fields into JSON.
Trivy doesn’t call custom marshal functions:
Status doesn’t become string, PURL for PkgIdentifier is empty.
Reproduction steps:
$ trivy image --format json --output report.json alpine:3.20.2
$ trivy convert --format template --template '{{ range . }}{{ toPrettyJson . }}{{ end }}' report.json | grep Status | head -n 1
"Status": "fixed",
$ trivy convert --format template --template '{{ range . }}{{ range .Vulnerabilities }}{{ toPrettyJson . }}{{ end }}{{ end }}' report.json | grep Status | head -n 1
"Status": 3,
Reason
toPrettyJson from sprig uses a standart JSON marshal function - json.MarshalIndent:
https://github.com/Masterminds/sprig/blob/e708470d529a10ac1a3f02ab6fdd339b65958372/defaults.go#L122-L124
Go JSON uses relection and reflect library marks some values as addressable:
// reports whether the value's address can be obtained with [Value.Addr].
// Such values are called addressable. A value is addressable if it is
// an element of a slice, an element of an addressable array,
// a field of an addressable struct, or the result of dereferencing a pointer.
// If CanAddr returns false, calling [Value.Addr] will panic.
Go uses different encoders for addressable and non-addressable values:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.1:src/encoding/json/encode.go;l=900-906
For non-addressable values encoder doesn’t call marshall function, so Status and PURL aren’t converted.
Solutions
-
We just add into the docs this case, and don’t do anything.
-
We convert
non-addressablefields withMarshalfunction intopointers. It makes these onesaddressable. Note: it’ll take some efforts. For example,Statusstruct describes intrivy-db.
Related PRs
this issue also affects on #7463.
Discussed in https://github.com/aquasecurity/trivy/discussions/7448
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 15 (7 by maintainers)
I’m also seeing this happen to my build agents now starting within the last few hours
can be this related also to this bug? suddenly after few months the trivy 0.53.0 started to fail on umarshaling the filed of Ubuntu scan ending with following error:
Fatal error image scan error: scan error: scan failed: scan failed: failed to detect vulnerabilities: unable to scan OS packages: failed vulnerability detection of OS packages: failed detection: failed to get Ubuntu advisories: failed to get Ubuntu advisories: failed to unmarshal advisory JSON: json: cannot unmarshal string into Go struct field dbAdvisory.Status of type intthank you for any help, Peter
@eissko @Altourus No, it’s not relevant and it was fixed. https://github.com/aquasecurity/trivy/discussions/7481
If the problem still persists, please try
trivy clean --vuln-db.I think we can continue to support template functionality, but not maintain templates in Trivy’s tree.
oh, you found out the root issue. that’s cool!
Also, I’ll fix https://github.com/aquasecurity/trivy/blob/5c37361600d922db27dd594b2a80c010a19b3a6e/pkg/fanal/types/package.go#L81
I opened a PR. https://github.com/aquasecurity/trivy-db/pull/436
In your example,
toPrettyJsonis called on eachDetectedVulnerability, and that’s the reason whyStatusis not marshalled correctly.It fixed the problem in my environment.