mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-24 20:56:27 +00:00
Fix sandbox bypass: PHP code injection via {% use %} template name
This commit is contained in:
committed by
Fabien Potencier
parent
afe54db2e5
commit
e9ff55f691
@@ -21,6 +21,7 @@ namespace Twig\Tests\Node;
|
||||
*/
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
use Twig\Node\BodyNode;
|
||||
use Twig\Node\EmptyNode;
|
||||
@@ -56,6 +57,29 @@ class ModuleTest extends NodeTestCase
|
||||
$this->assertEquals($source->getName(), $node->getTemplateName());
|
||||
}
|
||||
|
||||
public function testUseTagTemplateNameDoesNotInjectPhpInCompiledOutput()
|
||||
{
|
||||
$evilName = "evil' . print('BAD-EOL') . '.twig";
|
||||
$loader = new ArrayLoader([
|
||||
$evilName => '{% block existing %}ok{% endblock %}',
|
||||
'main.twig' => "{% use \"$evilName\" with absent_block as alias %}",
|
||||
]);
|
||||
$twig = new Environment($loader);
|
||||
|
||||
ob_start();
|
||||
$message = null;
|
||||
try {
|
||||
$twig->load('main.twig');
|
||||
} catch (RuntimeError $e) {
|
||||
$message = $e->getMessage();
|
||||
}
|
||||
$stdout = ob_get_clean();
|
||||
|
||||
$this->assertSame('', $stdout, 'No code from the template name must execute when the trait is loaded.');
|
||||
$this->assertNotNull($message, 'A RuntimeError must be raised for the missing block.');
|
||||
$this->assertStringContainsString($evilName, $message, 'The error message must contain the literal template name.');
|
||||
}
|
||||
|
||||
public static function provideTests(): iterable
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader(['foo.twig' => '{{ foo }}']));
|
||||
|
||||
Reference in New Issue
Block a user