<p>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?</p>

<p>I appreciate answers for JSP, ASP.net and PHP.</p>

[{'answer_id': 58712, 'author': 'Eugene Yokota', 'author_id': 3827, 'author_profile': 'https://Stackoverflow.com/users/3827', 'pm_score': 0, 'selected': False, 'text': "<p>If you do actually HTML encode every single output, the user will see plain text of &amp;lt;html&amp;gt; instead of a functioning web app.</p>\n\n<p>EDIT: If you HTML encode every single input, you'll have problem accepting external password containing &lt; etc..</p>\n"}, {'answer_id': 58731, 'author': 'David McLaughlin', 'author_id': 3404, 'author_profile': 'https://Stackoverflow.com/users/3404', 'pm_score': 3, 'selected': True, 'text': '<p>You don\'t want to encode all HTML, you only want to HTML-encode any user input that you\'re outputting. </p>\n\n<p>For PHP: <a href="http://us.php.net/htmlentities" rel="nofollow noreferrer">htmlentities</a> and <a href="http://us.php.net/manual/en/function.htmlspecialchars.php" rel="nofollow noreferrer">htmlspecialchars</a></p>\n'}, {'answer_id': 58773, 'author': 'fijter', 'author_id': 3215, 'author_profile': 'https://Stackoverflow.com/users/3215', 'pm_score': 1, 'selected': False, 'text': '<p>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. </p>\n\n<p>I don\'t have that modifier any more; but about the same version can be found here:</p>\n\n<p><a href="http://www.madcat.nl/martijn/archives/16-Using-smarty-to-prevent-HTML-injection..html" rel="nofollow noreferrer">http://www.madcat.nl/martijn/archives/16-Using-smarty-to-prevent-HTML-injection..html</a></p>\n\n<p>In the new Django 1.0 release this works exactly the same way, jay :)</p>\n'}, {'answer_id': 58791, 'author': 'reefnet_alex', 'author_id': 2745, 'author_profile': 'https://Stackoverflow.com/users/2745', 'pm_score': 1, 'selected': False, 'text': '<p>You could wrap echo / print etc. in your own methods which you can then use to escape output. i.e. instead of </p>\n\n<pre><code>echo "blah";\n</code></pre>\n\n<p>use </p>\n\n<pre><code>myecho(\'blah\');\n</code></pre>\n\n<p>you could even have a second param that turns off escaping if you need it. </p>\n\n<p>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 :)</p>\n'}, {'answer_id': 58938, 'author': 'Peter Bernier', 'author_id': 6112, 'author_profile': 'https://Stackoverflow.com/users/6112', 'pm_score': 1, 'selected': False, 'text': "<p>My personal preference is to diligently encode <em>anything</em> that's coming from the database, business layer or from the user.</p>\n\n<p>In ASP.Net this is done by using <code>Server.HtmlEncode(string)</code> .</p>\n\n<p>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).</p>\n"}, {'answer_id': 59103, 'author': 'Kieran Hall', 'author_id': 6085, 'author_profile': 'https://Stackoverflow.com/users/6085', 'pm_score': 0, 'selected': False, 'text': '<p>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 <a href="http://www.phpclasses.org/browse/package/2189.html" rel="nofollow noreferrer">PHP Filtering Class</a> (a complete solution) and also the <a href="http://framework.zend.com/manual/en/zend.filter.html" rel="nofollow noreferrer">Zend_Filter</a> package (a collection of classes you can use to build your own filter).</p>\n\n<p>PHP is my language of choice when it comes to web development, so apologies for the bias in my answer.</p>\n\n<p>Kieran.</p>\n'}, {'answer_id': 60690, 'author': 'JW.', 'author_id': 4321, 'author_profile': 'https://Stackoverflow.com/users/4321', 'pm_score': 3, 'selected': False, 'text': '<p>One thing that you <strong>shouldn\'t</strong> 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.</p>\n\n<p>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.)</p>\n\n<p>You can\'t anticipate all the places your input data will travel. The safe approach is to prepare the data <strong>just before</strong> 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.</p>\n\n<p>This is more work, but you can reduce it by using template engines or libraries.</p>\n'}, {'answer_id': 60730, 'author': 'ddowns', 'author_id': 5201, 'author_profile': 'https://Stackoverflow.com/users/5201', 'pm_score': 0, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>Var dsFirstName, uhsFirstName : String;\n\nBegin\n\nuhsFirstName := request.queryfields.value[\'firstname\'];\n\ndsFirstName := dsHtmlToDB(uhsFirstName);\n</code></pre>\n\n<p>Basically prefix your variables with something like <strong>"us"</strong> for unsafe string, <strong>"ds"</strong> for database safe, <strong>"hs"</strong> 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 you\'re going to need different encode/decode functions anyways.  </p>\n'}, {'answer_id': 69004, 'author': 'MetroidFan2002', 'author_id': 8026, 'author_profile': 'https://Stackoverflow.com/users/8026', 'pm_score': 2, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>&lt;input name="someName.someProperty" value="&lt;c:out value=\'${someName.someProperty}\' /&gt;" /&gt;\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 4857594, 'author': 'JMD', 'author_id': 597690, 'author_profile': 'https://Stackoverflow.com/users/597690', 'pm_score': 0, 'selected': False, 'text': '<p>Output encoding is by far the best defense. Validating input is great for many reasons, but not 100% defense. If a database becomes infected with XSS via attack (i.e. ASPROX), mistake, or maliciousness input validation does nothing. Output encoding will still work.</p>\n'}, {'answer_id': 8879609, 'author': 'leemes', 'author_id': 592323, 'author_profile': 'https://Stackoverflow.com/users/592323', 'pm_score': 0, 'selected': False, 'text': '<p>OWASP has a nice API to encode HTML output, either to use as HTML text (e.g. paragraph or <code>&lt;textarea&gt;</code> content) or as an attribute\'s value (e.g. for <code>&lt;input&gt;</code> tags after rejecting a form):</p>\n\n<pre><code>encodeForHTML($input) // Encode data for use in HTML using HTML entity encoding\nencodeForHTMLAttribute($input) // Encode data for use in HTML attributes.\n</code></pre>\n\n<p>The project (the PHP version) is hosted under <a href="http://code.google.com/p/owasp-esapi-php/" rel="nofollow">http://code.google.com/p/owasp-esapi-php/</a> and is also available for some other languages, e.g. .NET.</p>\n\n<p>Remember that you should encode <em>everything</em> (not only user input), and <em>as late as possible</em> (not when storing in DB but when outputting the HTTP response).</p>\n'}]
['https://Stackoverflow.com/questions/58694', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']