<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://keepitlocked.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>KeepItLocked.net : Input Validation, Software Security</title><link>http://keepitlocked.net/archive/tags/Input+Validation/Software+Security/default.aspx</link><description>Tags: Input Validation, Software Security</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP2 (Build: 20611.960)</generator><item><title>ASP.NET ValidateRequest and the HTML Attribute Based Cross Site Scripting </title><link>http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx</link><pubDate>Wed, 31 Oct 2007 03:45:00 GMT</pubDate><guid isPermaLink="false">a3f75fb5-0505-4d35-9795-aaa2ed659a71:16</guid><dc:creator>Alex</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://keepitlocked.net/rsscomments.aspx?PostID=16</wfw:commentRss><comments>http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx#comments</comments><description>&lt;p&gt;ASP.NET &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.configuration.pagessection.validaterequest.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.web.configuration.pagessection.validaterequest.aspx"&gt;ValidateRequest&lt;/a&gt; is a security mechanism designed to prevent cross-site scripting attacks in ASP.NET applications. It looks at data in the HTTP request parameters, and issues an error if it finds anything that is "suspicious". And, for the most part, it works fine. But, like many security technologies, there are two big problems - false positives and false negatives. First off, let's take a look at how ValidateRequest works. Using the .NET Reflector tool, we can see the attack detection algorithm in the &lt;span style="font-family: Courier New;"&gt;IsDangerousString&lt;/span&gt; method of the&lt;span style="font-family: Courier New;"&gt; CrossSiteScriptingValidation &lt;/span&gt;class, which is the crux of the ValidateRequest functionality:
&lt;/p&gt;

&lt;p align="center"&gt;
		&lt;img src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid1.png" mce_src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid1.png" alt=""&gt;
	&lt;/p&gt;

