Skip to content

Commit

Permalink
Refactor redirect URL on deletion of Federal Awards Audit Findings (#…
Browse files Browse the repository at this point in the history
…4386)

* Hardcode redirect path

* Validate redirect_uri

- Created new method in `Util` class to return a redirect based on whether or not the `url` is valid and safe.

* Lint

* Linting again

* One more lint. Hopefully
  • Loading branch information
rnovak338 authored Oct 16, 2024
1 parent d3627f2 commit c4eb9c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 10 additions & 0 deletions backend/audit/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.conf import settings
from django.shortcuts import redirect
from django.utils.http import url_has_allowed_host_and_scheme

from audit.fixtures.excel import FORM_SECTIONS
from audit.intakelib import (
Expand Down Expand Up @@ -72,6 +74,14 @@ def remove_extra_fields(general_information_data):
general_information_data.pop("audit_period_other_months", None)
return general_information_data

@staticmethod
def validate_redirect_url(url):
"""Ensure that the url received is safe to redirect to."""
if url_has_allowed_host_and_scheme(url, allowed_hosts=None):
return redirect(url)
else:
return redirect("/")


class ExcelExtractionError(Exception):
def __init__(
Expand Down
14 changes: 8 additions & 6 deletions backend/report_submission/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,16 @@ def get(self, request, *args, **kwargs):

def post(self, request, *args, **kwargs):
report_id = kwargs["report_id"]
path_name = request.path.split("/")[2]
section = self.additional_context[path_name]
redirect_uri = f"/report_submission/{section['view_id']}/{report_id}"
try:
sac = SingleAuditChecklist.objects.get(report_id=report_id)
accesses = Access.objects.filter(sac=sac, user=request.user)

if not accesses:
messages.error(request, "You do not have access to this audit.")
return redirect(request.path)
path_name = request.path.split("/")[2]
section = self.additional_context[path_name]
return Util.validate_redirect_url(redirect_uri)

try:
excel_files = ExcelFile.objects.filter(
Expand All @@ -597,7 +599,7 @@ def post(self, request, *args, **kwargs):
sac.save()
except ExcelFile.DoesNotExist:
messages.error(request, "File not found.")
return redirect(request.path)
return Util.validate_redirect_url(redirect_uri)

SubmissionEvent.objects.create(
sac_id=sac.id,
Expand All @@ -611,9 +613,9 @@ def post(self, request, *args, **kwargs):
except SingleAuditChecklist.DoesNotExist:
logger.error(f"Audit: {report_id} not found")
messages.error(request, "Audit not found.")
return redirect(request.path)
return Util.validate_redirect_url(redirect_uri)

except Exception as e:
logger.error(f"Unexpected error in DeleteFileView post: {str(e)}")
messages.error(request, "An unexpected error occurred.")
return redirect(request.path)
return Util.validate_redirect_url(redirect_uri)

0 comments on commit c4eb9c1

Please sign in to comment.