From bd3e0030d027815db746b83a36dad16824f3f6bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Souk=C3=A9ina=20Bojabza?= Date: Wed, 20 Sep 2023 11:27:09 +0000 Subject: [PATCH] [FIX] web_editor: make `loadImageInfo` robust to absolute URLs This commit makes the rpc call in the `loadImageInfo` method robust to the case where an image src would be an absolute URL, by only considering the relative part of the URL. The images src are generally always relative but commit [1] wrongly put absolute URLs to images in grid mode with the `_reloadLazyImages` method. While this behavior was fixed by commit [2], the images that were saved before this fix was available still have an absolute URL. Hence the need to make `loadImageInfo` robust. [1]: https://github.com/odoo/odoo/commit/cc406afcea7bf5846233a9f97a4a8ac5f618f3ec [2]: https://github.com/odoo/odoo/commit/3528f2c3f9d3b30266f3421672986d202c724a76 task-3514519 closes odoo/odoo#136951 X-original-commit: af5ddabb0285daa9e57bc01cf21628f2f94bc18f Signed-off-by: Quentin Smetz (qsm) --- .../static/src/js/editor/image_processing.js | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/image_processing.js b/addons/web_editor/static/src/js/editor/image_processing.js index 4c70a827802..2c688b62ca3 100644 --- a/addons/web_editor/static/src/js/editor/image_processing.js +++ b/addons/web_editor/static/src/js/editor/image_processing.js @@ -447,11 +447,24 @@ export async function loadImageInfo(img, rpc, attachmentSrc = '') { return; } - const {original} = await rpc('/web_editor/get_image_info', {src: src.split(/[?#]/)[0]}); - // Check that url is local. - const isLocal = original && new URL(original.image_src, window.location.origin).origin === window.location.origin - && !/\/web\/image\/\d+-redirect\//.test(original.image_src); - if (isLocal && original.image_src) { + // Only consider the "relative" part of the URL. Needed because some + // relative URLs were wrongly converted to absolute URLs at some point and + // user domains could have been changed meanwhile. + let relativeSrc; + try { + const srcUrl = new URL(src); + relativeSrc = srcUrl.pathname; + } catch { + relativeSrc = src; + } + const {original} = await rpc('/web_editor/get_image_info', {src: relativeSrc.split(/[?#]/)[0]}); + // If src was an absolute "external" URL, we consider unlikely that its + // relative part matches something from the DB and even if it does, nothing + // bad happens, besides using this random image as the original when using + // the options, instead of having no option. + // The "redirect" check is for when it is a redirect image attachment due to + // an external URL upload. + if (original && original.image_src && !/\/web\/image\/\d+-redirect\//.test(original.image_src)) { img.dataset.originalId = original.id; img.dataset.originalSrc = original.image_src; img.dataset.mimetype = original.mimetype;