-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CC-35136 Adjusted sensitive data exposure. (#11289)
CC-35136 Adjusted sensitive data exposure.
- Loading branch information
Showing
8 changed files
with
289 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/Spryker/Shared/Twig/Extension/EnvironmentCoreExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Shared\Twig\Extension; | ||
|
||
use Twig\Environment; | ||
use Twig\Extension\CoreExtension; | ||
use Twig\TwigFilter; | ||
|
||
class EnvironmentCoreExtension implements EnvironmentCoreExtensionInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected const SYSTEM_FUNCTIONS = [ | ||
'exec', | ||
'shell_exec', | ||
'system', | ||
'passthru', | ||
'popen', | ||
'proc_open', | ||
'eval', | ||
'assert', | ||
'create_function', | ||
'preg_replace', // с /e modifier | ||
'include', | ||
'include_once', | ||
'require', | ||
'require_once', | ||
'file_get_contents', | ||
'file_put_contents', | ||
'fopen', | ||
'fwrite', | ||
'fread', | ||
'unlink', | ||
'chmod', | ||
'chown', | ||
'curl_exec', | ||
'curl_multi_exec', | ||
'phpinfo', | ||
'base64_decode', | ||
'base64_encode', | ||
'mail', | ||
'header', | ||
'set_include_path', | ||
'ini_set', | ||
'dl', | ||
'putenv', | ||
'apache_setenv', | ||
]; | ||
|
||
/** | ||
* @param \Twig\Environment $twig | ||
* | ||
* @return \Twig\Environment | ||
*/ | ||
public function extend(Environment $twig): Environment | ||
{ | ||
foreach ($this->getFilters() as $filter) { | ||
$twig->addFilter($filter); | ||
} | ||
|
||
return $twig; | ||
} | ||
|
||
/** | ||
* @param \Twig\Environment $env | ||
* @param array $array | ||
* @param \Closure $arrow | ||
* | ||
* @return \CallbackFilterIterator|array | ||
*/ | ||
public function filter(Environment $env, $array, $arrow) | ||
{ | ||
if ($this->isDisallowedPhpFunction($arrow)) { | ||
return $array; | ||
} | ||
|
||
if (method_exists(CoreExtension::class, 'filter')) { | ||
return CoreExtension::filter($env, $array, $arrow); | ||
} | ||
|
||
return twig_array_filter($env, $array, $arrow); | ||
} | ||
|
||
/** | ||
* @param \Twig\Environment $env | ||
* @param array $array | ||
* @param \Closure $arrow | ||
* | ||
* @return array | ||
*/ | ||
public function find(Environment $env, $array, $arrow) | ||
{ | ||
if ($this->isDisallowedPhpFunction($arrow)) { | ||
return $array; | ||
} | ||
|
||
return CoreExtension::find($env, $array, $arrow); | ||
} | ||
|
||
/** | ||
* @param \Twig\Environment $env | ||
* @param array $array | ||
* @param \Closure $arrow | ||
* | ||
* @return array | ||
*/ | ||
public function map(Environment $env, $array, $arrow) | ||
{ | ||
if ($this->isDisallowedPhpFunction($arrow)) { | ||
return $array; | ||
} | ||
|
||
if (method_exists(CoreExtension::class, 'map')) { | ||
return CoreExtension::map($env, $array, $arrow); | ||
} | ||
|
||
return twig_array_map($env, $array, $arrow); | ||
} | ||
|
||
/** | ||
* @param \Twig\Environment $env | ||
* @param array $array | ||
* @param \Closure $arrow | ||
* @param mixed|null $initial | ||
* | ||
* @return mixed|null | ||
*/ | ||
public function reduce(Environment $env, $array, $arrow, $initial = null) | ||
{ | ||
if ($this->isDisallowedPhpFunction($arrow)) { | ||
return $array; | ||
} | ||
|
||
if (method_exists(CoreExtension::class, 'reduce')) { | ||
return CoreExtension::reduce($env, $array, $arrow, $initial); | ||
} | ||
|
||
return twig_array_reduce($env, $array, $arrow, $initial); | ||
} | ||
|
||
/** | ||
* @param \Closure|null $arrow | ||
* | ||
* @return bool | ||
*/ | ||
protected function isDisallowedPhpFunction($arrow): bool | ||
{ | ||
return in_array($arrow, static::SYSTEM_FUNCTIONS); | ||
} | ||
|
||
/** | ||
* @return array<\Twig\TwigFilter> | ||
*/ | ||
protected function getFilters(): array | ||
{ | ||
$filters = [ | ||
new TwigFilter('filter', [$this, 'filter'], ['needs_environment' => true]), | ||
new TwigFilter('map', [$this, 'map'], ['needs_environment' => true]), | ||
new TwigFilter('reduce', [$this, 'reduce'], ['needs_environment' => true]), | ||
]; | ||
|
||
if (method_exists(CoreExtension::class, 'find')) { | ||
$filters[] = new TwigFilter('find', [$this, 'find'], ['needs_environment' => true]); | ||
} | ||
|
||
return $filters; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Spryker/Shared/Twig/Extension/EnvironmentCoreExtensionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Shared\Twig\Extension; | ||
|
||
use Twig\Environment; | ||
|
||
interface EnvironmentCoreExtensionInterface | ||
{ | ||
/** | ||
* @param \Twig\Environment $twig | ||
* | ||
* @return \Twig\Environment | ||
*/ | ||
public function extend(Environment $twig): Environment; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
tests/SprykerTest/Shared/Twig/Extension/EnvironmentCoreExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerTest\Shared\Twig\Extension; | ||
|
||
use Codeception\Test\Unit; | ||
use Spryker\Shared\Twig\Extension\EnvironmentCoreExtension; | ||
use SprykerTest\Shared\Twig\TwigSharedTester; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
/** | ||
* Auto-generated group annotations | ||
* | ||
* @group SprykerTest | ||
* @group Shared | ||
* @group Twig | ||
* @group Extension | ||
* @group EnvironmentCoreExtensionTest | ||
* Add your own group annotations below this line | ||
*/ | ||
class EnvironmentCoreExtensionTest extends Unit | ||
{ | ||
/** | ||
* @var \SprykerTest\Shared\Twig\TwigSharedTester | ||
*/ | ||
protected TwigSharedTester $tester; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testFilterShouldExcludeSystemPhpFunctionFromExecutions(): void | ||
{ | ||
// Arrange | ||
$environmentCoreExtension = new EnvironmentCoreExtension(); | ||
$twig = $this->tester->createTwigEnvironment(new ArrayLoader([ | ||
'test' => "{{ ['id'] | map('system') | join }} {{ ['php -v'] | reduce('exec') | join }} {{ [' php '] | map(value => value | trim) | join }}", | ||
])); | ||
$environmentCoreExtension->extend($twig); | ||
|
||
// Act | ||
$output = $twig->render('test'); | ||
|
||
// Assert | ||
$this->assertSame('id php -v php', $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters