From 9164bcd04512dd0f92a3cbde5d6e8f984578ae8f Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Wed, 27 Nov 2019 20:41:45 +0200 Subject: [PATCH] Fix URL matching in automod, censor, and spam plugin --- backend/src/plugins/Automod.ts | 1 + backend/src/utils.test.ts | 21 +++++++++++++++++++++ backend/src/utils.ts | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 backend/src/utils.test.ts diff --git a/backend/src/plugins/Automod.ts b/backend/src/plugins/Automod.ts index e8982687..1ddac0a8 100644 --- a/backend/src/plugins/Automod.ts +++ b/backend/src/plugins/Automod.ts @@ -640,6 +640,7 @@ export class AutomodPlugin extends ZeppelinPlugin { protected evaluateMatchLinksTrigger(trigger: TMatchLinksTrigger, str: string): boolean { const links = getUrlsInString(str, true); + for (const link of links) { const normalizedHostname = link.hostname.toLowerCase(); diff --git a/backend/src/utils.test.ts b/backend/src/utils.test.ts new file mode 100644 index 00000000..cc0eca24 --- /dev/null +++ b/backend/src/utils.test.ts @@ -0,0 +1,21 @@ +import { getUrlsInString } from "./utils"; + +import test from "ava"; + +test("Detects full links", t => { + const urls = getUrlsInString("foo https://google.com/ bar"); + t.is(urls.length, 1); + t.is(urls[0].hostname, "google.com"); +}); + +test("Detects partial links", t => { + const urls = getUrlsInString("foo google.com bar"); + t.is(urls.length, 1); + t.is(urls[0].hostname, "google.com"); +}); + +test("Detects subdomains", t => { + const urls = getUrlsInString("foo photos.google.com bar"); + t.is(urls.length, 1); + t.is(urls[0].hostname, "photos.google.com"); +}); diff --git a/backend/src/utils.ts b/backend/src/utils.ts index dee6b8d9..b04d25e3 100644 --- a/backend/src/utils.ts +++ b/backend/src/utils.ts @@ -189,7 +189,7 @@ const urlRegex = /(\S+\.\S+)/g; const protocolRegex = /^[a-z]+:\/\//; export function getUrlsInString(str: string, unique = false): url.URL[] { - let matches = (str.match(urlRegex) || []).map(m => m[0]); + let matches = str.match(urlRegex) || []; if (unique) matches = Array.from(new Set(matches)); return matches.reduce((urls, match) => {