我需要用一些HTML标签保存一些数据,所以我不能使用strip_tags
所有的文本,我不能使用htmlentities
,因为文本必须由标签修改。为了捍卫其他用户对抗XSS,我必须从标签内部删除任何JavaScript。我怎样才能100%确定HTML标签内的JS?
这样做的最好方法是什么?
我需要用一些HTML标签保存一些数据,所以我不能使用strip_tags
所有的文本,我不能使用htmlentities
,因为文本必须由标签修改。为了捍卫其他用户对抗XSS,我必须从标签内部删除任何JavaScript。我怎样才能100%确定HTML标签内的JS?
这样做的最好方法是什么?
如果您需要保存的HTML标签在数据库中,而后者要打印回浏览器,没有100%使用内置的PHP函数来实现这一点。当没有html标签时,它很容易,当您只有文本时,您可以使用内置的PHP函数来清除文本。
有一些功能可以从文本中清除XSS,但它们不是100%安全的,并且始终有一种XSS未被注意的方法。你的正则表达式的例子很好,但如果我使用让我们说< script>alert('xss')</script>
,或者正则表达式可能会错过并且浏览器会执行的其他组合。
做到这一点,最好的方法是使用类似HTML Purifier
另外请注意,有两种级别的安全,首先是当事情进入你的数据库,二时,他们会从你的数据库。
希望这会有所帮助!
我建议你使用DOMDocument
(与loadHTML
)加载HTML说,除去各种标签和每个属性你不希望看到的,并保存回HTML(使用saveXML
或saveHTML
)。您可以通过递归迭代文档根目录的子项来完成此操作,并用内部内容替换不需要的标记。由于loadHTML
以类似于浏览器的方式加载代码,因此使用它比使用正则表达式更安全。
编辑这里的“净化”功能,我提出:
<?php
function purifyNode($node, $whitelist)
{
$children = array();
// copy childNodes since we're going to iterate over it and modify the collection
foreach ($node->childNodes as $child)
$children[] = $child;
foreach ($children as $child)
{
if ($child->nodeType == XML_ELEMENT_NODE)
{
purifyNode($child, $whitelist);
if (!isset($whitelist[strtolower($child->nodeName)]))
{
while ($child->childNodes->length > 0)
$node->insertBefore($child->firstChild, $child);
$node->removeChild($child);
}
else
{
$attributes = $whitelist[strtolower($child->nodeName)];
// copy attributes since we're going to iterate over it and modify the collection
$childAttributes = array();
foreach ($child->attributes as $attribute)
$childAttributes[] = $attribute;
foreach ($childAttributes as $attribute)
{
if (!isset($attributes[$attribute->name]) || !preg_match($attributes[$attribute->name], $attribute->value))
$child->removeAttribute($attribute->name);
}
}
}
}
}
function purifyHTML($html, $whitelist)
{
$doc = new DOMDocument();
$doc->loadHTML($html);
// make sure <html> doesn't have any attributes
while ($doc->documentElement->hasAttributes())
$doc->documentElement->removeAttributeNode($doc->documentElement->attributes->item(0));
purifyNode($doc->documentElement, $whitelist);
$html = $doc->saveHTML();
$fragmentStart = strpos($html, '<html>') + 6; // 6 is the length of <html>
return substr($html, $fragmentStart, -8); // 8 is the length of </html> + 1
}
?>
你会叫purifyHTML
与不安全的HTML字符串的标记和属性预定义的白名单。白名单格式为'tag'=> array('attribute'=>'regex')。白名单中不存在的标签被剥离,其内容嵌入父标签中。白名单中给定标签不存在的属性也会被删除;以及存在于白名单中但与正则表达式不匹配的属性也会被删除。
下面是一个例子:
<?php
$html = <<<HTML
<p>This is a paragraph.</p>
<p onclick="alert('xss')">This is an evil paragraph.</p>
<p><a href="javascript:evil()">Evil link</a></p>
<p><script>evil()</script></p>
<p>This is an evil image: <img src="error.png" onerror="evil()"/></p>
<p>This is nice <b>bold text</b>.</p>
<p>This is a nice image: <img src="http://example.org/image.png" alt="Nice image"></p>
HTML;
// whitelist format: tag => array(attribute => regex)
$whitelist = array(
'b' => array(),
'i' => array(),
'u' => array(),
'p' => array(),
'img' => array('src' => '@\Ahttp://.+\[email protected]', 'alt' => '@.*@'),
'a' => array('href' => '@\Ahttp://.+\[email protected]')
);
$purified = purifyHTML($html, $whitelist);
echo $purified;
?>
结果是:
<p>This is a paragraph.</p>
<p>This is an evil paragraph.</p>
<p><a>Evil link</a></p>
<p>evil()</p>
<p>This is an evil image: <img></p>
<p>This is nice <b>bold text</b>.</p>
<p>This is a nice image: <img src="http://example.org/image.png" alt="Nice image"></p>
显然,你不想让任何on*
属性,我会建议对style
因为怪异的专有属性如behavior
。确保所有网址属性都使用正确的正则表达式进行验证,与完整字符串(\Aregex\Z
)匹配。
如果您想允许指定标签,您必须解析HTML。
已经有用于该目的的很好的图书馆:HTML Purifier(LGPL下开源)
我写了这个代码,你可以设置标签的列表和属性进行删除
function RemoveTagAttribute($Dom,$Name){
$finder = new DomXPath($Dom);
if(!is_array($Name))$Name=array($Name);
foreach($Name as $Attribute){
$Attribute=strtolower($Attribute);
do{
$tag=$finder->query("//*[@".$Attribute."]");
//print_r($tag);
foreach($tag as $T){
if($T->hasAttribute($Attribute)){
$T->removeAttribute($Attribute);
}
}
}while($tag->length>0);
}
return $Dom;
}
function RemoveTag($Dom,$Name){
if(!is_array($Name))$Name=array($Name);
foreach($Name as $tagName){
$tagName=strtolower($tagName);
do{
$tag=$Dom->getElementsByTagName($tagName);
//print_r($tag);
foreach($tag as $T){
//
$T->parentNode->removeChild($T);
}
}while($tag->length>0);
}
return $Dom;
}
例如:
$dom= new DOMDocument;
$HTML = str_replace("&", "&", $HTML); // disguise &s going IN to loadXML()
// $dom->substituteEntities = true; // collapse &s going OUT to transformToXML()
$dom->recover = TRUE;
@$dom->loadHTML('<?xml encoding="UTF-8">' .$HTML);
// dirty fix
foreach ($dom->childNodes as $item)
if ($item->nodeType == XML_PI_NODE)
$dom->removeChild($item); // remove hack
$dom->encoding = 'UTF-8'; // insert proper
$dom=RemoveTag($dom,"script");
$dom=RemoveTagAttribute($dom,array("onmousedown","onclick"));
echo $dom->saveHTML();
http://stackoverflow.com/questions/1886740/php-remove -javascript – Michal 2013-04-09 16:07:28
如果您正在寻找使用JavaScript进行过滤,则在http://stackoverflow.com/questions/295566/sanitize-rewrite-html-on-the-client-side上提出了类似的问题。 – KernelPanik 2013-04-09 16:10:55