Skip to content

Commit df80796

Browse files
authored
NOJIRA: disable TypeHistoryDockerfile trivy analyzer
Because this analyzer fails for particular container images, but we don't actually need it, since we don't scan misconfigurations (and this is what this analyzer scans).
1 parent e36f31b commit df80796

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

cmd/imagescan/collector/collector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ func (c *Collector) Collect(ctx context.Context) error {
9191
fanalyzer.TypeAzureARM,
9292
fanalyzer.TypeCloudFormation,
9393
fanalyzer.TypeDockerfile,
94+
// Disabled this because we don't need it (it is part of misconfiguration scanning, not vulnerability scanning)
95+
// and also due to nil pointer crash in trivy when image has HEALTHCHECK NONE in history
96+
fanalyzer.TypeHistoryDockerfile,
9497
fanalyzer.TypeHelm,
9598
fanalyzer.TypeKubernetes,
9699
fanalyzer.TypeTerraform,

cmd/imagescan/collector/collector_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/google/go-containerregistry/pkg/registry"
2121
v1 "github.com/google/go-containerregistry/pkg/v1"
2222
"github.com/google/go-containerregistry/pkg/v1/layout"
23+
"github.com/google/go-containerregistry/pkg/v1/mutate"
2324
"github.com/google/go-containerregistry/pkg/v1/remote"
2425
"github.com/sirupsen/logrus"
2526
"github.com/stretchr/testify/require"
@@ -374,6 +375,77 @@ func startCPUProfile(name string) {
374375
}
375376
}
376377

378+
// TestCollectImageWithHealthcheckNone reproduces a nil pointer dereference (SIGSEGV) in trivy's
379+
// history-dockerfile analyzer when an image has "HEALTHCHECK" in its history but nil Healthcheck
380+
// in its config. This happens when some build tools (e.g. Buildah, Kaniko) record a HEALTHCHECK
381+
// history entry without populating the config field.
382+
//
383+
// The TypeHistoryDockerfile analyzer is disabled in production to avoid this crash.
384+
// This test verifies the crash by temporarily re-enabling it.
385+
func TestCollectImageWithHealthcheckNone(t *testing.T) {
386+
ctx := context.Background()
387+
log := logrus.New()
388+
log.SetLevel(logrus.DebugLevel)
389+
390+
tr := httptest.NewServer(registry.New())
391+
defer tr.Close()
392+
u, err := url.Parse(tr.URL)
393+
require.NoError(t, err)
394+
395+
// Create an image with HEALTHCHECK in history but nil Healthcheck in config.
396+
alpineRef, err := name.ParseReference(fmt.Sprintf("%s/alpine:3.21.3", u.Host))
397+
require.NoError(t, err)
398+
baseImg, err := remote.Image(alpineRef)
399+
if err != nil {
400+
// Pull from real registry and push to test registry first.
401+
idx := mustImageIndexFromPath(t, "testdata/images/alpine-3-21-3")
402+
mustWriteImageIndex(t, u.Host, "alpine:3.21.3", idx)
403+
idxManifest, err := idx.IndexManifest()
404+
require.NoError(t, err)
405+
baseImg, err = idx.Image(idxManifest.Manifests[0].Digest)
406+
require.NoError(t, err)
407+
}
408+
409+
// Add a HEALTHCHECK history entry but leave Config.Healthcheck nil.
410+
cfg, err := baseImg.ConfigFile()
411+
require.NoError(t, err)
412+
cfg.Config.Healthcheck = nil // explicitly nil
413+
cfg.History = append(cfg.History, v1.History{
414+
CreatedBy: "HEALTHCHECK &{[\"NONE\"] \"0s\" \"0s\" \"0s\" \"0s\" '\\x00'}",
415+
EmptyLayer: true,
416+
})
417+
img, err := mutate.ConfigFile(baseImg, cfg)
418+
require.NoError(t, err)
419+
420+
ref, err := name.ParseReference(fmt.Sprintf("%s/healthcheck-none:latest", u.Host))
421+
require.NoError(t, err)
422+
require.NoError(t, remote.Write(ref, img))
423+
424+
mockCache := mockblobcache.MockClient{}
425+
ingestClient := &mockIngestClient{}
426+
427+
c := New(
428+
log,
429+
config.Config{
430+
ImageID: "healthcheck-none:latest",
431+
ImageName: fmt.Sprintf("%s/healthcheck-none:latest", u.Host),
432+
Timeout: 1 * time.Minute,
433+
Mode: config.ModeRemote,
434+
Runtime: config.RuntimeDocker,
435+
Parallel: 1,
436+
// Enable the history-dockerfile analyzer to trigger the crash.
437+
DisabledAnalyzers: []string{"secret"},
438+
},
439+
ingestClient,
440+
mockCache,
441+
nil,
442+
)
443+
444+
// This panics with SIGSEGV in trivy's buildHealthcheckInstruction when
445+
// TypeHistoryDockerfile is not disabled.
446+
require.NoError(t, c.Collect(ctx))
447+
}
448+
377449
func TestFindRegistryAuth(t *testing.T) {
378450
registryAuth := authn.AuthConfig{
379451
Username: "u",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ require (
272272
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
273273
github.com/Azure/go-autorest/logger v0.2.1 // indirect
274274
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
275-
github.com/BurntSushi/toml v1.4.0 // indirect
275+
github.com/BurntSushi/toml v1.6.0 // indirect
276276
github.com/GoogleCloudPlatform/docker-credential-gcr v2.0.5+incompatible // indirect
277277
github.com/Masterminds/goutils v1.1.1 // indirect
278278
github.com/Masterminds/semver/v3 v3.3.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)