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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.