Spaces:
Running

messenger / middleware /botDetection.js
Vyacheslav
bugfix
d0612ee
Raw History Blame
575 Bytes
const botDetection = (req, res, next) => {
const dangerousChars = /["';<>]/g;
['user-agent', 'referer', 'x-forwarded-for'].forEach(h => {
if (req.headers[h] && dangerousChars.test(req.headers[h])) {
req.headers[h] = req.headers[h].replace(dangerousChars, "");
}
});
const ua = req.headers['user-agent'];
if (!ua || ua.length < 10) return res.status(403).send('Bot detected');
if (req.headers['content-length'] > 30 * 1024 * 1024) return res.status(413).send('Too large');
next();
};
module.exports = { botDetection };