details: https://code.tryton.org/tryton/commit/dd1bf56f5c6a
branch: 7.0
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
(grafted from ab0f324fe7fc1c341b68b198e7cf72ef2bcc5efe)
diffstat:
modules/web_user/CHANGELOG | 1 +
modules/web_user/email_reset_password.html | 4 +-
modules/web_user/tests/test_module.py | 55 +++++++++++++++----
modules/web_user/user.py | 81 ++++++++++++++++++++++++-----
4 files changed, 112 insertions(+), 29 deletions(-)
diffs (294 lines):
diff -r 1a698490c9cf -r dd1bf56f5c6a modules/web_user/CHANGELOG
--- a/modules/web_user/CHANGELOG Thu Nov 26 11:00:42 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 7.0.2 - 2026-02-18
--------------------------
diff -r 1a698490c9cf -r dd1bf56f5c6a modules/web_user/email_reset_password.html
--- a/modules/web_user/email_reset_password.html Thu Nov 26 11:00:42
2026 +0100
+++ b/modules/web_user/email_reset_password.html Tue Nov 24 12:47:34
2026 +0100
@@ -9,7 +9,7 @@
<div>
<h1>Reset Password</h1>
<p i18n:msg="email">We received a request to reset the
password for your account: ${user.email}</p>
- <a href="${user.get_email_reset_password_url()}"
+ <a href="${reset_password_url}"
style="display: block; text-decoration: none; width:
max-content; margin: 0 auto; padding: 0.5em 1em; font-size:2em; border: 1px
solid #2E6DA4; border-radius: 4px; color: #FFF; background-color: #337AB7;">
Reset Password
</a>
@@ -20,7 +20,7 @@
The link will expire in <time
datetime="${record.reset_password_token_expire.isoformat()}">${format_timedelta(expire_delay)}</time>.
</p>
<p>Button is 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 1a698490c9cf -r dd1bf56f5c6a modules/web_user/tests/test_module.py
--- a/modules/web_user/tests/test_module.py Thu Nov 26 11:00:42 2026 +0100
+++ b/modules/web_user/tests/test_module.py Tue Nov 24 12:47:34 2026 +0100
@@ -8,6 +8,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'
@@ -48,9 +49,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()
@@ -73,6 +72,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()
@@ -124,41 +129,67 @@
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, 'sendmail_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, 'sendmail_transactional') as sendmail:
User.reset_password([user])
sendmail.assert_called_once_with(FROM, ['[email protected]'], ANY)
- 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.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 1a698490c9cf -r dd1bf56f5c6a modules/web_user/user.py
--- a/modules/web_user/user.py Thu Nov 26 11:00:42 2026 +0100
+++ b/modules/web_user/user.py Tue Nov 24 12:47:34 2026 +0100
@@ -85,7 +85,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")
@@ -137,6 +137,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)
@@ -154,10 +156,39 @@
cls.save(users)
@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
@@ -196,6 +227,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()
@@ -210,10 +243,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)
@@ -369,15 +406,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()
+ 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 ''
@@ -393,18 +438,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 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 reset_password_token == token:
now = datetime.datetime.now()
expire = user.reset_password_token_expire
user.clear_reset_password_token()
@@ -562,4 +611,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