-
Jan 18th, 2023, 05:34 PM
#1
Thread Starter
Junior Member
VB.net - OleDB - How to use parametrized table names in a SQL Command?
Hi,
I was quite displeased to learn that I can't parameterize tablenames/fieldnames in an OledbCommand....
Code:
Dim CMDStr As String = "SELECT CustomerID FROM ?;" 'OleDB use a question mark as the placeholder?
Dim CMD As New OleDbCommand(CMDStr, vConnection)
CMD.Parameters.AddWithValue("@Tablename", Tablename)
Return CMD.ExecuteScalar 'fail...............
Use parametrized queries they say! SQL injection you will fall victim to, they say!
I suppose that leaves string concatenation as my only option to solve this problem? If that is true, any tips on validating user input? Any libraries/tools built-in to .NET that help with this, or is it as simple as using a white/black list to check for bad expressions / keywords?
For context: Scope of the application is not of huge security concern, but I always like to be secure!
Thanks for any tips.
-
Jan 18th, 2023, 05:39 PM
#2
Re: VB.net - OleDB - How to use parametrized table names in a SQL Command?
You don't have to worry about this unless you are concatenating in user content. If the user is allowed to type in some string, and you stick that directly into the SQL query, then you DO have a problem. If the user is selecting from a collection (like a drop down list of tables), then you don't need to worry about it. It's only if they are allowed to type in something, and you are sticking that into a query, that you open yourself up to SQL Injection attacks.
So, is that an issue? If so, I'd suggest that you design this in a different fashion. The set of tables that exist in a database can be determined, so it is ALWAYS possible to give the user a list of tables they can choose from.
My usual boring signature: Nothing
-
Jan 18th, 2023, 05:40 PM
#3
Re: VB.net - OleDB - How to use parametrized table names in a SQL Command?
Assuming your app has knowledge/visibility into the available table names, you could present the user with a dropdown list of tables to choose from. Then you could concatenate what are limited to known table names into the query.
If that isn't an option, perhaps you need to explain more what the end user is doing with your program.
-
Jan 18th, 2023, 05:46 PM
#4
Re: VB.net - OleDB - How to use parametrized table names in a SQL Command?
Originally Posted by OptionBase1
If that isn't an option, perhaps you need to explain more what the end user is doing with your program.
Absolutely, especially since you and I both assumed that the table name that was being concatenated in, was coming from the user. If that's not the case, then SQL injection doesn't matter anyways.
My usual boring signature: Nothing
-
Jan 18th, 2023, 06:11 PM
#5
Thread Starter
Junior Member
Re: VB.net - OleDB - How to use parametrized table names in a SQL Command?
Hi,
Thanks for the replies. The detail of this particular piece of the project is building some wrapper functions to interact with a DB. We are still far away from front end user interaction, so building security into this level of abstraction is a consideration to be had. My thought process is, why validate at every single source, when the called function can handle this security? The safe is unlocked inside the house. No problem if all the doors are locked. What if I forget to lock one of them?
-
Jan 18th, 2023, 06:48 PM
#6
Re: VB.net - OleDB - How to use parametrized table names in a SQL Command?
That path leads to madness.
SQL Injection only applies if user supplied data can be concatenated into the query. If the table name is not something that the user can type into a textbox, then you don't need to worry about it. A user typing a table name into a textbox...well, that would be mighty weird.
My usual boring signature: Nothing
-
Jan 18th, 2023, 08:00 PM
#7
Re: VB.net - OleDB - How to use parametrized table names in a SQL Command?
On the subject of why you can't do this in the first place, think about how VB works. If you're calling a method, you can use parameters to pass values into that but you can't use a parameter to specify the method itself. You can't use a parameter to store an identifier. Why should you be able to do it in SQL? Table names and column names in your SQL are identifiers. Parameters are for data values, not for identifiers.
As has already been pointed out, you can get a list of tables from a database and have the user select one from that list and know that it's safe. SQL injection is only an issue for arbitrary user input and if you're allowing arbitrary user input for a table name then any issues that may result are your fault for doing it wrong in the first place.
-
Jan 18th, 2023, 09:55 PM
#8
Re: VB.net - OleDB - How to use parametrized table names in a SQL Command?
If you’re worried about SQL Injection Attacks, have a look at this…
https://social.technet.microsoft.com...d-queries.aspx
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 18th, 2023, 10:07 PM
#9
Re: VB.net - OleDB - How to use parametrized table names in a SQL Command?
Originally Posted by .paul.
I don't think that addresses the issue. I think the OP understands the problem of SQL injection and how parameters can protect against it, but that doesn't help when the name of the table is variable.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|