Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Andrew Young
Andrew Young
Courses Plus Student 639 Points

How to prevent cross site script attacking?

I've watch a video here

And after that I was wondering how do I prevent that attack with ndoe.js and body-parser

Here is a short code and anybody can help me how to prevent this attack

Normal node.js form handle script

var express = require('express');
var app = express();

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/form', function(req, res) {
  res.send('<form action="/" method="post"><input type="text" name="input"><br /><input type="submit"></form>');

app.post('/', function(req, res) {

As the code above I can promise it can be attacked by cross site scripting but how to prevent??


Sorry for unclear instruction, I update the above code with a URL /form to easily explain how the cross site script attacking (XSS) can be used

As the above code, we have a form which you can submit text to the url / and it'll print out the text (info) you type so just imagine if I type hello world! it'll just print out all the thing in your input for this one it's hello world!, but how about I type <script>alert("you have been attack");</script> my code will print out the text and then the browser will treat it as a javascript, not a text which means browser will run it, so I'm asking how to prevent this?

Steven Parker
Steven Parker
227,141 Points

The vulnerability here is not obvious, can you give an example of how the attack would be performed?

1 Answer

Steven Parker
Steven Parker
227,141 Points

Your revised example is much better. The best defense against XSS attacks is known as "sanitizing the data". This means parsing any input that will be used to construct and execute code, and identifying and removing any components that might cause it to be interpreted as part of the code. For input that will be incorporated in HTML, sanitizing might be done by removing any instances of <script> tags, or perhaps not allowing tags of any kind.

But note that in this particular case, the vulnerability is not serious, since what the user inputs is only sent back to his own browser to perform. A user can always provide a script to his own browser and create the same results. I might not bother sanitizing this particular code since all the user can do with it is amuse himself.

And, I can't discuss data sanitizing without at least mentioning this XKCD comic strip.

Steven Parker
Steven Parker
227,141 Points

An HTML "escape" function would indeed be helpful for sanitizing, as it would covert tags like "<script>" into "&lt;script&gt;", to cause them to be displayed instead of acted on by the browser.