Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how safe are PDO prepared statements

Started using PDO prepared statements not too long ago, and, as i understand, it does all the escaping/security for you.

for example, assuming $_POST['title'] is a form field.

$title = $_POST['title']; $query = "insert into blog(userID, title) values (?, ?)" $st = $sql->prepare($query); $st->bindParam(1, $_SESSION['user']['userID'], PDO::PARAM_INT); $st->bindParam(2, $title); $st->execute(); 

Is this really safe? Do i have to do anything else? what else do i have to take into consideration?

Thanks.

like image 927
sqram Avatar asked Aug 21 '09 22:08

sqram


People also ask

Is PDO Quote safe?

Basically quote() is safe as prepared statements but it depends on the proper implementation of quote() and of course also on it's consequent usage. Additionally the implementation of the used database system/PDO driver has to be taken into account in order to answer the question.

Is prepared statement Secure?

A prepared statement is a parameterized and reusable SQL query which forces the developer to write the SQL command and the user-provided data separately. The SQL command is executed safely, preventing SQL Injection vulnerabilities.

What is the purpose and advantages of PDO prepared statement?

Prepared statements are so useful that they are the only feature that PDO will emulate for drivers that don't support them. This ensures that an application will be able to use the same data access paradigm regardless of the capabilities of the database.

Is PDO safe from SQL injection?

The short answer is NO, PDO prepares will not defend you from all possible SQL-Injection attacks.


2 Answers

Strictly speaking, there's actually no escaping needed, because the parameter value is never interpolated into the query string.

The way query parameters work is that the query is sent to the database server when you called prepare(), and parameter values are sent later, when you called execute(). So they are kept separate from the textual form of the query. There's never an opportunity for SQL injection (provided PDO::ATTR_EMULATE_PREPARES is false).

So yes, query parameters help you to avoid that form of security vulnerability.

Are they 100% proof against any security vulnerability? No, of course not. As you may know, a query parameter only takes the place of a single literal value in an SQL expression. You can't make a single parameter substitute for a list of values, for example:

SELECT * FROM blog WHERE userid IN ( ? ); 

You can't use a parameter to make table names or column names dynamic:

SELECT * FROM blog ORDER BY ?; 

You can't use a parameter for any other type of SQL syntax:

SELECT EXTRACT( ? FROM datetime_column) AS variable_datetime_element FROM blog; 

So there are quite a few cases where you have to manipulate the query as a string, prior to the prepare() call. In these cases, you still need to write code carefully to avoid SQL injection.

like image 103
Bill Karwin Avatar answered Sep 28 '22 04:09

Bill Karwin


It's safe from SQL injection.

A couple things it's NOT safe from:

  • Denial of service (causing excessive amounts of rows to be created)
  • Cross-site scripting attacks (if title is ever echoed back to another user)

Security is more than just preventing SQL injection.

like image 20
Yuliy Avatar answered Sep 28 '22 05:09

Yuliy