如何在 Google App Engine 中清理 python 中的 html blob 上传?

How to sanitize html blob uploads in python in Google App Engine?

如果 HTML 被上传到 Google App Engine 以供服务于 iframe,最好的清理方法是什么(例如删除脚本和其他恶意软件 html) ?

我在想

        reader = blobstore.BlobReader(binfo.key())
        value = reader.read()
        newHtml = lxml.html.clean.Cleaner().clean_html (value)
        #save newHtml as the blob in google cloud store to be served.

我想知道是否有更好的方法来做到这一点,非常感谢您的帮助。

注意:如果有一种方法可以删除锚点或使锚点(或源)成为非相对安全的 http 等(或完全不允许),那也很棒。

这可能是一个很难解决的问题,因为 HTML 可以通过多种方式注入恶意内容。 script 元素可能是最多的 well-known/common,但 forminputimg 元素也可能被滥用。恶意 JS 还可以通过事件处理程序包含在几乎任何元素中。 More information about XSS from OWASP,让你开始。

由于您正在使用 Python,这里有一些用于清理 HTML 的库可能对您有用。

就其价值而言,在 iframe 中托管用户提供的 HTML 是防止恶意内容访问您托管的应用程序的一种方法(尽管您可能想要采取措施防止 iframe 突破),但您可能希望向您的用户表明,那里托管的内容并不总是可信的。

希望对您有所帮助!