Merge pull request #3959 from aduffeck/fix/antivirus-clamd-error-response

Don't treat clamd scan errors as clean
This commit is contained in:
Andre Duffeck authored and GitHub committed 2026-11-12 09:30:07 +01:00
commit 5e1db08af6
4 files changed
+49 -2

No files matched your search

+1 -1
View File
@@ -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:"-"`
+12 -1
View File
@@ -41,7 +41,18 @@ 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 {
return Result{}, fmt.Errorf("%w: %s: %s", ErrScanFailed, s.Status, s.Description)
}
return Result{
Infected: s.Status == clamd.RES_FOUND,
Description: s.Description,
@@ -95,6 +95,40 @@ 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("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() {
@@ -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")
)