Skip to content

Commit 05f60c1

Browse files
committed
sanitize token before use in endpoint
1 parent 9fb1a21 commit 05f60c1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

discord-bot/app.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,16 @@ app.post('/interactions', verifyKeyMiddleware(process.env.DISCORD_PUBLIC_KEY), a
187187
if (name === 'ask') {
188188
const context = req.body.context;
189189
const userId = context === 0 ? req.body.member.user.id : req.body.user.id
190-
191190
const question = data.options[0]?.value || 'No question provided';
192-
const endpoint = `webhooks/${process.env.DISCORD_APP_ID}/${req.body.token}/messages/@original`;
191+
192+
// Sanitize token before use in endpoint
193+
const token = req.body.token;
194+
const tokenRegex = /^[A-Za-z0-9-_]+$/;
195+
if (!tokenRegex.test(token)) {
196+
return res.status(400).json({ error: 'Invalid token format' });
197+
}
198+
199+
const endpoint = `webhooks/${process.env.DISCORD_APP_ID}/${token}/messages/@original`;
193200
const initialMessage = `\n> ${question}\n\nLet me find the answer for you. This might take a moment`
194201
let followUpMessage = "Something went wrong! Please try again later.";
195202

0 commit comments

Comments
 (0)