在服务器端清理作为查询字符串参数之一从客户端接收的消息内容的最佳方法是什么?此消息还意味着将重新发送到其他连接的客户端,因此它在服务器或客户端的代码执行或注入(inject)(JavaScript 或 HTML)方面是安全的。
最佳答案
为了保护 node.js 免受 XSS 攻击,我从片段 jade 中借用了这个:
/**
* Escape the given string of `html`.
*
* @param {String} html
* @return {String}
* @api private
*/
function sanitize(html){
return String(html)
.replace(/&(?!\w+;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
附注:您应该始终进行适当的服务器端过滤
关于javascript - 服务器端消息数据安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4845530/