Skip to content

Fix broken RSS links #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2024
Merged
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
11 changes: 8 additions & 3 deletions DjangoPlugin/tracdjangoplugin/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,14 @@ def process_request(self, req):
raise RequestDone

def do_get(self, req):
# Not 100% sure why, but for some links (RSS especially) Trac likes
# to generate URLs pointing to `/login?referer=<the actual link>` when
# the user is already authenticated.
if req.is_authenticated:
req.redirect(self._get_safe_redirect_url(req))
return "plainlogin.html", {
"form": AuthenticationForm(),
"next": req.args.get("next", ""),
"referer": req.args.get("referer", ""),
}

def do_post(self, req):
Expand All @@ -132,11 +137,11 @@ def do_post(self, req):
req.environ["REMOTE_USER"] = form.get_user().username
LoginModule(self.compmgr)._do_login(req)
req.redirect(self._get_safe_redirect_url(req))
return "plainlogin.html", {"form": form, "next": req.args.get("next", "")}
return "plainlogin.html", {"form": form, "referer": req.args.get("referer", "")}

def _get_safe_redirect_url(self, req):
host = urlparse(req.base_url).hostname
redirect_url = iri_to_uri(req.args.get("next", ""))
redirect_url = iri_to_uri(req.args.get("referer", ""))

if not redirect_url:
redirect_url = settings.LOGIN_REDIRECT_URL
Expand Down
20 changes: 17 additions & 3 deletions DjangoPlugin/tracdjangoplugin/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_login_valid_with_custom_redirection(self):
username="test",
password="test",
check_redirect="/test",
extra_data={"next": "/test"},
extra_data={"referer": "/test"},
)

def test_login_valid_with_custom_redirection_with_hostname(self):
Expand All @@ -83,7 +83,7 @@ def test_login_valid_with_custom_redirection_with_hostname(self):
username="test",
password="test",
check_redirect="http://localhost/test",
extra_data={"next": "http://localhost/test"},
extra_data={"referer": "http://localhost/test"},
)

def test_login_valid_with_malicious_redirection(self):
Expand All @@ -108,7 +108,7 @@ def test_login_valid_with_malicious_redirection(self):
username="test",
password="test",
check_redirect="http://localhost/test",
extra_data={"next": redirect_url},
extra_data={"referer": redirect_url},
)

def assertLoginFails(self, username, password, error_message=None):
Expand Down Expand Up @@ -149,6 +149,20 @@ def test_login_invalid_inactive_user(self):
User.objects.create_user(username="test", password="test", is_active=False)
self.assertLoginFails(username="test", password="test")

def test_login_page_redirects_if_already_logged_in(self):
self.env.config.set("trac", "base_url", "")
request = self.request_factory(
method="GET",
path_info="/login",
args={"referer": "/test"},
authname="admin",
)

with self.assertRaises(RequestDone):
self.component.process_request(request)

self.assertEqual(request.headers_sent["Location"], "/test")


class DjangoDBManagementMiddlewareTestCase(SimpleTestCase):
@classmethod
Expand Down
2 changes: 1 addition & 1 deletion trac-env/templates/django_theme.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</form>
</li>
# else
<li><a href="/login?next=${req.path_info|urlencode()}">Login</a></li>
<li><a href="/login?referer=${req.path_info|urlencode()}">Login</a></li>
# endif
<li><a href="${req.href.prefs()}">Preferences</a></li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion trac-env/templates/plainlogin.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h2>Log in with your DjangoProject account</h2>
<p>
<button type="submit">Log in with DjangoProject</button>
<input type="hidden" name="__FORM_TOKEN" value="${req.form_token}">{# Trac's CSRF protection #}
<input type="hidden" name="next" value="${next|default('/')}">
<input type="hidden" name="referer" value="${referer|default('/')}">
</p>
</form>
</section>
Expand Down
Loading