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:"-"` diff --git a/services/antivirus/pkg/scanners/clamav.go b/services/antivirus/pkg/scanners/clamav.go index 9af56cc4a9..e9dc3a283e 100644 --- a/services/antivirus/pkg/scanners/clamav.go +++ b/services/antivirus/pkg/scanners/clamav.go @@ -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, diff --git a/services/antivirus/pkg/scanners/clamav_test.go b/services/antivirus/pkg/scanners/clamav_test.go index 9326ff72c9..e6d723c7bc 100644 --- a/services/antivirus/pkg/scanners/clamav_test.go +++ b/services/antivirus/pkg/scanners/clamav_test.go @@ -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() { 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") )