What are the risks of using Javascript and how to avoid them?
Share
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.
One of the most common errors is HTML injection, allowing third parties to inject JavaScript into your security context. That allows an attacker to control what a user does on your site, completely breaking account security.
Whilst there has been some slow progress trying to get web authors to remember to HTML-encode strings they output into web pages at the server side (eg
htmlspecialcharsin PHP), a new generation of webapps are using the same dumb string-concatenation hacks to create content at the client-side using JavaScript:often using jQuery:
This is just as vulnerable as server-side HTML injection and authors really need to stop building content this way. You can HTML-encode text content at the client side, but since JS doesn’t have a built-in HTML encoder you’d have to do it yourself:
However it’s usually much better to use the available DOM methods and properties that obviate the need for escaping:
and with jQuery use
attr(),text()and the creation shortcuts: