-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Prevent small risk of Token overwrite
#9754
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
auvipy
merged 9 commits into
encode:master
from
mahdirahimi1999:fix-token-overwrite-9250
Aug 10, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d381901
Fix #9250: Prevent token overwrite and improve security
mahdirahimi1999 06b0441
Fix code style: remove trailing whitespace and unused imports
mahdirahimi1999 705fc2b
Fix #9250: Prevent token overwrite with minimal changes
mahdirahimi1999 15a2164
Fix flake8 violations: remove extra blank lines and trailing whitespace
mahdirahimi1999 621968d
Update tests/test_authtoken.py
mahdirahimi1999 a720d71
Update tests/test_authtoken.py
mahdirahimi1999 bf3a782
Update tests/test_authtoken.py
mahdirahimi1999 e515e23
Fix token key regeneration behavior and add test
mahdirahimi1999 ae6161a
Update tests/test_authtoken.py
mahdirahimi1999 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| from django.contrib.admin import site | ||
| from django.contrib.auth.models import User | ||
| from django.core.management import CommandError, call_command | ||
| from django.db import IntegrityError | ||
| from django.test import TestCase, modify_settings | ||
|
|
||
| from rest_framework.authtoken.admin import TokenAdmin | ||
|
|
@@ -48,6 +49,45 @@ def test_whitespace_in_password(self): | |
| self.user.save() | ||
| assert AuthTokenSerializer(data=data).is_valid() | ||
|
|
||
| def test_token_creation_collision_raises_integrity_error(self): | ||
| user2 = User.objects.create_user('user2', '[email protected]', 'p') | ||
| existing_token = Token.objects.create(user=user2) | ||
|
|
||
| # Try to create another token with the same key | ||
| with self.assertRaises(IntegrityError): | ||
| Token.objects.create(key=existing_token.key, user=self.user) | ||
|
|
||
| def test_key_generated_on_save_when_cleared(self): | ||
| # Create a new user for this test to avoid conflicts with setUp token | ||
| user2 = User.objects.create_user('test_user2', '[email protected]', 'password') | ||
|
|
||
| # Create a token without a key - it should generate one automatically | ||
| token = Token(user=user2) | ||
| token.key = "" # Explicitly clear the key | ||
| token.save() | ||
mahdirahimi1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Verify the key was generated | ||
| self.assertEqual(len(token.key), 40) | ||
| self.assertEqual(token.user, user2) | ||
|
|
||
| def test_clearing_key_on_existing_token_raises_integrity_error(self): | ||
| """Test that clearing the key on an existing token raises IntegrityError.""" | ||
| user = User.objects.create_user('test_user3', '[email protected]', 'password') | ||
| token = Token.objects.create(user=user) | ||
| token.key = "" | ||
|
|
||
| # This should raise IntegrityError because: | ||
| # 1. We're trying to update a record with an empty primary key | ||
| # 2. The OneToOneField constraint would be violated | ||
| with self.assertRaises(Exception): # Could be IntegrityError or DatabaseError | ||
| token.save() | ||
mahdirahimi1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def test_saving_existing_token_without_changes_does_not_alter_key(self): | ||
| original_key = self.token.key | ||
|
|
||
| self.token.save() | ||
| self.assertEqual(self.token.key, original_key) | ||
|
|
||
|
|
||
| class AuthTokenCommandTests(TestCase): | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.