#47796 [Opn]: preg_replace /e modifier allows unexpected code execution

From: Date: Thu, 26 Mar 2009 22:38:24 +0000
Subject: #47796 [Opn]: preg_replace /e modifier allows unexpected code execution
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-135186@lists.php.net to get a copy of this message
ID: 47796 User updated by: spam04 at pornel dot net Reported By: spam04 at pornel dot net Status: Open Bug Type: PCRE related Operating System: * PHP Version: 5.2.9 New Comment: I forgot to add echo before preg_replace() in reproduce code. Previous Comments: ------------------------------------------------------------------------ [2009-03-26 22:36:08] spam04 at pornel dot net Description: ------------ preg_replace does not escape $ character. If double quotes are used in replacement code, this enables unwanted injection of variables or even execution of PHP code. My suggestion is to escape $ character and discourage use of single quotes in replacement code (because they're not compatible with the way $ and " are escaped). Reproduce code: --------------- // simple case: preg_replace('/.*/e','strtoupper("$0")', '$foo'); // code execution: class test { function pwnd() {echo "pwnd!\n";} function replace($str) { preg_replace('/.*/e','strtoupper("$0")', $str); } } $t = new test(); $t->replace('{$this->pwnd()}'); Expected result: ---------------- $FOO {$THIS->PWND()} Actual result: -------------- PHP Notice: Undefined variable: foo pwnd! ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=47796&edit=1

« previous php.bugs (#135186) next »