Skip to content

Commit

Permalink
Fix open redirect vulnerability in toggle_lang function
Browse files Browse the repository at this point in the history
- Add URL validation using Django's url_has_allowed_host_and_scheme
- Ensure redirects are only to allowed hosts
- Fallback to home page for potentially malicious redirects
- Maintain HTTPS when in use
  • Loading branch information
ragno-typhojem authored Nov 12, 2024
1 parent 1c1b2d2 commit 1b4bddb
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions _website/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.shortcuts import render, redirect, get_object_or_404
from django.http import Http404, FileResponse
from django.utils import translation

from django.utils.http import url_has_allowed_host_and_scheme
from django.conf import settings
from .models import *

def generate_menu_context(request):
Expand Down Expand Up @@ -112,10 +113,19 @@ def faq_view(request):
'contest_languages': contest_languages,
})


def toggle_lang(request):
lang = 'tr' if translation.get_language() == 'en' else 'en'

request.session[translation.LANGUAGE_SESSION_KEY] = lang

next_url = request.GET.get('next', '/')

# 12.11.2024
if not url_has_allowed_host_and_scheme(
url=next_url,
allowed_hosts={request.get_host()},
require_https=request.is_secure()
):
next_url = '/'

return redirect(next_url)

return redirect(request.GET.get('next', '/'))

0 comments on commit 1b4bddb

Please sign in to comment.