从Python字符串中删除不在允许列表中的HTML标签


72

我有一个包含文本和HTML的字符串。我想删除或以其他方式禁用某些HTML标记,例如<script>,同时允许其他HTML标记,以便我可以安全地在网页上呈现它。我有一个允许标签的列表,如何处理字符串以删除任何其他标签?


5
它也应该删除所有未列入白名单的属性...考虑<img src="heh.png" onload="(function(){/* do bad stuff */}());" />
Dagg Nabbit 2010年

..还有无用的空标签和可能是连续br标签
ducu 2011年

1
请注意,前两个答案很危险,因为从BS / lxml中隐藏XSS非常容易。
fatal_error

Answers:


44

这是一个使用BeautifulSoup的简单解决方案:

from bs4 import BeautifulSoup

VALID_TAGS = ['strong', 'em', 'p', 'ul', 'li', 'br']

def sanitize_html(value):

    soup = BeautifulSoup(value)

    for tag in soup.findAll(True):
        if tag.name not in VALID_TAGS:
            tag.hidden = True

    return soup.renderContents()

如果您也要删除无效标签的内容,请替换为 tag.extract()tag.hidden

您可能还会考虑使用lxmlTidy


谢谢,我不需要这个ATM,但知道以后我需要找到类似的东西。
约翰·法瑞尔

1
import语句应该为from BeautifulSoup import BeautifulSoup
Nikhil Chelliah,2009年

8
您可能还想限制属性的使用。为此,只需将其添加到上面的解决方案中即可:valid_attrs ='href src'.split()for ...:... tag.attrs = [(attr,val)attr,如果attr,则tag.attrs中的val invalid_attrs] hth
Gerald Senarclens de Grancy

9
这不安全!见克里斯·多斯特答案:stackoverflow.com/questions/699468/...
托马斯

这太棒了!不过要安装一件事,要运行BeautifulSoap 4,请运行:easy_install beautifulsoup4然后导入:从bs4导入BeautifulSoup有关详细信息,请参见crummy.com/software/BeautifulSoup/bs4/doc
somecallitblues 2014年

62

使用lxml.html.clean!非常简单!

from lxml.html.clean import clean_html
print clean_html(html)

假设以下html:

html = '''\
<html>
 <head>
   <script type="text/javascript" src="evil-site"></script>
   <link rel="alternate" type="text/rss" src="evil-rss">
   <style>
     body {background-image: url(javascript:do_evil)};
     div {color: expression(evil)};
   </style>
 </head>
 <body onload="evil_function()">
    <!-- I am interpreted for EVIL! -->
   <a href="javascript:evil_function()">a link</a>
   <a href="#" onclick="evil_function()">another link</a>
   <p onclick="evil_function()">a paragraph</p>
   <div style="display: none">secret EVIL!</div>
   <object> of EVIL! </object>
   <iframe src="evil-site"></iframe>
   <form action="evil-site">
     Password: <input type="password" name="password">
   </form>
   <blink>annoying EVIL!</blink>
   <a href="evil-site">spam spam SPAM!</a>
   <image src="evil!">
 </body>
</html>'''

结果...

<html>
  <body>
    <div>
      <style>/* deleted */</style>
      <a href="">a link</a>
      <a href="#">another link</a>
      <p>a paragraph</p>
      <div>secret EVIL!</div>
      of EVIL!
      Password:
      annoying EVIL!
      <a href="evil-site">spam spam SPAM!</a>
      <img src="evil!">
    </div>
  </body>
</html>

您可以自定义要清除的元素,不进行其他操作。


有关lxml.html.clean.clean()方法,请参见文档字符串。它有很多选择!
DenilsonSáMaia,2010年

2
请注意,这使用黑名单方法来过滤掉恶意比特,而不是白名单,但是只有白名单方法才能保证安全。
索伦Løvborg

5
@SørenLøvborg:清洁器还使用支持白名单allow_tags
的Martijn Pieters的

