Skip to content
Putting technology to work.
Insights to guide decisions and action.

Search articles

Smarty 5.9.1 and 4.6.1 fix an issue where PHP runs through the cache

Table of contents · 8 items

Member names, product reviews, inquiry subjects. PHP sites that display user-entered values through templates sometimes enable template caching to speed up rendering. In the template engine Smarty, the process that writes those cache files had an issue that allowed PHP code to be injected through displayed values.

On November 27, Smarty released 5.9.1 on the 5.x line and 4.6.1 on the 4.x line. All three entries in the changelog (CHANGELOG.md) are fixes labeled “Security.” Under Smarty's security policy, fixes cover 4.x and 5.x; 3.x and earlier are not covered.

On the same day, three security advisories (GHSA) were also published in the Smarty repository. As of December 1, none of them had a CVE number, and none were listed in the GitHub Advisory Database. Tools that alert based on that database, such as composer audit and Dependabot, may not show a warning until they are listed.

In this article, based on the changes in the Smarty repository and the results of displaying the same values on 4.6.0, 4.6.1, 5.9.0 and 5.9.1 in a test environment, we summarize which sites are affected and how to upgrade.

The three fixes of November 27

The changelogs for 5.9.1 and 4.6.1 list the same three fixes.

  1. Cache writing (GHSA-p927-2m49-9p39, severity High, CVSS 8.1): Assigning a value such as <script language=php'> could escape the neutralization of PHP tags applied when writing cache files, allowing PHP to be injected into the cache file. The fix also runs output filters before neutralization rather than after, and stops writing the cache when regular expression processing fails
  2. Form element functions (GHSA-966m-5mq6-5q48, severity Medium, CVSS 6.1): name, id and class of {html_options}, prefix and field_array of {html_select_date} and {html_select_time}, the label of the empty option and others are now HTML-escaped. According to the advisory, the output of these functions is not covered by escape_html automatic escaping, so sites are affected even with escape_html enabled
  3. {html_image} (GHSA-x8f6-7h7x-vqq8, severity Low): When a security policy is enabled, the process that loads remote images no longer follows HTTP redirects. This prevents a redirect from a trusted destination ($trusted_uri) from reaching beyond it. The report says that when the same issue in {fetch} (CVE-2026-62993) was fixed in 5.8.2 and 4.5.7 in June, {html_image} was left unfixed

What was happening in the cache files

Smarty cache files are PHP files that store the rendered output. If the rendered output contains strings that look like PHP tags, such as <?php, they would be executed as is, so Smarty wraps them in PHP statements that “display them as text” when writing.

According to the descriptions of the fix commits (1223a6ca for 5.x, de9c6697 for 4.x), the <script language="php"> pattern in the regular expression that finds what to wrap was written in a way that could hit a quotation mark and break out of the wrapping. Since PHP 7, this tag no longer opens PHP code, so the fix removes this pattern from the targets and also changes how wrapping is done.

According to the advisory, the same process had one more flaw. If a value contains a partial <script language and long runs of whitespace, regular expression processing hits PHP's limit (pcre.backtrack_limit) and fails. Versions before the fix treated this failure as “no PHP tags” and wrote the rendered output to the cache file as is, without wrapping. The advisory states that with PHP's default settings, this occurred with a value of about 1.5 KB. 5.9.1 and 4.6.1 remove this pattern from the targets and do not write the cache when processing fails.

In the repository, the same process (smarty_internal_runtime_updatecache.php) in 3.1.48, the last 3.x release, still contains the same regular expression as the pre-fix 4.6.0 and 5.9.0.

5.8.5 and 4.5.8 on October 4 also fixed a different issue that allowed PHP to be injected into cache files (GHSA-3w63-v7pm-cq9x, CVE-2026-82531; when templates are inherited with extends:). The current advisory describes a different flaw in the same function with the same result, and states that the 5.8.5 fix did not change this part. As of December 1, the October advisory is not listed in the advisory database either. Upgrading from 5.8.4 or earlier (4.5.7 or earlier on 4.x) to 5.9.1 or 4.6.1 fixes both.

