django-rest-framework/rest_framework/authtoken
Mahdi Rahimi c0166d95bb
Prevent small risk of Token overwrite (#9754)
* Fix #9250: Prevent token overwrite and improve security

- Fix key collision issue that could overwrite existing tokens
- Use force_insert=True only for new token instances
- Replace os.urandom with secrets.token_hex for better security
- Add comprehensive test suite to verify fix and backward compatibility
- Ensure existing tokens can still be updated without breaking changes

* Fix code style: remove trailing whitespace and unused imports

* Fix #9250: Prevent token overwrite with minimal changes

- Add force_insert=True to Token.save() for new objects to prevent overwriting existing tokens
- Revert generate_key method to original implementation (os.urandom + binascii)
- Update tests to work with original setUp() approach
- Remove verbose comments and unrelated changes per reviewer feedback

* Fix flake8 violations: remove extra blank lines and trailing whitespace

* Update tests/test_authtoken.py

Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>

* Update tests/test_authtoken.py

Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>

* Update tests/test_authtoken.py

Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>

* Fix token key regeneration behavior and add test

* Update tests/test_authtoken.py

Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>

---------

Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>
2025-08-10 16:52:32 +06:00
..
management Add pyupgrade to pre-commit hooks (#9682) 2025-04-09 06:24:18 +00:00
migrations Implemented Verbose Name Translation for TokenProxy (#8713) 2022-11-27 16:45:54 +06:00
__init__.py Remove unused code 2024-04-30 18:28:22 +02:00
admin.py Revert PR that adds autocomplete_fields to TokenAdmin, as this break some use cases. (#9301) 2024-03-21 12:46:06 +00:00
apps.py Replace all usage ugettext functions with the non-u versions (#6634) 2019-05-01 07:49:54 +02:00
models.py Prevent small risk of Token overwrite (#9754) 2025-08-10 16:52:32 +06:00
serializers.py Fix schema generation for ObtainAuthToken view. (#7211) 2020-03-03 13:27:34 +01:00
views.py Fix schema generation for ObtainAuthToken view. (#7211) 2020-03-03 13:27:34 +01:00