Skip to content

Commit

Permalink
Merge pull request #156 from soukicz/master
Browse files Browse the repository at this point in the history
php injection - validate eval input from plural forms
  • Loading branch information
oscarotero committed Aug 24, 2017
2 parents 4f57f00 + 5754451 commit 372d40f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ protected function getPluralIndex($domain, $n, $fallback)
*/
private static function fixTerseIfs($code, $inner = false)
{
if (preg_match('~[^\s0-9n<>|&=\-+%?:();\$]~', str_replace('return ', '', $code))) {
throw new \InvalidArgumentException('Invalid Plural form: ' . $code);
}
/*
* (?P<expression>[^?]+) Capture everything up to ? as 'expression'
* \? ?
Expand Down
17 changes: 17 additions & 0 deletions tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ public function testPluralFunction()
$this->assertEquals('beaucoup de commentaires', n__('One comment', '%s comments', 3, ['%s' => 'beaucoup de']));
}

public function testPluralInjection()
{
$translations = new Translations();
$translations->setPluralForms(2,'fuu_call()');
$translations[] =
(new Translation(null, 'One comment', '%s comments'))
->setTranslation('Un commentaire')
->setPluralTranslations(['%s commentaires']);
$t = new Translator();
$t->loadTranslations($translations);

$t->register();

$this->setExpectedException('InvalidArgumentException');
n__('One comment', '%s comments', 3);
}

public function testContextFunction()
{
$translations = new Translations();
Expand Down

0 comments on commit 372d40f

Please sign in to comment.