-
Notifications
You must be signed in to change notification settings - Fork 0
Add article management dashboard and enhance security features #19
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| from django.test import TestCase, Client | ||
| from django.contrib.auth.models import User | ||
| from django.urls import reverse | ||
| import time | ||
|
|
||
|
|
||
| class LoginSecurityTests(TestCase): | ||
| """Test cases for login security to prevent username enumeration attacks.""" | ||
|
|
||
| def setUp(self): | ||
| self.client = Client() | ||
| # Create a test user | ||
| self.test_user = User.objects.create_user( | ||
| username='testuser', | ||
| password='testpass123' | ||
| ) | ||
| self.login_url = reverse('login') # assuming the login view is named 'login' | ||
|
|
||
| def test_login_error_message_consistency(self): | ||
| """Test that error messages are generic for both existing and non-existing users.""" | ||
| # Test with non-existing user | ||
| response_nonexistent = self.client.post(self.login_url, { | ||
| 'username': 'nonexistentuser', | ||
| 'password': 'wrongpassword' | ||
| }) | ||
|
|
||
| # Test with existing user but wrong password | ||
| response_wrong_password = self.client.post(self.login_url, { | ||
| 'username': 'testuser', | ||
| 'password': 'wrongpassword' | ||
| }) | ||
|
|
||
| # Both should return the same generic error message | ||
| self.assertEqual(response_nonexistent.status_code, 401) | ||
| self.assertEqual(response_wrong_password.status_code, 401) | ||
|
|
||
| # Check that error messages are the same and generic | ||
| error_msg_nonexistent = response_nonexistent.context.get('error', '') | ||
| error_msg_wrong_password = response_wrong_password.context.get('error', '') | ||
|
|
||
| # Both should have the same generic error message | ||
| self.assertEqual(error_msg_nonexistent, error_msg_wrong_password) | ||
|
|
||
| # Error message should be generic (not revealing username existence) | ||
| self.assertEqual(error_msg_nonexistent, 'Invalid credentials') | ||
|
|
||
| def test_successful_login(self): | ||
| """Test that successful login still works correctly.""" | ||
| response = self.client.post(self.login_url, { | ||
| 'username': 'testuser', | ||
| 'password': 'testpass123' | ||
| }) | ||
|
|
||
| # Should redirect to dashboard on successful login | ||
| self.assertEqual(response.status_code, 302) | ||
| self.assertRedirects(response, reverse('dashboard')) | ||
|
|
||
| def test_login_timing_consistency(self): | ||
| """Test that login attempts take similar time regardless of username existence.""" | ||
| # This is a basic timing test - in a real security audit, more sophisticated timing analysis would be needed | ||
|
|
||
| times_nonexistent = [] | ||
| times_wrong_password = [] | ||
|
|
||
| # Run multiple attempts to get average timing | ||
| for _ in range(5): | ||
| # Time non-existent user login | ||
| start_time = time.time() | ||
| self.client.post(self.login_url, { | ||
| 'username': 'nonexistentuser', | ||
| 'password': 'wrongpassword' | ||
| }) | ||
| times_nonexistent.append(time.time() - start_time) | ||
|
|
||
| # Time existing user with wrong password | ||
| start_time = time.time() | ||
| self.client.post(self.login_url, { | ||
| 'username': 'testuser', | ||
| 'password': 'wrongpassword' | ||
| }) | ||
| times_wrong_password.append(time.time() - start_time) | ||
|
|
||
| # Calculate averages | ||
| avg_nonexistent = sum(times_nonexistent) / len(times_nonexistent) | ||
| avg_wrong_password = sum(times_wrong_password) / len(times_wrong_password) | ||
|
|
||
| # The difference should be minimal (less than 50ms) | ||
| # This is a very basic test - sophisticated timing attacks require more precise measurements | ||
| time_difference = abs(avg_nonexistent - avg_wrong_password) | ||
| self.assertLess(time_difference, 0.05, | ||
| f"Timing difference too large: {time_difference:.3f}s. " | ||
| f"Avg nonexistent: {avg_nonexistent:.3f}s, " | ||
| f"Avg wrong password: {avg_wrong_password:.3f}s") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Skipping CSRF token when TESTING=True breaks login flows
When the
TESTINGflag is set (either via environment variable or the pytest detection insettings.py), the login template stops rendering{% csrf_token %}but the view continues to be protected byCsrfViewMiddleware. Any browser-based login against a server started withTESTING=Truewill therefore submit no CSRF token and receive a 403 before the view executes, effectively disabling sign‑in for staging/test environments. The tests still pass because Django’s test client doesn’t enforce CSRF, so this regression is silent.Useful? React with 👍 / 👎.