File size: 4,086 Bytes
5fcd275 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | diff --git a/CHANGELOG.md b/CHANGELOG.md
index ee8eed885..31df422e3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Security
+- Prevent evasion of the `static_classes` security policy. This addresses CVE-2021-21408
+
## [4.0.2] - 2022-01-10
### Security
diff --git a/lexer/smarty_internal_templateparser.y b/lexer/smarty_internal_templateparser.y
index 3786466ff..620498765 100644
--- a/lexer/smarty_internal_templateparser.y
+++ b/lexer/smarty_internal_templateparser.y
@@ -747,6 +747,9 @@ value(res) ::= doublequoted_with_quotes(s). {
value(res) ::= varindexed(vi) DOUBLECOLON static_class_access(r). {
+ if ($this->security && $this->security->static_classes !== array()) {
+ $this->compiler->trigger_template_error('dynamic static class not allowed by security setting');
+ }
$prefixVar = $this->compiler->getNewPrefixVariable();
if (vi['var'] === '\'smarty\'') {
$this->compiler->appendPrefixCode("<?php {$prefixVar} = ". $this->compiler->compileTag('private_special_variable',array(),vi['smarty_internal_index']).';?>');
diff --git a/libs/sysplugins/smarty_internal_templateparser.php b/libs/sysplugins/smarty_internal_templateparser.php
index a065c94f7..a2dd0d6fb 100644
--- a/libs/sysplugins/smarty_internal_templateparser.php
+++ b/libs/sysplugins/smarty_internal_templateparser.php
@@ -2397,6 +2397,9 @@ public function yy_r90(){
}
// line 749 "../smarty/lexer/smarty_internal_templateparser.y"
public function yy_r94(){
+ if ($this->security && $this->security->static_classes !== array()) {
+ $this->compiler->trigger_template_error('dynamic static class not allowed by security setting');
+ }
$prefixVar = $this->compiler->getNewPrefixVariable();
if ($this->yystack[$this->yyidx + -2]->minor['var'] === '\'smarty\'') {
$this->compiler->appendPrefixCode("<?php {$prefixVar} = ". $this->compiler->compileTag('private_special_variable',array(),$this->yystack[$this->yyidx + -2]->minor['smarty_internal_index']).';?>');
diff --git a/tests/UnitTests/SecurityTests/SecurityTest.php b/tests/UnitTests/SecurityTests/SecurityTest.php
index 72bb3cdc0..4d1b8a3ab 100644
--- a/tests/UnitTests/SecurityTests/SecurityTest.php
+++ b/tests/UnitTests/SecurityTests/SecurityTest.php
@@ -257,19 +257,41 @@ public function testTrustedStaticClass()
$this->assertEquals('25', $this->smarty->fetch($tpl));
}
-/**
- * test not trusted PHP function
- * @runInSeparateProcess
- * @preserveGlobalState disabled
- */
- public function testNotTrustedStaticClass()
- {
+ /**
+ * test not trusted PHP function
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
+ public function testNotTrustedStaticClass()
+ {
$this->expectException('SmartyException');
$this->expectExceptionMessage('access to static class \'mysecuritystaticclass\' not allowed by security setting');
$this->smarty->security_policy->static_classes = array('null');
$this->smarty->fetch('string:{mysecuritystaticclass::square(5)}');
}
+ /**
+ * test not trusted PHP function
+ */
+ public function testNotTrustedStaticClassEval()
+ {
+ $this->expectException('SmartyException');
+ $this->expectExceptionMessage('dynamic static class not allowed by security setting');
+ $this->smarty->security_policy->static_classes = array('null');
+ $this->smarty->fetch('string:{$test = "mysecuritystaticclass"}{$test::square(5)}');
+ }
+
+ /**
+ * test not trusted PHP function
+ */
+ public function testNotTrustedStaticClassSmartyVar()
+ {
+ $this->expectException('SmartyException');
+ $this->expectExceptionMessage('dynamic static class not allowed by security setting');
+ $this->smarty->security_policy->static_classes = array('null');
+ $this->smarty->fetch('string:{$smarty.template_object::square(5)}');
+ }
+
public function testChangedTrustedDirectory()
{
$this->smarty->security_policy->secure_dir = array(
|