|
12 | 12 | */
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Twig\Environment; |
15 | 16 | use Twig\Error\RuntimeError;
|
16 | 17 | use Twig\Extension\CoreExtension;
|
| 18 | +use Twig\Extension\SandboxExtension; |
| 19 | +use Twig\Loader\ArrayLoader; |
| 20 | +use Twig\Sandbox\SecurityError; |
| 21 | +use Twig\Sandbox\SecurityPolicy; |
17 | 22 |
|
18 | 23 | class CoreTest extends TestCase
|
19 | 24 | {
|
@@ -354,6 +359,40 @@ public static function provideCompareCases()
|
354 | 359 | [1, 42, "\x00\x34\x32"],
|
355 | 360 | ];
|
356 | 361 | }
|
| 362 | + |
| 363 | + public function testSandboxedInclude() |
| 364 | + { |
| 365 | + $twig = new Environment(new ArrayLoader([ |
| 366 | + 'index' => '{{ include("included", sandboxed: true) }}', |
| 367 | + 'included' => '{{ "included"|e }}', |
| 368 | + ])); |
| 369 | + $policy = new SecurityPolicy(allowedFunctions: ['include']); |
| 370 | + $sandbox = new SandboxExtension($policy, false); |
| 371 | + $twig->addExtension($sandbox); |
| 372 | + |
| 373 | + // We expect a compile error |
| 374 | + $this->expectException(SecurityError::class); |
| 375 | + $twig->render('index'); |
| 376 | + } |
| 377 | + |
| 378 | + public function testSandboxedIncludeWithPreloadedTemplate() |
| 379 | + { |
| 380 | + $twig = new Environment(new ArrayLoader([ |
| 381 | + 'index' => '{{ include("included", sandboxed: true) }}', |
| 382 | + 'included' => '{{ "included"|e }}', |
| 383 | + ])); |
| 384 | + $policy = new SecurityPolicy(allowedFunctions: ['include']); |
| 385 | + $sandbox = new SandboxExtension($policy, false); |
| 386 | + $twig->addExtension($sandbox); |
| 387 | + |
| 388 | + // The template is loaded without the sandbox enabled |
| 389 | + // so, no compile error |
| 390 | + $twig->load('included'); |
| 391 | + |
| 392 | + // We expect a runtime error |
| 393 | + $this->expectException(SecurityError::class); |
| 394 | + $twig->render('index'); |
| 395 | + } |
357 | 396 | }
|
358 | 397 |
|
359 | 398 | final class CoreTestIteratorAggregate implements \IteratorAggregate
|
|
0 commit comments