Spaces:
Running
Running
Download middleware/sanitizeMongo.js from 4send/messenger: direct link, hf CLI and curl.
- Browser
- Download file 728 Bytes
-
https://huggingface.co/spaces/4send/messenger/resolve/main/middleware/sanitizeMongo.js
- Command line
-
hf download hf://spaces/4send/messenger/middleware/sanitizeMongo.js
-
curl -L -o sanitizeMongo.js https://huggingface.co/spaces/4send/messenger/resolve/main/middleware/sanitizeMongo.js
728 Bytes
| const sanitizeMongo = (obj, depth = 0) => { | |
| if (!obj || typeof obj !== 'object') return; | |
| if (depth > 10) return; | |
| if (Array.isArray(obj)) { | |
| obj.forEach(item => sanitizeMongo(item, depth + 1)); | |
| return; | |
| } | |
| for (const key of Object.keys(obj)) { | |
| if (key.startsWith('$') || key.includes('.') || key === '__proto__' || key === 'constructor' || key === 'prototype') { | |
| delete obj[key]; | |
| } else { | |
| sanitizeMongo(obj[key], depth + 1); | |
| } | |
| } | |
| }; | |
| const sanitizeMiddleware = (req, res, next) => { | |
| sanitizeMongo(req.body); | |
| sanitizeMongo(req.query); | |
| sanitizeMongo(req.params); | |
| next(); | |
| }; | |
| module.exports = { sanitizeMongo, sanitizeMiddleware }; | |