39

通过美丽汤的上述解决方案将不起作用。您可能可以使用“ Beautiful Soup”之上和之外的内容来破解某些东西,因为“ Beautiful Soup”可以访问解析树。有一段时间,我想我会尽力解决问题,但这是一个为期一周的项目,而且我很快就没有空闲的一周。

具体地说,Beautiful Soup不仅会因上述代码无法捕获的某些解析错误而引发异常;而且,还有很多尚未发现的非常真实的XSS漏洞,例如:

<<script>script> alert("Haha, I hacked your page."); </</script>script>

可能最好的办法是改为将<元素剥离为&lt;,以禁止所有HTML,然后使用受限制的子集(如Markdown)正确呈现格式。特别是,您还可以返回并使用正则表达式重新引入HTML的通用位。大致如下所示:

_lt_     = re.compile('<')
_tc_ = '~(lt)~'   # or whatever, so long as markdown doesn't mangle it.     
_ok_ = re.compile(_tc_ + '(/?(?:u|b|i|em|strong|sup|sub|p|br|q|blockquote|code))>', re.I)
_sqrt_ = re.compile(_tc_ + 'sqrt>', re.I)     #just to give an example of extending
_endsqrt_ = re.compile(_tc_ + '/sqrt>', re.I) #html syntax with your own elements.
_tcre_ = re.compile(_tc_)

def sanitize(text):
    text = _lt_.sub(_tc_, text)
    text = markdown(text)
    text = _ok_.sub(r'<\1>', text)
    text = _sqrt_.sub(r'&radic;<span style="text-decoration:overline;">', text)
    text = _endsqrt_.sub(r'</span>', text)
    return _tcre_.sub('&lt;', text)

我尚未测试该代码,因此可能存在错误。但是您会看到一个大致的想法:将所有HTML列入白名单之前,必须将所有HTML列入黑名单。


3
如果您首先尝试这样做:从markdown导入re导入markdown如果您没有markdown,则可以尝试easy_install
Luke Stanley 2010年

25

这是我在自己的项目中使用的东西。accept_elements / attributes来自feedparser,BeautifulSoup完成了工作。

from BeautifulSoup import BeautifulSoup

acceptable_elements = ['a', 'abbr', 'acronym', 'address', 'area', 'b', 'big',
      'blockquote', 'br', 'button', 'caption', 'center', 'cite', 'code', 'col',
      'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em',
      'font', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 
      'ins', 'kbd', 'label', 'legend', 'li', 'map', 'menu', 'ol', 
      'p', 'pre', 'q', 's', 'samp', 'small', 'span', 'strike',
      'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th',
      'thead', 'tr', 'tt', 'u', 'ul', 'var']

acceptable_attributes = ['abbr', 'accept', 'accept-charset', 'accesskey',
  'action', 'align', 'alt', 'axis', 'border', 'cellpadding', 'cellspacing',
  'char', 'charoff', 'charset', 'checked', 'cite', 'clear', 'cols',
  'colspan', 'color', 'compact', 'coords', 'datetime', 'dir', 
  'enctype', 'for', 'headers', 'height', 'href', 'hreflang', 'hspace',
  'id', 'ismap', 'label', 'lang', 'longdesc', 'maxlength', 'method',
  'multiple', 'name', 'nohref', 'noshade', 'nowrap', 'prompt', 
  'rel', 'rev', 'rows', 'rowspan', 'rules', 'scope', 'shape', 'size',
  'span', 'src', 'start', 'summary', 'tabindex', 'target', 'title', 'type',
  'usemap', 'valign', 'value', 'vspace', 'width']

