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.

https://cs.opensource.google/go/go/+/refs/tags/go1.23.1:src/reflect/value.go;drc=5fee159bc2e60736ce967560ee5be738fe5d5bd2;l=336-343

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

  1. We just add into the docs this case, and don’t do anything.

  2. We convert non-addressable fields with Marshal function into pointers. It makes these ones addressable. Note: it’ll take some efforts. For example, Status struct describes in trivy-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)

Most upvoted comments

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 int

thank you for any help, Peter

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 int

thank 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.

What about user templates? Will we support templates? Or will we ask users to migrate to plugins and deprecate the template format?

I think we can continue to support template functionality, but not maintain templates in Trivy’s tree.

I opened a PR. aquasecurity/trivy-db#436

In your example, toPrettyJson is called on each DetectedVulnerability, and that’s the reason why Status is not marshalled correctly.

It fixed the problem in my environment.

oh, you found out the root issue. that’s cool!

I opened a PR. https://github.com/aquasecurity/trivy-db/pull/436

In your example, toPrettyJson is called on each DetectedVulnerability, and that’s the reason why Status is not marshalled correctly.

It fixed the problem in my environment.

./trivy convert --format template --template '{{ range . }}{{ range .Vulnerabilities }}{{ toPrettyJson . }}{{ end }}{{ end }}' report.json | grep Status | head -n 1
  "Status": "fixed",