<p>I am not concerned about other kinds of attacks. Just want to know whether HTML Encode can prevent all kinds of XSS attacks.</p>

<p>Is there some way to do an XSS attack even if HTML Encode is used?</p>

[{'answer_id': 53739, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': -1, 'selected': False, 'text': "<p>I don't believe so. Html Encode converts all functional characters (characters which could be interpreted by the browser as code) in to entity references which cannot be parsed by the browser and thus, cannot be executed.</p>\n\n<pre><code>&amp;lt;script/&amp;gt;\n</code></pre>\n\n<p>There is no way that the above can be executed by the browser.</p>\n\n<p>**Unless their is a bug in the browser ofcourse.*</p>\n"}, {'answer_id': 53741, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 2, 'selected': False, 'text': "<p>If you encode everything it will. (depending on your platform and the implementation of htmlencode) But any usefull web application is so complex that it's easy to forget to check every part of it. Or maybe a 3rd party component isn't safe. Or maybe some code path that you though did encoding didn't do it so you forgot it somewhere else. </p>\n\n<p>So you might want to check things on the input side too. And you might want to check stuff you read from the database.</p>\n"}, {'answer_id': 53755, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 3, 'selected': False, 'text': '<p>If you systematically encode all user input before displaying <s>then yes, you are safe</s> you are still not 100 % safe.<br>\n(See @Avid\'s post for more details)</p>\n\n<p>In addition problems arise when you need to let <strong>some</strong> tags go unencoded so that you allow users to post images or bold text or any feature that requires user\'s input be processed as (or converted to) un-encoded markup.</p>\n\n<p>You will have to set up a  decision making system to decide which tags are allowed and which are not, and it is always possible that someone will figure out a way to let a non allowed tag to pass through.</p>\n\n<p>It helps if you follow Joel\'s advice of <a href="http://www.joelonsoftware.com/articles/Wrong.html" rel="noreferrer">Making Wrong Code Look Wrong</a> or if <a href="http://blog.moertel.com/articles/2006/10/18/a-type-based-solution-to-the-strings-problem" rel="noreferrer">your language helps you</a> by warning/not compiling when you are outputting unprocessed user data (static-typing).</p>\n'}, {'answer_id': 53816, 'author': 'metavida', 'author_id': 5539, 'author_profile': 'https://Stackoverflow.com/users/5539', 'pm_score': 1, 'selected': False, 'text': '<p>As mentioned by everyone else, you\'re safe as long as you encode <em>all</em> user input before displaying it. This includes all request parameters and data retrieved from the database that can be changed by user input.</p>\n\n<p>As <a href="https://stackoverflow.com/questions/53728/will-html-encoding-prevent-all-kinds-of-xss-attacks#53755">mentioned by Pat</a> you\'ll sometimes want to display some tags, just not all tags. One common way to do this is to use a markup language like <a href="http://en.wikipedia.org/wiki/Textile_(markup_language)" rel="nofollow noreferrer">Textile</a>, <a href="http://en.wikipedia.org/wiki/Markdown" rel="nofollow noreferrer">Markdown</a>, or <a href="http://en.wikipedia.org/wiki/BBCode" rel="nofollow noreferrer">BBCode</a>. However, even markup languages can be vulnerable to XSS, just be aware.</p>\n\n<pre><code># Markup example\n[foo](javascript:alert\\(\'bar\'\\);)\n</code></pre>\n\n<p>If you do decide to let "safe" tags through I would recommend finding some existing library to parse &amp; sanitize your code before output. There are <a href="http://ha.ckers.org/xss.html" rel="nofollow noreferrer">a lot of XSS vectors</a> out there that you would have to detect before your sanitizer is fairly safe.</p>\n'}, {'answer_id': 57434, 'author': 'tqbf', 'author_id': 5674, 'author_profile': 'https://Stackoverflow.com/users/5674', 'pm_score': 1, 'selected': False, 'text': '<p>I second metavida\'s advice to find a third-party library to handle output filtering. Neutralizing HTML characters is a good approach to stopping XSS attacks. However, the code you use to transform metacharacters can be vulnerable to evasion attacks; for instance, if it doesn\'t properly handle Unicode and internationalization.</p>\n\n<p>A classic simple mistake homebrew output filters make is to catch only &lt; and >, but miss things like ", which can break user-controlled output out into the attribute space of an HTML tag, where Javascript can be attached to the DOM.</p>\n'}, {'answer_id': 70222, 'author': 'AviD', 'author_id': 10080, 'author_profile': 'https://Stackoverflow.com/users/10080', 'pm_score': 8, 'selected': True, 'text': '<p>No.</p>\n\n<p>Putting aside the subject of allowing some tags (not really the point of the question), HtmlEncode simply does NOT cover all XSS attacks.</p>\n\n<p>For instance, consider server-generated client-side javascript - the server dynamically outputs htmlencoded values directly into the client-side javascript, htmlencode will <strong>not stop</strong> injected script from executing.</p>\n\n<p>Next, consider the following pseudocode:</p>\n\n<pre><code>&lt;input value=&lt;%= HtmlEncode(somevar) %&gt; id=textbox&gt;\n</code></pre>\n\n<p>Now, in case its not immediately obvious, if somevar (sent by the user, of course) is set for example to</p>\n\n<pre><code>a onclick=alert(document.cookie)\n</code></pre>\n\n<p>the resulting output is</p>\n\n<pre><code>&lt;input value=a onclick=alert(document.cookie) id=textbox&gt;\n</code></pre>\n\n<p>which would clearly work. Obviously, this can be (almost) any other script... and HtmlEncode would not help much.</p>\n\n<p>There are a few additional vectors to be considered... including the third flavor of XSS, called DOM-based XSS (wherein the malicious script is generated dynamically on the client, e.g. based on # values).</p>\n\n<p>Also don\'t forget about UTF-7 type attacks - where the attack looks like </p>\n\n<pre><code>+ADw-script+AD4-alert(document.cookie)+ADw-/script+AD4-\n</code></pre>\n\n<p>Nothing much to encode there...</p>\n\n<p>The solution, of course (in addition to proper and restrictive white-list input validation), is to perform <strong>context-sensitive</strong> encoding: HtmlEncoding is great IF you\'re output context IS HTML, or maybe you need JavaScriptEncoding, or VBScriptEncoding, or AttributeValueEncoding, or... etc.</p>\n\n<p>If you\'re using MS ASP.NET, you can use their Anti-XSS Library, which provides all of the necessary context-encoding methods.</p>\n\n<p>Note that all encoding should not be restricted to user input, but also stored values from the database, text files, etc.</p>\n\n<p>Oh, and don\'t forget to explicitly set the charset, both in the HTTP header AND the META tag, otherwise you\'ll still have UTF-7 vulnerabilities...</p>\n\n<p>Some more information, and a pretty definitive list (constantly updated), check out RSnake\'s Cheat Sheet: <a href="http://ha.ckers.org/xss.html" rel="noreferrer">http://ha.ckers.org/xss.html</a></p>\n'}, {'answer_id': 91509, 'author': 'Buzz', 'author_id': 13113, 'author_profile': 'https://Stackoverflow.com/users/13113', 'pm_score': 0, 'selected': False, 'text': '<p>I\'d like to suggest HTML Purifier (<a href="http://htmlpurifier.org/" rel="nofollow noreferrer">http://htmlpurifier.org/</a>) It doesn\'t just filter the html, it basically tokenizes and re-compiles it. It is truly industrial-strength. </p>\n\n<p>It has the additional benefit of allowing you to ensure valid html/xhtml output. </p>\n\n<p>Also n\'thing textile, its a great tool and I use it all the time, but I\'d run it though html purifier too. </p>\n\n<p>I don\'t think you understood what I meant re tokens. HTML Purifier doesn\'t just \'filter\', it actually reconstructs the html. <a href="http://htmlpurifier.org/comparison.html" rel="nofollow noreferrer">http://htmlpurifier.org/comparison.html</a></p>\n'}, {'answer_id': 94086, 'author': 'Chris Kite', 'author_id': 9573, 'author_profile': 'https://Stackoverflow.com/users/9573', 'pm_score': 1, 'selected': False, 'text': '<p>No, just encoding common HTML tokens DOES NOT completely protect your site from XSS attacks. See, for example, this XSS vulnerability found in google.com:</p>\n\n<p><a href="http://www.securiteam.com/securitynews/6Z00L0AEUE.html" rel="nofollow noreferrer">http://www.securiteam.com/securitynews/6Z00L0AEUE.html</a></p>\n\n<p>The important thing about this type of vulnerability is that the attacker is able to encode his XSS payload using UTF-7, and if you haven\'t specified a different character encoding on your page, a user\'s browser could interpret the UTF-7 payload and execute the attack script.</p>\n'}, {'answer_id': 100912, 'author': 'Mladen Mihajlovic', 'author_id': 11421, 'author_profile': 'https://Stackoverflow.com/users/11421', 'pm_score': 0, 'selected': False, 'text': "<p>One other thing you need to check is where your input comes from. You can use the referrer string (most of the time) to check that it's from your own page, but putting in a hidden random number or something in your form and then checking it (with a session set variable maybe) also helps knowing that the input is coming from your own site and not some phishing site.</p>\n"}, {'answer_id': 68540513, 'author': 'NguyenPD_LoH', 'author_id': 16403867, 'author_profile': 'https://Stackoverflow.com/users/16403867', 'pm_score': -1, 'selected': False, 'text': '<p>myString.replace(/&lt;[^&gt;]*&gt;?/gm, \'\');</p>\n<p>I use it, then successfully.\n<a href="https://stackoverflow.com/questions/822452/strip-html-from-text-javascript">Strip HTML from Text JavaScript</a></p>\n'}]
['https://Stackoverflow.com/questions/53728', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']