|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Twig\Environment;
|
16 | 16 | use Twig\Error\RuntimeError;
|
| 17 | +use Twig\Extension\SandboxExtension; |
| 18 | +use Twig\Loader\ArrayLoader; |
17 | 19 | use Twig\Loader\LoaderInterface;
|
| 20 | +use Twig\Sandbox\SecurityError; |
| 21 | +use Twig\Sandbox\SecurityPolicy; |
18 | 22 |
|
19 | 23 | class CoreTest extends TestCase
|
20 | 24 | {
|
@@ -251,6 +255,40 @@ public function provideSliceFilterCases()
|
251 | 255 | [[], new \ArrayIterator([1, 2]), 3],
|
252 | 256 | ];
|
253 | 257 | }
|
| 258 | + |
| 259 | + public function testSandboxedInclude() |
| 260 | + { |
| 261 | + $twig = new Environment(new ArrayLoader([ |
| 262 | + 'index' => '{{ include("included", sandboxed=true) }}', |
| 263 | + 'included' => '{{ "included"|e }}', |
| 264 | + ])); |
| 265 | + $policy = new SecurityPolicy([], [], [], [], ['include']); |
| 266 | + $sandbox = new SandboxExtension($policy, false); |
| 267 | + $twig->addExtension($sandbox); |
| 268 | + |
| 269 | + // We expect a compile error |
| 270 | + $this->expectException(SecurityError::class); |
| 271 | + $twig->render('index'); |
| 272 | + } |
| 273 | + |
| 274 | + public function testSandboxedIncludeWithPreloadedTemplate() |
| 275 | + { |
| 276 | + $twig = new Environment(new ArrayLoader([ |
| 277 | + 'index' => '{{ include("included", sandboxed=true) }}', |
| 278 | + 'included' => '{{ "included"|e }}', |
| 279 | + ])); |
| 280 | + $policy = new SecurityPolicy([], [], [], [], ['include']); |
| 281 | + $sandbox = new SandboxExtension($policy, false); |
| 282 | + $twig->addExtension($sandbox); |
| 283 | + |
| 284 | + // The template is loaded without the sandbox enabled |
| 285 | + // so, no compile error |
| 286 | + $twig->load('included'); |
| 287 | + |
| 288 | + // We expect a runtime error |
| 289 | + $this->expectException(SecurityError::class); |
| 290 | + $twig->render('index'); |
| 291 | + } |
254 | 292 | }
|
255 | 293 |
|
256 | 294 | final class CoreTestIteratorAggregate implements \IteratorAggregate
|
|
0 commit comments