Results of testing on four versions

In a test environment (PHP 8.3.6), we installed Smarty 4.6.0, 4.6.1, 5.9.0 and 5.9.1 and rendered the template Hello {$name}! with a value assigned in the same form as the fix's tests (containing PHP code that creates one test file when executed). The first render wrote the cache file, and the second render displayed from the cache with a different value (one run each).

Results of displaying the same value on Smarty 4.6.0, 4.6.1, 5.9.0 and 5.9.1. With caching enabled and no escaping, the PHP code in the value was executed on 4.6.0 and 5.9.0 but not on 4.6.1 and 5.9.1. With caching disabled or escaping enabled, it was not executed on any version. Only 4.6.1 and 5.9.1 escaped the name of html_options

  • Caching enabled, no escaping: On 4.6.0 and 5.9.0, the PHP code in the value was executed when the cache file was written during the first render. On 4.6.1 and 5.9.1, it was not executed, and the value was displayed as text
  • Caching disabled: Not executed on any version
  • Caching enabled, escape_html enabled: Not executed on any version, because < in the value is turned into &lt; by HTML escaping

When pref" onfocus="alert(1)" autofocus=" was passed to name of {html_options}, 4.6.0 and 5.9.0 output <select name="pref" onfocus="alert(1)" autofocus="">, adding an attribute. 4.6.1 and 5.9.1 escaped it to &quot;.

We tested the advisory's second flaw (long whitespace) on 5.9.0 and 5.9.1 only, in a separate test environment (PHP 8.3.35, PCRE JIT enabled, pcre.backtrack_limit at the default of 1,000,000). When a value consisting of <script language=, 1,100 spaces, php, 1,100 spaces, one character and PHP code (about 2.3 KB) was rendered with caching enabled and no escaping, the PHP code was executed on 5.9.0 but not on 5.9.1. The result was the same when line breaks were used instead of spaces (one run each).

Conditions for being affected (editorial summary)

For the cache issue, check the following two points.

  1. Is caching enabled? Look for places where setCaching() (or $smarty->caching) is set to CACHING_LIFETIME_CURRENT or similar
  2. Are values entered by users or others displayed without escaping? This applies if you do not use setEscapeHtml(true) ($escape_html on 4.x) and some output in your templates lacks |escape. According to the advisory, output with nofilter (and |raw on 5.x) and output that does not apply HTML escaping, such as JSON, CSV and email, are also affected

The form element issue affects templates that pass user-entered values or URL values to name, id and class of functions such as {html_options}, or to prefix and field_array of {html_select_date} and {html_select_time}, among others. Enabling escape_html does not prevent it. If field names are written as fixed strings, this fix does not change the output.

If you use it with EC-CUBE 2.x

EC-CUBE 2.x, used by e-commerce sites in Japan, uses Smarty. Which Smarty line it uses depends on the EC-CUBE version. Looking at the EC-CUBE/ec-cube2 repository, we found the following.

  • The latest stable release, eccube-2.25.0 (March 2025), and the main branch (as of November 30) use 5.x. The composer.json constraint is "smarty/smarty": "^5.0.2", and composer.lock is 5.4.3 for 2.25.0 and 5.8.4 for main. Upgrading to 5.9.1 with composer update smarty/smarty is within range
  • The 2.17 line (composer.lock of eccube-2.17.2-p2) uses 3.1.40, and the 2.13 line (eccube-2.13.5) uses the bundled 2.6.27. Neither is covered by Smarty's fixes
  • We found no setting that enables caching in data/class on the main branch (nor in SC_View.php of 2.17.2-p2)
  • The default display modifier is set to script_escape, which replaces <script and others with #script escaped# (the same in 2.13.5, 2.17.2-p2 and main). When we reproduced this setting in the test environment using main's script_escape and enabled caching on 5.9.0, the PHP code was not executed with the value used for the four-version comparison. However, with the long-whitespace value, it was executed when line breaks were used instead of spaces. This is because the regular expression in script_escape is written so that it does not search for <script across line breaks
  • In the main branch templates, {html_options} was used in 51 files, and none passed name

