From 212bf88eb23791edcb9ab662ea438743b40f5943 Mon Sep 17 00:00:00 2001 From: Jack Dane Date: Sun, 22 Oct 2023 22:49:46 +0100 Subject: [PATCH 1/6] Fix case-sensitive tag names (#907) (#910) * Don't lower tags until they are used for extensions so custom tags can be case-sensitive. --- src/Compiler/Template.php | 5 +++-- src/Security.php | 2 ++ .../RegisterFunction/RegisterFunctionTest.php | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Compiler/Template.php b/src/Compiler/Template.php index 03ee5110..e584236a 100644 --- a/src/Compiler/Template.php +++ b/src/Compiler/Template.php @@ -464,7 +464,7 @@ class Template extends BaseCompiler { public function compileTag($tag, $args, $parameter = []) { $this->prefixCodeStack[] = $this->prefix_code; $this->prefix_code = []; - $result = $this->compileTag2(strtolower($tag), $args, $parameter); + $result = $this->compileTag2($tag, $args, $parameter); $this->prefix_code = array_merge($this->prefix_code, array_pop($this->prefixCodeStack)); return $result; } @@ -591,6 +591,7 @@ class Template extends BaseCompiler { * @return ?\Smarty\Compile\CompilerInterface tag compiler object or null if not found or untrusted by security policy */ public function getTagCompiler($tag): ?\Smarty\Compile\CompilerInterface { + $tag = strtolower($tag); if (isset($this->smarty->security_policy) && !$this->smarty->security_policy->isTrustedTag($tag, $this)) { return null; @@ -1114,7 +1115,7 @@ class Template extends BaseCompiler { } } - // call to function previousely defined by {function} tag + // call to function previously defined by {function} tag if ($this->canCompileTemplateFunctionCall($tag)) { if (!empty($parameter['modifierlist'])) { diff --git a/src/Security.php b/src/Security.php index 46cddcee..2f654d4e 100644 --- a/src/Security.php +++ b/src/Security.php @@ -261,6 +261,8 @@ class Security { * @return boolean true if tag is trusted */ public function isTrustedTag($tag_name, $compiler) { + $tag_name = strtolower($tag_name); + // check for internal always required tags if (in_array($tag_name, ['assign', 'call'])) { return true; diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php index 6aa91240..7c738505 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php @@ -39,6 +39,17 @@ class RegisterFunctionTest extends PHPUnit_Smarty $this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testfunction value=1}')); } + /** + * test registerPlugin method for function case-sensitive + */ + public function testRegisterFunctionCaseInsensitive() + { + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testFunction', 'myfunction'); + $this->assertEquals('myfunction', + $this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testFunction')[0]); + $this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testFunction value=1}')); + } + /** * test registerPlugin method for function class */ From babec0f29b47fb878d4b144913c6f1e567b186ec Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Sun, 22 Oct 2023 22:41:21 +0200 Subject: [PATCH 2/6] add case sensitivity test --- .../RegisterFunction/RegisterFunctionTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php index 7c738505..473cbb0b 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php @@ -166,6 +166,18 @@ class RegisterFunctionTest extends PHPUnit_Smarty $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction'); $this->assertIsArray($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testfunction')); } + + /** + * Test case (in)sensitivy of plugin functions + * @return void + * @throws \Smarty\Exception + * @group issue907 + */ + public function testCaseSensitivity() { + $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'customTag', 'myfunction'); + $this->assertEquals('hello world ', $this->smarty->fetch('string:{customTag}')); + } + } function myfunction($params, $smarty) From 9ee3cce380434b1f4802943da339ca192c026dde Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Sun, 22 Oct 2023 23:55:07 +0200 Subject: [PATCH 3/6] Added some extra unit test --- src/Compiler/Template.php | 2 ++ src/Security.php | 2 +- .../RegisterFunction/RegisterFunctionTest.php | 24 +++++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Compiler/Template.php b/src/Compiler/Template.php index e584236a..a782eedd 100644 --- a/src/Compiler/Template.php +++ b/src/Compiler/Template.php @@ -593,6 +593,8 @@ class Template extends BaseCompiler { public function getTagCompiler($tag): ?\Smarty\Compile\CompilerInterface { $tag = strtolower($tag); + $tag = strtolower($tag); + if (isset($this->smarty->security_policy) && !$this->smarty->security_policy->isTrustedTag($tag, $this)) { return null; } diff --git a/src/Security.php b/src/Security.php index 2f654d4e..250b3bca 100644 --- a/src/Security.php +++ b/src/Security.php @@ -261,7 +261,7 @@ class Security { * @return boolean true if tag is trusted */ public function isTrustedTag($tag_name, $compiler) { - $tag_name = strtolower($tag_name); + $tag_name = strtolower($tag_name); // check for internal always required tags if (in_array($tag_name, ['assign', 'call'])) { diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php index 473cbb0b..81549525 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php @@ -154,7 +154,7 @@ class RegisterFunctionTest extends PHPUnit_Smarty public function testUnregisterFunctionNotRegistered() { $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction'); - $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction')); + $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction')); } /** @@ -164,18 +164,32 @@ class RegisterFunctionTest extends PHPUnit_Smarty { $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testfunction', 'myfunction'); $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction'); - $this->assertIsArray($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testfunction')); + $this->assertIsArray($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testfunction')); } + /** * Test case (in)sensitivy of plugin functions + * @param $registerName + * @param $templateString * @return void * @throws \Smarty\Exception * @group issue907 + * @dataProvider dataProviderForCaseSensitivity */ - public function testCaseSensitivity() { - $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'customTag', 'myfunction'); - $this->assertEquals('hello world ', $this->smarty->fetch('string:{customTag}')); + public function testCaseSensitivity($registerName, $templateString) { + $this->smarty->registerPlugin( + Smarty::PLUGIN_FUNCTION, + $registerName, + function($params, $smarty) { return 'function-output'; }); + $this->assertEquals('function-output', $this->smarty->fetch('string:' . $templateString)); + } + + public function dataProviderForCaseSensitivity() { + return [ + ['customTag', '{customTag}'], + ['customtag', '{customtag}'], + ]; } } From 4d22803c32f5a8260472b397e938863177030c97 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Sun, 22 Oct 2023 23:56:37 +0200 Subject: [PATCH 4/6] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5947423a..6bcc5555 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Registered output filters wouldn't run [#899](https://github.com/smarty-php/smarty/issues/899) - Use of negative numbers in {math} equations [#895](https://github.com/smarty-php/smarty/issues/895) +- Fix case-sensitive tag names [#907](https://github.com/smarty-php/smarty/issues/907) ### Removed - Removed `$smarty->registered_filters` array From ad73f4943be05bbd2c63fe74b5966abd5be43eb9 Mon Sep 17 00:00:00 2001 From: Shad Date: Sat, 28 Oct 2023 16:41:10 +0200 Subject: [PATCH 5/6] Typos in documentation (#914) --- .../language-builtin-functions/language-function-include.md | 4 ++-- .../language-builtin-functions/language-function-section.md | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/designers/language-builtin-functions/language-function-include.md b/docs/designers/language-builtin-functions/language-function-include.md index bb481f79..0710db7d 100644 --- a/docs/designers/language-builtin-functions/language-function-include.md +++ b/docs/designers/language-builtin-functions/language-function-include.md @@ -85,11 +85,11 @@ The template above includes the example `links.tpl` below ```smarty
-

{$title}{/h3> +

{$title}

    {foreach from=$links item=l} .. do stuff ... -
``` diff --git a/docs/designers/language-builtin-functions/language-function-section.md b/docs/designers/language-builtin-functions/language-function-section.md index ba17224c..ff57e560 100644 --- a/docs/designers/language-builtin-functions/language-function-section.md +++ b/docs/designers/language-builtin-functions/language-function-section.md @@ -326,10 +326,10 @@ The template to output the database result in a HTML table ```smarty - + {section name=co loop=$contacts} - + @@ -465,7 +465,7 @@ header block every five rows.
 Name>HomeCellEmail
 NameHomeCellEmail
viewview {$contacts[co].name} {$contacts[co].home} {$contacts[co].cell}
{section name=co loop=$contacts} {if $smarty.section.co.iteration is div by 5} - + {/if} From b28b85dbf48d6f82f48079065922a25eaa33088b Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Mon, 6 Nov 2023 13:22:07 +0100 Subject: [PATCH 6/6] Add support for MacOS test running using mutagen-compose. --- docker-compose.yml | 34 ++++++++++++++++++++++++++++++- run-tests-for-all-php-versions.sh | 14 +++++++------ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 799cdfb8..770b243a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: context: . dockerfile: ./utilities/testrunners/php72/Dockerfile volumes: - - .:/app + - smarty-code:/app working_dir: /app php72: extends: @@ -37,3 +37,35 @@ services: service: base build: dockerfile: ./utilities/testrunners/php82/Dockerfile + +volumes: + smarty-code: + +x-mutagen: + sync: + defaults: + symlink: + mode: "posix-raw" + ignore: + vcs: true + paths: + - ".docker" + - ".env.docker" + - ".bundles" + - "docker-compose.yml" + - ".idea" + - ".DS_Store" + mode: "two-way-resolved" + configurationBeta: + permissions: + defaultOwner: "id:${APP_USER_ID}" + defaultGroup: "id:${APP_GROUP_ID}" + defaultFileMode: 0666 + defaultDirectoryMode: 0755 + permissions: + defaultOwner: "id:${APP_USER_ID}" + defaultGroup: "id:${APP_GROUP_ID}" + smarty-code: + alpha: "." + beta: "volume://smarty-code" + diff --git a/run-tests-for-all-php-versions.sh b/run-tests-for-all-php-versions.sh index 1f1b94c2..b2a4133a 100755 --- a/run-tests-for-all-php-versions.sh +++ b/run-tests-for-all-php-versions.sh @@ -5,9 +5,11 @@ # - ./run-tests-for-all-php-versions.sh --group 20221124 # - ./run-tests-for-all-php-versions.sh --exclude-group slow -docker-compose run php72 ./run-tests.sh $@ && \ -docker-compose run php73 ./run-tests.sh $@ && \ -docker-compose run php74 ./run-tests.sh $@ && \ -docker-compose run php80 ./run-tests.sh $@ && \ -docker-compose run php81 ./run-tests.sh $@ && \ -docker-compose run php82 ./run-tests.sh $@ +COMPOSE_CMD="mutagen-compose" + +$COMPOSE_CMD run --rm php72 ./run-tests.sh $@ && \ +$COMPOSE_CMD run --rm php74 ./run-tests.sh $@ && \ +$COMPOSE_CMD run --rm php74 ./run-tests.sh $@ && \ +$COMPOSE_CMD run --rm php80 ./run-tests.sh $@ && \ +$COMPOSE_CMD run --rm php81 ./run-tests.sh $@ && \ +$COMPOSE_CMD run --rm php82 ./run-tests.sh $@
 Name>HomeCellEmail
 NameHomeCellEmail
view