From 1fe0f6e20b4b01f66615aa8da9376f1daeb31991 Mon Sep 17 00:00:00 2001 From: Raffael Meyer <14891507+barredterra@users.noreply.github.com> Date: Tue, 16 Jul 2024 14:15:43 +0200 Subject: [PATCH] fix: handle invalid images in HTML (#27095) * fix: handle invalid image * refactor: simplify broken image replacement * fix: add alt attribute to broken image --- frappe/core/doctype/file/test_file.py | 21 ++++++++++++++++++++- frappe/core/doctype/file/utils.py | 12 +++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/frappe/core/doctype/file/test_file.py b/frappe/core/doctype/file/test_file.py index 6c9b9f5872..03a8927dac 100644 --- a/frappe/core/doctype/file/test_file.py +++ b/frappe/core/doctype/file/test_file.py @@ -17,7 +17,7 @@ from frappe.core.api.file import ( unzip_file, ) from frappe.core.doctype.file.exceptions import FileTypeNotAllowed -from frappe.core.doctype.file.utils import get_extension +from frappe.core.doctype.file.utils import get_corrupted_image_msg, get_extension from frappe.desk.form.utils import add_comment from frappe.exceptions import ValidationError from frappe.tests.utils import FrappeTestCase, change_settings @@ -768,6 +768,25 @@ class TestFileUtils(FrappeTestCase): ) self.assertRegex(communication.content, r"") + def test_broken_image(self): + """Ensure that broken inline images don't cause errors.""" + is_private = not frappe.get_meta("Communication").make_attachments_public + communication = frappe.get_doc( + doctype="Communication", + communication_type="Communication", + communication_medium="Email", + content='
', + recipients="to ", + cc=None, + bcc=None, + sender="sender@test.com", + ).insert(ignore_permissions=True) + + self.assertFalse( + frappe.db.exists("File", {"attached_to_name": communication.name, "is_private": is_private}) + ) + self.assertIn(f'{get_corrupted_image_msg()}', communication.content) + def test_create_new_folder(self): folder = create_new_folder("test_folder", "Home") self.assertTrue(folder.is_folder) diff --git a/frappe/core/doctype/file/utils.py b/frappe/core/doctype/file/utils.py index 578c760389..d16f76b090 100644 --- a/frappe/core/doctype/file/utils.py +++ b/frappe/core/doctype/file/utils.py @@ -2,6 +2,7 @@ import hashlib import mimetypes import os import re +from binascii import Error as BinasciiError from io import BytesIO from typing import TYPE_CHECKING, Optional from urllib.parse import unquote @@ -234,7 +235,12 @@ def extract_images_from_html(doc: "Document", content: str, is_private: bool = F content = content.encode("utf-8") if b"," in content: content = content.split(b",")[1] - content = safe_b64decode(content) + + try: + content = safe_b64decode(content) + except BinasciiError: + frappe.flags.has_dataurl = True + return f'{get_corrupted_image_msg()} str: extn = None if content_type: