details:   https://code.tryton.org/tryton/commit/15f536d7a233
branch:    7.8
user:      Cédric Krier <[email protected]>
date:      Mon Nov 23 11:12:48 2026 +0100
description:
        Forbid to read the user's reset password and set expiration

        Closes #15258
        (grafted from 713f57589e14806bfa2d02c3bee636c5780ea38e)
diffstat:

 trytond/CHANGELOG                             |   1 +
 trytond/trytond/res/email_reset_password.html |   2 +-
 trytond/trytond/res/user.py                   |  37 +++++++++++++++++---------
 trytond/trytond/tests/test_user.py            |  31 +++++++++++++++++++--
 4 files changed, 53 insertions(+), 18 deletions(-)

diffs (186 lines):

diff -r d9b8a4f37910 -r 15f536d7a233 trytond/CHANGELOG
--- a/trytond/CHANGELOG Sat Nov 21 18:13:00 2026 +0100
+++ b/trytond/CHANGELOG Mon Nov 23 11:12:48 2026 +0100
@@ -1,3 +1,4 @@
+* Forbid to read the user's reset password (issue15258)
 * Limit the decompressed size of request (issue15257)
 * Define the forbidden fields that cannot be set as preferences (issue15256)
 
diff -r d9b8a4f37910 -r 15f536d7a233 
trytond/trytond/res/email_reset_password.html
--- a/trytond/trytond/res/email_reset_password.html     Sat Nov 21 18:13:00 
2026 +0100
+++ b/trytond/trytond/res/email_reset_password.html     Mon Nov 23 11:12:48 
2026 +0100
@@ -10,7 +10,7 @@
                 <p i18n:msg="login,password,host,database,http_host,database">
                 Hello,
                 we have received a request to reset the password for the 
account associated with <strong>${record.login}</strong>. No changes have been 
made to your account yet.<br/>
-                You can connect with this temporary password 
<strong>${record.password_reset}</strong> to<br/>
+                You can connect with this temporary password 
<strong>${password_reset}</strong> to<br/>
                 <a 
href="tryton://${host}/${database}">tryton://${host}/${database}</a><br/>
                 <a 
href="${http_host}/#${database}">${http_host}/#${database}</a><br/>
                 You must set a new one from the user's preferences.<br/>
diff -r d9b8a4f37910 -r 15f536d7a233 trytond/trytond/res/user.py
--- a/trytond/trytond/res/user.py       Sat Nov 21 18:13:00 2026 +0100
+++ b/trytond/trytond/res/user.py       Mon Nov 23 11:12:48 2026 +0100
@@ -38,7 +38,7 @@
     avatar_mixin, dualmethod, fields)
 from trytond.model.exceptions import ValidationError
 from trytond.pool import Pool
-from trytond.pyson import Bool, Eval, PYSONEncoder
+from trytond.pyson import Eval, PYSONEncoder
 from trytond.report import Report, get_email
 from trytond.rpc import RPC
 from trytond.sendmail import send_message_transactional
