AA2e72E said:
Thanks for the example; I take the point.
I agree that what you describe as the "old/bad" way is bad even though it is
much better with C# than used to be with VB 6.0. That is what I was trying to
avoid.
I can see the advantages of SQLCommand and using parameters. You, like Jon,
mention "escaping": what is that?
Let's say you use the second query I posted. By default, that code is
vulnerable if the group variable is passed in, instead of set just above
the code block. Another example:
string group = "' haha it broke";
If the value of group contains a quotation mark, the query then becomes:
SELECT widgetId, description, createdDate FROM Widgets WHERE widgetGroup
= '' haha it broke' AND widgetType = 'A'
The problem here is the two single quotes will break.
Worse yet, a user could make group = "' --;". That may not seem bad
right, it's just an SQL error. However, consider this is your login
query and you handle authentication on your own:
public void auth(string user, string pass)
{
string sql = "SELECT username FROM users WHERE username = '" +
user + "' AND pass = '" + pass + "'";
// Check to see if we got the result
}
Now, let's say there's no input sanitizing going on, and a malicious
user puts in: "admin' --" as their username. The resulting query would
look like:
SELECT username FROM users WHERE username = 'admin' --' AND pass = ''
This results in it finding the users entry that matches just the
username, without the password being taken into account at all.
Obviously very very bad.
Anyhow, to the point, escaping is something done on a variety of SQL
server clients that basically takes unsafe characters and escapes them
similarly to newlines (\n).
If you did the above with parameters, the database client will escape
the values so that the last one would be:
SELECT username FROM users WHERE username = 'admin\' --' AND pass = ''
Some implementations will use '' instead of \', but ultimately to the
database server it will see that the user is trying to match "admin' --"
to a username, and probably fail.
Chris.