discomatt Posted June 10, 2008 Share Posted June 10, 2008 I'm building a template class, and I want to have the option of sanitizing PHP tags out of strings that will get plugged into a given template ( the template will get eval'ed at some point ) Here's my class method public function sanitize ( $string ) { $search = array( '/<\?/', '/<script language=[\'"]?+php[\'"]?+[^>]*+>/i', '/<%/' ); $replace = array( '<?', '<script language="php">', '<%' ); return preg_replace( $search, $replace, $string ); } If anyone can see any way a crafty user might be able to inject some PHP tags in there let me know. The template will be eval'ed using the following code eval( '?>' . $buffer ); Quote Link to comment Share on other sites More sharing options...
lemmin Posted June 10, 2008 Share Posted June 10, 2008 'language' doesn't always have to be the first property in the script tag. Someone could even make up a property like: <script name="name" language="php"> Quote Link to comment Share on other sites More sharing options...
discomatt Posted June 10, 2008 Author Share Posted June 10, 2008 Yes, but PHP won't parse code tagged like that... at least, not with my tests. If I'm wrong please correct me. But you did help me notice one thing I'm missing... PHP will parse if there are several spaces after script Sooo my regex has changed to '/<script[ ]++language=[\'"]?+php[\'"]?+[^>]*+>/i' Thank you for the indirect help Quote Link to comment Share on other sites More sharing options...
rarebit Posted June 10, 2008 Share Posted June 10, 2008 I use bbcode. First use htmlentities, then regex the bb into html... This way you only let through exactly what you want, everything else is sanitised... Quote Link to comment Share on other sites More sharing options...
discomatt Posted June 10, 2008 Author Share Posted June 10, 2008 This is a template system... I will want full HTML in most cases, and in some cases, PHP code parsing. Hence the eval call. This is simply a paranoid check to make sure a malicious user hasn't managed to inject bad code into template files. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.