From c65fb9d19815f388d0913b01e83610f856101ff8 Mon Sep 17 00:00:00 2001 From: "Merel Geens (mege)" Date: Fri, 4 Aug 2023 08:49:52 +0000 Subject: [PATCH] [IMP] odoo: prevent unsafe import from file It's possible to specify an existing module as the xml id prefix when importing records from a file. This will result in `ir_module_data` records being created with `noupdate` set to False. When the module is upgraded, these records will be deleted. This can lead to undesired side effects like journal items for accounts imported this way being deleted. This change prevents creating new records linked to existing modules when importing from a file. Instead, the user should either use no prefix or the name of a non-existent module, like `__import__`. opw-3231987 closes odoo/odoo#133263 X-original-commit: 8f12d14c4c4354a63c94f14e7a89abc6048525f9 Signed-off-by: Merel Geens --- odoo/addons/test_impex/tests/test_load.py | 7 ++++--- odoo/models.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/odoo/addons/test_impex/tests/test_load.py b/odoo/addons/test_impex/tests/test_load.py index d1269d00e3f..00a25ab0f4b 100644 --- a/odoo/addons/test_impex/tests/test_load.py +++ b/odoo/addons/test_impex/tests/test_load.py @@ -571,11 +571,12 @@ class test_m2o(ImporterCase): # 1 x SAVEPOINT load # 3 x name_search # 1 x SAVEPOINT _load_records - # 3 x insert + # 1 x select on list of existing modules + # 1 x insert # 1 x RELEASE SAVEPOINT _load_records # 1 x RELEASE SAVEPOINT load - # => 10 - with self.assertQueryCount(8): + # => 9 + with self.assertQueryCount(9): result = self.import_(['value'], [ # import by display_name [record1.display_name], diff --git a/odoo/models.py b/odoo/models.py index 9d94c5e159a..7f20801c79c 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -1225,6 +1225,10 @@ class BaseModel(metaclass=MetaModel): # Failed to write, log to messages, rollback savepoint (to # avoid broken transaction) and keep going errors += 1 + except UserError as e: + info = rec_data['info'] + messages.append(dict(info, type='error', message=str(e))) + errors += 1 except Exception as e: _logger.debug("Error while loading record", exc_info=True) info = rec_data['info'] @@ -4655,6 +4659,17 @@ class BaseModel(metaclass=MetaModel): if data.get('xml_id') and not data['xml_id'].startswith(prefix): _logger.warning("Creating record %s in module %s.", data['xml_id'], module) + if self.env.context.get('import_file'): + existing_modules = self.env['ir.module.module'].search([]).mapped('name') + for data in to_create: + xml_id = data.get('xml_id') + if xml_id: + module_name, sep, record_id = xml_id.partition('.') + if sep and module_name in existing_modules: + raise UserError( + _("The record %(xml_id)s has the module prefix %(module_name)s. This is the part before the '.' in the external id. Because the prefix refers to an existing module, the record would be deleted when the module is upgraded. Use either no prefix and no dot or a prefix that isn't an existing module. For example, __import__, resulting in the external id __import__.%(record_id)s.", + xml_id=xml_id, module_name=module_name, record_id=record_id)) + # create records if to_create: records = self._load_records_create([data['values'] for data in to_create])