I am a tyro in web security and have been researching on it for two days. According to OWSAP, SQL Injection and XSS attacks are the most common over the internet and at the minimal must be handled by every programmer.
So whatever I understood to protect them is the following (you are requested to correct it or add if I am wrong):
Use PDO and prepared statements to prevent SQL Injection
PDO and prepared statements are sufficient to prevent (first-order) SQL Injection and we do not need to do any escaping on input data as the driver handles that.
BUT this may lead you prone to second order SQL injection (see this for more) where a data like ' OR '1'='
may get stored into the database after passing through the PDO and prepared statements as they store raw data and to prevent this makes me feel to rather escape the string first and hence
use $pdo->quote($string) before passing it to prepared statement for storage
But since I also want protection against XSS attack I should use htmlentities()
as well (or htmlspecialchars()
for minimal case) .I should do this at the output but I may prefer to use at the input side if my output is targeted for HTML only
To summarize,my steps would be
$string ='raw input from user';
$escaped_string=$pdo->quote(htmlentities($string));
$pdo->execute('query to store $escaped_string into the database');
while ouputting
simply echo the stored field from the database.
I want to know whether my approach is secure or not?