From f048c1d07704ff0828e791bb7b7f858289a812c0 Mon Sep 17 00:00:00 2001 From: ThePendulum Date: Sun, 18 Oct 2026 02:15:23 +0200 Subject: [PATCH] Escaping trivia dataset before passing it into regex. --- src/games/trivia.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/games/trivia.js b/src/games/trivia.js index b0019a5..eccc53d 100755 --- a/src/games/trivia.js +++ b/src/games/trivia.js @@ -226,6 +226,15 @@ function onCommand(args, context) { } } +// answers are plain text and may contain regex characters, like '*batteries not included' +function getAnswerRegexp(answer) { + const pattern = answer + .replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + .replace(/\\[.?*]|!/g, '$&?'); // punctuation is optional, so T.S. Eliot also matches TS Eliot, and *NSYNC matches NSYNC + + return new RegExp(pattern, 'i'); +} + async function onMessage(message, context) { const game = games.get(context.room?.id); @@ -235,7 +244,7 @@ async function onMessage(message, context) { const { answer, fullAnswer } = game.questions[game.round]; - if (new RegExp(answer, 'i').test(decode(message.originalBody || message.body)) && !game.answers.has(context.user.id)) { // resolve HTML entities in case original body is not available, such as & to & + if (getAnswerRegexp(answer).test(decode(message.originalBody || message.body)) && !game.answers.has(context.user.id)) { // resolve HTML entities in case original body is not available, such as & to & game.answers.set(context.user.id, { user: context.user, answer: message.body,