[tainting] improve twig template names resolving (#122)

This commit is contained in:
Adrien LUCAS 2020-12-15 08:52:58 +01:00 committed by GitHub
parent f5e4b97c53
commit a7d3a76ca0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -17,6 +17,18 @@ class TwigUtils
{
public static function extractTemplateNameFromExpression(Expr $templateName, StatementsSource $source): string
{
return self::resolveStringFromExpression($templateName, $source);
}
private static function resolveStringFromExpression(Expr $templateName, StatementsSource $source): string
{
if ($templateName instanceof Expr\BinaryOp\Concat) {
$right = self::resolveStringFromExpression($templateName->right, $source);
$left = self::resolveStringFromExpression($templateName->left, $source);
return $left.$right;
}
if ($templateName instanceof Variable) {
$type = $source->getNodeTypeProvider()->getType($templateName) ?? new Union([new TNull()]);
$templateName = array_values($type->getAtomicTypes())[0];

View File

@ -50,6 +50,8 @@ class TwigUtilsTest extends TestCase
['dummy("expected.twig");'],
['dummy(\'expected.twig\');'],
['$a = "expected.twig"; dummy($a);'],
['$a = "expected"; $b = ".twig"; dummy($a.$b);'],
['$a = "pected"; dummy("ex".$a.".twig");'],
];
}