Skip to content

Commit ec74864

Browse files
authored
fix: ClamAV scan crash on codebase root directory (#2190)
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 6db790c commit ec74864

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ run:
3232
@echo "-> Run the Docker compose services in dev mode (hot reload on code changes)"
3333
${COMPOSE} up
3434

35+
start:
36+
@echo "-> Start the Docker compose services in background"
37+
${COMPOSE} up -d
38+
3539
bash:
3640
# Open a bash session in the running web container
3741
${COMPOSE} exec web bash
@@ -219,4 +223,4 @@ offline-package: docker-images
219223
@mkdir -p dist/
220224
@tar -cf dist/scancodeio-offline-package-`git describe --tags`.tar build/
221225

222-
.PHONY: virtualenv conf dev envfile install doc8 check valid check-deploy clean migrate lock makemigrations restart-worker postgresdb sqlitedb backupdb run test fasttest regen-fixtures fix docs build build-core build-full bash shell docker-images offline-package
226+
.PHONY: virtualenv conf dev envfile install doc8 check valid check-deploy clean migrate lock makemigrations restart-worker postgresdb sqlitedb backupdb run test fasttest regen-fixtures fix docs build build-core build-full bash start shell docker-images offline-package

scanpipe/pipes/clamav.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
2121
# Visit https://github.com/aboutcode-org/scancode.io for support and download.
2222

23+
import logging
2324
from pathlib import Path
2425

2526
from django.conf import settings
2627

2728
import clamd
2829

30+
logger = logging.getLogger("scanpipe.pipes")
31+
2932

3033
def scan_for_virus(project):
3134
"""
@@ -46,11 +49,17 @@ def scan_for_virus(project):
4649
except clamd.ClamdError as e:
4750
raise Exception(f"Error with the ClamAV service: {e}")
4851

52+
infected_count = 0
4953
for resource_location, results in scan_response.items():
5054
status, reason = results
55+
5156
resource_path = Path(resource_location).relative_to(project.codebase_path)
57+
if str(resource_path) == ".":
58+
# ClamAV reports the codebase directory itself, rather than a file,
59+
# as a summary entry when the scan found nothing to flag.
60+
continue
5261

53-
resource = project.codebaseresources.get(path=resource_path)
62+
resource = project.codebaseresources.get(path=str(resource_path))
5463
virus_report = {
5564
"clamav": {
5665
"status": status,
@@ -68,3 +77,6 @@ def scan_for_virus(project):
6877
"resource_path": str(resource_path),
6978
},
7079
)
80+
infected_count += 1
81+
82+
logger.info(f"ClamAV scan completed: {infected_count} infected")

scanpipe/tests/pipes/test_clamav.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,18 @@ def test_scanpipe_pipes_clamav_scan_for_virus(self, mock_multiscan):
6767
}
6868
}
6969
self.assertEqual(expected_virus_report_extra_data, resource1.extra_data)
70+
71+
@mock.patch("clamd.ClamdNetworkSocket.multiscan")
72+
def test_scanpipe_pipes_clamav_scan_for_virus_no_detection(self, mock_multiscan):
73+
project = Project.objects.create(name="project")
74+
make_resource_file(project=project, path="file.txt")
75+
76+
mock_multiscan.return_value = {
77+
str(project.codebase_path): ("OK", None),
78+
}
79+
80+
clamav.scan_for_virus(project)
81+
self.assertEqual(0, len(project.projectmessages.all()))
82+
83+
resource = project.codebaseresources.first()
84+
self.assertEqual({}, resource.extra_data)

0 commit comments

Comments
 (0)