tags:

views:

189

answers:

5

In other words, what are the most-used techniques to sanitize input and/or output nowadays? What do people in industrial (or even just personal-use) websites use to combat the problem?

+1  A: 

The best way to filter XSS is to simply encode all user input.

orokusaki
... with html entity (he can encode it in base 64 :) )
remi bourgarel
@remi - are you serious?
orokusaki
@orokusaki [You never know :)](http://www.recursion.com/interpolique.html)
Sidnicious
A: 

If you are developing in .NET one of the most effective ways to avoid XSS is to use the Microsoft AntiXSS Library. It's a very effective way to sanitize your input.

backslash17
A: 

In JSTL/JSP the best way to protect against XSS is to use the c:out tag without setting the default escapeXml parameter equal to false.

<c:out value="${somePossiblyDangerousVar}"/>
BuckWild
A: 

There are only two major areas in your code which need to be addressed properly to avoid xss issues.

  1. before using any user input value in queries, use the database helper functions like mysql_escape_string over the data and then use it in query. It will gurantee xss safety.

  2. before displaying user input values back into form input fields, pass them through htmlspecialchars or htmlentities. This will convert all xss prone values into characters that the browser can display without being compromised.

Once you have done the above, you are more than 95% safe from xss attacks. Then you can go on and learn advanced techniques from security websites and apply additional security on your site.

What most frameworks do is that they discourage you to directly write html form code or do queries in string form, so that using the framework helper functions your code remains clean, while any serious problem can be addressed quickly by just updating one or two lines of code in the framework. You can simply write a little library of your own with common functions and reuse them in all your projects.

Samnan
to be clear, when you're dealing with data going to the database, you're talking SQL injection, not cross site scripting. Also, per http://php.net/manual/en/function.mysql-escape-string.php, this function is deprecated (since it ignores the charset, and may be vulnerable to charset encoding filter evasion techniques), so mysql_real_escape_string() should be used. Of course, with PHP, there's more that needs to be done, too: http://php.net/manual/en/security.database.sql-injection.php
atk
A: 

A client's company had a security audit for its website, stripping these tags from user input prevented XSS tests from red flagging.

array('<','>',"'",'(',')','\\','"','-','script'); // edit: Limited use, pls disregard.

Added: Pádraic Brady has an article about this just 2 days ago, HTML Sanitisation: The Devil's In The Details (And The Vulnerabilities)

doingsitups
This list does not work in all contexts, such as html tag attributes are not quoted (space becomes dangerous), or when script is split with a newline or unprintable 0-width character (such as BOM, IIRC). Also, this encourages a blacklisting approach, assuming that we can come up with everything that's dangerous, and a mistake (or enhancement to what web browsers interpret) results in a security vulnerability. Furthermore, what if those characters are really needed? Like what about the name "O'Malley" - it's inappropriate to modify the input. Output encoding is a far better solution.
atk
you are right, this is not a good way, we did this just to get past the audit, we only had login and search field on that website.
doingsitups
even. just to get past an audit, it's a bad idea. the client and the client's customers are now using p(likely) vulnerable software for a vulnerability they spicifi ally do not want, as evidenced by hhe audit. Your copmany made a risk decision on behalf o fyour customer instead of properly fixing the probel, whiich could create liabilty for yor company. not a wise decision.
atk
You make it sound like it doesn't work, it does, we don't save what customer's type in, and we no longer use GET to propagate error messages, so I actually don't quite understand why those are vectors for attack, this is probably ignorant of me, but doesn't XSS require someway to propagete to another user? In anycase, thanks to this thread, I am more aware of the importance of keeping safe from XSS.
doingsitups
It works for certain circumstances and it doesn't work for other circumstances. And errors result in vulnerabilities. There's a whole class of research called filter evasion, which makes blacklisting even more risky. And yes, for XSS to be valuable, it has to target another user, but it doesn't have to be stored, and it doesn't have to be error messages. It simply has to be in the page. Take a look at http://www.cgisecurity.com/xss-faq.html and owasp.org and ha.ckers.org and sla.ckers.org for more details if you want to do some interesting reading.
atk
Thanks, I will, this knowledge will come in handy.
doingsitups