From e9e1864153d09d676b5a10abe272876d7b63a052 Mon Sep 17 00:00:00 2001 From: Benoit Socias Date: Thu, 3 Mar 2022 09:59:44 +0000 Subject: [PATCH] [FIX] web_editor: decode base64 of uploaded non-image documents Since [1] non-image documents uploaded in web editor were stored as received in base64 without being decoded. This led to downloading them as they were stored in base64. After this commit uploaded documents are base64-decoded before being stored. Steps to reproduce: - edit a web page - drop a "Text - Image" snippet - replace the image - upload a document (PDF, TXT...) - save page - download document => received document was base64-encoded [1]: https://github.com/odoo/odoo/commit/6b8752604898bf2b583b7f5334e35f6a1583595e task-2782269 closes odoo/odoo#86812 X-original-commit: b0218b8ff50c7cea2a018d20a62e63510bfdd01e Signed-off-by: Quentin Smetz (qsm) --- addons/web_editor/controllers/main.py | 3 +- addons/web_editor/tests/__init__.py | 1 + addons/web_editor/tests/test_controller.py | 32 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 addons/web_editor/tests/test_controller.py diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py index adad212bd87..db7bab818e8 100644 --- a/addons/web_editor/controllers/main.py +++ b/addons/web_editor/controllers/main.py @@ -199,10 +199,11 @@ class Web_Editor(http.Controller): @http.route('/web_editor/attachment/add_data', type='json', auth='user', methods=['POST'], website=True) def add_data(self, name, data, is_image, quality=0, width=0, height=0, res_id=False, res_model='ir.ui.view', **kwargs): + data = b64decode(data) if is_image: format_error_msg = _("Uploaded image's format is not supported. Try with: %s", ', '.join(SUPPORTED_IMAGE_EXTENSIONS)) try: - data = tools.image_process(b64decode(data), size=(width, height), quality=quality, verify_resolution=True) + data = tools.image_process(data, size=(width, height), quality=quality, verify_resolution=True) mimetype = guess_mimetype(data) if mimetype not in SUPPORTED_IMAGE_MIMETYPES: return {'error': format_error_msg} diff --git a/addons/web_editor/tests/__init__.py b/addons/web_editor/tests/__init__.py index 8b3c5e99c8b..cad4102a2d7 100644 --- a/addons/web_editor/tests/__init__.py +++ b/addons/web_editor/tests/__init__.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import test_controller from . import test_converter from . import test_odoo_editor from . import test_views diff --git a/addons/web_editor/tests/test_controller.py b/addons/web_editor/tests/test_controller.py new file mode 100644 index 00000000000..a2188bf29b4 --- /dev/null +++ b/addons/web_editor/tests/test_controller.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.tools.json import scriptsafe as json_safe + +import odoo.tests +from odoo.tests.common import HttpCase + + +@odoo.tests.tagged('-at_install', 'post_install') +class TestController(HttpCase): + def test_01_upload_document(self): + self.authenticate('admin', 'admin') + # Upload document. + response = self.url_open( + '/web_editor/attachment/add_data', + headers={'Content-Type': 'application/json'}, + data=json_safe.dumps({'params': { + 'name': 'test.txt', + 'data': 'SGVsbG8gd29ybGQ=', # base64 Hello world + 'is_image': False, + }}) + ).json() + self.assertFalse('error' in response, 'Upload failed: %s' % response.get('error', {}).get('message')) + attachment_id = response['result']['id'] + checksum = response['result']['checksum'] + # Download document and check content. + response = self.url_open( + '/web/content/%s?unique=%s&download=true' % (attachment_id, checksum) + ) + self.assertEqual(200, response.status_code, 'Expect response') + self.assertEqual(b'Hello world', response.content, 'Expect raw content')