@@ -116,12 +116,10 @@
             'invisible': not _has_password,
             })
     password_reset_expire = fields.Timestamp(
-        "Reset Password Expire",
+        "Reset Password Expire", readonly=True,
         states={
-            'required': Bool(Eval('password_reset')),
             'invisible': not _has_password,
-            },
-        depends=['password_reset'])
+            })
     signature = fields.Text('Signature')
     menu = fields.Many2One(
         'ir.action', "Menu",
@@ -299,8 +297,15 @@
         _send_email(from_, users, cls.get_email_reset_password)
 
     def get_email_reset_password(self):
-        return get_email(
-            'res.user.email_reset_password', self, self.languages)
+        transaction = Transaction()
+        table = self.__class__.__table__()
+        cursor = transaction.connection.cursor()
+        cursor.execute(*table.select(
+                table.password_reset, where=table.id == self.id))
+        password_reset, = cursor.fetchone()
+        with transaction.set_context(password_reset=password_reset):
+            return get_email(
+                'res.user.email_reset_password', self, self.languages)
 
     @property
     def languages(self):
@@ -381,10 +386,11 @@
         result = super().read(ids, fields_names)
         cache = Transaction().get_cache().get(cls.__name__)
         for values in result:
-            if 'password_hash' in values:
-                values['password_hash'] = None
-            if cache and values['id'] in cache:
-                cache[values['id']]['password_hash'] = None
+            for name in ['password_hash', 'password_reset']:
+                if name in values:
+                    values[name] = None
+                if cache and values['id'] in cache:
+                    cache[values['id']][name] = None
         return result
 
     @classmethod
@@ -399,7 +405,8 @@
             if cache is not None:
                 for user in users:
                     if user.id in cache:
-                        cache[user.id]['password_hash'] = None
+                        for name in ['password_hash', 'password_reset']:
+                            cache[user.id][name] = None
         return users
 
     @classmethod
@@ -428,6 +435,7 @@
 
         default['password_hash'] = None
         default['password_reset'] = None
+        default['password_reset_expire'] = None
         default.setdefault('warnings')
         default.setdefault('applications')
 
@@ -1108,10 +1116,13 @@
 
     @classmethod
     def get_context(cls, records, header, data):
+        transaction = Transaction()
         context = super().get_context(records, header, data)
         context['host'] = host()
         context['http_host'] = http_host()
-        context['database'] = Transaction().database.name
+        context['database'] = transaction.database.name
+        context['password_reset'] = transaction.context.get(
+            'password_reset', '')
         expire_delay = (
             records[0].password_reset_expire - datetime.datetime.now())
         # Use a precision of minutes
diff -r d9b8a4f37910 -r 15f536d7a233 trytond/trytond/tests/test_user.py
--- a/trytond/trytond/tests/test_user.py        Sat Nov 21 18:13:00 2026 +0100
+++ b/trytond/trytond/tests/test_user.py        Mon Nov 23 11:12:48 2026 +0100
@@ -142,6 +142,18 @@
             User.validate_password('email', [user])
 
     @with_transaction()
+    def test_read_password_reset(self):
+        "Test password_reset can not be read"
+        pool = Pool()
+        User = pool.get('res.user')
+
+        user = self.create_user('user', '12345')
+        with patch.object(user_module, 'send_message_transactional'):
+            User.reset_password([user], length=8)
+
+        self.assertIsNone(user.password_reset)
+
+    @with_transaction()
     def test_reset_password(self):
         "Test reset password"
         pool = Pool()
@@ -156,22 +168,28 @@
                 user_module, 'send_message_transactional') as send_message:
             User.reset_password([user], length=8)
             send_message.assert_called_once()
+            msg, = send_message.call_args.args
 
         cursor.execute(*user_table.select(
                 user_table.password_hash,
+                user_table.password_reset,
                 where=user_table.id == user.id))
-        password_hash, = cursor.fetchone()
+        password_hash, password_reset = cursor.fetchone()
 
-        self.assertEqual(len(user.password_reset), 8)
+        self.assertEqual(len(password_reset), 8)
         self.assertTrue(user.password_reset_expire)
+        self.assertIn(password_reset, msg.get_body().get_content())
         self.check_user('user', '12345')
-        self.check_user('user', user.password_reset)
+        self.check_user('user', password_reset)
 
     @with_transaction()
     def test_reset_password_expired(self):
         "Test reset password not working when expired"
         pool = Pool()
         User = pool.get('res.user')
+        user_table = User.__table__()
+        transaction = Transaction()
+        cursor = transaction.connection.cursor()
 
         user = User(login='user', email='[email protected]')
         user.save()
@@ -179,11 +197,16 @@
         with patch.object(user_module, 'send_message_transactional'):
             User.reset_password([user], length=8)
 
+        cursor.execute(*user_table.select(
+                user_table.password_reset,
+                where=user_table.id == user.id))
+        password_reset, = cursor.fetchone()
+
         user.password_reset_expire = (
             datetime.datetime.now() - datetime.timedelta(10))
         user.save()
         self.assertFalse(User.get_login('user', {
-                    'password': user.password_reset,
+                    'password': password_reset,
                     }))
 
     @with_transaction()

Reply via email to