Static analysis (SAST) check · PHP

Template injection from request input (PHP)

A value read from the HTTP request is compiled as a Twig or Blade template.

Why it matters

Template code reaches objects and filters, so the caller can read data or run code on this server.

How to fix it

Keep template text in files on the server and pass the request value in the context the template renders.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (PHP)
<?php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MailController
{
    public function preview(Request $request, \Twig\Environment $twig): Response
    {
        $template = $twig->createTemplate($request->request->get('body'));
        return new Response($template->render(['name' => 'Ada']));
    }
}
Fixed: not reported (PHP)
<?php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MailController
{
    public function preview(Request $request, \Twig\Environment $twig): Response
    {
        return new Response($twig->render('mail/welcome.html.twig', ['name' => $request->request->get('name')]));
    }
}

What TrustIvan reads

Runs on repository scans of PHP source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.