From d8e8f2fe17c849c85dc6d66f1bf0f540410ed461 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Tue, 23 Jan 2024 18:05:19 +1300 Subject: [PATCH 1/3] new URL crash protection. --- lib/chat-utils/link-regex.js | 2 +- lib/services/message-matching.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/chat-utils/link-regex.js b/lib/chat-utils/link-regex.js index f324d57..e2a547b 100644 --- a/lib/chat-utils/link-regex.js +++ b/lib/chat-utils/link-regex.js @@ -1427,5 +1427,5 @@ const scheme = '(https?|ftp|wss?)://'; const strict = `\\b${scheme}${pathCont}`; const relaxed = `${strict}|${webURL}`; -const linkRegex = new RegExp(relaxed, 'i'); +const linkRegex = new RegExp(relaxed, 'gi'); module.exports = linkRegex; diff --git a/lib/services/message-matching.js b/lib/services/message-matching.js index eb0eaf7..b80999c 100644 --- a/lib/services/message-matching.js +++ b/lib/services/message-matching.js @@ -5,7 +5,18 @@ module.exports = { return linkRegex.test(message); }, getLinks(message) { - return Array.from(message.matchAll(new RegExp(linkRegex, 'g')), (k) => new URL(k[0])); + return Array.from( + message.matchAll(linkRegex), + (k) => (k[0].startsWith('http') ? k[0] : `http://${k[0]}`), // add protocol to link + ) + .map((link) => { + try { + return new URL(link); // crash protection + } catch (e) { + return null; + } + }) + .filter((link) => link); // remove null }, mentionsUser(message, user) { if (!user) return false; From 83d286297e05ac74ec6f8123c84278e9f67fdf73 Mon Sep 17 00:00:00 2001 From: Farhad Jay Date: Tue, 23 Jan 2024 11:01:37 -0800 Subject: [PATCH 2/3] Restore original regex global flag use Including the flag in the original, shared regex requires resetting `lastIndex` after each use. You'll get unexpected results otherwise. Also fixes all failing tests for `getLinks()`. --- lib/chat-utils/link-regex.js | 2 +- lib/services/message-matching.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chat-utils/link-regex.js b/lib/chat-utils/link-regex.js index e2a547b..f324d57 100644 --- a/lib/chat-utils/link-regex.js +++ b/lib/chat-utils/link-regex.js @@ -1427,5 +1427,5 @@ const scheme = '(https?|ftp|wss?)://'; const strict = `\\b${scheme}${pathCont}`; const relaxed = `${strict}|${webURL}`; -const linkRegex = new RegExp(relaxed, 'gi'); +const linkRegex = new RegExp(relaxed, 'i'); module.exports = linkRegex; diff --git a/lib/services/message-matching.js b/lib/services/message-matching.js index b80999c..3c011ec 100644 --- a/lib/services/message-matching.js +++ b/lib/services/message-matching.js @@ -6,7 +6,7 @@ module.exports = { }, getLinks(message) { return Array.from( - message.matchAll(linkRegex), + message.matchAll(new RegExp(linkRegex, 'g')), (k) => (k[0].startsWith('http') ? k[0] : `http://${k[0]}`), // add protocol to link ) .map((link) => { From 8d4bdc4c07eba04cbc99f0d667949fbfbd7c9f0d Mon Sep 17 00:00:00 2001 From: Farhad Jay Date: Tue, 23 Jan 2024 11:15:45 -0800 Subject: [PATCH 3/3] No longer use `message` as a key in logger context This break message parsing in our log store. --- lib/services/dgg-rolling-chat-cache.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/services/dgg-rolling-chat-cache.js b/lib/services/dgg-rolling-chat-cache.js index 86ceaff..139d8af 100644 --- a/lib/services/dgg-rolling-chat-cache.js +++ b/lib/services/dgg-rolling-chat-cache.js @@ -86,7 +86,10 @@ class ChatCache { return; } this.messageMatching.getLinks(message).forEach((link) => { - this.logger.info({ user, url: link.hostname + link.pathname, message }, 'Cached viewer url'); + this.logger.info( + { user, url: link.hostname + link.pathname, msg: message }, + 'Cached viewer url', + ); if (!_.has(this.viewerUrlMap, user)) this.viewerUrlMap[user] = []; this.viewerUrlMap[user].push({ url: link.hostname + link.pathname,