&lt;p&gt;This method looks for either a less than or ampersand character in the string. If one exists, it first checks to make sure it is not the last character (I'm not entirely sure why, but it seems this would only allow fairly benign strings). Then, if the offending character is a less than character, the method checks if the next character is a letter, an exclamation point, or a forward slash. If so, it is considered dangerous. Also, if the offending character was an ampersand, and the next character is a hash mark, the string is considered dangerous. This algorithm iterates through the string, stopping at each instance of one of these symbols.
&lt;/p&gt;

&lt;p&gt;Now that we have a good idea how this functionality works, let's examine why it isn't always ideal.
&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;False Positives
&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;ValidateRequest looks for anything that resembles HTML that code be used to execute script. Unfortunately, the detection technique can be a bit trigger-happy.  Some strings that appear to be malicious are perfectly normal and expected. Examples:&lt;span style="text-decoration: underline;"&gt;
		&lt;/span&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rich text controls often use HTML characters for markup.
&lt;/li&gt;

&lt;li&gt;XML in AJAX calls has been known to trip the ValidateRequest error.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Many people advise turning ValidateRequest off at the first sign of problems. The first &lt;a href="http://www.google.com/search?q=validaterequest" mce_href="http://www.google.com/search?q=validaterequest"&gt;Google hit for ValidateRequest&lt;/a&gt; is a link to an article from 2004 titled "&lt;a href="http://www.aspnetpro.com/NewsletterArticle/2004/03/asp200403dk_l/asp200403dk_l.asp" mce_href="http://www.aspnetpro.com/NewsletterArticle/2004/03/asp200403dk_l/asp200403dk_l.asp"&gt;Surviving ValidateRequest&lt;/a&gt;" discussing why it's not always in a developer's best interest to use the mechanism (although it does discuss the threats and countermeasures regarding cross-site scripting in the article as well). Here's a quote:
&lt;/p&gt;

&lt;p style="margin-left: 36pt;"&gt;"Another problem with ValidateRequest set to true is that it is a rather broad and stupid protection, erring on the side of catching too much rather than letting something dangerous slide by."
&lt;/p&gt;

&lt;p&gt;OK, fair enough, simply disable ValidateRequest when it causes problems, and figure out how to prevent XSS by yourself in those cases. But something so strict that it chokes on regular input has got to prevent all bad input, right?
&lt;/p&gt;

&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;False Negatives
&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;No one claims that ValidateRequest is perfectly effective in stopping cross-site scripting attacks. But what does it miss? From &lt;a href="http://blogs.msdn.com/esiu/archive/2007/10/19/asp-net-validaterequest-does-not-mitigate-xss-completely.aspx" mce_href="http://blogs.msdn.com/esiu/archive/2007/10/19/asp-net-validaterequest-does-not-mitigate-xss-completely.aspx"&gt;a recent blog post&lt;/a&gt;:
&lt;/p&gt;

&lt;p style="margin-left: 36pt;"&gt;"ValidateRequest may miss some crafty inputs."
&lt;/p&gt;

&lt;p&gt;Well, what are these "crafty" values? One is mentioned in the article - an exploit which is mentioned in the &lt;a href="http://www.microsoft.com/technet/security/Bulletin/MS07-040.mspx" mce_href="http://www.microsoft.com/technet/security/Bulletin/MS07-040.mspx"&gt;Microsoft Security Bulletin MS07-040&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;There is another, perhaps more common (and still unpatched) form of XSS which isn't stopped by ValidateRequest. It is known as &lt;a href="http://jeremiahgrossman.blogspot.com/2007/07/attribute-based-cross-site-scripting.html" mce_href="http://jeremiahgrossman.blogspot.com/2007/07/attribute-based-cross-site-scripting.html"&gt;HTML Attribute-Based Cross Site Scripting&lt;/a&gt;, according to Jeremiah Grossman. The attacker uses an HTML attribute to insert an event handler that causes a script to run. 
&lt;/p&gt;

&lt;p&gt;ValidateRequest doesn't even attempt to look for this - there are no angle brackets required.
&lt;/p&gt;

&lt;p&gt;For example, take the following ASP.NET code:
&lt;/p&gt;

&lt;p align="center"&gt;&lt;img src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid2.png" mce_src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid2.png" alt=""&gt;
	&lt;/p&gt;

&lt;p&gt;This code is used to display a page which renders a link to an article on Wikipedia.
&lt;/p&gt;

&lt;p align="center"&gt;&lt;img src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid3.png" mce_src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid3.png" alt=""&gt;
	&lt;/p&gt;

&lt;p&gt;We can enter this value:
&lt;/p&gt;

&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;

&lt;img src="http://keepitlocked.net/images/keepitlockednet/xss.png" mce_src="http://keepitlocked.net/images/keepitlockednet/xss.png" alt=""&gt;

&lt;br&gt;
&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;This will cause the following HTML to be rendered:
&lt;/p&gt;

&lt;p align="center"&gt;&lt;img src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid4.png" mce_src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid4.png" alt=""&gt;
	&lt;/p&gt;

&lt;p&gt;This will cause script to execute when the person moves their mouse over the link:
&lt;/p&gt;

&lt;p align="center"&gt;&lt;img src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid5.png" mce_src="http://keepitlocked.net/images/keepitlockednet/103107_0344_ASPNETValid5.png" alt=""&gt;
	&lt;/p&gt;

&lt;p&gt;So we have caused cross-site scripting in spite of ValidateRequest being enabled. This is due to the fact that not all cross-site scripting attacks require the use of less than or ampersand characters. For example, consider what would happen if a parameter value was echoed directly in JavaScript (this can happen in AJAX apps). The results can be scary!
&lt;/p&gt;

&lt;p&gt;ValidateRequest is not a panacea. Instead, consider augmenting the functionality with stronger protection afforded by the &lt;a href="http://msdn2.microsoft.com/en-us/security/aa973814.aspx" mce_href="http://msdn2.microsoft.com/en-us/security/aa973814.aspx"&gt;Anti XSS&lt;/a&gt; library, and as always, implement and enforce strict validation.&lt;/p&gt;

&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx&amp;amp;;subject=ASP.NET+ValidateRequest+and+the+HTML+Attribute+Based+Cross+Site+Scripting+" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx&amp;amp;;title=ASP.NET+ValidateRequest+and+the+HTML+Attribute+Based+Cross+Site+Scripting+" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx&amp;amp;title=ASP.NET+ValidateRequest+and+the+HTML+Attribute+Based+Cross+Site+Scripting+" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx&amp;amp;;title=ASP.NET+ValidateRequest+and+the+HTML+Attribute+Based+Cross+Site+Scripting+" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx&amp;amp;;title=ASP.NET+ValidateRequest+and+the+HTML+Attribute+Based+Cross+Site+Scripting+&amp;amp;;top=1" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/30/asp-net-validaterequest-and-the-html-attribute-based-cross-site-scripting.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://keepitlocked.net/aggbug.aspx?PostID=16" width="1" height="1"&gt;</description><category domain="http://keepitlocked.net/archive/tags/Software+Security/default.aspx">Software Security</category><category domain="http://keepitlocked.net/archive/tags/Input+Validation/default.aspx">Input Validation</category><category domain="http://keepitlocked.net/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://keepitlocked.net/archive/tags/XSS/default.aspx">XSS</category></item><item><title>Input versus Data, Validation versus Sanitization</title><link>http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx</link><pubDate>Mon, 08 Oct 2007 20:41:00 GMT</pubDate><guid isPermaLink="false">a3f75fb5-0505-4d35-9795-aaa2ed659a71:11</guid><dc:creator>Alex</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://keepitlocked.net/rsscomments.aspx?PostID=11</wfw:commentRss><comments>http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx#comments</comments><description>&lt;p&gt;Reading articles, browsing marketing materials, and listening to presentations about application security, you hear variations on a theme:
&lt;/p&gt;&lt;p&gt;"Input validation is absolutely critical to application security, and most application risks involve tainted input at some level." – &lt;a href="http://www.owasp.org/index.php/Input_Validation" mce_href="http://www.owasp.org/index.php/Input_Validation"&gt;OWASP&lt;/a&gt;
	&lt;/p&gt;&lt;p&gt;While I don't think authors overstate the importance of problems stemming from invalid data, I have noticed these discussions gloss over two key points.
&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Input validation is only part of the problem. Output validation is important as well.
&lt;/li&gt;&lt;li&gt;Validation (in the general sense) has two distinct concerns: validation and sanitization.
&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;i&gt;Input validation is only part of the problem. Output validation is important as well.
&lt;/i&gt;&lt;/p&gt;&lt;p&gt;All data input from an untrusted source should be validated. If you enter a blog comment, I want to make sure it isn't empty, it is less than 500 words, and it isn't spam and won't get my readers &lt;a href="http://wiki.answers.com/Q/What_does_it_mean_to_be_Rickrolled" mce_href="http://wiki.answers.com/Q/What_does_it_mean_to_be_Rickrolled"&gt;RickRolled&lt;/a&gt;. However, as that data is output from the web application, it should be validated as well. Here's why:
&lt;/p&gt;&lt;p&gt;Think about cross-site scripting – we really want to prevent tainted data from exiting the system to the rendered web page on the client. This occurs when the data is output, not input. SQL injection is also tainted data exiting the system (through a SQL query) and parameterized queries are output validation. And since these different validation rules might process the same data (say, a blog comment that is reflected in a subsequent page for approval and then stored to the database), it makes more sense to validate the data upon exit, rather than on entrance.
&lt;/p&gt;&lt;p&gt;It's like international air travel – you pass through customs at your arrival airport (output), because at your departure airport (input), they don't know the rules for what's allowed and what isn't.
&lt;/p&gt;&lt;p&gt;Thus, I propose that "Data Validation" be used in favor of "Input Validation" as a more accurate (although less precise) term to include input and output validation.
&lt;/p&gt;&lt;p&gt;&lt;i&gt;Validation (in the general sense) has two distinct concerns: validation and sanitization.
&lt;/i&gt;&lt;/p&gt;&lt;p&gt;Validation is a Boolean operation which gives a yes or no answer to the question "Is this data acceptable in the current context?"
&lt;/p&gt;&lt;p&gt;Sanitization (which includes encoding, escaping, and stripping) refers to transforming data in some manner so as to make it acceptable in the current context.
&lt;/p&gt;&lt;p&gt;These two approaches can be used independently or in concert and the correct way to perform these operations from a security perspective is highly dependent on the context in which they are used. 
&lt;/p&gt;&lt;p&gt;So validation is (usually) both validation and sanitization.
&lt;/p&gt;&lt;p&gt;Another issue which might be brought up in the subject of validation is canonicalization, which is a separate issue that warrants its own future blog post.
&lt;/p&gt;&lt;p&gt;Just some food for thought when designing validation mechanisms – it's not all yes or no decisions, and it's not all at the front door.&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx&amp;amp;;subject=Input+versus+Data%2c+Validation+versus+Sanitization" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx&amp;amp;;title=Input+versus+Data%2c+Validation+versus+Sanitization" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx&amp;amp;title=Input+versus+Data%2c+Validation+versus+Sanitization" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx&amp;amp;;title=Input+versus+Data%2c+Validation+versus+Sanitization" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx&amp;amp;;title=Input+versus+Data%2c+Validation+versus+Sanitization&amp;amp;;top=1" target="_blank" title = "Post http://keepitlocked.net/archive/2007/10/08/input-versus-data-validation-versus-sanitization.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://keepitlocked.net/aggbug.aspx?PostID=11" width="1" height="1"&gt;</description><category domain="http://keepitlocked.net/archive/tags/Software+Security/default.aspx">Software Security</category><category domain="http://keepitlocked.net/archive/tags/Secure+Design/default.aspx">Secure Design</category><category domain="http://keepitlocked.net/archive/tags/Input+Validation/default.aspx">Input Validation</category></item></channel></rss>