forked from EC-CUBE/ec-cube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request EC-CUBE#6058 from EC-CUBE/dev/4.2_twig_sandbox
脆弱性対応
- Loading branch information
Showing
7 changed files
with
403 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
parameters: | ||
level: 1 | ||
ignoreErrors: | ||
- | ||
message: "#^Function twig_include not found\\.$#" | ||
path: src/Eccube/Twig/Extension/IgnoreTwigSandboxErrorExtension.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
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
78 changes: 78 additions & 0 deletions
78
src/Eccube/Twig/Extension/IgnoreTwigSandboxErrorExtension.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,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of EC-CUBE | ||
* | ||
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. | ||
* | ||
* http://www.ec-cube.co.jp/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Eccube\Twig\Extension; | ||
|
||
use Twig\Environment; | ||
use Twig\Error\LoaderError; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\Extension\SandboxExtension; | ||
use Twig\Sandbox\SecurityError; | ||
use Twig\TwigFunction; | ||
|
||
/** | ||
* \vendor\twig\twig\src\Extension\CoreExtension の拡張 | ||
*/ | ||
class IgnoreTwigSandboxErrorExtension extends AbstractExtension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('include', [$this, 'twig_include'], ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['all']]), | ||
]; | ||
} | ||
|
||
/** | ||
* twig sandboxの例外を操作します | ||
* app_env = devの場合、エラーを表示する | ||
* app_env = prodの場合、エラーを表示しない | ||
* | ||
* @param Environment $env | ||
* @param $context | ||
* @param $template | ||
* @param $variables | ||
* @param $withContext | ||
* @param $ignoreMissing | ||
* @param $sandboxed | ||
* | ||
* @return string|void | ||
* | ||
* @throws LoaderError | ||
* @throws SecurityError | ||
*/ | ||
public function twig_include(Environment $env, $context, $template, $variables = [], $withContext = true, $ignoreMissing = false, $sandboxed = false) | ||
{ | ||
try { | ||
return \twig_include($env, $context, $template, $variables, $withContext, $ignoreMissing, $sandboxed); | ||
} catch (SecurityError $e) { | ||
|
||
// devではエラー画面が表示されるようにする | ||
$appEnv = env('APP_ENV'); | ||
if ($appEnv === 'dev') { | ||
throw $e; | ||
} else { | ||
// ログ出力 | ||
log_warning($e->getMessage(), ['exception' => $e]); | ||
|
||
// 例外がスローされた場合、sandboxが効いた状態になってしまうため追加 | ||
$sandbox = $env->getExtension(SandboxExtension::class); | ||
if (!$sandbox->isSandboxedGlobally()) { | ||
$sandbox->disableSandbox(); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of EC-CUBE | ||
* | ||
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. | ||
* | ||
* http://www.ec-cube.co.jp/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Eccube\Twig\Sandbox; | ||
|
||
use Twig\Sandbox\SecurityPolicy as BasePolicy; | ||
use Twig\Sandbox\SecurityPolicyInterface; | ||
|
||
class SecurityPolicyDecorator implements SecurityPolicyInterface { | ||
|
||
/** @var BasePolicy */ | ||
private $securityPolicy; | ||
|
||
public function __construct(BasePolicy $securityPolicy) | ||
{ | ||
$this->securityPolicy = $securityPolicy; | ||
} | ||
|
||
public function checkSecurity($tags, $filters, $functions) | ||
{ | ||
$this->securityPolicy->checkSecurity($tags, $filters, $functions); | ||
} | ||
|
||
public function checkMethodAllowed($obj, $method) | ||
{ | ||
// __toStringの場合はチェックをスキップする | ||
if ($method === '__toString') { | ||
return; | ||
} | ||
$this->securityPolicy->checkMethodAllowed($obj, $method); | ||
} | ||
|
||
public function checkPropertyAllowed($obj, $method) | ||
{ | ||
$this->securityPolicy->checkPropertyAllowed($obj, $method); | ||
} | ||
} |
Oops, something went wrong.