mirror of
https://github.com/itsnaveenk/bazar3.git
synced 2025-12-19 23:17:07 +00:00
37 lines
996 B
JavaScript
37 lines
996 B
JavaScript
const crypto = require('crypto');
|
|
|
|
function sanitize(input) {
|
|
if (typeof input === 'string') {
|
|
return input.replace(/</g, "<").replace(/>/g, ">");
|
|
}
|
|
if (Array.isArray(input)) {
|
|
return input.map(sanitize);
|
|
}
|
|
if (input && typeof input === 'object') {
|
|
const sanitizedObj = {};
|
|
for (const key in input) {
|
|
if (Object.hasOwnProperty.call(input, key)) {
|
|
sanitizedObj[key] = sanitize(input[key]);
|
|
}
|
|
}
|
|
return sanitizedObj;
|
|
}
|
|
return input;
|
|
}
|
|
|
|
module.exports = {
|
|
anonymizeIP: (req, res, next) => {
|
|
const ip = req.ip || '127.0.0.1';
|
|
const salt = Math.floor(Date.now() / 3600000);
|
|
req.anonymizedIP = crypto.createHash('sha3-256')
|
|
.update(ip + salt + process.env.IP_PEPPER)
|
|
.digest('hex');
|
|
next();
|
|
},
|
|
sanitizeInput: (req, res, next) => {
|
|
if (req.body) req.body = sanitize(req.body);
|
|
if (req.query) req.query = sanitize(req.query);
|
|
if (req.params) req.params = sanitize(req.params);
|
|
next();
|
|
}
|
|
}; |