views:

67

answers:

4

Hi there,

this question had been evolving in my mind, how do i totally stop the users from entering some crazy SQL injections. isn't mysql_real_escape_string powerful enough to stop it? i followed some guidelines though there were some users in here who criticized my code and gave me thumbs down for the security. i was unable to understand the reason behind it. though i am not using $_GET, the only user input is through commenting system. i just want to make sure i am not going wrong. here is my sample code.

$name = htmlspecialchars(strip_tags(mysql_real_escape_string($_POST['com_name'])));

I have used the same for some 5 fields. what is your take on my above code?

+1  A: 

The idea is not to stop the users from entering it, but to make sure you enter them in the database query safely. This means using mysql_real_escape_string or using parameterized queries with mysqli:

/* Prepare an insert statement */
$query = "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)";
$stmt = mysqli_prepare($link, $query);

mysqli_stmt_bind_param($stmt, "sss", $val1, $val2, $val3);

You also need to make sure your properly html encode fields you read from the database, to avoid that the user is able to inject html (and also javascript)

Sander Rijken
i do not want my user to enter any html or javascript codes, it should be just the plain text for atleast this project. don't you think my code is taking care of it?
Ibrahim Azhar Armar
Your general code should allow the user to insert any(valid) data into the DB. If you need extra filtering then you should do it in a separate validation phase, not when entering it in the database.
Quamis
@Quamis i guess that's what i am doing, i am filtering the code first and then passing it to database, i never passed it directly to the database. correct me if i am wrong.
Ibrahim Azhar Armar
@Ibrahim look.You are thinking of it from wrong point of view(as almost everyone do): database escaping/parametrising has nothing to do with user input.It is not a propection from user input! It's just query formattting rules. Which should be followed for ANY data, no matter where it came from. Your database layer should be responsible for the SQL only.It has nothing to do with user input. You have to separate these tacks.Database escaping should be done for ANY data, no matter id user input or not. While stripping tags should be done un unterusted input, not admin's input.It's different tasks
Col. Shrapnel
+2  A: 

If you are inserting user input into the database than mysql_real_escape_string will suffice. Better yet, make use of prepared statements - PDO or MySQLi.

If you are simply displaying user data on the page you should make use of htmlspecialchars() or htmlentities()

As for the code you have posted. Lets break it down.

  • htmlspecialchars - converts html characters into respective entities.
  • strip_tags - Strips HTML and PHP tags from the code
  • mysql_real_escape_string - Escapes characters prior to DB entry.

So, as you can see, the first two functions you are using seem rather counter productive. You are covnerting HTML chars into entities, but you are also stripping tags.

To make sure you are securing user input before DB entry mysql_real_escape_string will suffice. Or as I mentioned earlier, make use of prepared statements.

Russell Dias
mysql_real_escape_string will not suffice. mysql_real_escape_string **plus quotes** will
Col. Shrapnel
+2  A: 

It's possible your call to mysql_real_escape_string will fail if a connection to your database cannot be found or created.

I tend to put such calls as deep down as possible right before I actually perform a query. It may kill my performance a little, but I don't have to worry about missing a parameter higher up (with respect to sql injection), and I know I always have a valid connection to the database at that point.

The first thing I also do on all user input is run it through filter_var with FILTER_SANITIZE_SPECIAL_CHARS before doing anything else with it.

James
there is nothing to kill any performance, but you fail to see a proper reason to put escaping "right before query". It is not because escaping needed for input data, as everyone in this poor world thinks. But for SQL query. So, right before query is the ONLY possible place.
Col. Shrapnel
Ah yes, I see what you're getting out. Thanks for pointing out what isn't quite as obvious as it should be. :)
James
+2  A: 

No, your code isn't quite secure and even less sensible.

Here is a complete answer about SQL injections I posted before
In short, mysql_real_escape_string itself doesn't protect anything. It works only if you put your data in quotes.

As for the htmlspecialchars/strip_tags, both has nothing to do with SQL at all, it's HTML protection, not SQL. And using both of them is redundant. Just one of them is enough. I'd prefer htmlspecialchars.

Col. Shrapnel