<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx</link><description>When I&amp;rsquo;ve asked people about their strategies for preventing SQL injection, one response is sometimes &amp;ldquo;I use stored procedures.&amp;rdquo; But, stored procedures do not , by themselves, necessarily protect against SQL injection . The usefulness</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10132580</link><pubDate>Tue, 22 Feb 2011 10:05:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10132580</guid><dc:creator>Andy P</dc:creator><description>&lt;p&gt;Bob, I was reluctant to make my (rather long) comment even longer by discussing secondary SQL injection. My view is that the same rule still applies, because anything you&amp;#39;re getting from a table is EXTERNAL to your stored procedure (or function, or query, or whatever). Just to clarify, when I talk about EXTERNAL input, I don&amp;#39;t just mean somthing that comes in from the internet, or that comes from the user: I mean anything that comes from outside the scope of the query, function or procedure you&amp;#39;re writing. &lt;/p&gt;
&lt;p&gt;Perhaps we can refine the rule to:&lt;/p&gt;
&lt;p&gt;-- &amp;nbsp; NEVER concatenate dynamic SQL out of EXTERNAL input.&lt;/p&gt;
&lt;p&gt;-- &amp;nbsp; EXTERNAL input can ONLY be used as a PARAMETER.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10132580" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10132339</link><pubDate>Mon, 21 Feb 2011 19:38:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10132339</guid><dc:creator>Bob</dc:creator><description>&lt;p&gt;Never building sql by concatenating EXTERNAL input is fine advice ... as far as it goes. The problem is: sometimes developers forget the source of their inputs. Secondary sql injection is a technique whereby malicious code is inserted into a database table where it lies in wait for a naive developer to forget that the source of the data in that table was EXTERNAL. &lt;/p&gt;
&lt;p&gt;The proper answer to the question of how to prevent sql injection is: always use parameters instead of concatenation.&lt;/p&gt;
&lt;p&gt;Attempting to sanitize/cleanse input is not a technique for preventing sql injection: it should be more properly thought of as a technique for attempting to _detect_ injection attempts, and should never be the only technique used to secure your application. It is &amp;nbsp;the first layer in a multi-layer defense.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10132339" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10132248</link><pubDate>Mon, 21 Feb 2011 16:19:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10132248</guid><dc:creator>Brian Swan - MSFT</dc:creator><description>&lt;p&gt;Thanks for the questions and comments. I think Andy P summed things up nicely. I tried to point out what he is saying in the post, but I didn&amp;#39;t quite say it as eloquently. :-) As Andy P points out, in my example the SQL injection vulnerability is just moved from the PHP script to the stored procedure. His NEVER build dynamic SQL out of EXTERNAL input is a nice, simple summary.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10132248" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10132208</link><pubDate>Mon, 21 Feb 2011 15:00:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10132208</guid><dc:creator>Andy P</dc:creator><description>&lt;p&gt;I would suggest that there is a very simple rule that protects agains SQL injection:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;NEVER build dynamic SQL out of EXTERNAL input&lt;/p&gt;
&lt;p&gt;Dynamic SQL is fine, as long as all the building blocks are inside your procedure/function/query. For example, you might have to select from a different combination of tables depending on some user-input parameter. That&amp;#39;s perfectly safe. Trouble comes when you allow the user input to be pasted into your query.&lt;/p&gt;
&lt;p&gt;In the example, the SQL injection vulnerability has simply been moved out of the PHP script and into the stored procedure. The stored procedure is guilty of building dynamic SQL out of something (the input parameters) whose value is set OUTSIDE the stored procedure. &lt;/p&gt;
&lt;p&gt;If you&amp;#39;re the developer working on that stored procedure, you can&amp;#39;t be sure who or what will end up calling your code. For this reason, SQL injection is not just for web developers - it&amp;#39;s every developer&amp;#39;s responsibility. System variables, input parameters, results from other functions - all these could contain some horrible SQL that you don&amp;#39;t want in your query. &lt;/p&gt;
&lt;p&gt;Sure, you can check the external input for suspect characters, try to strip out punctuation and so on. But string parsing is notoriously inefficient, and how can you be sure you&amp;#39;ve covered every possibility? What happens if the input you&amp;#39;ve been given - like the password in the example - is allowed to contain strings like &amp;#39; or -- ?&lt;/p&gt;
&lt;p&gt;Follow the simple rule and you should be completely safe. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10132208" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10132107</link><pubDate>Mon, 21 Feb 2011 10:38:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10132107</guid><dc:creator>Dooby Duck</dc:creator><description>&lt;p&gt;Won&amp;#39;t passing parameters into dynamic SQL also protect against SQL injection? &amp;nbsp;I would have expected this to form one of the &amp;#39;right way&amp;#39; examples&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10132107" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10131999</link><pubDate>Mon, 21 Feb 2011 00:31:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10131999</guid><dc:creator>Geezer</dc:creator><description>&lt;p&gt;The author did not comment about input validations because it is irrelevant to the specific topic being discussed, i.e. whether stored procedures can protect against SQL injection. Input validation is a necessary thing, but it&amp;#39;s beside the point in the context of this article.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10131999" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10131499</link><pubDate>Fri, 18 Feb 2011 17:29:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10131499</guid><dc:creator>Brian Swan - MSFT</dc:creator><description>&lt;p&gt;@David - Thanks. I probably should not have been so subtle about the &amp;quot;plan&amp;quot;. I was referring to query plans (&lt;a rel="nofollow" target="_new" href="http://en.wikipedia.org/wiki/Query_plan"&gt;en.wikipedia.org/.../Query_plan&lt;/a&gt;) which can improve performance as well as protect against injection.&lt;/p&gt;
&lt;p&gt;@Owolabi - IMHO, validation should be done on both the client and the server. Obviously, if speed is the most important factor in your application, you favor one over the other, but that&amp;#39;s going to depend on the application. Just my 2 cents.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10131499" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10130975</link><pubDate>Thu, 17 Feb 2011 16:02:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10130975</guid><dc:creator>Owolabi Charles</dc:creator><description>&lt;p&gt;What about input validations and strictly disallowing some injectable characters? Although, a Client side validation will only work for the honest user, A combination of serverside validation will help. Again, Trading off speed or performance for security!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10130975" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10130492</link><pubDate>Wed, 16 Feb 2011 17:23:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10130492</guid><dc:creator>Evan</dc:creator><description>&lt;p&gt;Using a decent O/RM will prevent the SQL injection attack (by sanitizing input) and remove the burden of writing all those sprocs..&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10130492" width="1" height="1"&gt;</description></item><item><title>re: Do Stored Procedures Protect Against SQL Injection?</title><link>http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx#10130475</link><pubDate>Wed, 16 Feb 2011 17:01:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10130475</guid><dc:creator>David Malouf</dc:creator><description>&lt;p&gt;I know it&amp;#39;s subtle, but I really like the way you call a stored procedure a &amp;#39;plan.&amp;#39; &amp;nbsp;It puts me in the right mindset when creating SPs to think of them as plans and not queries.&lt;/p&gt;
&lt;p&gt;As always, so well worded, Brian!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10130475" width="1" height="1"&gt;</description></item></channel></rss>