@@ -423,6 +423,40 @@ public function testSandboxAllowProperty()
423423 $ this ->assertEquals ('bar ' , $ twig ->load ('1_basic4 ' )->render (self ::$ params ), 'Sandbox allow some properties ' );
424424 }
425425
426+ public function testSandboxAllowDestructuring ()
427+ {
428+ $ template = '{% do {bar: x, foo: y} = obj %}{{ x }}-{{ y }} ' ;
429+ $ twig = $ this ->getEnvironment (true , [], ['index ' => $ template ], ['do ' ], [], ['Twig\Tests\Extension\FooObject ' => 'foo ' ], ['Twig\Tests\Extension\FooObject ' => 'bar ' ]);
430+ FooObject::reset ();
431+ $ this ->assertSame ('bar-foo ' , $ twig ->load ('index ' )->render (self ::$ params ), 'Sandbox allows destructuring when properties and methods are allowed ' );
432+ }
433+
434+ public function testSandboxUnallowedDestructuringProperty ()
435+ {
436+ $ template = '{% do {bar: x} = obj %}{{ x }} ' ;
437+ $ twig = $ this ->getEnvironment (true , [], ['index ' => $ template ], ['do ' ]);
438+ try {
439+ $ twig ->load ('index ' )->render (self ::$ params );
440+ $ this ->fail ('Sandbox throws a SecurityError exception if an unallowed property is read via destructuring ' );
441+ } catch (SecurityNotAllowedPropertyError $ e ) {
442+ $ this ->assertSame ('Twig\Tests\Extension\FooObject ' , $ e ->getClassName ());
443+ $ this ->assertSame ('bar ' , $ e ->getPropertyName ());
444+ }
445+ }
446+
447+ public function testSandboxUnallowedDestructuringMethod ()
448+ {
449+ $ template = '{% do {foo: y} = obj %}{{ y }} ' ;
450+ $ twig = $ this ->getEnvironment (true , [], ['index ' => $ template ], ['do ' ], [], [], ['Twig\Tests\Extension\FooObject ' => 'foo ' ]);
451+ try {
452+ $ twig ->load ('index ' )->render (self ::$ params );
453+ $ this ->fail ('Sandbox throws a SecurityError exception if an unallowed method is called via destructuring ' );
454+ } catch (SecurityNotAllowedMethodError $ e ) {
455+ $ this ->assertSame ('Twig\Tests\Extension\FooObject ' , $ e ->getClassName ());
456+ $ this ->assertSame ('foo ' , $ e ->getMethodName ());
457+ }
458+ }
459+
426460 public function testSandboxAllowFunction ()
427461 {
428462 $ twig = $ this ->getEnvironment (true , [], self ::$ templates , [], [], [], [], ['cycle ' ]);
0 commit comments