tags:

views:

2943

answers:

9

I want to prevent XSS attacks in my web application. I found that HTML Encoding the output can really prevent XSS attacks. Now the problem is that how do I HTML encode every single output in my application? I there a way to automate this?

I appreciate answers for JSP, ASP.net and PHP.

A: 

If you do actually HTML encode every single output, the user will see plain text of <html> instead of a functioning web app.

EDIT: If you HTML encode every single input, you'll have problem accepting external password containing < etc..

eed3si9n
I mean output string.....
Niyaz
Everything from ASP.NET or PHP's point of view is just a string in response buffer.
eed3si9n
You don't encode the input before you process it. You encode the input when you output it to the user... this is how XSS attacks work.
David McLaughlin
@David Of course.
eed3si9n
+4  A: 

You don't want to encode all HTML, you only want to HTML-encode any user input that you're outputting.

For PHP: htmlentities and htmlspecialchars

David McLaughlin
A: 

A nice way I used to escape all user input is by writing a modifier for smarty wich escapes all variables passed to the template; except for the ones that have |unescape attached to it. That way you only give HTML access to the elements you explicitly give access to.

I don't have that modifier any more; but about the same version can be found here:

http://www.madcat.nl/martijn/archives/16-Using-smarty-to-prevent-HTML-injection..html

In the new Django 1.0 release this works exactly the same way, jay :)

D4V360
+1  A: 

You could wrap echo / print etc. in your own methods which you can then use to escape output. i.e. instead of

echo "blah";

use

myecho('blah');

you could even have a second param that turns off escaping if you need it.

In one project we had a debug mode in our output functions which made all the output text going through our method invisible. Then we knew that anything left on the screen HADN'T been escaped! Was very useful tracking down those naughty unescaped bits :)

reefnet_alex
A: 

My personal preference is to diligently encode anything that's coming from the database, business layer or from the user.

In ASP.Net this is done by using Server.HtmlEncode(string) .

The reason so encode anything is that even properties which you might assume to be boolean or numeric could contain malicious code (For example, checkbox values, if they're done improperly could be coming back as strings. If you're not encoding them before sending the output to the user, then you've got a vulnerability).

Peter Bernier
A: 

The only way to truly protect yourself against this sort of attack is to rigorously filter all of the input that you accept, specifically (although not exclusively) from the public areas of your application. I would recommend that you take a look at Daniel Morris's PHP Filtering Class (a complete solution) and also the Zend_Filter package (a collection of classes you can use to build your own filter).

PHP is my language of choice when it comes to web development, so apologies for the bias in my answer.

Kieran.

Kieran Hall
+5  A: 

One thing that you shouldn't do is filter the input data as it comes in. People often suggest this, since it's the easiest solution, but it leads to problems.

Input data can be sent to multiple places, besides being output as HTML. It might be stored in a database, for example. The rules for filtering data sent to a database are very different from the rules for filtering HTML output. If you HTML-encode everything on input, you'll end up with HTML in your database. (This is also why PHP's "magic quotes" feature is a bad idea.)

You can't anticipate all the places your input data will travel. The safe approach is to prepare the data just before it's sent somewhere. If you're sending it to a database, escape the single quotes. If you're outputting HTML, escape the HTML entities. And once it's sent somewhere, if you still need to work with the data, use the original un-escaped version.

This is more work, but you can reduce it by using template engines or libraries.

JW
A: 

there was a good essay from Joel on software (making wrong code look wrong I think, I'm on my phone otherwise I'd have a URL for you) that covered the correct use of Hungarian notation. The short version would be something like:

Var

dsFirstName, uhsFirstName : String;

Begin

uhsFirstName := request.queryfields.value['firstname'];

dsFirstName := dsHtmlToDB(uhsFirstName);

Basically prefix your variables with something like "us" for unsafe string, "ds" for database safe, "hs" for HTML safe. You only want to encode and decode where you actually need it, not everything. But by using they prefixes that infer a useful meaning looking at your code you'll see real quick if something isn't right. And your gong to need different encode/decode functions anyways.

ddowns
The really short version would be to never use Hungarian notation, because if you're not using an IDE that can immediately tell you what type the variable is or provide a decently descriptive variable name, you're not doing it right.
MetroidFan2002
+3  A: 

For JSPs, you can have your cake and eat it too, with the c:out tag, which escapes XML by default. This means you can bind to your properties as raw elements:

<input name="someName.someProperty" value="<c:out value='${someName.someProperty}' />" />

When bound to a string, someName.someProperty will contain the XML input, but when being output to the page, it will be automatically escaped to provide the XML entities. This is particularly useful for links for page validation.

MetroidFan2002