from:http://stackoverflow.com/questions/699468/python-html-sanitizer-scrubber-filter

通过下面这个代码就可以把内容过滤成干净的HTML内容,说明,这个代码来自上面Stackoverflow的回答

Use lxml.html.clean! It's VERY easy!

from lxml.html.clean import clean_html
print clean_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>

You can customize the elements you want to clean and whatnot.

关于Web开发中的安全过滤问题,摘引一下OWASP的ESAPI( http://owasp-esapi-java.googlecode.com/svn/trunk_doc/latest/index.html )的过滤方法接口列表,以供大家有所参考:

Java.lang.String canonicalize(java.lang.String input) 
          This method is equivalent to calling
 java.lang.String canonicalize(java.lang.String input, boolean strict) 
          This method is the equivalent to calling
 java.lang.String canonicalize(java.lang.String input, boolean restrictMultiple, boolean restrictMixed) 
          Canonicalization is simply the operation of reducing a possibly encoded string down to its simplest form.
 java.lang.String decodeForHTML(java.lang.String input) 
          Decodes HTML entities.
 byte[] decodeFromBase64(java.lang.String input) 
          Decode data encoded with BASE-64 encoding.
 java.lang.String decodeFromURL(java.lang.String input) 
          Decode from URL.
 java.lang.String encodeForBase64(byte[] input, boolean wrap) 
          Encode for Base64.
 java.lang.String encodeForCSS(java.lang.String input) 
          Encode data for use in Cascading Style Sheets (CSS) content.
 java.lang.String encodeForDN(java.lang.String input) 
          Encode data for use in an LDAP distinguished name.
 java.lang.String encodeForHTML(java.lang.String input) 
          Encode data for use in HTML using HTML entity encoding
 java.lang.String encodeForHTMLAttribute(java.lang.String input) 
          Encode data for use in HTML attributes.
 java.lang.String encodeForJavaScript(java.lang.String input) 
          Encode data for insertion inside a data value or function argument in JavaScript.
 java.lang.String encodeForLDAP(java.lang.String input) 
          Encode data for use in LDAP queries.
 java.lang.String encodeForOS(Codec codec, java.lang.String input) 
          Encode for an operating system command shell according to the selected codec (appropriate codecs include the WindowsCodec and UnixCodec).
 java.lang.String encodeForSQL(Codec codec, java.lang.String input) 
          Encode input for use in a SQL query, according to the selected codec (appropriate codecs include the MySQLCodec and OracleCodec).
 java.lang.String encodeForURL(java.lang.String input) 
          Encode for use in a URL.
 java.lang.String encodeForVBScript(java.lang.String input) 
          Encode data for insertion inside a data value in a Visual Basic script.
 java.lang.String encodeForXML(java.lang.String input) 
          Encode data for use in an XML element.
 java.lang.String encodeForXMLAttribute(java.lang.String input) 
          Encode data for use in an XML attribute.
 java.lang.String encodeForXPath(java.lang.String input) 
          Encode data for use in an XPath query.

Python Django开发中XSS内容过滤问题的解决的更多相关文章

  1. Python Web开发中的WSGI协议简介

    在Python Web开发中,我们一般使用Flask.Django等web框架来开发应用程序,生产环境中将应用部署到Apache.Nginx等web服务器时,还需要uWSGI或者Gunicorn.一个 ...

  2. django开发中利用 缓存文件 进行页面缓存

    首先我们先来了解下浏览器的缓存 浏览器缓存机制 Cache-control策略 Cache-Control与Expires的作用一致,都是指明当前资源的有效期,控制浏览器是否直接从浏览器缓存取数据还是 ...

  3. [更新中]【South使用总结】django开发中使用South进行数据库迁移

    Django开发中使用South进行数据库迁移的使用总结 South的详细资料可产看官方文档http://south.readthedocs.org/en/latest South安装配置 pip i ...

  4. django开发中关于外键设置

    django开发中关于外键设置 我们建模型的时候会用到ForeignKey 而由于外键的约数会导致一些保存 所有我们ctrl+左键进入源码 源码 def __init__(self, to, on_d ...

  5. 记一次SpringBoot 开发中所遇到的坑和解决方法

    记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...

  6. 使用 Visual Studio Code(VSCode)搭建简单的 Python + Django 开发环境

    写在前面的话 作为有个 Python 菜逼,之前一直用的 Pycharm,但是在主题这一块怎么调整都感觉要么太骚,看起来不舒服,要么就是简直不能看.似乎用大 JB 公司 IDE 的人似乎都不怎么重视主 ...

  7. python 修改文件中的内容

    在python的文件操作中,是没有办法对文件中具体某行或者某个位置的内容进行局部的修改的,如果需要对文件的某一行内容进行修改,可以先将文件中的所有的内容全部读取出来,再进行内容判断,是否是需要修改的内 ...

  8. Python Django开发遇到的坑(版本不匹配)

    这个问题 进入django 后台, 添加,修改都不可以,只有删除可以,那么百分之百是这个问题 对照一下,是你的django 版本低了还是 python版本高了,对照的话就没问题了 这个坑,弄了两天啊! ...

  9. python django 更改模型字段出错时的一个解决办法

    python/django 框架自带的 orm 无疑是django框架最拿得出手的一个亮点,orm无疑极大的方便了项目的开发,提高了开发的效率. 在实际的项目开发过程中,我们有时候需要修改模型的字段, ...

随机推荐

  1. 常用js总结

    通过radio来显示/隐藏一个div <div id="ArbAcei" > <br/> 有无不良反应停药 <input type="rad ...

  2. matlab函数interp2及其c++代码

    最近将一个matlab程序转为c++,途中遇到interp2这个家伙,我是左查右查,发现网上没有人总结这个玩意,于是我来初探一下,还是别有洞天的,嘿嘿. 1.关于interp2 Vq = interp ...

  3. Android自制浏览器WebView-android学习之旅(64)

    简单讲解如何使用WebView加载百度的网页 acticity代码 public class MainActivity extends Activity { private WebView webVi ...

  4. 【一天一道LeetCode】#99. Recover Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Two ele ...

  5. C++对象模型(一):The Semantics of Constructors The Default Constructor (默认构造函数什么时候会被创建出来)

    本文是 Inside The C++ Object Model, Chapter 2的部分读书笔记. C++ Annotated Reference Manual中明确告诉我们: default co ...

  6. div+css基础教程

    本文存下来作为备忘. 第一节  了解div+css 一.什么是div+css div元素是html(超文本语言)中的一个元素,是标签,用来为html文档内大块(block-level)的内容提供结构和 ...

  7. SpriteBuilder弹出菜单层造成卡顿的解决办法

    如果你注意到略微的卡顿(延时)(lag due to low framerate)当显示一个全屏的弹出菜单时,你可以简单的设置_levelNode(_levelNode是主游戏界面中的背景层,所有游戏 ...

  8. Spring--ClassPathXmlApplicationContext

    public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext { private Resource ...

  9. 介绍一种很棒的wince 如何替换系统声音的方法

    Topic:介绍一种很棒的wince 如何替换系统声音的方法(作者:Baiduluckyboy) //------------------------------------------------- ...

  10. java--加强之 Java5的线程并发库

    转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9945499 01. 传统线程技术回顾 创建线程的两种传统方式: 1.在Thread子类覆盖的r ...