details:   https://code.tryton.org/tryton/commit/1a698490c9cf
branch:    7.0
user:      Cédric Krier <[email protected]>
date:      Thu Nov 26 11:00:42 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            |  39 +++++++++++++++++++++++---
 4 files changed, 60 insertions(+), 19 deletions(-)

diffs (209 lines):

diff -r 53b58829d26b -r 1a698490c9cf trytond/CHANGELOG
--- a/trytond/CHANGELOG Sat Nov 21 18:13:00 2026 +0100
+++ b/trytond/CHANGELOG Thu Nov 26 11:00:42 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 53b58829d26b -r 1a698490c9cf 
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     Thu Nov 26 11:00:42 
2026 +0100
@@ -10,7 +10,7 @@
                 <p i18n:msg="login,password,host,database,http_host,database">
                 The password for your account, 
<strong>${record.login}</strong>, has been reset.<br/>
                 You must set a new one from the user's preferences.<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>
                 </p>
diff -r 53b58829d26b -r 1a698490c9cf 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       Thu Nov 26 11:00:42 2026 +0100
@@ -49,7 +49,7 @@
     avatar_mixin, 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 sendmail_transactional
@@ -130,12 +130,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 Action',
         domain=[('usage', '=', 'menu')], required=True)
@@ -311,8 +309,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):
@@ -363,10 +368,11 @@
         result = super(User, cls).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
@@ -381,7 +387,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
@@ -445,6 +452,7 @@
 
         default['password_hash'] = None
         default['password_reset'] = None
+        default['password_reset_expire'] = None
         default.setdefault('warnings')
         default.setdefault('applications')
 
@@ -1218,10 +1226,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 53b58829d26b -r 1a698490c9cf 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        Thu Nov 26 11:00:42 2026 +0100
@@ -141,6 +141,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, 'sendmail_transactional'):
+            User.reset_password([user], length=8)
+
+        self.assertIsNone(user.password_reset)
+
+    @with_transaction()
     def test_reset_password(self):
         "Test reset password"
         pool = Pool()
@@ -157,19 +169,23 @@
 
         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.assertIsNone(password_hash)
-        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()
@@ -177,11 +193,16 @@
         with patch.object(user_module, 'sendmail_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()
@@ -189,6 +210,9 @@
         "Test reset password not working when password is set"
         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()
@@ -196,11 +220,16 @@
         with patch.object(user_module, 'sendmail_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 = '12345'
         user.save()
         self.check_user('user', '12345')
         self.assertFalse(User.get_login('user', {
-                    'password': user.password_reset,
+                    'password': password_reset,
                     }))
 
     @with_transaction()

Reply via email to