【发布时间】:2011-11-04 09:40:16
【问题描述】:
我的一个朋友贴了一段关于如何使用 DOM 防止 xss 攻击的代码。
你觉得这段代码怎么样? 我们可以优化它吗?
<?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);
}
}
【问题讨论】:
标签: php dom tags detection xss