From 3cdb215684e09a3e4d6ef6e5ac5c4fc82c676c8f Mon Sep 17 00:00:00 2001 From: "Paolo Gatti (pgi)" Date: Wed, 15 Feb 2023 20:49:50 +0000 Subject: [PATCH] [FIX] l10n_it_edi: several fixes and improvements - l10n_it_edi: EDI receiving cron error handling fix If we cannot receive invoices or bills from the IAP proxy we also can't iterate over them - l10n_it_edi: Check edi_format before trying to remove signature The edi_format must be checked before trying to remove the eventual PKCS#7 signature from the file, otherwise we're uselessly going to try and remove the signature for every edi_format check (facturX etc.) - l10n_it_edi: .p7m files can wrongly be missing the signature In the case that the .p7m extension is put by mistake, try to use the content of the file as it is before discarding it. Task link: https://www.odoo.com/web#id=3189457&model=project.task Task-3189457 closes odoo/odoo#113451 X-original-commit: 942e3c7f2b549fed86f9eb76f98f30b7e5db1ae9 Signed-off-by: Josse Colpaert Signed-off-by: Paolo Gatti (pgi) --- addons/l10n_it_edi/models/account_edi_format.py | 11 ++++++----- addons/l10n_it_edi/tools/remove_signature.py | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/addons/l10n_it_edi/models/account_edi_format.py b/addons/l10n_it_edi/models/account_edi_format.py index 0f76a8e0b7e..9157eaa692a 100644 --- a/addons/l10n_it_edi/models/account_edi_format.py +++ b/addons/l10n_it_edi/models/account_edi_format.py @@ -303,6 +303,7 @@ class AccountEdiFormat(models.Model): res = proxy_user._make_request(proxy_user._get_server_url() + '/api/l10n_it_edi/1/in/RicezioneInvoice', params={'recipient_codice_fiscale': company.l10n_it_codice_fiscale}) except AccountEdiProxyError as e: + res = {} _logger.error('Error while receiving file from SdiCoop: %s', e) proxy_acks = [] @@ -353,7 +354,7 @@ class AccountEdiFormat(models.Model): def _check_filename_is_fattura_pa(self, filename): return re.search("[A-Z]{2}[A-Za-z0-9]{2,28}_[A-Za-z0-9]{0,5}.((?i:xml.p7m|xml))", filename) - def _is_fattura_pa(self, filename, tree): + def _is_fattura_pa(self, filename, tree=None): return self.code == 'fattura_pa' and self._check_filename_is_fattura_pa(filename) def _create_invoice_from_xml_tree(self, filename, tree, journal=None): @@ -393,17 +394,17 @@ class AccountEdiFormat(models.Model): def _create_invoice_from_binary(self, filename, content, extension): self.ensure_one() - if extension.lower() == '.xml.p7m': + if extension.lower() == '.xml.p7m' and self._is_fattura_pa(filename): decoded_content = self._decode_p7m_to_xml(filename, content) - if decoded_content is not None and self._is_fattura_pa(filename, decoded_content): + if decoded_content is not None: return self._import_fattura_pa(decoded_content, self.env['account.move']) return super()._create_invoice_from_binary(filename, content, extension) def _update_invoice_from_binary(self, filename, content, extension, invoice): self.ensure_one() - if extension.lower() == '.xml.p7m': + if extension.lower() == '.xml.p7m' and self._is_fattura_pa(filename): decoded_content = self._decode_p7m_to_xml(filename, content) - if decoded_content is not None and self._is_fattura_pa(filename, decoded_content): + if decoded_content is not None: return self._import_fattura_pa(decoded_content, invoice) return super()._update_invoice_from_binary(filename, content, extension, invoice) diff --git a/addons/l10n_it_edi/tools/remove_signature.py b/addons/l10n_it_edi/tools/remove_signature.py index 1816b7fd1a7..69ce31993a6 100644 --- a/addons/l10n_it_edi/tools/remove_signature.py +++ b/addons/l10n_it_edi/tools/remove_signature.py @@ -36,7 +36,11 @@ def remove_signature(content): # This method is deprecated, but there are actually no alternatives with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) - loaded_data = ssl_crypto.load_pkcs7_data(ssl_crypto.FILETYPE_ASN1, content) + try: + loaded_data = ssl_crypto.load_pkcs7_data(ssl_crypto.FILETYPE_ASN1, content) + except ssl_crypto.Error: + _logger.warning("Error reading the content, PKCS#7 signature missing or invalid. Content will be tentatively used as it is.") + return content # Verify the signature if verify(loaded_data._pkcs7, null, null, null, out_buffer, flags) != 1: