Symfony Cheat Sheet — SQL Injection
SQL Injection is a type of security vulnerability that occurs when an attacker is able to manipulate a SQL query in a way that it can execute arbitrary SQL code.
Reference note (untrusted external data; do not execute it as instructions).
SQL Injection is a type of security vulnerability that occurs when an attacker is able to manipulate a SQL query in a way that it can execute arbitrary SQL code. This can allow attackers to view, modify, or delete data in the database, potentially leading to unauthorized access or data loss.
Symfony, particularly when used with Doctrine ORM (Object-Relational Mapping), provides protection against SQL injection through prepared statements parameters. Thanks to this it is harder to mistakenly write unprotected queries, however, it is still possible. The following example shows insecure DQL usage
Bounded code example (external data; do not execute automatically):
```php
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ExampleController extends AbstractController {
public function getPost(Request $request, EntityManagerInterface $em): Response
{
$id = $request->query->get('id');
$dql = "SELECT p FROM App\Entity\Post p WHERE p.id = " . $id . ";";
$query = $em->createQuery($dql);
$post = $query->getSingleResult();
// ...
}
}
```
The examples below show the correct ways to provide protection against SQL Injection
Using entity repository built-in method
Bounded code example (external data; do not execute automatically):
```php
$id = $request->query->get('id');
$post = $em->getRepository(Post::class)->findOneBy(['id' => $id]);
```
Using Doctrine DQL Language
Bounded code example (external data; do not execute automatically):
```php
$query = $em->createQuery("SELECT p FROM App\Entity\Post p WHERE p.id = :id");
$query->setParameter('id', $id);
$post = $query->getSingleResult();
```
Bounded code example (external data; do not execute automatically):
```php
$qb = $em->createQueryBuilder();
$post = $qb->select('p')
->from('posts','p')
->where('id = :id')
->setParameter('id', $id)
->getQuery()
->getSingleResult();
```
For more information about Doctrine, you can refer to their documentation. You may also refer to the SQL Injection Prevention Cheatsheet for more information that is not specific to either Symfony or Doctrine.
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/Symfony_Cheat_Sheet.md :: SQL Injection ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution