Skip to content
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

fix: return an empty profile page when not found #2680

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion templates/users/user_detail.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "users/base.html" %}
{% load users_tags %}

{% block page_title %}{% firstof object.get_full_name object %} | Our Users & Members | {{ SITE_INFO.site_name }}{% endblock %}
{% block page_title %}Our Users & Members | {{ SITE_INFO.site_name }}{% endblock %}

{% block body_attributes %}class="psf users default-page"{% endblock %}

Expand Down
10 changes: 5 additions & 5 deletions users/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ def test_user_update_redirect(self):
profile_url = reverse('users:user_detail', kwargs={'slug': 'username'})
self.assertRedirects(response, profile_url)

# should return 404 for another user
# should return 200 for another user
another_user_url = reverse('users:user_detail', kwargs={'slug': 'spameggs'})
response = self.client.get(another_user_url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, 200)

# should return 404 if the user is not logged-in
# should return 200 if the user is not logged-in
self.client.logout()
response = self.client.get(profile_url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, 200)

def test_user_detail(self):
# Ensure detail page is viewable without login, but that edit URLs
Expand All @@ -184,7 +184,7 @@ def test_user_detail(self):
self.assertFalse(user.is_active)
detail_url = reverse('users:user_detail', kwargs={'slug': user.username})
response = self.client.get(detail_url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, 200)

def test_special_usernames(self):
# Ensure usernames in the forms of:
Expand Down
8 changes: 8 additions & 0 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.auth.models import AnonymousUser
from django.conf import settings
from django.core.mail import send_mail
from django.db.models import Subquery
Expand Down Expand Up @@ -126,13 +127,20 @@ def get_object(self, queryset=None):

class UserDetail(DetailView):
slug_field = 'username'
template_name = 'users/user_detail.html'

def get_queryset(self):
queryset = User.objects.select_related()
if self.request.user.username == self.kwargs['slug']:
return queryset
return queryset.searchable()

def get_object(self, queryset=None):
try:
return super().get_object(queryset)
except Http404:
return AnonymousUser()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would returning an anonymous user make it known that a user doesn't exist and the malicious actor could continue enumerating over usernames?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, yes - since the HTML contents used to include the user's name in the <title> tag - that's been removed in the template.

I examined the HTML output of an existing vs anonymous user to confirm that the only difference is in the actual URL requested - nothing else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I examined the HTML output of an existing vs anonymous user to confirm that the only difference is in the actual URL requested - nothing else.

non-blocking suggestion: If it's not too expensive in terms of effort, it would be good to add a test that does this.



class HoneypotSignupView(SignupView):

Expand Down
Loading