Skip to content

Commit 797b405

Browse files
xovishnukosurivishnuclawd-007
andauthored
fix: close unclosed file handles in views, management commands (#14446)
Co-authored-by: vishnuclawd-007 <[email protected]>
1 parent 80cf277 commit 797b405

4 files changed

Lines changed: 38 additions & 31 deletions

File tree

‎dojo/api_v2/views.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,17 @@ def download_proof(self, request, pk=None):
777777
)
778778
# Get the path of the file in media root
779779
file_path = Path(settings.MEDIA_ROOT) / file_object.name
780+
# NOTE: FileResponse takes ownership of closing the file handle when the response is closed.
781+
# Explicitly register the closer to avoid potential resource leaks and satisfy static analyzers.
780782
file_handle = file_path.open("rb")
781783
# send file
782784
response = FileResponse(
783785
file_handle,
784786
content_type=mimetypes.guess_type(str(file_path))[0] or "application/octet-stream",
785787
status=status.HTTP_200_OK,
786788
)
789+
if hasattr(response, "_resource_closers"):
790+
response._resource_closers.append(file_handle.close)
787791
response["Content-Length"] = file_object.size
788792
response[
789793
"Content-Disposition"

‎dojo/engagement/views.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,9 +1578,10 @@ def download_risk_acceptance(request, eid, raid):
15781578
# Ensure the risk acceptance is under the supplied engagement
15791579
if not Engagement.objects.filter(risk_acceptance=risk_acceptance, id=eid).exists():
15801580
raise PermissionDenied
1581-
response = StreamingHttpResponse(
1582-
FileIterWrapper(
1583-
(Path(settings.MEDIA_ROOT) / "risk_acceptance.path.name").open(mode="rb")))
1581+
file_handle = (Path(settings.MEDIA_ROOT) / "risk_acceptance.path.name").open(mode="rb")
1582+
response = StreamingHttpResponse(FileIterWrapper(file_handle))
1583+
if hasattr(response, "_resource_closers"):
1584+
response._resource_closers.append(file_handle.close)
15841585
response["Content-Disposition"] = f'attachment; filename="{risk_acceptance.filename()}"'
15851586
mimetype, _encoding = mimetypes.guess_type(risk_acceptance.path.name)
15861587
response["Content-Type"] = mimetype or "application/octet-stream"

‎dojo/management/commands/csv_findings_export.py‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,31 @@ def handle(self, *args, **options):
2323
findings = Finding.objects.filter(verified=True,
2424
active=True).select_related(
2525
"test__engagement__product")
26-
writer = csv.writer(file_path.open("w", encoding="utf-8"))
27-
28-
headers = [
29-
"product_name",
30-
"id",
31-
"title",
32-
"cwe",
33-
"date",
34-
"url",
35-
"severity",
36-
]
37-
38-
# for field in opts.fields:
39-
# headers.append(field.name)
40-
41-
writer.writerow(headers)
42-
for obj in findings:
43-
row = []
44-
row.append(obj.test.engagement.product)
45-
for field in headers:
46-
if field != "product_name":
47-
value = getattr(obj, field)
48-
if isinstance(value, str):
49-
value = value.encode("utf-8").strip()
50-
51-
row.append(value)
52-
writer.writerow(row)
26+
with file_path.open("w", encoding="utf-8", newline="") as fout:
27+
writer = csv.writer(fout)
28+
29+
headers = [
30+
"product_name",
31+
"id",
32+
"title",
33+
"cwe",
34+
"date",
35+
"url",
36+
"severity",
37+
]
38+
39+
# for field in opts.fields:
40+
# headers.append(field.name)
41+
42+
writer.writerow(headers)
43+
for obj in findings:
44+
row = []
45+
row.append(obj.test.engagement.product)
46+
for field in headers:
47+
if field != "product_name":
48+
value = getattr(obj, field)
49+
if isinstance(value, str):
50+
value = value.encode("utf-8").strip()
51+
52+
row.append(value)
53+
writer.writerow(row)

‎dojo/management/commands/import_surveys.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def handle(self, *args, **options):
2424
ctype_id = row[0]
2525
# Find the current id in the surveys file
2626
path = Path(__file__).parent.parent.parent / "fixtures" / "initial_surveys.json"
27-
contents = path.open(encoding="utf-8").readlines()
27+
with path.open(encoding="utf-8") as fin:
28+
contents = fin.readlines()
2829
for line in contents:
2930
if '"polymorphic_ctype": ' in line:
3031
matchedLine = line

0 commit comments

Comments
 (0)