Spaces:
Running

File size: 575 Bytes
71601b1
 
 
 
 
 
 
 
 
d0612ee
71601b1
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 };