Bugfix/issue 1189 inheritance state leak (#1190)

* Reproduce block override leakage in template inheritance
* Fixes #1189
This commit is contained in:
Simon Wisselink
2026-06-23 22:50:39 +02:00
committed by GitHub
parent 383e20e6b4
commit 56ab75df2c
5 changed files with 43 additions and 2 deletions
+6 -2
View File
@@ -69,8 +69,12 @@ class InheritanceRuntime {
* @param array $blockNames outer level block name
*/
public function init(Template $tpl, $initChild, $blockNames = []) {
// if called while executing parent template it must be a sub-template with new inheritance root
if ($initChild && $this->state === 3 && (strpos($tpl->template_resource, 'extendsall') === false)) {
// if called while executing parent template it must be a sub-template with new inheritance root.
// A new root is started either by a child template ($initChild) or by a sub-template included
// outside of any block rendering (empty source stack); the latter must not inherit the leftover
// block overrides of a previously completed inheritance tree (see issue #1189).
if (($initChild || empty($this->sourceStack)) && $this->state === 3
&& (strpos($tpl->template_resource, 'extendsall') === false)) {
$tpl->setInheritance(clone $tpl->getSmarty()->getRuntime('Inheritance'));
$tpl->getInheritance()->init($tpl, $initChild, $blockNames);
return;
@@ -0,0 +1,33 @@
<?php
/**
* Smarty PHPunit test reproducing issue #1189.
*
* When a parent template is {include}d, then a child template that {extends}
* the parent overrides a {block}, a subsequent {include} of the parent in the
* same render must still show the parent's block content.
*
* The block override from the extending child must not leak into the later
* include of the parent template.
*
* @see https://github.com/smarty-php/smarty/issues/1189
*
* @preserveGlobalState disabled
*/
class IncludeExtendsBlockLeakIssue1189Test extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* Sequence: include parent -> include child(extends parent) -> include parent.
* Expected: PARENT CHILD PARENT
* Bug (#1189): PARENT CHILD CHILD
*/
public function testBlockOverrideDoesNotLeakIntoLaterParentInclude()
{
$result = $this->smarty->fetch('top.tpl');
$this->assertSame('PARENT CHILD PARENT', preg_replace('/\s+/', ' ', trim($result)));
}
}
@@ -0,0 +1,2 @@
{extends file="parent.tpl"}
{block name=message}CHILD{/block}
@@ -0,0 +1 @@
{block name=message}PARENT{/block}
@@ -0,0 +1 @@
{include file="parent.tpl"} {include file="child.tpl"} {include file="parent.tpl"}