<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Invested Development &#187; AJAX</title>
	<atom:link href="http://devblog.stuartthompson.net/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://devblog.stuartthompson.net</link>
	<description>Thoughtful Approaches to Software Architecture</description>
	<lastBuildDate>Tue, 18 Oct 2011 17:08:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CascadingDropDownList and Page Validation</title>
		<link>http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/</link>
		<comments>http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/#comments</comments>
		<pubDate>Mon, 09 Apr 2007 10:40:34 +0000</pubDate>
		<dc:creator>stuartthompson</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[CascadingDropDownList]]></category>
		<category><![CDATA[Page Validation]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://stuartthompsontech.wordpress.com/?p=7</guid>
		<description><![CDATA[While working with the AJAX Control Toolkit (http://ajax.asp.net), I came across something interesting with the CascadingDropDownList control and ASP.NET page validation. What Does the CascadingDropDownList Do? The control is used to create tiered drop-down lists that each depend upon parent values for their own data population.  The canonical example given regards using three DropDownList controls to narrow a [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;color:blue;"><span style="color:#000000;">While working with the AJAX Control Toolkit (<a href="http://ajax.asp.net/">http://ajax.asp.net</a>), I came across something interesting </span></span><span style="font-size:10pt;font-family:&quot;color:black;">with the CascadingDropDownList control and ASP.NET page validation.</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:12pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;line-height:115%;font-family:&quot;color:blue;">What Does the CascadingDropDownList Do?</span></strong></p>
<p class="MsoNormal" style="margin:0;"><strong></strong><span style="font-size:10pt;font-family:&quot;">The control is used to create tiered drop-down lists that each depend upon parent values for their own data population.  The canonical example given regards using three DropDownList controls to narrow a selection of a car.  The first list displays a list of manufacturers, the second a list of models, and the final a list of common packages, with each successive list control populating only the relevant values based upon its parent.  For example, selecting &#8220;Ford&#8221; from the manufacturer list would populate &#8220;Focus, Sierra, Probe, F-Series, etc&#8230;&#8221; in the model list.  Selecting &#8220;F-Series&#8221; would populate &#8220;F150, F250, F350, etc&#8230;&#8221; in the package list.</span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:small;"><span style="font-family:Times New Roman;"> </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;font-family:&quot;color:blue;">What&#8217;s the Problem?</span></strong></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;">When the CascadingDropDownList control renders the child lists, it makes an AJAX server request to a web service supplying the selected parent value and requesting the list of relevant child values.  It then uses client-side script to populate the child DropDownList control with the list of returned values. </span><span style="font-size:10pt;font-family:&quot;">This means that the contents of the child drop-down lists are being modified on the fly by JavaScript and the final values in the list will not match the list that the ASP.NET page thinks it rendered.  The ASP.NET security model contains validation upon a postback to ensure that the contents of a drop-down list being posted back match those that were rendered to avoid injection attacks (see the section below on injection attacks if you want a refresher).</span></p>
<p class="MsoNormal" style="margin:0;">
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;">When the page posts back, an exception is raised by the ASP.NET framework because the contents of the originally rendered list don&#8217;t match the newly populated list.  ASP.NET provides a solution to &#8220;permit&#8221; a control to be modified on the fly, however it requires a call to <em><span style="font-family:&quot;">ClientScriptManager.RegisterForEventValidation </span></em>for each of the additional valid values that might appear.  Since we don&#8217;t know (server-side) ahead of time which option the user will choose client-side, we can&#8217;t know the list of options that the JavaScript control will add, thus preventing us from using that solution.</span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:small;"><span style="font-family:Times New Roman;"> </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;font-family:&quot;color:blue;">The Atlas Solution</span></strong></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;">The only official solution provided by the Atlas team is to disable validation for the page:</span></p>
<p class="MsoNormal" style="margin:0;"><span><a href="http://ajax.asp.net/ajaxtoolkit/Walkthrough/CCDWithDB.aspx"><span style="font-size:small;font-family:Times New Roman;">http://ajax.asp.net/ajaxtoolkit/Walkthrough/CCDWithDB.aspx</span></a></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;">However, even that team admits that this must be done with extreme caution and a complete understanding of the consequences.  It is fine to disable the automatic validation for a page as long as you are validating the received values manually or if you <strong><span style="font-family:&quot;">really</span></strong> trust the users of the application not to inject.  (hint: even on intranet apps you never trust the users not to inject because automated bots and viruses running on client machines inside the intranet are becoming advanced enough to post injections to page controls without the user&#8217;s knowledge, not to mention the &#8220;password on a sticky note on the monitor&#8221; hole.)</span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:small;"><span style="font-family:Times New Roman;"> </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;font-family:&quot;color:blue;">Why is the Solution a Problem?</span></strong></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;">The solution can be worked.  Server-side validation of incoming data from input controls (including DropDownList controls) is a good practice to begin with.  However, it raises some interesting limitations with regards to the standard integration models used by most developers.  If, for example, you are using the &lt;ObjectDataSource&gt; control to take parameters from your page controls and feed their selected values directly into a stored procedure or SQL query, you won&#8217;t get the opportunity to manually validate the input.  This means that a control on the page that is used as a parameter in the ObjectDataSource could have an injection string posted back as its value and sent directly to the stored procedure without the opportunity for server-side validation.  This opens up the injection hole and could only be solved by coding some pretty awkward stored procedures.  There is a better way.</span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:small;"><span style="font-family:Times New Roman;"> </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;font-family:&quot;color:blue;">Finding a Solution</span></strong></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;font-family:&quot;color:maroon;">Things We Know</span></strong></p>
<ul>
<li>
<div><strong></strong><span style="font-size:10pt;font-family:&quot;">I love the CascadingDropDownList control and want to still use it. </span></div>
</li>
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">To avoid ASP.NET raising validation errors, page-validation must be disabled for the page.</span></li>
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">Disabling automatic validation and failing to perform manual validation can open a security hole.</span></li>
</ul>
<p><strong><span style="font-size:10pt;font-family:&quot;color:maroon;">Compromises?</span></strong></p>
<ul type="disc">
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:black;">For the particular page using CascadingDropDownList, don&#8217;t use the ObjectDataSource directly.  Bind to the control with the three lines of code it takes in the code-behind file.  You can still use the TableAdapters and fantastic ASP.NET 2.0 DataSet pattern, you just need to validate the incoming paramters with a few assertions before running off with the input to the database.</span></li>
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">As a lesser, potentially still dangerous, and (to my mind) somewhat messy solution, you could bullet-proof your stored procedures with validation &#8211; but this feels like a future stumble and could get ugly in maintenance, testing, and debugging.  Just write the manual validation code and be done!</span> <span><span style="font-size:small;"><span style="font-family:Times New Roman;"> </span></span></span></li>
</ul>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;font-family:&quot;color:blue;">Injection Attacks You Say?</span></strong></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;">Without page validation a security hole appears.  Consider the following scenario.  A page is rendered with three items in a drop-down list, arbitrarily the names of three authors.  The ID and name of the author are rendered as the value and text respectively.  Because this is client-side html, the contents of the list can be modified before the page is posted back.  Say you insert the following into the html for the drop-down list:</span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;"> <span style="font-family:Courier New;">&lt;option value=&#8221;1</span></span><span style="font-size:10pt;font-family:&quot;"> or 1=1;</span><span style="font-size:10pt;font-family:&quot;"><span style="font-family:Courier New;">&#8220;&gt;</span>Foo<span style="font-family:Courier New;">&lt;/option&gt;</span></span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:small;"><span style="font-family:Times New Roman;"> </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:&quot;">This adds a new item to the list whose value is </span><span style="font-size:10pt;font-family:&quot;">&#8216;foo&#8217; or 1=1;</span></p>
<p class="MsoNormal" style="margin:0;">
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:x-small;">If the stored procedure being called was:</span></span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:x-small;"> <span style="font-family:Courier New;">SELECT * </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:x-small;font-family:Courier New;"> FROM   Author</span></span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:x-small;font-family:Courier New;"> WHERE  AuthorID = @selectedAuthorID</span></span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:x-small;">then the executed SQL would now end with:</span></span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:x-small;"> <span style="font-family:Courier New;">WHERE AuthorID = 1 or 1=1;</span></span></span></p>
<p class="MsoNormal" style="margin:0;"><span><span style="font-size:small;"><span style="font-family:Times New Roman;"> </span></span></span></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;font-family:&quot;color:blue;">Why is that a Bad Thing?</span></strong></p>
<p class="MsoNormal" style="margin:0;"><strong></strong><span style="font-size:10pt;font-family:&quot;">Well, for one the stored procedure or sql query will now return every item in the table because OR 1=1 will always evaluate to true.  Secondly, there are far, far worse injections that can be performed depending upon the rights of the user that the web application uses to connect to the database.  Because most applications have a single user account for the entire web application (restricting admin features only by url or declarative security), that account usually has vastly elevated rights.  You can now inject any SQL command after the WHERE clause.  For more information on SQL injection attacks, see <span style="font-family:Arial;"><a href="http://www.unixwiz.net/techtips/sql-injection.html">http://www.unixwiz.net/techtips/sql-injection.html</a></span></span></p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" class="tt" href="http://twitter.com/home/?status=CascadingDropDownList+and+Page+Validation%3A+http%3A%2F%2Fdevblog.stuartthompson.net%2F%3Fp%3D7" title="Post to Twitter"><img class="nothumb" src="http://devblog.stuartthompson.net/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://buzz.yahoo.com/buzz?targetUrl=http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/&amp;headline=CascadingDropDownList+and+Page+Validation" title="Post to Yahoo Buzz"><img class="nothumb" src="http://devblog.stuartthompson.net/wp-content/plugins/tweet-this/icons/en/buzz/tt-buzz-micro3.png" alt="Post to Yahoo Buzz" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/&amp;title=CascadingDropDownList+and+Page+Validation" title="Post to Delicious"><img class="nothumb" src="http://devblog.stuartthompson.net/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious-micro3.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/&amp;title=CascadingDropDownList+and+Page+Validation" title="Post to Digg"><img class="nothumb" src="http://devblog.stuartthompson.net/wp-content/plugins/tweet-this/icons/en/digg/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/&amp;t=CascadingDropDownList+and+Page+Validation" title="Post to Facebook"><img class="nothumb" src="http://devblog.stuartthompson.net/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/&amp;title=CascadingDropDownList+and+Page+Validation" title="Post to Reddit"><img class="nothumb" src="http://devblog.stuartthompson.net/wp-content/plugins/tweet-this/icons/en/reddit/tt-reddit-micro3.png" alt="Post to Reddit" /></a> <a target="_blank" class="tt" href="http://stumbleupon.com/submit?url=http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/&amp;title=CascadingDropDownList+and+Page+Validation" title="Post to StumbleUpon"><img class="nothumb" src="http://devblog.stuartthompson.net/wp-content/plugins/tweet-this/icons/en/su/tt-su-micro3.png" alt="Post to StumbleUpon" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://devblog.stuartthompson.net/2007/04/cascadingdropdownlist-and-page-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

