details: https://code.tryton.org/tryton/commit/ab0f324fe7fc
branch: default
user: Cédric Krier <[email protected]>
date: Tue Nov 24 12:47:34 2026 +0100
description:
Forbid to read the user's reset password and password hash and set
expiration
And make get_email_reset_password_url private to forbid access via
report.
Closes #15259 #15263
diffstat:
modules/web_user/CHANGELOG | 1 +
modules/web_user/email_reset_password.html | 4 +-
modules/web_user/tests/test_module.py | 57 +++++++++++++++----
modules/web_user/user.py | 85 ++++++++++++++++++++++++------
4 files changed, 116 insertions(+), 31 deletions(-)
diffs (315 lines):
diff -r 713f57589e14 -r ab0f324fe7fc modules/web_user/CHANGELOG
--- a/modules/web_user/CHANGELOG Mon Nov 23 11:12:48 2026 +0100
+++ b/modules/web_user/CHANGELOG Tue Nov 24 12:47:34 2026 +0100
@@ -1,3 +1,4 @@
+* Forbid to read the user's reset password and password hash (issue15259
issue15263)
Version 8.2.0 - 2026-10-05
--------------------------
diff -r 713f57589e14 -r ab0f324fe7fc modules/web_user/email_reset_password.html
--- a/modules/web_user/email_reset_password.html Mon Nov 23 11:12:48
2026 +0100
+++ b/modules/web_user/email_reset_password.html Tue Nov 24 12:47:34
2026 +0100
@@ -11,7 +11,7 @@
<p i18n:msg="email,url">
Hello, we received a request to reset the password for the
account associated with <strong>${user.email}</strong>. No changes have been
made to your account yet.<br/>
You can reset your password by clicking the link below:<br/>
- <a href="${user.get_email_reset_password_url()}"
+ <a href="${reset_password_url}"
style="display: block; text-decoration: none; width:
max-content; margin: 0.5em auto; padding: 0.5em 1em; font-size:1em; border: 1px
solid #2E6DA4; border-radius: 4px; color: #FFF; background-color: #337AB7;">
Reset Password
</a>
@@ -23,7 +23,7 @@
The link will expire in <time
datetime="${record.reset_password_token_expire.isoformat()}">${format_timedelta(expire_delay)}</time>.
</p>
<p>Button not working? Paste this into your browser:</p>
- <p>${user.get_email_reset_password_url()}</p>
+ <p>${reset_password_url}</p>
<p>If you didn't make this request, you can ignore this
email.</p>
</div>
</div>
diff -r 713f57589e14 -r ab0f324fe7fc modules/web_user/tests/test_module.py
--- a/modules/web_user/tests/test_module.py Mon Nov 23 11:12:48 2026 +0100
+++ b/modules/web_user/tests/test_module.py Tue Nov 24 12:47:34 2026 +0100
@@ -9,6 +9,7 @@
from trytond.modules.web_user import user as user_module
from trytond.pool import Pool
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
+from trytond.transaction import Transaction
VALIDATION_URL = 'http://www.example.com/validation'
RESET_PASSWORD_URL = 'http://www.example.com/reset_password'
@@ -50,9 +51,7 @@
user = User(email=email)
user.password = password
user.save()
-
- self.assertEqual(user.password, 'x' * 10)
- self.assertNotEqual(user.password_hash, password)
+ return user
def check_user(self, email, password):
pool = Pool()
@@ -75,6 +74,12 @@
self.check_user('[email protected]', 'secret')
@with_transaction()
+ def test_read_password_hash(self):
+ "Test password_hash can not be read"
+ user = self.create_user('[email protected]', 'secret')
+ self.assertIsNone(user.password_hash)
+
+ @with_transaction()
def test_session(self):
'Test session'
pool = Pool()
@@ -127,42 +132,70 @@
self.assertFalse(user.email_token)
@with_transaction()
- def test_reset_password(self):
- 'Test reset password'
+ def test_read_reset_password_token(self):
+ "Test reset_password_token can not be read"
pool = Pool()
User = pool.get('web.user')
user = User(email='[email protected]', email_valid=True)
user.save()
- self.assertFalse(user.password_hash)
+ with patch.object(user_module, 'send_message_transactional'):
+ User.reset_password([user])
+
+ self.assertIsNone(user.reset_password_token)
+
+ @with_transaction()
+ def test_reset_password(self):
+ 'Test reset password'
+ pool = Pool()
+ User = pool.get('web.user')
+ user_table = User.__table__()
+ transaction = Transaction()
+ cursor = transaction.connection.cursor()
+
+ user = User(email='[email protected]', email_valid=True)
+ user.save()
with patch.object(
user_module, 'send_message_transactional') as send_message:
User.reset_password([user])
send_message.assert_called_once()
+ msg, = send_message.call_args.args
- token = user.reset_password_token
+ cursor.execute(*user_table.select(
+ user_table.password_hash,
+ user_table.reset_password_token,
+ where=user_table.id == user.id))
+ password_hash, token = cursor.fetchone()
+
self.assertTrue(token)
self.assertTrue(user.reset_password_token_expire)
+ self.assertIn(token, msg.get_body().get_content())
self.assertEqual(
- user.get_email_reset_password_url(),
+ user._get_reset_password_url(),
'%s?email=user%%40example.com&token=%s'
% (RESET_PASSWORD_URL, token))
+ self.assertFalse(password_hash)
self.assertFalse(
User.set_password_token('[email protected]', '12345', 'secret'))
self.assertFalse(
User.set_password_token('[email protected]', '12345', 'secret'))
self.assertFalse(
User.set_password_token('[email protected]', token, 'secret'))
- self.assertFalse(user.password_hash)
self.assertTrue(User.set_password_url(
- user.get_email_reset_password_url(), 'secret'))
+ user._get_reset_password_url(), 'secret'))
- self.assertTrue(user.password_hash)
- self.assertFalse(user.reset_password_token)
+ cursor.execute(*user_table.select(
+ user_table.password_hash,
+ user_table.reset_password_token,
+ where=user_table.id == user.id))
+ password_hash, token = cursor.fetchone()
+
+ self.assertTrue(password_hash)
+ self.assertFalse(token)
self.assertFalse(user.reset_password_token_expire)
@with_transaction()
diff -r 713f57589e14 -r ab0f324fe7fc modules/web_user/user.py
--- a/modules/web_user/user.py Mon Nov 23 11:12:48 2026 +0100
+++ b/modules/web_user/user.py Tue Nov 24 12:47:34 2026 +0100
@@ -13,6 +13,7 @@
from sql.operators import Equal
import trytond.config as config
+from trytond.backend import scalar_row
from trytond.exceptions import RateLimitException
from trytond.i18n import gettext
from trytond.model import (
@@ -72,7 +73,7 @@
fields.Char('Password'), 'get_password', setter='set_password')
reset_password_token = fields.Char("Reset Password Token", strip=False)
reset_password_token_expire = fields.Timestamp(
- 'Reset Password Token Expire')
+ 'Reset Password Token Expire', readonly=True)
party = fields.Many2One('party.party', 'Party', ondelete='RESTRICT')
secondary_parties = fields.Many2Many(
'web.user-party.party.secondary', 'user', 'party', "Secondary Parties")
@@ -130,6 +131,8 @@
for user in users:
to_write.extend([[user], {
'password_hash': cls.hash_password(value),
+ 'reset_password_token': None,
+ 'reset_password_token_expire': None,
}])
cls.write(*to_write)
@@ -139,10 +142,39 @@
self.email = self.party.email
@classmethod
+ def read(cls, ids, fields_names):
+ result = super().read(ids, fields_names)
+ cache = Transaction().get_cache().get(cls.__name__)
+ for values in result:
+ for name in ['password_hash', 'reset_password_token']:
+ if name in values:
+ values[name] = None
+ if cache and values['id'] in cache:
+ cache[values['id']][name] = None
+ return result
+
+ @classmethod
+ def search(
+ cls, domain, offset=0, limit=None, order=None, count=False,
+ query=False):
+ users = super().search(
+ domain, offset=offset, limit=limit, order=order, count=count,
+ query=query)
+ if not count and not query:
+ cache = Transaction().get_cache().get(cls.__name__)
+ if cache is not None:
+ for user in users:
+ if user.id in cache:
+ for name in ['password_hash', 'reset_password_token']:
+ cache[user.id][name] = None
+ return users
+
+ @classmethod
def copy(cls, users, default=None):
default = default.copy() if default is not None else {}
default['password_hash'] = None
default['reset_password_token'] = None
+ default['reset_password_token_expire'] = None
return super().copy(users, default=default)
@classmethod
@@ -176,6 +208,8 @@
def authenticate(cls, email, password):
pool = Pool()
Attempt = pool.get('web.user.authenticate.attempt')
+ cursor = Transaction().connection.cursor()
+ table = cls.__table__()
email = email.lower()
count_ip = Attempt.count_ip()
@@ -190,10 +224,14 @@
# Prevent brute force attack
Transaction().atexit(time.sleep, 2 ** count - 1)
- users = cls.search([('email', '=', email)])
- if users:
- user, = users
- valid, new_hash = cls.check_password(password, user.password_hash)
+ cursor.execute(*table.select(
+ table.id, table.password_hash,
+ where=(table.email == email)
+ & (table.active == Literal(True))))
+ user_id, password_hash = cursor.fetchone() or (None, None)
+ if user_id is not None:
+ user = cls(user_id)
+ valid, new_hash = cls.check_password(password, password_hash)
if valid:
if new_hash:
logger.info("Update password hash for %s", user.id)
@@ -275,8 +313,7 @@
def reset_password(cls, users, from_=None):
now = datetime.datetime.now()
for user in users:
- if (user.reset_password_token
- and user.reset_password_token_expire
+ if (user.reset_password_token_expire
and user.reset_password_token_expire > now):
# Prevent abusive reset
continue
@@ -296,15 +333,23 @@
self.reset_password_token_expire = None
def get_email_reset_password(self):
- return get_email(
- 'web.user.email_reset_password', self, self.languages)
+ reset_password_url = self._get_reset_password_url()
+ with Transaction().set_context(reset_password_url=reset_password_url):
+ return get_email(
+ 'web.user.email_reset_password', self, self.languages)
- def get_email_reset_password_url(self, url=None):
+ def _get_reset_password_url(self, url=None):
if url is None:
url = config.get('web', 'reset_password_url')
if url is not None:
+ transaction = Transaction()
+ table = self.__class__.__table__()
+ cursor = transaction.connection.cursor(row_factory=scalar_row)
+ cursor.execute(*table.select(
+ table.reset_password_token, where=table.id == self.id))
+ reset_password_token = cursor.fetchone()
return _add_params(
- url, token=self.reset_password_token, email=self.email)
+ url, token=reset_password_token, email=self.email)
else:
return ''
@@ -320,18 +365,22 @@
def set_password_token(cls, email, token, password):
pool = Pool()
Attempt = pool.get('web.user.authenticate.attempt')
+ cursor = Transaction().connection.cursor()
+ table = cls.__table__()
email = email.lower()
# Prevent brute force attack
Transaction().atexit(
time.sleep, random.randint(0, 2 ** Attempt.count(email) - 1))
- users = cls.search([
- ('email', '=', email),
- ])
- if users:
- user, = users
- if compare_digest(user.reset_password_token, token):
+ cursor.execute(*table.select(
+ table.id, table.reset_password_token,
+ where=(table.email == email)
+ & (table.active == Literal(True))))
+ user_id, reset_password_token = cursor.fetchone() or (None, None)
+ if user_id is not None:
+ user = cls(user_id)
+ if compare_digest(reset_password_token, token):
now = datetime.datetime.now()
expire = user.reset_password_token_expire
user.clear_reset_password_token()
@@ -485,4 +534,6 @@
days=expire_delay.days,
minutes=round(expire_delay.seconds / 60))
context['expire_delay'] = expire_delay
+ context['reset_password_url'] = Transaction().context.get(
+ 'reset_password_url', '')
return context