def clean_html( fragment ):
    while True:
        soup = BeautifulSoup( fragment )
        removed = False        
        for tag in soup.findAll(True): # find all tags
            if tag.name not in acceptable_elements:
                tag.extract() # remove the bad ones
                removed = True
            else: # it might have bad attributes
                # a better way to get all attributes?
                for attr in tag._getAttrMap().keys():
                    if attr not in acceptable_attributes:
                        del tag[attr]

        # turn it back to html
        fragment = unicode(soup)

        if removed:
            # we removed tags and tricky can could exploit that!
            # we need to reparse the html until it stops changing
            continue # next round

        return fragment

一些小测试,以确保其行为正确:

tests = [   #text should work
            ('<p>this is text</p>but this too', '<p>this is text</p>but this too'),
            # make sure we cant exploit removal of tags
            ('<<script></script>script> alert("Haha, I hacked your page."); <<script></script>/script>', ''),
            # try the same trick with attributes, gives an Exception
            ('<div on<script></script>load="alert("Haha, I hacked your page.");">1</div>',  Exception),
             # no tags should be skipped
            ('<script>bad</script><script>bad</script><script>bad</script>', ''),
            # leave valid tags but remove bad attributes
            ('<a href="good" onload="bad" onclick="bad" alt="good">1</div>', '<a href="good" alt="good">1</a>'),
]

for text, out in tests:
    try:
        res = clean_html(text)
        assert res == out, "%s => %s != %s" % (text, res, out)
    except out, e:
        assert isinstance(e, out), "Wrong exception %r" % e

3
这不安全!见克里斯·多斯特答案:stackoverflow.com/questions/699468/...
托马斯

1
@Thomas:您有什么证据可以支持该主张吗?Chris Dost的“不安全”代码实际上只是引发了一个Exception,所以我想您实际上并没有尝试过。
Jochen Ritzel 2010年

2
@ THC4k:对不起,我忘了提到我必须修改示例。这是一个<<script></script>script> alert("Haha, I hacked your page."); <<script></script>script>
Thomas

另外,tag.extract()修改我们要遍历的列表。这会使循环感到困惑,并导致其跳过下一个孩子。
托马斯2010年

@Thomas:真的很棒!我想我已经解决了两个问题,非常感谢!
Jochen Ritzel 2010年

23

使用更多有用的选项,Bleeach会做得更好。它基于html5lib构建,可以投入生产。检查该bleack.clean功能的文档。其默认配置会转义不安全的标签(例如),<script>而允许使用的有用标签(例如)<a>

import bleach
bleach.clean("<script>evil</script> <a href='http://example.com'>example</a>")
# '&lt;script&gt;evil&lt;/script&gt; <a href="http://example.com">example</a>'

漂白程序是否仍允许数据:默认情况下通过html5lib的url?例如,可以嵌入data:内容类型为html的url。
Antti Haapala'8

2019年,为此苦苦挣扎:stackoverflow.com/questions/7538600/…-对我来说,lxml.html.cleaner更加牢固,完全删除了样式标签,而漂白让您将CSS作为内容可见。
benzkji

11

用BeautifulSoup修改了Bryan解决方案,以解决Chris Drost提出问题。有点粗糙,但能做到:

from BeautifulSoup import BeautifulSoup, Comment

VALID_TAGS = {'strong': [],
              'em': [],
              'p': [],
              'ol': [],
              'ul': [],
              'li': [],
              'br': [],
              'a': ['href', 'title']
              }

def sanitize_html(value, valid_tags=VALID_TAGS):
    soup = BeautifulSoup(value)
    comments = soup.findAll(text=lambda text:isinstance(text, Comment))
    [comment.extract() for comment in comments]
    # Some markup can be crafted to slip through BeautifulSoup's parser, so
    # we run this repeatedly until it generates the same output twice.
    newoutput = soup.renderContents()
    while 1:
        oldoutput = newoutput
        soup = BeautifulSoup(newoutput)
        for tag in soup.findAll(True):
            if tag.name not in valid_tags:
                tag.hidden = True
            else:
                tag.attrs = [(attr, value) for attr, value in tag.attrs if attr in valid_tags[tag.name]]
        newoutput = soup.renderContents()
        if oldoutput == newoutput:
            break
    return newoutput