With the core settings unchanged, caching is not enabled, so the conditions for the cache issue are not met. However, script_escape does not protect against this issue. Each site needs to check whether caching has been enabled by plugins or customizations. Because security fixes were also released for 5.8.x and earlier in June and October (5.8.5 in October addressed a different cache issue), we recommend upgrading 2.25.0 and later to 5.9.1. For the 2.17 and 2.13 lines, Smarty would have to be upgraded from 3.x or 2.x, so refer to the measures in “If you use 3.x” below (editorial suggestion). EC-CUBE 4.x uses Twig for templates. We covered Twig's advisories in the article on Twig's 11 advisories.

How to upgrade

If you installed it with Composer, check the version and upgrade as follows.

composer show smarty/smarty
# 5系
composer require smarty/smarty:^5.9.1
# 4系
composer require smarty/smarty:^4.6.1

If you placed the Smarty files on the server without Composer, check the version with SMARTY_VERSION in src/Smarty.php for 5.x or libs/Smarty.class.php for 4.x.

After upgrading, delete the contents of the cache directory (the location specified with setCacheDir()) and rebuild the cache. Cache files written by pre-fix versions are still loaded if they remain (editorial suggestion).

If you use 3.x

The last 3.x release is 3.1.48 from March 2023, and the PHP versions supported in composer.json are ^5.2 || ^7.0. No fix will be released. The affected versions in these advisories are listed as “below 4.6.1,” which includes 3.x. In advisories such as {html_options}, the reporter confirmed that 3.1.30 through 3.1.48 are also affected.

If you cannot move to 4.x or 5.x right away, possible stopgaps until you migrate are to disable caching, or to enable $escape_html and check the output. Issues such as {html_options} cannot be prevented with $escape_html, so check your templates for user values passed to name, id, class and similar (editorial suggestion; not tested on 3.x). Moving to 4.x or 5.x also means upgrading your PHP version. We covered PHP version updates in the article on PHP 8.6 session cookies and the article on PHP's November fixes.

Scope of verification

On December 1, 2026, we checked the smarty-php/smarty repository (tags v5.9.1, v4.6.1, v5.9.0, v4.6.0 and v3.1.48, CHANGELOG.md, SECURITY.md, the descriptions and diffs of fix commits 1223a6ca, de9c6697, 3d2a879c and e34c3eea, and the added tests), the repository's security advisories (the three from November 27 and one from October 4; we checked their text via the GitHub API), the publication dates on Packagist, and the EC-CUBE/ec-cube2 repository (composer.json and composer.lock, data/class/SC_View.php, data/smarty_extends/modifier.script_escape.php and templates at 0bd217eb on the main branch and at tag eccube-2.25.0; composer.lock at tag eccube-2.17.2-p2 and the bundled version in eccube-2.13.5) (desk research). On the same day, in a test environment using PHP 8.3.6 and Smarty 4.6.0, 4.6.1, 5.9.0 and 5.9.1, we displayed the same values while changing the caching and escaping settings (hands-on testing; one run each). The long-whitespace value was tested only on 5.9.0 and 5.9.1 in a separate test environment (PHP 8.3.35) (one run each). 3.x (3.1.48) cannot be installed on PHP 8.3, so we did not run it and only compared the source. We did not check the {html_image} redirect fix, the behavior of EC-CUBE 2.x itself, or usage in plugins. As of December 1, none of the four advisories were listed in the GitHub Advisory Database, and only the one from October has a CVE number.

For maintenance of business systems and e-commerce sites built with PHP, or for how to approach library updates, please contact us through Development, AI and automation consultation.

References

Share this articleXFacebook
Kakeru Suzuki

Fascinated by the possibilities of technology, has had a deep interest in programming and digital art since student days

Turn this article's theme into your company's next step

Concrete steps forward for your organization.

We organize your desired architecture, legacy systems, and operational requirements to formulate your next steps toward execution.

  • Desired architecture
  • Integration with existing environments
  • Operational requirements
Consult on development & operations initiatives

You can consult with us from the initial conceptual stage. Details from this article will be carried over to the inquiry form.

Receive the latest articles by email