screentinker/server/middleware/sanitize.js
ScreenTinker 1594a9d4a4 Initial open source release
ScreenTinker - open source digital signage management software.
MIT License, all features included, no license gates.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 12:14:53 -05:00

26 lines
673 B
JavaScript

// Simple XSS sanitizer for user input strings
function sanitizeString(str) {
if (typeof str !== 'string') return str;
return str
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#x27;');
}
// Middleware: sanitize common body fields
function sanitizeBody(req, res, next) {
if (req.body) {
const fieldsToSanitize = ['name', 'title', 'filename'];
for (const field of fieldsToSanitize) {
if (typeof req.body[field] === 'string') {
req.body[field] = sanitizeString(req.body[field]);
}
}
}
next();
}
module.exports = { sanitizeString, sanitizeBody };