-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tainting] Allow Twig\Environment::render to be tainted even with a v…
…ariable as template name (#97) Allow Twig\Environment::render to be tainted even with a variable as template parameters Allow using a variable as template name for CachedTemplatesTainter too Add TwigUtils::extractTemplateNameFromExpression tests
- Loading branch information
1 parent
f75effe
commit 0397c58
Showing
7 changed files
with
221 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psalm\SymfonyPsalmPlugin\Twig; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Scalar\String_; | ||
use Psalm\StatementsSource; | ||
use Psalm\Type\Atomic\TLiteralString; | ||
use Psalm\Type\Atomic\TNull; | ||
use Psalm\Type\Union; | ||
use RuntimeException; | ||
|
||
class TwigUtils | ||
{ | ||
public static function extractTemplateNameFromExpression(Expr $templateName, StatementsSource $source): string | ||
{ | ||
if ($templateName instanceof Variable) { | ||
$type = $source->getNodeTypeProvider()->getType($templateName) ?? new Union([new TNull()]); | ||
$templateName = array_values($type->getAtomicTypes())[0]; | ||
} | ||
|
||
if (!$templateName instanceof String_ && !$templateName instanceof TLiteralString) { | ||
throw new RuntimeException(sprintf('Can not retrieve template name from given expression (%s)', get_class($templateName))); | ||
} | ||
|
||
return $templateName->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psalm\SymfonyPsalmPlugin\Tests\Symfony; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
use Psalm\Codebase; | ||
use Psalm\Config; | ||
use Psalm\Context; | ||
use Psalm\Internal\Analyzer\FileAnalyzer; | ||
use Psalm\Internal\Analyzer\ProjectAnalyzer; | ||
use Psalm\Internal\Analyzer\StatementsAnalyzer; | ||
use Psalm\Internal\Provider\FileProvider; | ||
use Psalm\Internal\Provider\NodeDataProvider; | ||
use Psalm\Internal\Provider\Providers; | ||
use Psalm\Internal\Provider\StatementsProvider; | ||
use Psalm\Plugin\Hook\AfterEveryFunctionCallAnalysisInterface; | ||
use Psalm\StatementsSource; | ||
use Psalm\Storage\FunctionStorage; | ||
use Psalm\SymfonyPsalmPlugin\Twig\TwigUtils; | ||
use RuntimeException; | ||
|
||
class TwigUtilsTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideExpressions | ||
*/ | ||
public function testItCanExtractTheTemplateNameFromAnExpression(string $expression) | ||
{ | ||
$code = '<?php'."\n".$expression; | ||
$statements = StatementsProvider::parseStatements($code, '7.1'); | ||
|
||
$assertionHook = new class() implements AfterEveryFunctionCallAnalysisInterface { | ||
public static function afterEveryFunctionCallAnalysis(FuncCall $expr, string $function_id, Context $context, StatementsSource $statements_source, Codebase $codebase): void | ||
{ | ||
Assert::assertSame('expected.twig', TwigUtils::extractTemplateNameFromExpression($expr->args[0]->value, $statements_source)); | ||
} | ||
}; | ||
|
||
$statementsAnalyzer = self::createStatementsAnalyzer($assertionHook); | ||
$statementsAnalyzer->analyze($statements, new Context()); | ||
} | ||
|
||
public function provideExpressions(): array | ||
{ | ||
return [ | ||
['dummy("expected.twig");'], | ||
['dummy(\'expected.twig\');'], | ||
['$a = "expected.twig"; dummy($a);'], | ||
]; | ||
} | ||
|
||
public function testItThrowsAnExceptionWhenTryingToExtractTemplateNameFromAnUnsupportedExpression() | ||
{ | ||
$code = '<?php'."\n".'dummy(123);'; | ||
$statements = StatementsProvider::parseStatements($code, '7.1'); | ||
|
||
$assertionHook = new class() implements AfterEveryFunctionCallAnalysisInterface { | ||
public static function afterEveryFunctionCallAnalysis(FuncCall $expr, string $function_id, Context $context, StatementsSource $statements_source, Codebase $codebase): void | ||
{ | ||
TwigUtils::extractTemplateNameFromExpression($expr->args[0]->value, $statements_source); | ||
} | ||
}; | ||
|
||
$statementsAnalyzer = self::createStatementsAnalyzer($assertionHook); | ||
|
||
$this->expectException(RuntimeException::class); | ||
$statementsAnalyzer->analyze($statements, new Context()); | ||
} | ||
|
||
private static function createStatementsAnalyzer(AfterEveryFunctionCallAnalysisInterface $hook): StatementsAnalyzer | ||
{ | ||
$config = (function () { return new self(); })->bindTo(null, Config::class)(); | ||
$config->after_every_function_checks[] = $hook; | ||
|
||
$nullFileAnalyzer = new FileAnalyzer(new ProjectAnalyzer($config, new Providers(new FileProvider())), '', ''); | ||
$nullFileAnalyzer->codebase->functions->addGlobalFunction('dummy', new FunctionStorage()); | ||
$nullFileAnalyzer->codebase->file_storage_provider->create(''); | ||
|
||
$nodeData = new NodeDataProvider(); | ||
(function () use ($nodeData) { | ||
$this->node_data = $nodeData; | ||
})->bindTo($nullFileAnalyzer, $nullFileAnalyzer)(); | ||
|
||
return new StatementsAnalyzer($nullFileAnalyzer, $nodeData); | ||
} | ||
} |