From 36a49052ca2db4357102993bbd14a224b910b91b Mon Sep 17 00:00:00 2001 From: Oliver Skroblin Date: Thu, 22 Jun 2017 13:51:11 +0200 Subject: [PATCH 1/2] SW-18917 - Optimize template security for mail preview and merchant mails --- engine/Shopware/Components/StringCompiler.php | 1 - .../Shopware/Components/Template/Security.php | 41 +++++++++++++++++++ engine/Shopware/Configs/Default.php | 3 ++ engine/Shopware/Controllers/Backend/Mail.php | 8 ++++ .../Shopware/Controllers/Backend/Widgets.php | 8 ++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 engine/Shopware/Components/Template/Security.php diff --git a/engine/Shopware/Components/StringCompiler.php b/engine/Shopware/Components/StringCompiler.php index cd86bd0858e..6c065d0d5c3 100644 --- a/engine/Shopware/Components/StringCompiler.php +++ b/engine/Shopware/Components/StringCompiler.php @@ -164,7 +164,6 @@ public function compileSmartyString($value, $context) try { $template = $templateEngine->createTemplate('string:' . $value); - $template->enableSecurity(); $template->assign($context); $template = $template->fetch(); } catch (SmartyCompilerException $e) { diff --git a/engine/Shopware/Components/Template/Security.php b/engine/Shopware/Components/Template/Security.php new file mode 100644 index 00000000000..232c0dd8208 --- /dev/null +++ b/engine/Shopware/Components/Template/Security.php @@ -0,0 +1,41 @@ + $value) { + if (property_exists($this, $key)) { + $this->$key = $value; + } + } + } + + parent::__construct($smarty); + } +} diff --git a/engine/Shopware/Configs/Default.php b/engine/Shopware/Configs/Default.php index c247b48f21c..d34ff21bdf3 100644 --- a/engine/Shopware/Configs/Default.php +++ b/engine/Shopware/Configs/Default.php @@ -199,4 +199,7 @@ 'use_trans_sid' => 0, 'locking' => false, ], + 'template_security' => [ + 'php_modifiers' => ['nl2br', 'escape', 'count'], + ], ], $customConfig); diff --git a/engine/Shopware/Controllers/Backend/Mail.php b/engine/Shopware/Controllers/Backend/Mail.php index 4016fbe21ce..0e8f07cbc3c 100644 --- a/engine/Shopware/Controllers/Backend/Mail.php +++ b/engine/Shopware/Controllers/Backend/Mail.php @@ -22,6 +22,7 @@ * our trademarks remain entirely with us. */ +use Shopware\Components\Template\Security; use Shopware\Models\Mail\Attachment; use Shopware\Models\Mail\Mail; use Shopware\Models\Shop\Shop; @@ -353,6 +354,13 @@ public function verifySmartyAction() $this->View()->assign(['success' => false, 'message' => 'Value not found']); } + $this->View()->Engine()->enableSecurity( + new Security( + $this->View()->Engine(), + $this->container->getParameter('shopware.template_security') + ) + ); + $compiler = new Shopware_Components_StringCompiler($this->View()->Engine()); $shop = Shopware()->Models()->getRepository(Shop::class)->getActiveDefault(); diff --git a/engine/Shopware/Controllers/Backend/Widgets.php b/engine/Shopware/Controllers/Backend/Widgets.php index 986773d7823..daee4fd143a 100644 --- a/engine/Shopware/Controllers/Backend/Widgets.php +++ b/engine/Shopware/Controllers/Backend/Widgets.php @@ -22,6 +22,7 @@ * our trademarks remain entirely with us. */ +use Shopware\Components\Template\Security; use Shopware\Models\Shop\Locale; /** @@ -677,6 +678,13 @@ public function sendMailToMerchantAction() $content = preg_replace('`([\\n\\r])`', '$1', $params['content']); + $this->View()->Engine()->enableSecurity( + new Security( + $this->View()->Engine(), + $this->container->getParameter('shopware.template_security') + ) + ); + $compiler = new Shopware_Components_StringCompiler($this->View()->Engine()); $defaultContext = [ 'sConfig' => Shopware()->Config(), From 283bb52301af9fef4da9bc8d8cc4b6a1f27bc767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20K=C3=BCper?= Date: Thu, 22 Jun 2017 14:39:30 +0200 Subject: [PATCH 2/2] SW-18917 - Add whitelist for smarty functions --- engine/Shopware/Configs/Default.php | 3 +- engine/Shopware/Configs/smarty_functions.php | 358 +++++++++++++++++++ 2 files changed, 360 insertions(+), 1 deletion(-) create mode 100644 engine/Shopware/Configs/smarty_functions.php diff --git a/engine/Shopware/Configs/Default.php b/engine/Shopware/Configs/Default.php index d34ff21bdf3..e10a3f7be89 100644 --- a/engine/Shopware/Configs/Default.php +++ b/engine/Shopware/Configs/Default.php @@ -200,6 +200,7 @@ 'locking' => false, ], 'template_security' => [ - 'php_modifiers' => ['nl2br', 'escape', 'count'], + 'php_modifiers' => include __DIR__ . '/smarty_functions.php', + 'php_functions' => include __DIR__ . '/smarty_functions.php', ], ], $customConfig); diff --git a/engine/Shopware/Configs/smarty_functions.php b/engine/Shopware/Configs/smarty_functions.php new file mode 100644 index 00000000000..d303b691180 --- /dev/null +++ b/engine/Shopware/Configs/smarty_functions.php @@ -0,0 +1,358 @@ +