How do I make sure I don’t escape something twice?
I’ve heard that its good practice to escape values as you receive them from a form, and also escape when you output. That way you have two chances to catch something.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I presume that you’re using JSP.
Just escape during display only. There for the JSTL
<c:out>tag is perfectly suitable. It escapes HTML entities by default. Use it to display every user-controlled input, such as request URL, request headers and request parameters.E.g.
Escaping during input is not needed. XSS doesn’t harm in raw Java code nor in SQL databases. On the other hand, you would also rather save data unmodified in DB so that you can still see what the user actually entered, so that you can if necessary do social actions on mailicious users.
If you’d like to know what to escape during input, it would be SQL injection. In such case just use
PreparedStatementinstead of regularStatementwhenever you want to save any user-controlled input in the database.E.g.