From e9daa1c475ba047fd13ad50079cd64f730e58c29 Mon Sep 17 00:00:00 2001 From: byte1012 Date: Thu, 9 Nov 2023 10:03:22 +0300 Subject: [PATCH] fix-#27080 --- __tests__/URL-test.js | 7 +++++++ lib/Url.js | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/__tests__/URL-test.js b/__tests__/URL-test.js index 553f4b55..1474663b 100644 --- a/__tests__/URL-test.js +++ b/__tests__/URL-test.js @@ -48,6 +48,7 @@ describe('Loose URL validation', () => { expect(regexToTest.test('http://a.b')).toBeTruthy(); expect(regexToTest.test('http://expensify')).toBeTruthy(); expect(regexToTest.test('http://google.com/abcd')).toBeTruthy(); + expect(regexToTest.test('http://my.localhost.local-domain')).toBeTruthy(); }); it('correctly tests invalid urls', () => { @@ -55,5 +56,11 @@ describe('Loose URL validation', () => { expect(regexToTest.test('localhost:3000')).toBeFalsy(); expect(regexToTest.test('local.url')).toBeFalsy(); expect(regexToTest.test('https://otherexample.com links get rendered first')).toBeFalsy(); + expect(regexToTest.test('http://-localhost')).toBeFalsy(); + expect(regexToTest.test('http://_')).toBeFalsy(); + expect(regexToTest.test('http://_localhost')).toBeFalsy(); + expect(regexToTest.test('http://-77.com')).toBeFalsy(); + expect(regexToTest.test('http://77-.com')).toBeFalsy(); + expect(regexToTest.test('http://my.localhost....local-domain:8080')).toBeFalsy(); }); }); diff --git a/lib/Url.js b/lib/Url.js index 17c90030..5b89696c 100644 --- a/lib/Url.js +++ b/lib/Url.js @@ -11,7 +11,7 @@ const URL_REGEX = `((${URL_WEBSITE_REGEX})${URL_PATH_REGEX}(?:${URL_PARAM_REGEX} const URL_REGEX_WITH_REQUIRED_PROTOCOL = URL_REGEX.replace(`${URL_PROTOCOL_REGEX}?`, URL_PROTOCOL_REGEX); -const LOOSE_URL_WEBSITE_REGEX = `${URL_PROTOCOL_REGEX}([-\\w]+(\\.[-\\w]+)*)(?:\\:${ALLOWED_PORTS}|\\b|(?=_))`; +const LOOSE_URL_WEBSITE_REGEX = `${URL_PROTOCOL_REGEX}([a-z0-9](?:[-a-z0-9]*[a-z0-9])?\\.)*(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?)(?:\\:${ALLOWED_PORTS}|\\b|(?=_))`; const LOOSE_URL_REGEX = `((${LOOSE_URL_WEBSITE_REGEX})${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`;