4

我想允许很多用户为用户配置文件提交 html,我目前尝试过滤掉我不想要的内容,但我现在想要更改并使用白名单方法。

这是我目前的非白名单方法

function FilterHTML($string) {
    if (get_magic_quotes_gpc()) {
        $string = stripslashes($string);
    }
    $string = html_entity_decode($string, ENT_QUOTES, "ISO-8859-1");
    // convert decimal
    $string = preg_replace('/&#(\d+)/me', "chr(\\1)", $string); // decimal notation
    // convert hex
    $string = preg_replace('/&#x([a-f0-9]+)/mei', "chr(0x\\1)", $string); // hex notation
    //$string = html_entity_decode($string, ENT_COMPAT, "UTF-8");
    $string = preg_replace('#(&\#*\w+)[\x00-\x20]+;#U', "$1;", $string);
    $string = preg_replace('#(<[^>]+[\s\r\n\"\'])(on|xmlns)[^>]*>#iU', "$1>", $string);
    //$string = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $string); //bad line
    $string = preg_replace('#/*\*()[^>]*\*/#i', "", $string); // REMOVE /**/
    $string = preg_replace('#([a-z]*)[\x00-\x20]*([\`\'\"]*)[\\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU', '...', $string); //JAVASCRIPT
    $string = preg_replace('#([a-z]*)([\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU', '...', $string); //VBSCRIPT
    $string = preg_replace('#([a-z]*)[\x00-\x20]*([\\\]*)[\\x00-\x20]*@([\\\]*)[\x00-\x20]*i([\\\]*)[\x00-\x20]*m([\\\]*)[\x00-\x20]*p([\\\]*)[\x00-\x20]*o([\\\]*)[\x00-\x20]*r([\\\]*)[\x00-\x20]*t#iU', '...', $string); //@IMPORT
    $string = preg_replace('#([a-z]*)[\x00-\x20]*e[\x00-\x20]*x[\x00-\x20]*p[\x00-\x20]*r[\x00-\x20]*e[\x00-\x20]*s[\x00-\x20]*s[\x00-\x20]*i[\x00-\x20]*o[\x00-\x20]*n#iU', '...', $string); //EXPRESSION
    $string = preg_replace('#</*\w+:\w[^>]*>#i', "", $string);
    $string = preg_replace('#</?t(able|r|d)(\s[^>]*)?>#i', '', $string); // strip out tables
    $string = preg_replace('/(potspace|pot space|rateuser|marquee)/i', '...', $string); // filter some words
    //$string = str_replace('left:0px; top: 0px;','',$string);
    do {
        $oldstring = $string;
        //bgsound|
        $string = preg_replace('#</*(applet|meta|xml|blink|link|script|iframe|frame|frameset|ilayer|layer|title|base|body|xml|AllowScriptAccess|big)[^>]*>#i', "...", $string);
    } while ($oldstring != $string);
    return addslashes($string);
}

上面的效果很好,使用它 2 年后我从来没有遇到过任何问题,但是对于白名单方法,是否有任何类似于 stackoverflows C# 方法但在 PHP 中的方法? http://refactormycode.com/codes/333-sanitize-html

4

7 回答 7

14

HTML Purifier是一个用 PHP 编写的符合标准的 HTML 过滤器库。HTML Purifier 不仅会使用经过全面审核、安全且允许的白名单删除所有恶意代码(更广为人知的 XSS),它还将确保您的文档符合标准,这只有在全面了解 W3C 规范的情况下才能实现。

于 2009-09-04T01:52:22.693 回答
8

也许使用DOMDocument正确分析它更安全,使用 removeChild() 删除不允许的标签,然后获取结果。用正则表达式过滤东西并不总是安全的,特别是当事情开始变得如此复杂时。黑客可以找到一种方法来欺骗您的过滤器,论坛和社交网络确实非常清楚这一点。

例如,浏览器会忽略 < 之后的空格。您的正则表达式过滤器 <script,但如果我使用 < script... 大失败!

于 2009-09-04T01:47:16.343 回答
3

HTML Purifier是目前最好的 HTML 解析器/清理器。

于 2009-09-04T01:49:55.220 回答
1

对于那些建议仅使用 strip_tags 的人...请注意:strip_tags不会去除标签属性,并且损坏的标签也会弄乱它。

从手册页:

警告因为 strip_tags() 实际上并不验证 HTML,部分或损坏的标签可能会导致删除比预期更多的文本/数据。

Warning This function does not modify any attributes on the tags that you allow using allowable_tags , including the style and onmouseover attributes that a mischievous user may abuse when posting text that will be shown to other users.

You CANNOT rely on just this one solution.

于 2009-09-04T16:27:28.690 回答
0

您可以只使用strip_tags () 函数

由于函数定义为

string strip_tags  ( string $str  [, string $allowable_tags  ] )

你可以这样做:

$html = $_POST['content'];
$html = strip_tags($html, '<b><a><i><u><span>');

但请注意,使用 strip_tags,您将无法过滤掉属性。例如

<a href="javascript:alert('haha caught cha!');">link</a>
于 2009-09-04T03:25:36.933 回答
0

试试下面这个函数“getCleanHTML”,从除白名单中带有标签名称的元素之外的元素中提取文本内容。这段代码干净,易于理解和调试。

<?php

$TagWhiteList = array(
    'b', 'i', 'u', 'strong', 'em', 'a', 'img'
);

function getHTMLCode($Node) {
    $Document = new DOMDocument();    
    $Document->appendChild($Document->importNode($Node, true));
    return $Document->saveHTML();
}
function getCleanHTML($Node, $Text = "") {
    global $TagWhiteList;

    $TextName = $Node->tagName;
    if ($TextName == null)
        return $Text.$Node->textContent;

    if (in_array($TextName, $TagWhiteList)) 
        return $Text.getHTMLCode($Node);

    $Node = $Node->firstChild;
    if ($Node != null)
        $Text = getCleanHTML($Node, $Text);

    while($Node->nextSibling != null) {
        $Text = getCleanHTML($Node->nextSibling, $Text);
        $Node = $Node->nextSibling;
    }
    return $Text;
}

$Doc = new DOMDocument();
$Doc->loadHTMLFile("Test.html");
echo getCleanHTML($Doc->documentElement)."\n";

?>

希望这可以帮助。

于 2009-09-04T03:38:32.113 回答
-1

实际上,这是一个非常简单的目标 - 您只需要从白名单标签列表中检查任何不是某些标签的内容,然后将它们从源中删除。使用一个正则表达式可以很容易地完成。

function sanitize($html) {
  $whitelist = array(
    'b', 'i', 'u', 'strong', 'em', 'a'
  );

  return preg_replace("/<(^".implode("|", $whitelist).")(.*)>(.*)<\/(^".implode("|", $whitelist).")>/", "", $html);
}

我还没有对此进行测试,并且可能在某个地方存在错误,但是您会了解它的工作原理。您可能还想查看使用诸如 Textile 或 Markdown 之类的格式化语言。

杰米

于 2009-09-04T01:48:36.033 回答