A friend of me posted a code about how to prevent xss attack using DOM.
What do you think about this code ? Can we optimize it ?
<?php
function parseDoc(DOMDocument $codeHtml){
$forbiddenTag=array('script');
$forbiddenAttr=array('onmouseover','onmouseup','onclick');
foreach($forbiddenTag as $tag){
$liste=$codeHtml->getElementsByTagName($tag);
foreach($liste as $element){
$codeHtml->removeChild($element);
}
}
stripAttr($codeHtml,$forbiddenAttr);
}
function stripAttr(DOMNode $root, array $forbiddenAttr){
foreach($rootl->childNodes as $child){
foreach($forbiddenAttr as $attr){
if($child->hasAttribute($attr)) $child->removeAttribute($attr);
}.
if($child->hasChildNodes())strippAttr($child,$forbiddenAttr);
}
}
` in some browsers or [JavaScript in style sheets](http://stackoverflow.com/questions/3607894/cross-site-scripting-in-css-stylesheets)) to include JavaScript. Also, there will be new DOM events in the future which you can't possibly handle now. You should really use a whitelist instead.– phihag Aug 26 '11 at 10:15