From abb00f01c9fcad9e41d104b055fa94a09401675a Mon Sep 17 00:00:00 2001 From: Ryo Kusnadi Date: Wed, 4 Nov 2026 05:18:59 +0700 Subject: [PATCH 1/3] fix(antivirus): treat a clamd ERROR reply as a failed scan Signed-off-by: Ryo Kusnadi --- services/antivirus/pkg/scanners/clamav.go | 6 ++++++ .../antivirus/pkg/scanners/clamav_test.go | 21 +++++++++++++++++++ services/antivirus/pkg/scanners/scanners.go | 2 ++ 3 files changed, 29 insertions(+) diff --git a/services/antivirus/pkg/scanners/clamav.go b/services/antivirus/pkg/scanners/clamav.go index 9af56cc4a9..f8db072878 100644 --- a/services/antivirus/pkg/scanners/clamav.go +++ b/services/antivirus/pkg/scanners/clamav.go @@ -42,6 +42,12 @@ func (s ClamAV) Scan(in Input) (Result, error) { abort <- true return Result{}, fmt.Errorf("%w: %s", ErrScanTimeout, in.Url) case s := <-ch: + // only a definite answer counts: anything but OK or FOUND (ERROR, + // PARSE ERROR, ...) means the scan did not finish, so it must not pass as clean. + if s.Status != clamd.RES_OK && s.Status != clamd.RES_FOUND { + return Result{}, fmt.Errorf("%w: %s: %s", ErrScanFailed, s.Status, s.Description) + } + return Result{ Infected: s.Status == clamd.RES_FOUND, Description: s.Description, diff --git a/services/antivirus/pkg/scanners/clamav_test.go b/services/antivirus/pkg/scanners/clamav_test.go index 9326ff72c9..fa74aa0cce 100644 --- a/services/antivirus/pkg/scanners/clamav_test.go +++ b/services/antivirus/pkg/scanners/clamav_test.go @@ -95,6 +95,27 @@ func TestNewClamAV_Scan(t *testing.T) { assert.True(t, <-done) }) + t.Run("returns an error if clamd answers with an error", func(t *testing.T) { + for name, reply := range map[string]string{ + "ERROR": "stream: Can't write to file ERROR\n", + "PARSE ERROR": "stream: Some parse problem PARSE ERROR\n", + } { + t.Run(name, func(t *testing.T) { + ul := newUnixListener(t, net.ListenConfig{}, "PONG\n", reply) + defer func() { + assert.NoError(t, ul.Close()) + }() + + scanner, err := scanners.NewClamAV(ul.Addr().String(), 10*time.Second) + require.NoError(t, err) + + result, err := scanner.Scan(scanners.Input{Body: strings.NewReader("DATA")}) + assert.ErrorIs(t, err, scanners.ErrScanFailed) + assert.False(t, result.Infected) + }) + } + }) + t.Run("aborts after a certain time", func(t *testing.T) { ul := newUnixListener(t, net.ListenConfig{}, "PONG\n", "stream: Win.Test.EICAR_HDB-1 FOUND\n") defer func() { diff --git a/services/antivirus/pkg/scanners/scanners.go b/services/antivirus/pkg/scanners/scanners.go index 223bd4fc1d..d3df797371 100644 --- a/services/antivirus/pkg/scanners/scanners.go +++ b/services/antivirus/pkg/scanners/scanners.go @@ -9,6 +9,8 @@ import ( var ( // ErrScanTimeout is returned when a scan times out ErrScanTimeout = errors.New("time out waiting for clamav to respond while scanning") + // ErrScanFailed is returned when the scanner answers with an error instead of a verdict + ErrScanFailed = errors.New("scanner failed to scan the file") // ErrScannerNotReachable is returned when the scanner is not reachable ErrScannerNotReachable = errors.New("failed to reach the scanner") ) From fabb775d17542d510307369884d26025ae784536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Duffeck?= Date: Wed, 11 Nov 2026 10:12:08 +0100 Subject: [PATCH 2/3] Fix panic when clamd hangs up without a scan result --- services/antivirus/pkg/scanners/clamav.go | 7 ++++++- services/antivirus/pkg/scanners/clamav_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/services/antivirus/pkg/scanners/clamav.go b/services/antivirus/pkg/scanners/clamav.go index f8db072878..e9dc3a283e 100644 --- a/services/antivirus/pkg/scanners/clamav.go +++ b/services/antivirus/pkg/scanners/clamav.go @@ -41,7 +41,12 @@ func (s ClamAV) Scan(in Input) (Result, error) { case <-time.After(s.timeout): abort <- true return Result{}, fmt.Errorf("%w: %s", ErrScanTimeout, in.Url) - case s := <-ch: + case s, ok := <-ch: + // go-clamd closes the channel without a result when clamd hangs up without replying + if !ok || s == nil { + return Result{}, fmt.Errorf("%w: no reply from clamd", ErrScanFailed) + } + // only a definite answer counts: anything but OK or FOUND (ERROR, // PARSE ERROR, ...) means the scan did not finish, so it must not pass as clean. if s.Status != clamd.RES_OK && s.Status != clamd.RES_FOUND { diff --git a/services/antivirus/pkg/scanners/clamav_test.go b/services/antivirus/pkg/scanners/clamav_test.go index fa74aa0cce..e6d723c7bc 100644 --- a/services/antivirus/pkg/scanners/clamav_test.go +++ b/services/antivirus/pkg/scanners/clamav_test.go @@ -116,6 +116,19 @@ func TestNewClamAV_Scan(t *testing.T) { } }) + t.Run("returns an error if clamd closes the connection without replying", func(t *testing.T) { + ul := newUnixListener(t, net.ListenConfig{}, "PONG\n", "") + defer func() { + assert.NoError(t, ul.Close()) + }() + + scanner, err := scanners.NewClamAV(ul.Addr().String(), 10*time.Second) + require.NoError(t, err) + + _, err = scanner.Scan(scanners.Input{Body: strings.NewReader("DATA")}) + assert.ErrorIs(t, err, scanners.ErrScanFailed) + }) + t.Run("aborts after a certain time", func(t *testing.T) { ul := newUnixListener(t, net.ListenConfig{}, "PONG\n", "stream: Win.Test.EICAR_HDB-1 FOUND\n") defer func() { From 6ef16a956948069d5b7a6f62c0324704c06c55d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Duffeck?= Date: Wed, 11 Nov 2026 11:11:02 +0100 Subject: [PATCH 3/3] Document that the max scan size must not exceed clamd's StreamMaxLength --- services/antivirus/pkg/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/antivirus/pkg/config/config.go b/services/antivirus/pkg/config/config.go index 32f89eb0b5..87288eab21 100644 --- a/services/antivirus/pkg/config/config.go +++ b/services/antivirus/pkg/config/config.go @@ -41,7 +41,7 @@ type Config struct { Workers int `yaml:"workers" env:"ANTIVIRUS_WORKERS" desc:"The number of concurrent go routines that fetch events from the event queue." introductionVersion:"1.0.0"` Scanner Scanner - MaxScanSize string `yaml:"max-scan-size" env:"ANTIVIRUS_MAX_SCAN_SIZE" desc:"The maximum scan size the virus scanner can handle.0 means unlimited. Usable common abbreviations: [KB, KiB, MB, MiB, GB, GiB, TB, TiB, PB, PiB, EB, EiB], example: 2GB." introductionVersion:"1.0.0"` + MaxScanSize string `yaml:"max-scan-size" env:"ANTIVIRUS_MAX_SCAN_SIZE" desc:"The maximum scan size the virus scanner can handle. 0 means unlimited. When using ClamAV, keep it at or below clamd's StreamMaxLength, otherwise clamd refuses bigger files and their uploads fail after the postprocessing retries. Usable common abbreviations: [KB, KiB, MB, MiB, GB, GiB, TB, TiB, PB, PiB, EB, EiB], example: 2GB." introductionVersion:"1.0.0"` MaxScanSizeMode MaxScanSizeMode `yaml:"max-scan-size-mode" env:"ANTIVIRUS_MAX_SCAN_SIZE_MODE" desc:"Defines the mode of handling files that exceed the maximum scan size. Supported options are: 'skip', which skips files that are bigger than the max scan size, and 'truncate' (default), which only uses the file up to the max size." introductionVersion:"2.1.0"` Context context.Context `json:"-" yaml:"-"`