I have this string ($str) and I'm trying to prevent Javascript execution:
<script> ... </script>
<link onload="...">
<div onmouseover="...">
I apply these commands:
$find = Array("script", "onload", "onmouseover");
$rep = Array("noscript", "no_onload", "no_onmouseover");
$new = str_replace($find, $rep, $str);
Working example: http://codepad.org/kwuQvRiM
I know there are other event attributes but to make the question shorter I've choosen just 3 elements/attributes, but.
If I allow users to populate $str
instead of declare it in the PHP source ( $str = $_GET["str"]
for example), so an user can changes the $str
code can somehow exploit this protection?
Imagine that $new
is stored in a database and used later, I want to know if the string will be safe from javascript execution.
N.B. I know there are safer ways to perform this task but this question is for a didactic study and I need to perform in this way