Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php injection - validate eval input from plural forms #156

Merged
merged 4 commits into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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