Skip to content

Commit 6856624

Browse files
committed
Merge branch 'js_escape_security_fix'
2 parents 5512d64 + 71d1135 commit 6856624

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Security
10+
- Fixed Cross site scripting vulnerability in Javascript escaping. This addresses CVE-2023-28447.
11+
912
### Fixed
1013
- `$smarty->muteUndefinedOrNullWarnings()` now also mutes PHP7 notices for undefined array indexes [#736](https://github.com/smarty-php/smarty/issues/736)
1114
- `$smarty->muteUndefinedOrNullWarnings()` now treats undefined vars and array access of a null or false variables

libs/plugins/modifier.escape.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
115115
// see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
116116
'<!--' => '<\!--',
117117
'<s' => '<\s',
118-
'<S' => '<\S'
118+
'<S' => '<\S',
119+
"`" => "\\\\`",
120+
"\${" => "\\\\\\$\\{"
119121
)
120122
);
121123
case 'mail':

libs/plugins/modifiercompiler.escape.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ function smarty_modifiercompiler_escape($params, Smarty_Internal_TemplateCompile
6464
// see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
6565
return 'strtr((string)' .
6666
$params[ 0 ] .
67-
', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", "</" => "<\/", "<!--" => "<\!--", "<s" => "<\s", "<S" => "<\S" ))';
67+
', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r",
68+
"\\n" => "\\\n", "</" => "<\/", "<!--" => "<\!--", "<s" => "<\s", "<S" => "<\S",
69+
"`" => "\\\\`", "\${" => "\\\\\\$\\{"))';
6870
}
6971
} catch (SmartyException $e) {
7072
// pass through to regular plugin fallback

tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierEscapeTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,25 @@ public function testNonstdWithoutMbstring()
237237
$this->assertEquals("sma'rty@&#187;example&#171;.com", $this->smarty->fetch($tpl));
238238
Smarty::$_MBSTRING = true;
239239
}
240+
241+
public function testTemplateLiteralBackticks()
242+
{
243+
$tpl = $this->smarty->createTemplate('string:{"`Hello, World!`"|escape:"javascript"}');
244+
$this->assertEquals("\\`Hello, World!\\`", $this->smarty->fetch($tpl));
245+
}
246+
247+
public function testTemplateLiteralInterpolation()
248+
{
249+
$tpl = $this->smarty->createTemplate('string:{$vector|escape:"javascript"}');
250+
$this->smarty->assign('vector', "`Hello, \${name}!`");
251+
$this->assertEquals("\\`Hello, \\\$\\{name}!\\`", $this->smarty->fetch($tpl));
252+
}
253+
254+
public function testTemplateLiteralBackticksAndInterpolation()
255+
{
256+
$this->smarty->assign('vector', '`${alert(`Hello, ${name}!`)}${`\n`}`');
257+
$tpl = $this->smarty->createTemplate('string:{$vector|escape:"javascript"}');
258+
$this->assertEquals("\\`\\\$\\{alert(\\`Hello, \\\$\\{name}!\\`)}\\\$\\{\\`\\\\n\\`}\\`", $this->smarty->fetch($tpl));
259+
}
260+
240261
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore anything in here, but keep this directory
2+
*

0 commit comments

Comments
 (0)