Jump to content

Will this clean a string of PHP tags?


discomatt

Recommended Posts

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 );

Link to comment
https://forums.phpfreaks.com/topic/109635-will-this-clean-a-string-of-php-tags/
Share on other sites

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 :D

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.