What is the best way to sanitize message content on the server side which is received from client as one of the query string parameters? This message is also meant to be resend to other connected clients so it have be secure in terms of code execution or injection (JavaScript or HTML) on server or client side.
asked Jan 30, 2011 at 21:05
yojimbo87
68.8k26 gold badges128 silver badges133 bronze badges
-
2What is your server-side technology? Javascript is purely client-side so the server-side sanitization is done in whatever programming language you are using at the server end. Example server-side languages would be PHP, Java etcchristophmccann– christophmccann2011年01月30日 21:08:42 +00:00Commented Jan 30, 2011 at 21:08
-
He's using node.js. nodejs.org Unfortunately I don't have enough knowledge to answer his question with that server-side technology.Macy Abbey– Macy Abbey2011年01月30日 21:11:15 +00:00Commented Jan 30, 2011 at 21:11
-
1What exactly are you trying to prevent by sanitizing the data? SQL injection? XSS?Matt Ball– Matt Ball2011年01月30日 21:24:22 +00:00Commented Jan 30, 2011 at 21:24
-
3This totally depends on what exactly is going to be done with the data, through what protocols it is going to be re-sent etc. There is no general answer for this.Pekka– Pekka2011年01月30日 21:31:02 +00:00Commented Jan 30, 2011 at 21:31
-
@Matt Ball: Prevent XSS and server side code execution (don't know if it's possible in this case because node.js is JS code and you can send malicious JS code as a query string parameter to be executed on the server side).yojimbo87– yojimbo872011年01月30日 21:34:34 +00:00Commented Jan 30, 2011 at 21:34
2 Answers 2
To protect node.js against XSS I borrowed this from snippet 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, '"');
}
P.S: You should always do proper server-side filtering
answered Jan 31, 2011 at 0:43
Alfred
62k33 gold badges151 silver badges186 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You could use node-validator, it looks like a more comprehensive solution to the aboce snippet.
Comments
lang-js