From de34fed8346e5ede58018fda1df3c7970e10ec2d Mon Sep 17 00:00:00 2001 From: Deependra Solanki Date: Wed, 20 Sep 2023 13:05:05 +0000 Subject: [PATCH] [FIX] web_editor: improve url regex Before this commit: There were certain issues with the URL regex, wherein it failed to validate against domain list under certain circumstances involving URLs with two dots. for eg. this.document.anything After this commit: The URL regex is modified to ensure that if a URL regex does not ends with the domains in tldWhiteList, it must start with http://, https://, www. or if it does not start with http://, https://, www., it must end with a domain from the tldWhiteList. task-3468646 Part-of: odoo/odoo#143250 --- .../static/src/js/editor/odoo-editor/src/utils/utils.js | 2 +- .../static/src/js/editor/odoo-editor/test/spec/link.test.js | 2 +- .../src/js/editor/odoo-editor/test/spec/urlRegex.test.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js index bbfe6d81b52..f4b9626cee5 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js @@ -50,7 +50,7 @@ const tldWhitelist = [ 'ug', 'uk', 'um', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws', 'ye', 'yt', 'yu', 'za', 'zm', 'zr', 'zw', 'co\\.uk']; -const urlRegexBase = `|(?:[-a-zA-Z0-9@:%._\\+~#=]{1,64}\\.))[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-zA-Z][a-zA-Z0-9]{1,62}|(?:[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.(?:${tldWhitelist.join('|')})\\b))(?:\\/[^\\s.,'")}\\]]*[?#.,)}\\]'"]?[^\\s.,'")}\\]]+|(?:[^!(){}.,\\[\\]'"\\s]+)|\\/[^\\s.,'")}\\]]*[^\\s?#.,'")}\\]]*)?`; +const urlRegexBase = `|(?:www.))[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-zA-Z][a-zA-Z0-9]{1,62}|(?:[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.(?:${tldWhitelist.join('|')})\\b))(?:\\/[^\\s.,'")}\\]]*[?#.,)}\\]'"]?[^\\s.,'")}\\]]+|(?:[^!(){}.,\\[\\]'"\\s]+)|\\/[^\\s.,'")}\\]]*[^\\s?#.,'")}\\]]*)?`; const httpCapturedRegex= `(https?:\\/\\/)`; export const URL_REGEX = new RegExp(`((?:(?:${httpCapturedRegex}${urlRegexBase})`, 'i'); diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js index 92a8423f45b..257ff164f2b 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js @@ -87,7 +87,7 @@ describe('Link', () => { testUrlRegex('https://www.google.com/'); testNotUrlRegex('google.shop/'); testUrlRegex('http://google.com/foo#test'); - testUrlRegex('a.bcd.ef'); + testNotUrlRegex('a.bcd.ef'); testUrlRegex('a.bc.de'); testNotUrlRegex('a.bc.d'); testNotUrlRegex('a.b.bc'); diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js index 265f901e83f..f812e87013c 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js @@ -15,8 +15,8 @@ describe('urlRegex', () => { const match = text.match(URL_REGEX); chai.expect(match).to.be.equal(null); }); - it('should match abc.abc.abc', () => { - const url = 'abc.abc.abc'; + it('should match www.abc.abc', () => { + const url = 'www.abc.abc'; const text = `abc ${url} abc`; const match = text.match(URL_REGEX); chai.expect(match[0]).to.be.equal(url);