Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dojo/engagement/ui/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ def view_edit_risk_acceptance(request, eid, raid, *, edit_mode=False):
# deleting before instantiating the form otherwise django messes up and we end up with an empty path value
if len(request.FILES) > 0:
logger.debug("new proof uploaded")
risk_acceptance.path.delete()
ra_helper.delete_proof(risk_acceptance)

if "decision" in request.POST:
old_expiration_date = risk_acceptance.expiration_date
Expand Down
11 changes: 10 additions & 1 deletion dojo/risk_acceptance/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,19 @@ def delete(eng, risk_acceptance):
eng.risk_acceptance.remove(risk_acceptance)
eng.save()

risk_acceptance.path.delete()
delete_proof(risk_acceptance)
risk_acceptance.delete()


def delete_proof(risk_acceptance):
proof = risk_acceptance.path
if proof and Risk_Acceptance.objects.filter(path=proof.name).exclude(pk=risk_acceptance.pk).exists():
risk_acceptance.path = None
risk_acceptance.save()
return
proof.delete()


def remove_finding_from_risk_acceptance(user: Dojo_User, risk_acceptance: Risk_Acceptance, finding: Finding) -> None:
logger.debug("removing finding %i from risk acceptance %i", finding.id, risk_acceptance.id)
risk_acceptance.accepted_findings.remove(finding)
Expand Down
6 changes: 5 additions & 1 deletion dojo/risk_acceptance/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

from django.core.files.base import ContentFile
from django.db import models
from django.urls import reverse
from django.utils import timezone
Expand Down Expand Up @@ -102,7 +103,10 @@ def engagement(self):

def copy(self, engagement=None):
from dojo.models import Finding # noqa: PLC0415 -- lazy import, avoids circular dependency
copy = copy_model_util(self)
copy = copy_model_util(self, exclude_fields=["path"])
if self.path and self.path.storage.exists(self.path.name):
with self.path.open("rb") as proof:
copy.path = ContentFile(proof.read(), name=Path(self.path.name).name)
# Save the necessary ManyToMany relationships
old_notes = list(self.notes.all())
old_accepted_findings_hash_codes = [finding.hash_code for finding in self.accepted_findings.all()]
Expand Down
48 changes: 47 additions & 1 deletion unittests/test_copy_model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

import tempfile
from unittest.mock import patch

from django.core.files.base import ContentFile
from django.test import override_settings

from dojo.engagement.services import copy_engagement, reassign_engagement_product_endpoints
from dojo.location.models import Location, LocationFindingReference, LocationProductReference
from dojo.location.status import ProductLocationStatus
from dojo.models import Endpoint, Endpoint_Status, Engagement, Finding, Product, Test, User
from dojo.models import Endpoint, Endpoint_Status, Engagement, Finding, Product, Risk_Acceptance, Test, User
from dojo.risk_acceptance import helper as ra_helper
from dojo.test.services import copy_test
from dojo.url.models import URL

Expand Down Expand Up @@ -393,6 +398,47 @@ def test_duplicate_engagement_with_tags_and_notes(self):
self.assertEqual(engagement.tags, engagement_copy.tags)


class TestCopyRiskAcceptanceProof(DojoTestCase):

def setUp(self):
media = tempfile.TemporaryDirectory()
self.addCleanup(media.cleanup)
media_settings = override_settings(MEDIA_ROOT=media.name)
media_settings.enable()
self.addCleanup(media_settings.disable)
self.user, _ = User.objects.get_or_create(username="admin")
product = self.create_product("ra_proof_product", prod_type=self.create_product_type("ra_proof_type"))
self.engagement = self.create_engagement("ra_proof_eng", product)
self.risk_acceptance = Risk_Acceptance.objects.create(name="ra", owner=self.user)
self.risk_acceptance.path.save("proof.pdf", ContentFile(b"original proof"))
self.engagement.risk_acceptance.add(self.risk_acceptance)

def read(self, risk_acceptance):
with risk_acceptance.path.open("rb") as proof:
return proof.read()

def test_copy_stores_its_own_proof_file(self):
engagement_copy = self.engagement.copy()
copied = engagement_copy.risk_acceptance.get()
self.assertNotEqual(self.risk_acceptance.path.name, copied.path.name)
self.assertEqual(b"original proof", self.read(copied))

ra_helper.delete(engagement_copy, copied)
self.assertTrue(self.risk_acceptance.path.storage.exists(self.risk_acceptance.path.name))
self.assertEqual(b"original proof", self.read(self.risk_acceptance))

def test_delete_keeps_a_proof_file_another_acceptance_names(self):
shared = Risk_Acceptance.objects.create(name="shared", owner=self.user, path=self.risk_acceptance.path.name)
self.engagement.risk_acceptance.add(shared)

ra_helper.delete(self.engagement, shared)
self.assertEqual(b"original proof", self.read(self.risk_acceptance))

name = self.risk_acceptance.path.name
ra_helper.delete(self.engagement, self.risk_acceptance)
self.assertFalse(self.risk_acceptance.path.storage.exists(name))


class TestCopyEngagementService(DojoTestCase):

"""Phase 2: the copy_engagement service holds the copy workflow extracted from the UI view."""
Expand Down
Loading