Fix logout 405 — Django's LogoutView only accepts POST

The sidebar's "Log out" was a plain <a href> GET link. Django 5's
LogoutView stopped accepting GET (CSRF hardening — a bare link or even an
<img> tag could otherwise log someone out), so clicking it 405'd instead
of logging out. Changed it to a POST form, matching every other
state-changing action in the sidebar/app (unsubscribe, delete tag, etc.).

Extended .sidebar-top-nav's styling to cover a <button> the same as an
<a>, so the form-submit button looks identical to the other nav items.

No existing test exercised logout at all — added one for the POST case
and one confirming GET now correctly 405s, then verified end-to-end
against the running dev server (login, POST /logout/, confirm the session
is gone).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-30 22:01:06 -06:00
1 parent b08a78e747
commit 1a8ae95db0
3 files changed
+38 -3

No files matched your search

+24
View File
@@ -72,6 +72,30 @@ def test_setup_rejects_mismatched_passwords(client):
assert not User.objects.exists()
@pytest.mark.django_db
def test_logout_via_post_logs_the_user_out(client):
User.objects.create_user("keith", password="pw-keith-123")
client.login(username="keith", password="pw-keith-123")
response = client.post(reverse("accounts:logout"))
assert response.status_code == 302
stream_response = client.get(reverse("reader:stream"))
assert stream_response.status_code == 302
assert stream_response.url.startswith(reverse("accounts:login"))
@pytest.mark.django_db
def test_logout_rejects_get(client):
# Django's LogoutView only accepts POST (see templates/core/_sidebar.html,
# which submits it as a form rather than a plain link).
User.objects.create_user("keith", password="pw-keith-123")
client.login(username="keith", password="pw-keith-123")
response = client.get(reverse("accounts:logout"))
assert response.status_code == 405
@pytest.mark.django_db
def test_non_admin_cannot_manage_users(client):
User.objects.create_user("regular", password="pw-regular-123")
+8 -2
View File
@@ -238,16 +238,22 @@ form p {
padding: 0 0.5rem;
}
.sidebar-top-nav li a {
.sidebar-top-nav li a,
.sidebar-top-nav li button {
display: block;
width: 100%;
padding: 0.4rem 0.75rem;
color: var(--text-muted);
text-decoration: none;
border: none;
border-radius: 0.375rem;
font-size: 0.9rem;
text-align: left;
background: none;
}
.sidebar-top-nav li a:hover {
.sidebar-top-nav li a:hover,
.sidebar-top-nav li button:hover {
background: var(--bg-row-hover);
color: var(--text);
}
+6 -1
View File
@@ -75,7 +75,12 @@
<li><a href="{% url 'articles:tag_list' %}">Manage tags</a></li>
<li><a href="{% url 'rules:rule_list' %}">Rules</a></li>
{% if request.user.is_staff %}<li><a href="{% url 'core:dashboard' %}">Dashboard</a></li>{% endif %}
<li><a href="{% url 'accounts:logout' %}">Log out</a></li>
<li>
<form method="post" action="{% url 'accounts:logout' %}">
{% csrf_token %}
<button type="submit">Log out</button>
</form>
</li>
</ul>
<div class="theme-switcher" role="group" aria-label="Theme">