Skip to content

Commit 66870ee

Browse files
authored
Merge pull request #146 from destinygg/new-url-crash-protection
new URL crash protection.
2 parents 0778b94 + 8d4bdc4 commit 66870ee

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/services/dgg-rolling-chat-cache.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ class ChatCache {
8686
return;
8787
}
8888
this.messageMatching.getLinks(message).forEach((link) => {
89-
this.logger.info({ user, url: link.hostname + link.pathname, message }, 'Cached viewer url');
89+
this.logger.info(
90+
{ user, url: link.hostname + link.pathname, msg: message },
91+
'Cached viewer url',
92+
);
9093
if (!_.has(this.viewerUrlMap, user)) this.viewerUrlMap[user] = [];
9194
this.viewerUrlMap[user].push({
9295
url: link.hostname + link.pathname,

lib/services/message-matching.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ module.exports = {
55
return linkRegex.test(message);
66
},
77
getLinks(message) {
8-
return Array.from(message.matchAll(new RegExp(linkRegex, 'g')), (k) => new URL(k[0]));
8+
return Array.from(
9+
message.matchAll(new RegExp(linkRegex, 'g')),
10+
(k) => (k[0].startsWith('http') ? k[0] : `http://${k[0]}`), // add protocol to link
11+
)
12+
.map((link) => {
13+
try {
14+
return new URL(link); // crash protection
15+
} catch (e) {
16+
return null;
17+
}
18+
})
19+
.filter((link) => link); // remove null
920
},
1021
mentionsUser(message, user) {
1122
if (!user) return false;

0 commit comments

Comments
 (0)