Delirious":2ushprti said:
Ok, I'm relatively new at this so explain; "input sanitation"?
It is internet security 101: You sanitize any and all data that you get from the user end. You do this for reasons of 1. security and 2. stability.
First reason: Security.
If you do not remove bad data from the input you prevent the user from changing, reading or deleting your database content. SQL injections are common (perhaps the most common) security breaches today. It works by sending your script data that would change your query into something new.
Let us say your PHP script is called with this URL:
Code:
http://www.example.com/form.php?user=12&form_id=account_details&action=update&email=mail@example.com
Your code would probably look something like this (shortened for brevity):
Code:
<?php
// Get form data
$action = $_POST['action'];
$email = $_POST['email'];
$user = $_POST['user'];
$form = $_POST['form_id'];
if ($form == "update") {
// Build SQL update query
$query = "UPDATE users SET user_email=$email WHERE user_id=$user";
}
// Then run the query, no code written because >99% of all security problems are already present in the code above
?>
I count three types (some occurring more than once) of security problems in the code above.
- The code does not check that the $user is an integer by removing anything that is not in the range of [0-9] and that the remaining integer is the same as the logged in user has.
- The code does not remove or escape unwanted data from any of the inputs. If I wanted to do something nefarious I could send something along the lines of
Code:
http://www.example.com/form.php?user=*&form_id=account_details&action=update&email=mail@example.com
and have everyones account set to my e-mail address. Succeeding with this I am now a 1337 haX0r and can brag about it on 4chan.
- The code does not parametize or prepare the SQL query.
- Other good things to use is LIMIT 1 as often as possible which might sometimes mitigate some damage. This is a pretty good beginners article, and I am sure someone on this forum will discover at least one security consideration that I have forgotten to mention above.
PHP has many halfbaked solutions like mysql_real_escape_string or similar functions. I blame the PHP developers for not providing a solid single function and I blame every goddamned tutorial on the net that disregards security in the name of laziness. I always recommend developers to use PDO, not only is it more secure, it is also provides rudimentary database abstraction and some other nice feature you may appreciate when you get into more advanced development.
Second reason: Stability.
If I happen to include a cite or some html code or odd glyph in the input I could potentially cause problems for your form (returning errors and such unfriendly for the user behaviour). For example the someone with the name O'Connor will have trouble submitting their name in some forms. If you escape/encode the user input O'Connor, Jönsson, 陈 (Chén) will be as happy as you are because you wrote good code.