编辑:更新为支持有效属性。


tag.attrs = [(attr, value) for attr, value in tag.attrs if attr in valid_tags[tag.name]]- tag.attrs是一个字典,所以这应该是 tag.attrs = {attr: value for attr, value in tag.attrs.items() if attr in valid_tags[tag.name]}使用BS4
凌晨

3

我使用FilterHTML。它很简单,可让您定义一个控制良好的白名单,清理URL甚至将属性值与regex匹配,或对每个属性具有自定义过滤功能。如果仔细使用,可能是安全的解决方案。这是自述文件中的简化示例:

import FilterHTML

# only allow:
#   <a> tags with valid href URLs
#   <img> tags with valid src URLs and measurements
whitelist = {
  'a': {
    'href': 'url',
    'target': [
      '_blank',
      '_self'
    ],
    'class': [
      'button'
    ]
  },
  'img': {
    'src': 'url',
    'width': 'measurement',
    'height': 'measurement'
  },
}

filtered_html = FilterHTML.filter_html(unfiltered_html, whitelist)

2

您可以使用html5lib,它使用白名单进行清理。

一个例子:

import html5lib
from html5lib import sanitizer, treebuilders, treewalkers, serializer

def clean_html(buf):
    """Cleans HTML of dangerous tags and content."""
    buf = buf.strip()
    if not buf:
        return buf

    p = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("dom"),
            tokenizer=sanitizer.HTMLSanitizer)
    dom_tree = p.parseFragment(buf)

    walker = treewalkers.getTreeWalker("dom")
    stream = walker(dom_tree)

    s = serializer.htmlserializer.HTMLSerializer(
            omit_optional_tags=False,
            quote_attr_values=True)
    return s.render(stream) 

为什么sanitizer_factory存在?您应该HTMLSanitizer直接通过。
克里斯·摩根

@ChrisMorgan好问题。我想我是从html5lib网站上获得此示例的,他们在退还之前对工厂的消毒剂做了一些操作。但是他们所做的只是在开发版本中,而在发行版本中却无效。因此,我刚刚删除了该行。这里看起来确实很奇怪。我将对其进行研究,并可能会更新答案。
布莱恩·尼尔

@ChrisMorgan看来我所指的功能(将令牌剥离而不是转义)从未在上游实现,因此我只是删除了工厂业务。谢谢。
布莱恩·尼尔

1

我更喜欢lxml.html.clean解决方案,就像nosklo 指出的那样。这里也要删除一些空标签:

from lxml import etree
from lxml.html import clean, fromstring, tostring

remove_attrs = ['class']
remove_tags = ['table', 'tr', 'td']
nonempty_tags = ['a', 'p', 'span', 'div']

cleaner = clean.Cleaner(remove_tags=remove_tags)

def squeaky_clean(html):
    clean_html = cleaner.clean_html(html)
    # now remove the useless empty tags
    root = fromstring(clean_html)
    context = etree.iterwalk(root) # just the end tag event
    for action, elem in context:
        clean_text = elem.text and elem.text.strip(' \t\r\n')
        if elem.tag in nonempty_tags and \
        not (len(elem) or clean_text): # no children nor text
            elem.getparent().remove(elem)
            continue
        elem.text = clean_text # if you want
        # and if you also wanna remove some attrs:
        for badattr in remove_attrs:
            if elem.attrib.has_key(badattr):
                del elem.attrib[badattr]
    return tostring(root)

最好使用“ return _transform_result(type(clean_html),root)”而不是“ return tostring(root)”。它将处理类型检查。
luckyjazzbo 2011年

@luckyjazzbo:是的,但是我将使用以下划线开头的方法。这些是私有实现的细节,不应使用,因为它们可能会在将来的lxml版本中更改。
nosklo 2011年

显然正确:_transform_result在lxml中今天不存在(不再存在)。
西蒙·斯坦伯格
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.