Most Recent developments in Software engineering, ICT, Artificial Intelligence, Programming Languages, Cyber Security and other tech articles around the globe.
Avoiding HTML injections and Cross Site Attacks on Input Fields
Get link
Facebook
X
Pinterest
Email
Other Apps
Django templates are often used to pass data to JavaScript code. Unfortunately, if implemented incorrectly, this opens up the possibility of HTML injection, and thus XSS (Cross-Site Scripting) attacks.
This is one of the most common security problems I’ve encountered on Django projects. In fact I’ve probably seen it on every considerably-sized Django project, in some form or another.
Also, not naming and shaming, but I’ve also seen it in lots of community resources. This includes conference talks, blog posts, and Stack Overflow answers.
It’s hard to get right! It’s also been historically difficult, since it’s only Django 2.1 that added the json_script template tag to do this securely. (And the ticket was open six years!)
Let’s look the problem and how we can fix it with json_script.
Unfortunately as written, the template is open to HTML injection. This is because if the data contains </script> anywhere, the rest of the result will be parsed as extra HTML. We call this HTML injection, and attackers can use it to add arbitrary (evil) content to your site.
If the mydata is controllable by third parties in any way, for example a user’s comment, or an API’s return data, attackers might try and use it for HTML injection.
The browser first parses the page by HTML tags only - with no inspection of the JavaScript inside.
So it sees the first <script> as closing after mydata = ". It will attempt to run that JavaScript, which will crash with an error about the incomplete string.
It will then parse the second, injected <script> tag as a legitimate part of the page. This means it loads evil.js.
Finally it will render the trailing "; as text, and ignore the last </script> as it doesn’t match an opening <script>.
evil.jsprobably does some evil, such as stealing your user’s session cookie and sending it to the attacker.
Ruh roh.
Beware ‘safe’
Our template would be safe if we didn’t use |safe. Whenever we use the safe template filter, what we’re really saying is “I promise this data is safe for direct inclusion in HTML”. And that’s not the case here.
If we remove it from the template:
<script>constmydata="{{ mydata }}";</script>
Then we’d not be open to the above attack. But the data would not render as intended:
Because all the HTML entities have been escaped, the string will not be usable in JavaScript as intended. Or you’d need to write extra JavaScript to unescape them, which would also open up the opportunity for attack again.
Another Vulnerable Way
Another common vulnerable pattern is to use json.dumps() in the view, and call that value “safe” in the template. For example, take this view:
This looks safer, since we’re serializing the data into JSON, and using the “safe” template filter. Unfortunately it’s just as vulnerable, because it’s also not HTML safe.
Imagine again that mydata was again the same string as above. That would make mydata_json equal to:
Again we have the same problem. The browser will parse the HTML as an incomplete <script>, then another <script> to include evil.js, then the text ";, and finally an ignored </script>.
The Secure Way
The best way to avoid this vulnerability with Django is to use the json_script template tag. This outputs the data in an HTML injection proof way, by using a JSON script tag.
This is a <script>, but since its type is "application/json" and not a JavaScript type, the browser won’t execute it. Django has replaced every HTML sensitive character with its JSON string unicode escape form, such as \u003C. Thus the browser will never see any closing </script> tags or similar.
We also need to change our JavaScript to fetch the data from that element. Adapting from the Django documentation, the end result would look like:
Update (2020-02-19): Added this section, thanks to James Bligh and Tom Grainger for pushing me.
If you want to be even more secure, you can go one step further and avoid using inline <script> tags in your template altogether. That is, move your JavaScript into its own static file, mypage.js:
This is admittedly a litle more effort. But it prevents the problem from ever occurring in your code, because your JavaScript never passes through templating.
You can reduce your XSS risk even further by banning inline scripts on your site. You’d do this with a Content Security Policy (CSP) using the script-src directive. See my post How to Score A+ for Security Headers on Your Django Website for more information.
What about ‘escapejs’?
Update (2020-02-19): Added this section, thanks to a question from Ed Rivas on Twitter.
Django also provides the escapejs template tag, that looks like it might work. You might be tempted to use it like this:
Escapes characters for use in JavaScript strings. This does not make the string safe for use in HTML or JavaScript template literals, but does protect you from syntax errors when using templates to generate JavaScript/JSON.
It’s also not generally useful since it only works for strings, not dictionaries or lists.
Technical documentation in software engineering is the umbrella term that encompasses all written documents and materials dealing with software product development. All software development products, whether created by a small team or a large corporation, require some related documentation. And different types of documents are created through the whole software development lifecycle (SDLC). Documentation exists to explain product functionality, unify project-related information, and allow for discussing all significant questions arising between stakeholders and developers. Project documentation by stages and purpose On top of that, documentation errors can set gaps between the visions of stakeholders and engineers and, as a result, a proposed solution won’t meet stakeholders expectations. Consequently, managers should pay a lot of attention to documentation quality. Agile and waterfall approaches The documentation types that the team produces and its scope depending...
7 Steps of effective software product development life cycle #Product label Tech-intensive lifestyle induces software to be an integral part of the everyday routine in the 21st century. Today, it is hardly possible to imagine any activity not powered by some kind of computer-related processes. When digging deeper, software product development is a highly organized process with precise procedures and strictly defined steps known as Software Development Life Cycle (SDLC). Whenever you need a sophisticated system, software suite or end-user web or mobile app your outstanding project delivery, besides all the other important factors, largely depends on a set of processes practiced by the development team. The Software Development Life Cycle as a collection of rules and practices helps to connect tech, non-tech team members and project stakeholders to transform your exceptional idea into a unique software product or solution. It structures the work of the development teams enabling th...
On Demand Services App Remember Richie Rich, the animated show that we used to watch? It had an Robot maid named Irona. We all wished we had someone like her who could complete all our household chores in seconds. Well, that dream still seems to be far from us for at least a century; however, there is a substitute to it which is as effective as Irona. on-demand apps for Home Services is that substitute. We all know how on-demand apps have disrupted majority of traditional industries. From the way we travel, eat, shop, and even date, all has undergone a tremendous change. So, why not our household chores and errands? After all we all need an Irona in our lives who can complete our household chores and run our errands in a jiffy. Before we understand the nitty gritty of on-demand home services apps , let us start from the basic at what exact services that it provides. As the name suggests it serves as a platform where you can hire professionals for all you...
Comments
Post a Comment