Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP's DateTime class escape date from SQL injection

Tags:

php

Usually, I use PDO's prepared statements, type casting to (int), or PDO::quote() to prevent SQL injection. For this application, I need to modify the date using PHP before adding it to the query. Do I need to take extra steps to prevent SQL injection, or am I safe? Thanks

$date = new DateTime($_GET['suspect_user_provided_date']);
$date->add(new DateInterval('P1D'));
$sql='SELECT * FROM table WHERE date<"'.$date->format('Y-m-d').'"';
like image 209
user1032531 Avatar asked Jan 22 '13 16:01

user1032531


People also ask

What is SQL injection How are insecure exceptions and SQL injections handled?

SQL Injection (SQLi) is a type of an injection attack that makes it possible to execute malicious SQL statements. These statements control a database server behind a web application. Attackers can use SQL Injection vulnerabilities to bypass application security measures.

What is SQL injection attack in PHP?

Direct SQL Command Injection is a technique where an attacker creates or alters existing SQL commands to expose hidden data, or to override valuable ones, or even to execute dangerous system level commands on the database host.

What is manual SQL injection?

SQL injection is a code injection technique used to attack data-driven applications, in which malicious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker).


1 Answers

It doesn't matter if the DateTime object is safe or not. You should escape the data you are passing to the query and not to rely on the safety of the provided library. If you change the implementation, you will not need to care if the new implementation is safe or not. You should always escape. Otherwise you will try to answer - and to remember - for each function - was it safe for SQL? for HTML? for CSV? for http / mail headers? for... don't! The line of code that send a query should know nothing about the DateTime implementation and if it's safe or not

like image 69
Maxim Krizhanovsky Avatar answered Nov 15 '22 08:11

Maxim Krizhanovsky