Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: standardise the usage of url validator #17564

Merged
merged 9 commits into from
Apr 26, 2023
37 changes: 31 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"babel-polyfill": "^6.26.0",
"dom-serializer": "^0.2.2",
"domhandler": "^4.3.0",
"expensify-common": "git+ssh://git@github.com/Expensify/expensify-common.git#c15668f73079d3f5f42618b70c852d36d387e121",
"expensify-common": "git+ssh://git@github.com/Expensify/expensify-common.git#3cdaa947fe77016206c15e523017cd50678f2359",
"fbjs": "^3.0.2",
"html-entities": "^1.3.1",
"htmlparser2": "^7.2.0",
Expand Down
2 changes: 0 additions & 2 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,8 +984,6 @@ const CONST = {
PAYPAL_ME_USERNAME: /^[a-zA-Z0-9]+$/,
ROOM_NAME: /^#[a-z0-9-]{1,80}$/,

WEBSITE: /^((https?|ftp):\/\/)(([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}(:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(#[-a-z\d_]*)?$/i,

// eslint-disable-next-line max-len, no-misleading-character-class
EMOJIS: /[\p{Extended_Pictographic}\u200d\u{1f1e6}-\u{1f1ff}\u{1f3fb}-\u{1f3ff}\u{e0020}-\u{e007f}\u20E3\uFE0F]|[#*0-9]\uFE0F?\u20E3/gu,
TAX_ID: /^\d{9}$/,
Expand Down
3 changes: 2 additions & 1 deletion src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moment from 'moment';
import _ from 'underscore';
import {URL_REGEX_WITH_REQUIRED_PROTOCOL} from 'expensify-common/lib/Url';
import CONST from '../CONST';
import * as CardUtils from './CardUtils';
import * as LoginUtils from './LoginUtils';
Expand Down Expand Up @@ -238,7 +239,7 @@ function getAgeRequirementError(date, minimumAge, maximumAge) {
* @returns {Boolean}
*/
function isValidWebsite(url) {
return CONST.REGEX.WEBSITE.test(url);
return new RegExp(`^${URL_REGEX_WITH_REQUIRED_PROTOCOL}$`, 'i').test(url);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/ValidationUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ describe('ValidationUtils', () => {
expect(ValidationUtils.isValidWebsite('https://www.expensify.com')).toBe(true);
expect(ValidationUtils.isValidWebsite('https://expensify.com/inbox/')).toBe(true);
expect(ValidationUtils.isValidWebsite('https://we.are.expensify.com/how-we-got-here')).toBe(true);
expect(ValidationUtils.isValidWebsite('https://blog.google')).toBe(true);
expect(ValidationUtils.isValidWebsite('https://blog.google:65535')).toBe(true);
});

test('Valid URLs with http protocol', () => {
Expand All @@ -69,6 +71,8 @@ describe('ValidationUtils', () => {
expect(ValidationUtils.isValidWebsite('expensify')).toBe(false);
expect(ValidationUtils.isValidWebsite('expensify.')).toBe(false);
expect(ValidationUtils.isValidWebsite('192.168.0.1')).toBe(false);
expect(ValidationUtils.isValidWebsite('www.googlecom')).toBe(false);
expect(ValidationUtils.isValidWebsite('www.google.com:65536')).toBe(false);
});

test('Invalid URLs without protocols', () => {
Expand Down