def validate_file_infection(file): # Taken from: https://github.com/vstoykov/django-clamd # with a small change that will prevent failure when clamd is not running # If django-clamd is disabled (for debugging) then do not check the file. # Ensure file pointer is at begingin of the file file.seek(0) try: scanner = ClamdUnixSocket(settings.CLAMD_SOCKET) result = scanner.instream(file) except ConnectionError: logger.warn('Clamav connection failed') return except IOError: # Ping the server if it fails than the server is down scanner.ping() # Server is up. This means that the file is too big. logger.warn( 'The file is too large for ClamD to scan it. Bytes Read {}'.format( file.tell())) file.seek(0) return if result and result['stream'][0] == 'FOUND': raise ValidationError(_('File is infected with malware.'), code='infected') # Return file pointer at initial state file.seek(0)
def get_client(self): if ':' not in self.addr: return ClamdUnixSocket(path=self.addr) host, port = self.addr.split(':') return ClamdNetworkSocket(host=host, port=int(port), timeout=self.timeout)
"""""" import io import logging import os import pathlib from abilian.core.models.blob import Blob from ..base import Service logger = logging.getLogger(__name__) try: from clamd import ClamdUnixSocket clamd = ClamdUnixSocket() except ImportError: clamd = None CLAMD_CONF = {"StreamMaxLength": "25M", "MaxFileSize": "25M"} CLAMD_STREAMMAXLENGTH = 26214400 CLAMD_MAXFILESIZE = 26214400 if clamd: conf_path = pathlib.Path("/etc", "clamav", "clamd.conf") if conf_path.exists(): conf_lines = [ line.strip() for line in conf_path.open("rt").readlines() ] CLAMD_CONF = dict( line.split(" ", 1) for line in conf_lines