Skip to content

Commit

Permalink
Fixed use of rand() without a parameter in math function (for v3.1)
Browse files Browse the repository at this point in the history
Fixes #794
  • Loading branch information
wisskid committed Sep 12, 2022
1 parent b3ade90 commit 0a84d52
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixes
- Fixed use of `rand()` without a parameter in math function [#794](https://github.com/smarty-php/smarty/issues/794)

## [3.1.46] - 2022-08-01

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion libs/plugins/function.math.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function smarty_function_math($params, $template)
$number = '(?:\d+(?:[,.]\d+)?|pi|π)'; // What is a number
$functionsOrVars = '((?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))';
$operators = '[,+\/*\^%-]'; // Allowed math operators
$regexp = '/^(('.$number.'|'.$functionsOrVars.'|('.$functionsOrVars.'\s*\((?1)+\)|\((?1)+\)))(?:'.$operators.'(?1))?)+$/';
$regexp = '/^(('.$number.'|'.$functionsOrVars.'|('.$functionsOrVars.'\s*\((?1)*\)|\((?1)+\)))(?:'.$operators.'(?1))?)+$/';

if (!preg_match($regexp, $equation)) {
trigger_error("math: illegal characters", E_USER_WARNING);
Expand Down
8 changes: 8 additions & 0 deletions tests/UnitTests/TemplateSource/ValueTests/Math/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,12 @@ public function testBracketsIllegal()
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}

public function testRand()
{
$tpl = $this->smarty->createTemplate('eval:{$x = "0"}{math equation="x * rand()" x=$x}');
// this assertion may seem silly, but it serves to prove that using rand() without a parameter
// will not trigger a security error (see https://github.com/smarty-php/smarty/issues/794)
$this->assertEquals("0", $this->smarty->fetch($tpl));
}

}

0 comments on commit 0a84d52

Please sign in to comment.