<?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>Dynamic SQL &amp;amp; SQL injection</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx</link><description>I know there are a lot of papers that talk about dynamic SQL in more depth than what I am going to cover, but as SQL injection is still one of the biggest security problems in the relation databases world, that I decided to include this part as a quick</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Dynamic SQL &amp; SQL injection</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx#1413392</link><pubDate>Fri, 05 Jan 2007 04:31:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1413392</guid><dc:creator>wenchingchua</dc:creator><description>&lt;P&gt;Hi, thanks for sharing. Is there anyway which I can prevent to write dynamic sql if i need to dynamic pass the table name? For example, there are 2 values in a dropdownlist - A and B. If I select A, then press search button, it will go back to my database and pass into a dynamic sql - e.g. tb_A_Logistics and perform some query. Same to the B. I cannot find a way to overcome this for a dynamic table name. Any help? Yes, I will like to prevent dynamic sql for my case, but I cannot find a better way. Thanks.&lt;/P&gt;</description></item><item><title>re: Dynamic SQL &amp; SQL injection</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx#1413681</link><pubDate>Fri, 05 Jan 2007 05:32:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1413681</guid><dc:creator>raulga</dc:creator><description>&lt;p&gt; &amp;nbsp; If I understand your scenario, you construct the table name based on user input (from the dropdown list), correct? I am not aware of an alternative from dynamic SQL for your particular scenario. As far as I understand, statements such as SELECT cannot use a variable in the FROM clause to specify the table/view name, and it also makes this scenario one that cannot be easily parameterized.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; I will strongly recommend in your case validating the input (i.e. make sure the value from the dropdown list follows your business rules) and make sure you properly delimit and escape the table name (in this case, as table names are sysnames, using QUOTENAME or an equivalent will be quite useful) in order to prevent SQL injection attacks.&lt;/p&gt;
&lt;p&gt; &amp;nbsp;I will appreciate if anyone knows an alternative mechanism without using dynamic SQL or a way to use a variable to specify the table name in such statements and wants to share the information.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Thanks a lot,&lt;/p&gt;
&lt;p&gt;-Raul&lt;/p&gt;
</description></item><item><title>re: Dynamic SQL &amp; SQL injection</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx#1415104</link><pubDate>Fri, 05 Jan 2007 09:49:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1415104</guid><dc:creator>wenchingchua</dc:creator><description>&lt;p&gt;Thanks for your feedback. Looking forward on that too if anyone can share some lights here :)&lt;/p&gt;</description></item><item><title> Передача параметров в динамический SQL. Продолжение.</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx#1415197</link><pubDate>Fri, 05 Jan 2007 10:02:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1415197</guid><dc:creator>Константин Косинский</dc:creator><description>&lt;P&gt;Пару недель назад я написал в своем блоге о Передача параметров в динамический SQL . Где пытался показать,&lt;/P&gt;</description></item><item><title>re: Dynamic SQL &amp; SQL injection</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx#1434289</link><pubDate>Mon, 08 Jan 2007 16:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1434289</guid><dc:creator>yiangos</dc:creator><description>&lt;P&gt;Hello.&lt;/P&gt;
&lt;P&gt;Well, I might have an alternative without Dynamic SQL, however it is of limited use, and with a lot of constraints. &lt;/P&gt;
&lt;P&gt;Basically, what you select from either table must have the same structure (i.e. same datatypes) and there's only a predefined number of different tables you can use the solution on. Moreover, I'm pretty sure that performance-wise, this gives you quite an overhead. &lt;/P&gt;
&lt;P&gt;Having taken all these into consideration, here goes:&lt;/P&gt;
&lt;P&gt;Suppose you have two tables, tbl_A and tbl_B, with 3 columns that have the same datatypes (and probably the same meaning data-wise). &lt;/P&gt;
&lt;P&gt;Suppose now, that you have the dropdown with the two values. This makes for the user input. After proper validation (i.e. that it is actually one of the allowed values) the input is passed as an argument to a function. The function is just a big if-else if:&lt;/P&gt;
&lt;P&gt;if the argument is, say "A", then do one select (write out the full select statement, with the table name tbl_A hard coded).&lt;/P&gt;
&lt;P&gt;Else if the argument is, say, "B", do another select (again, hard coded select statement, with tbl_B instead of tbl_A).&lt;/P&gt;
&lt;P&gt;I guess you can see where this fails: if you want different datatypes in the output, depending on the table you select, this solution won't be of any help, as the return type of the function is a table with statically defined columns.&lt;/P&gt;
&lt;P&gt;Also, performance-wise, I don't know what overhead this might give, so if you have any feedback, it's most welcome.&lt;/P&gt;
&lt;P&gt;Anyway, hope it helps.&lt;/P&gt;</description></item><item><title>Fun and fumes about Dynamic SQL &amp; SQL injection</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx#4027792</link><pubDate>Tue, 24 Jul 2007 15:27:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4027792</guid><dc:creator>SQL Server Transact-SQL (SSQA.net)</dc:creator><description>&lt;p&gt;D-SQLInjection things you need to consider and take care, just caught my eye recently.&lt;/p&gt;</description></item><item><title>Guard against SQL injection in dynamic PL/SQL</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx#7372188</link><pubDate>Fri, 01 Feb 2008 13:23:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7372188</guid><dc:creator>dave^2=-1</dc:creator><description>&lt;p&gt;I am slowly coming to terms with Oracle again, after a decade or so of using SQL Server exclusively.&lt;/p&gt;
</description></item><item><title>SQL Injection attacks</title><link>http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx#8649790</link><pubDate>Wed, 25 Jun 2008 03:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8649790</guid><dc:creator>Troubleshooting and Tips - Cindy Gross</dc:creator><description>&lt;p&gt;&amp;amp;lt;p&amp;amp;gt;This year SQL injection attacks are being stepped up and even automated against SQL Server. While SQL injection attacks can occur against any DBMS, my blog will only address SQL Server.&amp;amp;lt;/p ...&lt;/p&gt;
</description></item></channel></rss>