Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ test('Test url replacements', () => {
+ '<a href="https://expensify.cash/#/r/1234" target="_blank" rel="noreferrer noopener">https://expensify.cash/#/r/1234</a> '
+ '<a href="https://github.com/Expensify/ReactNativeChat/pull/6.45" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/6.45</a> '
+ '<a href="https://github.com/Expensify/Expensify/issues/143,231" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/Expensify/issues/143,231</a> '
+ '<a href="https://testRareTLDs.beer" target="_blank" rel="noreferrer noopener">testRareTLDs.beer</a> '
+ '<a href="https://testraretlds.beer" target="_blank" rel="noreferrer noopener">testRareTLDs.beer</a> '
+ '<a href="mailto:test@expensify.com">test@expensify.com</a> '
+ 'test.completelyFakeTLD '
+ '<a href="https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&amp;index=logs_expensify-008878" target="_blank" rel="noreferrer noopener">https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&amp;index=logs_expensify-008878</a>) '
Expand Down
12 changes: 12 additions & 0 deletions __tests__/Str-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ describe('Str.isValidMention', () => {
expect(Str.isValidMention('"@username@expensify.com"')).toBeTruthy();
});
});

describe('Str.sanitizeURL', () => {
it('Normalize domain name to lower case and add missing https:// protocol', () => {
expect(Str.sanitizeURL('https://google.com')).toBe('https://google.com');
expect(Str.sanitizeURL('google.com')).toBe('https://google.com');
expect(Str.sanitizeURL('Https://google.com')).toBe('https://google.com');
expect(Str.sanitizeURL('https://GOOgle.com')).toBe('https://google.com');
expect(Str.sanitizeURL('FOO.com/blah_BLAH')).toBe('https://foo.com/blah_BLAH');
expect(Str.sanitizeURL('http://FOO.com/blah_BLAH')).toBe('http://foo.com/blah_BLAH');
expect(Str.sanitizeURL('HTtp://FOO.com/blah_BLAH')).toBe('http://foo.com/blah_BLAH');
});
});
16 changes: 5 additions & 11 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,11 @@ export default class ExpensiMark {
return this.modifyTextForUrlLinks(regex, textToProcess, replacement);
},

// We use a function here to check if there is already a https:// on the link.
// If there is not, we force the link to be absolute by prepending '//' to the target.
replacement: (match, g1, g2, g3) => {
replacement: (match, g1, g2) => {
if (g1.match(CONST.REG_EXP.EMOJIS) || !g1.trim()) {
return match;
}

const href = g3 ? g2.replace(g3, g3.toLowerCase()) : `https://${g2}`;
return `<a href="${href}" target="_blank" rel="noreferrer noopener">${g1.trim()}</a>`;
return `<a href="${Str.sanitizeURL(g2)}" target="_blank" rel="noreferrer noopener">${g1.trim()}</a>`;
},
},

Expand Down Expand Up @@ -154,10 +150,8 @@ export default class ExpensiMark {
return this.modifyTextForUrlLinks(regex, textToProcess, replacement);
},

// We use a function here to check if there is already a https:// on the link.
// If there is not, we force the link to be absolute by prepending '//' to the target.
replacement: (match, g1, g2, g3) => {
const href = g3 ? g2.replace(g3, g3.toLowerCase()) : `https://${g2}`;
replacement: (match, g1, g2) => {
const href = Str.sanitizeURL(g2);
return `${g1}<a href="${href}" target="_blank" rel="noreferrer noopener">${g2}</a>${g1}`;
},
},
Expand Down Expand Up @@ -465,7 +459,7 @@ export default class ExpensiMark {
filterRules: ['bold', 'strikethrough', 'italic'],
shouldEscapeText: false,
});
replacedText = replacedText.concat(replacement(match[0], linkText, match[2], match[3]));
replacedText = replacedText.concat(replacement(match[0], linkText, match[2]));
}
startIndex = match.index + (match[0].length);

Expand Down
2 changes: 1 addition & 1 deletion lib/Url.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const addEscapedChar = reg => `(?:${reg}|&(?:amp|quot|#x27);)`;
const URL_PATH_REGEX = `(?:${addEscapedChar('[.,=(+$!*]')}?\\/${addEscapedChar('[-\\w$@.+!*:(),=%~]')}*${addEscapedChar('[-\\w~@:%)]')}|\\/)*`;
const URL_PARAM_REGEX = `(?:\\?${addEscapedChar('[-\\w$@.+!*()\\/,=%{}:;\\[\\]\\|_|~]')}*)?`;
const URL_FRAGMENT_REGEX = `(?:#${addEscapedChar('[-\\w$@.+!*()[\\],=%;\\/:~]')}*)?`;
const URL_REGEX = `(${URL_WEBSITE_REGEX}${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`;
const URL_REGEX = `((${URL_WEBSITE_REGEX})${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`;

const URL_REGEX_WITH_REQUIRED_PROTOCOL = URL_REGEX.replace(`${URL_PROTOCOL_REGEX}?`, URL_PROTOCOL_REGEX);

Expand Down
17 changes: 17 additions & 0 deletions lib/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _ from 'underscore';
import {AllHtmlEntities} from 'html-entities';
import replaceAll from 'string.prototype.replaceall';
import {CONST} from './CONST';
import {URL_REGEX} from './Url';

const REMOVE_SMS_DOMAIN_PATTERN = new RegExp(`@${CONST.SMS.DOMAIN}`, 'gi');

Expand Down Expand Up @@ -1021,6 +1022,22 @@ const Str = {
return (typeof url === 'string' && url.startsWith('/')) ? url : `/${url}`;
},

/**
* Formats a URL by converting the domain name to lowercase and adding the missing 'https://' protocol.
*
* @param {url} url The URL to be formatted
* @returns {String} The formatted URL
*/
sanitizeURL(url) {
const regex = new RegExp(`^${URL_REGEX}$`, 'i');
const match = regex.exec(url);
if (!match) {
return url;
}
const website = match[3] ? match[2] : `https://${match[2]}`;
return website.toLowerCase() + this.cutBefore(match[1], match[2]);
},

/**
* Checks if parameter is a string or function
* if it is a function then we will call it with
Expand Down