服务器端消息数据安全

将从客户端接收的服务器端消息内容清理为查询string参数之一的最佳方法是什么? 这个消息也意味着被重新发送到其他连接的客户端,所以它在服务器或客户端的代码执行或注入(JavaScript或HTML)方面是安全的。

为了保护node.js免受XSS的影响,我从片断玉中借用了这个:

/** * Escape the given string of `html`. * * @param {String} html * @return {String} * @api private */ function sanitize(html){ return String(html) .replace(/&(?!\w+;)/g, '&amp;') .replace(/</g, '&lt;') .replace(/>/g, '&gt;') .replace(/"/g, '&quot;'); } 

PS:你应该总是做适当的服务器端过滤

你可以使用节点validation器 ,它看起来像一个更全面的解决scheme的aboce片段。