Linkify URLs containing `@`

This commit is contained in:
Ken Powers 2020-04-24 12:57:04 -04:00 committed by Scott Nonnenberg
parent d387481001
commit 980862768b
3 changed files with 26 additions and 6 deletions

View File

@ -133,6 +133,15 @@ function findLinks(text, caretLocation) {
);
}
function hasAuth(url) {
try {
const urlObject = new URL(url);
return Boolean(urlObject.username);
} catch (e) {
return null;
}
}
function getDomain(url) {
try {
const urlObject = new URL(url);
@ -215,6 +224,11 @@ function assembleChunks(chunkDescriptors) {
const ASCII_PATTERN = new RegExp('[\\u0000-\\u007F]', 'g');
function isLinkSneaky(link) {
// Any links which contain auth are considered sneaky
if (hasAuth(link)) {
return true;
}
const domain = getDomain(link);
// If the domain is falsy, something fishy is going on
if (!domain) {

View File

@ -391,5 +391,16 @@ describe('Link previews', () => {
const link = 'r.id^s.id';
assert.strictEqual(isLinkSneaky(link), true);
});
it('returns true for auth (or pretend auth)', () => {
const link = 'http://whatever.com&login=someuser@77777777';
assert.strictEqual(isLinkSneaky(link), true);
});
it('returns false for regular @ in url', () => {
const link =
'https://lbry.tv/@ScammerRevolts:b0/DELETING-EVERY-FILE-OFF-A-SCAMMERS-LAPTOP-Destroyed:1';
assert.strictEqual(isLinkSneaky(link), false);
});
});
});

View File

@ -14,7 +14,6 @@ interface Props {
}
const SUPPORTED_PROTOCOLS = /^(http|https):/i;
const HAS_AT = /@/;
export class Linkify extends React.Component<Props> {
public static defaultProps: Partial<Props> = {
@ -51,11 +50,7 @@ export class Linkify extends React.Component<Props> {
}
const { url, text: originalText } = match;
if (
SUPPORTED_PROTOCOLS.test(url) &&
!isLinkSneaky(url) &&
!HAS_AT.test(url)
) {
if (SUPPORTED_PROTOCOLS.test(url) && !isLinkSneaky(url)) {
results.push(
<a key={count++} href={url}>
{originalText}