XSS (Cross-Site Scripting) — injecting malicious scripts into web pages.
Prevention techniques:
1// Using helmet for CSP2const helmet = require("helmet");3app.use(helmet());45// Manual escaping6function escapeHtml(str) {7 return str8 .replace(/&/g, "&")9 .replace(/</g, "<")10 .replace(/>/g, ">")11 .replace(/"/g, """)12 .replace(/'/g, "'");13}1415// Input validation with express-validator16const { body, validationResult } = require("express-validator");17app.post("/comment", [18 body("text").trim().escape(),19], (req, res) => {20 const errors = validationResult(req);21 if (!errors.isEmpty()) return res.status(400).json(errors);22});
Key points: