Skip to content

Commit 90736a8

Browse files
Add query params to login redirect (#129)
* add query params to login redirect * fix pipeline and tests * version bump to 1.1.0
1 parent 0d51ccd commit 90736a8

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

.github/workflows/validate.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ on:
66
- develop
77
- master
88
- main
9-
- 'release/**'
9+
- "release/**"
1010
pull_request:
1111
branches:
12-
- '*'
12+
- "*"
1313
workflow_dispatch:
1414

1515
jobs:
@@ -34,4 +34,4 @@ jobs:
3434
- name: Security
3535
run: make bandit
3636
- name: Testing
37-
run: make tests
37+
run: make test

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-security"
3-
version = "1.0.7"
3+
version = "1.1.0"
44
homepage = "https://github.com/sdelements/django-security"
55
description = "Models, views, middlewares and forms to facilitate security hardening of Django applications."
66
authors = ["Security Compass <[email protected]>"]

security/middleware.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import logging
55
import pstats
6+
from urllib.parse import quote_plus
67
import warnings
78
from io import StringIO
89
from re import compile
@@ -1058,6 +1059,8 @@ def process_request(self, request):
10581059
else:
10591060
login_url = self.login_url
10601061
next_url = request.path
1062+
if len(request.META["QUERY_STRING"]):
1063+
next_url += quote_plus("?" + request.META["QUERY_STRING"])
10611064

10621065
if request.headers.get("x-requested-with") == "XMLHttpRequest":
10631066
return HttpResponse(

tests/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
"security.middleware.ProfilingMiddleware",
5151
)
5252

53+
ENABLE_PROFILING = True
54+
5355
ROOT_URLCONF = "tests.urls"
5456

5557
TEMPLATES = [

tests/tests.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import json
33
import time # We monkeypatch this.
4+
from urllib.parse import quote_plus
45

56
from django.conf import settings
67
from django.contrib.auth import logout
@@ -16,16 +17,23 @@
1617

1718
from security.auth import min_length
1819
from security.auth_throttling import Middleware as AuthThrottlingMiddleware
19-
from security.auth_throttling import (attempt_count, default_delay_function,
20-
delay_message, increment_counters,
21-
reset_counters, throttling_delay)
22-
from security.middleware import (BaseMiddleware,
23-
ContentSecurityPolicyMiddleware,
24-
DoNotTrackMiddleware,
25-
MandatoryPasswordChangeMiddleware,
26-
ReferrerPolicyMiddleware,
27-
SessionExpiryPolicyMiddleware,
28-
XFrameOptionsMiddleware)
20+
from security.auth_throttling import (
21+
attempt_count,
22+
default_delay_function,
23+
delay_message,
24+
increment_counters,
25+
reset_counters,
26+
throttling_delay,
27+
)
28+
from security.middleware import (
29+
BaseMiddleware,
30+
ContentSecurityPolicyMiddleware,
31+
DoNotTrackMiddleware,
32+
MandatoryPasswordChangeMiddleware,
33+
ReferrerPolicyMiddleware,
34+
SessionExpiryPolicyMiddleware,
35+
XFrameOptionsMiddleware,
36+
)
2937
from security.models import PasswordExpiry
3038
from security.password_expiry import never_expire_password
3139
from security.views import csp_report, require_ajax
@@ -152,6 +160,14 @@ def test_redirects_unauthenticated_request(self):
152160
response = self.client.get("/home/")
153161
self.assertRedirects(response, self.login_url + "?next=/home/")
154162

163+
def test_redirects_unauthenticated_request_to_custom_path_with_query_url(self):
164+
path = "/custom-path/"
165+
query = "priority=7,8,9,10&type=Custom"
166+
response = self.client.get(f"{path}?{query}")
167+
168+
expected_redirect = f"{self.login_url}?next={path}{quote_plus("?" + query)}"
169+
self.assertRedirects(response, expected_redirect)
170+
155171
def test_redirects_unauthenticated_ajax_request(self):
156172
response = self.client.get(
157173
"/home/",

0 commit comments

Comments
 (0)