Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP regular expression to remove all javascript with exception

I'm looking for a way to remove all JavaScripts tags from a html string.

Following regex works fine, but I would like to add an exception:

$html = preg_replace('#<script[^>]*>.*?</script>#is', '', $html);

How can I add a rule that scripts of a type text/html are getting ignored?

<script type="text/html" ... > ... </script> 

Any suggestion?

Thanks in advance.

like image 465
Mayko Avatar asked Aug 15 '26 12:08

Mayko


1 Answers

You may not be trying to sanitize untrusted HTML, but just so readers of this question don't get the wrong idea:

This won't remove javascript outside <script> elements : <img src=bogus onerror=alert(42)>.

It won't remove barely obfuscated scripts : <script>alert(42)</script >.

It will turn invalid content into scripts : <scrip<script></script>t>alert(42)</script>.

I'm not saying this is what you're trying to do. You may have perfectly good reasons for doing this that don't have to do with untrusted inputs, but, for later readers, don't try to roll your own HTML sanitizer with just regular expressions.

like image 193
Mike Samuel Avatar answered Aug 17 '26 03:08

Mike Samuel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!