473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SQL Injection: Parameters Collection

Paxxer
22
Microsoft recommends using the parameters collection at this link to help prevent SQL Injection.

I am using the following code snipet

Expand|Select|Wrap|Line Numbers
  1. cmd.CommandText = "SELECT plazaName from Plaza WHERE zip=@ZipCode";
  2. cmd.Parameters.Clear();
  3. cmd.Parameters.Add("@ZipCode", SqlDbType.VarChar, 4);
  4. cmd.Parameters["@ZipCode"].Value = "45840";
  5.  
Basically, I expect an exception to be raised when the value of @ZipCode is set to a string of length 5. But no error is raised?

Can any of you shed light on why this error isn't being raised?

Thanks
Tom
Feb 16 '09 #1
8 3718
PRR
750 Expert 512MB
You need to have client and server side validation on your inputs before you can submit you input to be put into database .....You can raise exception or tell user about the requirements of fields beforehand( while validating ) ....
check this too
Feb 17 '09 #2
Paxxer
22
DeepBlue,

Thank you for the reply and link to the article.

Yes, I have validation, but my question is not about how to use validation to limit SQL Injection, but why doesn't the Parameters collection raise an error.
Feb 17 '09 #3
PRR
750 Expert 512MB
i think when you execute query you get the error ... and not when adding parameters...parameter are added as literals i guess , so they as passed as it is..so when you are passing "45840" its passed as it is... (not as int or number etc).. the checking (and or conversion ) happens at the stored procedure end .... i may be wrong here...
Feb 17 '09 #4
Frinavale
9,735 Expert Mod 8TB
Parameters are not meant to validate your input.

Your code should validate your input before it reaches the stage where you want to use the input in an SQL query.

SQL Insertion attack can occur if you dynamically create your SQL query using unvalidated input from the user.

Say you create your SQL query as such:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM myTable WHERE theid = '" + myTextBox.Text + "'"
This is compiled by the database into a command which it executes.
So, under normal use the compiled command would be something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM myTable WHERE theid = '12345'
But, if someone were trying to do something malicious to your application they may enter their own SQL command instead of the expected input. This would be compiled along with your code and they would be able to execute what ever they wanted to....all they have to do is end your SQL query, add their own SQL query and then comment out the rest of your query.

So, in order to prevent this, you should use parameters.
These allow you to provide user input as literals. It indicates that these values should not be compiled as SQL but used as harmless Strings etc instead.

The parameters are not meant to validate your input.
You should still check to make sure that the values you're updating with will fit in the database and match the type that is expected etc etc....data validation. This will not only avoid SQL Insertion Attacks but will also avoid other problems that may result from leaving these things unchecked (liked Buffer Over Flow Attacks etc etc).

Check out this article for more information about SQL Injection Attacks

-Frinny
Feb 18 '09 #5
Paxxer
22
Hey Frinny,

It seems to be the case, that the parameters collection is not doing any validations. From my link at the beginning of this thread I was lead to believe that validation would occur.
here is a snippet:

Use type-safe SQL parameters for data access. You can use these parameters with stored procedures or dynamically constructed SQL command strings. Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code. An additional benefit of using a parameters collection is that you can enforce type and length checks. Values outside of the range trigger an exception. This is a good example of defense in depth.
.
So I was trying to us "DEFENSE in DEPTH", yet, no exception is triggered, by a parameter value that is too long for the specified parameter.

(I think I will write a routine that takes the parameter collection as a parameter, then just loop through and do the checks myself and raise errors as needed. That way I'll still get the "DEFENSE in DEPTH", incase a field gets through my client and server side validation).

Thanks for you interest in this issue Frinny,
I get alot of benefit from bytes.com
Tom
..
Feb 18 '09 #6
Frinavale
9,735 Expert Mod 8TB
@Paxxer
I've been looking into this more, despite what that article said about the SqlParameterCollection raising an exception if the data is out of range, I cannot find any information on any exceptions the SqlParameterCollection should raise.

Could you try something for me?
Could you increase the length of your string to include at least 1 more character for your zip code parameter? See if this raises an exception....it's kind of a strange thought but in VB.NET arrays are declared by providing the number of indexes expected as apposed to the size expected and I'm wondering if this might be what's going on here...by specifying 4, it's actually creating space for 5. It probably wont raise the exception but it would satisfy my curiosity.

Either that article is wrong, or an exception is supposed to be raised if the data doesn't fit.

There is another SqlParameterCollection Add Method:
Add(String, SqlDbType, Int32, String)
This one adds a SqlParameter to the SqlParameterCollection with the parameter name, the data type, and the column length. I wonder if this one would raise an exception?
Feb 19 '09 #7
Paxxer
22
The SqlParameterCollection does not seem to be raising the errors.

I tried what you suggested. I made the string input much longer than the length of the specified parameter, and the parameter was happy to get the value.

Then I set a breakpoint so I could examine the parameter. The parameter Size property shows the Int32 value as expected. Yet when you look at the Value property or SqlValue property they are both holding strings with a longer lenght then the size.

Next I thought SQL Server might throw an error if the size was too long. But no, it only truncates the input without an error.

---
Future ideas:
When i get to it. I will definately write a routine that loops through all of the parameters just prior to executing the query. What I like about this is, I will be able to create one error message for all of the parameter since I am checking the parameters together as a "batch".

--
Another note
If you use a stored procedure, that defines the length, then it will raise an error if you pass in a parameter that is too long.

Thanks for you efforts
Tom
Feb 24 '09 #8
Frinavale
9,735 Expert Mod 8TB
@Paxxer
I actually do this quite often.
It makes sense to do things in batches rather than having the user constantly have to post back just to find that there's something else that they've entered incorrectly.

@Paxxer
This is interesting.
I'll have to keep this in mind for the future.
Feb 24 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: | last post by:
Hi, I was performing SQL UPDATE queries and I notice that they SUCCEED on the ExecuteNonQuery() call with NO exceptions raised BUT they fail at the Database. They say they succeed in the code...
11
by: Bã§TãRÐ | last post by:
I have been working on this particular project for a little over 2 weeks now. This product contains between 700-900 stored procedures to handle just about all you can imagine within the product. I...
6
by: Arpan | last post by:
Microsoft advises not to pass parameters to the Command object in the Execute statement. Why? Thanks, Arpan
12
by: Mr Beaton | last post by:
Hey all, I'm far from new to programming, neither ASP nor SQL. However, my SQL knowhow is apparently wanting... I have allways validated user input to pieces prior to integrating it into a...
2
by: Adam | last post by:
Hey guys I’m pretty new to this parameterized commands thing and I don’t know why this code isn’t working. I have a Connection object set up as Conn, and a Rec object which is holding my values....
0
by: martin | last post by:
Hi, I am writin a function to execute stored procedures. The function gets passed an array with the values of the parameters and if they are input out put params. I attach these to the command...
0
by: Wouter | last post by:
Hi, I am working on some transformations of XML data using Xslt sheets. One part in this process uses the XsltArguments class to pass on additional parameters to the xslt. My question is the...
1
by: John Kotuby | last post by:
Hi all, I am working on porting an application from VB6 to VB.NET 2003 and am running into some problems. When declaring and populating the parameters for a SQL Stored Procedure by using the...
7
by: keyser soze | last post by:
hi i have a stored proc, pointed by a synonym i wish to execute it vía: cmd.commandType= adStoredProc cmd.commandText= "s_MyStoredProc" cmd.parameters.refresh ---to get the collection the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.