Skip to content

Commit

Permalink
Merge pull request #146 from destinygg/new-url-crash-protection
Browse files Browse the repository at this point in the history
new URL crash protection.
  • Loading branch information
11k authored Jan 23, 2024
2 parents 0778b94 + 8d4bdc4 commit 66870ee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/services/dgg-rolling-chat-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion lib/services/message-matching.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(new RegExp(linkRegex, 'g')),
(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;
Expand Down

0 comments on commit 66870ee

Please sign in to comment.