@@ -9,6 +9,19 @@ const cyrillicPattern = /^\p{Script=Cyrillic}+$/u;
9
9
const { findCourseFromDb } = require ( "../../db/services/courseService" ) ;
10
10
const { bridgedMessagesCounter } = require ( "../../promMetrics/promCounters" ) ;
11
11
12
+
13
+ // github copilot generated function to prevent telegram & discord links
14
+ const isMessageSafeToSend = ( message ) => {
15
+ const telegramLinkPattern = / ( h t t p s ? : \/ \/ ) ? ( w w w \. ) ? t ( e l e g r a m ) ? \. m e \/ [ a - z A - Z 0 - 9 \- _ ] * / g;
16
+ const discordLinkPattern = / ( h t t p s ? : \/ \/ ) ? ( w w w \. ) ? d i s c o r d \. g g \/ [ a - z A - Z 0 - 9 \- _ ] * / g;
17
+
18
+ if ( telegramLinkPattern . test ( message ) || discordLinkPattern . test ( message ) ) {
19
+ return false ;
20
+ }
21
+
22
+ return true ;
23
+ } ;
24
+
12
25
const validDiscordChannel = async ( courseName ) => {
13
26
const guild = await discordClient . guilds . fetch ( process . env . GUILD_ID ) ;
14
27
courseName = courseName . replace ( / / g, "-" ) . toLowerCase ( ) ;
@@ -48,6 +61,11 @@ const sendMessageToDiscord = async (ctx, message, channel) => {
48
61
if ( message . content . text && message . content . text [ 0 ] === "/" ) {
49
62
return ;
50
63
}
64
+ if ( ! isMessageSafeToSend ( message . content . text ) ) {
65
+ console . log ( "Message contains a telegram or discord link" ) ;
66
+ return ;
67
+ }
68
+
51
69
const webhooks = await channel . fetchWebhooks ( ) ;
52
70
const webhook = webhooks . first ( ) ;
53
71
if ( message . content . text ) {
@@ -246,9 +264,10 @@ const validateContent = (content) => {
246
264
} ;
247
265
248
266
const sendMessageToTelegram = async ( telegramId , content , sender , channel ) => {
249
- if ( content === "" ) {
267
+ if ( content === "" || ! isMessageSafeToSend ( content ) ) {
250
268
return ;
251
269
}
270
+
252
271
sender ? sender = escapeChars ( sender ) : sender = null ;
253
272
content = validateContent ( content ) ;
254
273
try {
0 commit comments