|
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 | {
|
@@ -313,6 +318,40 @@ public function provideCompareCases()
|
313 | 318 | [1, 42, "\x00\x34\x32"],
|
314 | 319 | ];
|
315 | 320 | }
|
| 321 | + |
| 322 | + public function testSandboxedInclude() |
| 323 | + { |
| 324 | + $twig = new Environment(new ArrayLoader([ |
| 325 | + 'index' => '{{ include("included", sandboxed=true) }}', |
| 326 | + 'included' => '{{ "included"|e }}', |
| 327 | + ])); |
| 328 | + $policy = new SecurityPolicy(allowedFunctions: ['include']); |
| 329 | + $sandbox = new SandboxExtension($policy, false); |
| 330 | + $twig->addExtension($sandbox); |
| 331 | + |
| 332 | + // We expect a compile error |
| 333 | + $this->expectException(SecurityError::class); |
| 334 | + $twig->render('index'); |
| 335 | + } |
| 336 | + |
| 337 | + public function testSandboxedIncludeWithPreloadedTemplate() |
| 338 | + { |
| 339 | + $twig = new Environment(new ArrayLoader([ |
| 340 | + 'index' => '{{ include("included", sandboxed=true) }}', |
| 341 | + 'included' => '{{ "included"|e }}', |
| 342 | + ])); |
| 343 | + $policy = new SecurityPolicy(allowedFunctions: ['include']); |
| 344 | + $sandbox = new SandboxExtension($policy, false); |
| 345 | + $twig->addExtension($sandbox); |
| 346 | + |
| 347 | + // The template is loaded without the sandbox enabled |
| 348 | + // so, no compile error |
| 349 | + $twig->load('included'); |
| 350 | + |
| 351 | + // We expect a runtime error |
| 352 | + $this->expectException(SecurityError::class); |
| 353 | + $twig->render('index'); |
| 354 | + } |
316 | 355 | }
|
317 | 356 |
|
318 | 357 | final class CoreTestIteratorAggregate implements \IteratorAggregate
|
|
0 commit comments