Skip to content

Commit

Permalink
add clean_next_url method to reduce mitigate url redirection from rem…
Browse files Browse the repository at this point in the history
…ote source risks
  • Loading branch information
vincentporte committed Nov 14, 2024
1 parent 75020d6 commit 542c9b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lacommunaute/utils/tests/tests_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest
from django.urls import NoReverseMatch, clear_url_caches, reverse

from lacommunaute.utils.urls import clean_next_url


@pytest.fixture(autouse=True)
def _clear_url_caches():
Expand All @@ -22,3 +24,10 @@ def test_django_urls_prod(settings):
reverse("login")
with pytest.raises(NoReverseMatch):
reverse("djdt:render_panel")


@pytest.mark.parametrize(
"url, expected", [(None, "/"), ("http://www.unallowed.com", "/"), ("/", "/"), ("/topics/", "/topics/")]
)
def test_clean_next_url(url, expected):
assert clean_next_url(url) == expected
6 changes: 6 additions & 0 deletions lacommunaute/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ def get_safe_url(request, param_name=None, fallback_url=None, url=None):
return url

return fallback_url


def clean_next_url(url):
if not url_has_allowed_host_and_scheme(url, allowed_hosts=settings.ALLOWED_HOSTS):
return "/"
return url

0 comments on commit 542c9b8

Please sign in to comment.