Reader small image

You're reading from  JavaScript Security

Product typeBook
Published inNov 2014
Reading LevelIntermediate
Publisher
ISBN-139781783988006
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Eugene Liang
Eugene Liang
author image
Eugene Liang

Y.E Liang is a researcher, author, web developer, and business developer. He has experience in both frontend and backend development, particularly in engineering, user experience using JavaScript/CSS/HTML, and performing social network analysis. He has authored multiple books and research papers.
Read more about Eugene Liang

Right arrow

Defending against cross-site scripting


We will go through the basic techniques of defending against cross-site scripting. This is by no means a comprehensive list of defenses against cross-site scripting, but it should be enough to get you started.

Do not trust users – parsing input by users

We can parse the user's input using various techniques. Since we are talking about JavaScript in this book, we can apply the following JavaScript function to prevent the execution of malicious code:

    function htmlEntities(str) {
        return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    }

This function effectively strips the malicious code from the user's input and output as normal strings. To see this function in action, simply refer to the source code for this chapter. You can find this function in use at python_server/templates/todos_secure.html. For ease of reference, the code snippet is being applied here as...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
JavaScript Security
Published in: Nov 2014Publisher: ISBN-13: 9781783988006

Author (1)

author image
Eugene Liang

Y.E Liang is a researcher, author, web developer, and business developer. He has experience in both frontend and backend development, particularly in engineering, user experience using JavaScript/CSS/HTML, and performing social network analysis. He has authored multiple books and research papers.
Read more about Eugene Liang