使用kingedit别人是可以输入script代码。这在后台是不允许script代码运行的。

  这里主要使用beatifulSoup过滤

示例1

beatufulsoup4

    from bs4 import Beatifulsoup
soup=Beatifulsoup(content,"html.parse")#html.parse python内置解析器
tag=soup.find("scrip")
tag.hidden=True #把标签隐藏
tag.clear #内容清空 span=soup.find("span")
del span.attr("style") #删除span的style属性 content=soup.decode() #把解析的内容转字符串 #只显示固定标签内容
tags=["p","span"] for tag in soup.find_all():
if tag.name in tags:
pass
else:
tag.hidden=True
tag.clear() #显示固定属性
tags={
"p":["class"],
"span":["id"],
} for tag in soup.find_all():
if tag.name in tags:
pass
else:
tag.hidden=True
tag.clear()
continue #用户提交标签的所有属性
input_attrs=tag.attrs #{"class":"c1","id":"i1"}
valid_attrs=tags[tag.name] # for k in list(input_attrs.keys()):
if k in valid_attrs:
pass
else:
del input_attrs[k]

实例

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup class XSSFilter(object):
__instance = None def __init__(self):
# XSS白名单
self.valid_tags = {
"font": ['color', 'size', 'face', 'style'],
'b': [],
'div': [],
"span": [],
"table": [
'border', 'cellspacing', 'cellpadding'
],
'th': [
'colspan', 'rowspan'
],
'td': [
'colspan', 'rowspan'
],
"a": ['href', 'target', 'name'],
"img": ['src', 'alt', 'title'],
'p': [
'align'
],
"pre": ['class'],
"hr": ['class'],
'strong': []
} def __new__(cls, *args, **kwargs):
"""
单例模式
:param cls:
:param args:
:param kwargs:
:return:
"""
if not cls.__instance:
obj = object.__new__(cls, *args, **kwargs)
cls.__instance = obj
return cls.__instance def process(self, content):
soup = BeautifulSoup(content, 'html.parser')
# 遍历所有HTML标签
for tag in soup.find_all(recursive=True):
# 判断标签名是否在白名单中
if tag.name not in self.valid_tags:
tag.hidden = True
if tag.name not in ['html', 'body']:
tag.hidden = True
tag.clear()
continue
# 当前标签的所有属性白名单
attr_rules = self.valid_tags[tag.name]
keys = list(tag.attrs.keys())
for key in keys:
if key not in attr_rules:
del tag[key] return soup.decode() if __name__ == '__main__':
html = """<p class="title">
<b>The Dormouse's story</b>
</p>
<p class="story">
<div name='root'>
Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister c1" style='color:red;background-color:green;' id="link1"><!-- Elsie --></a>
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tilffffffffffffflie</a>;
and they lived at the bottom of a well.
<script>alert(123)</script>
</div>
</p>
<p class="story">...</p>""" obj = XSSFilter()
v = obj.process(html)
print(v)

39)django-XSS 过滤的更多相关文章

  1. django xss过滤

    django对于xss的过滤有其本身自带的safe等 但是如果通过jsonResponse返回再在前端加载,无法对XSS进行有效的过滤. 因此需自己写一个XSS过滤器,作为装饰器对request的GE ...

  2. python(Django之组合搜索、JSONP、XSS过滤 )

    一.组合搜索 二.jsonp 三.xss过滤 一.组合搜索 首先,我们在做一个门户网站的时候,前端肯定是要进行搜索的,但是如果搜索的类型比较多的话,怎么做才能一目了然的,这样就引出了组合搜索的这个案例 ...

  3. Python开发【Django】:组合搜索、JSONP、XSS过滤

    组合搜索 做博客后台时,需要根据文章的类型做不同的检索 1.简单实现 关联文件: from django.conf.urls import url from . import views urlpat ...

  4. Django XSS攻击

    Django XSS攻击 XSS(cross-site scripting跨域脚本攻击)攻击是最常见的web攻击,其特点是“跨域”和“客户端执行”,XSS攻击分为三种: Reflected XSS(基 ...

  5. xss 过滤

    一. xss过滤 用户通过Form获取展示在终端, 提交数据,Form验证里面加入xss验证(对用户提交的内容验证是否有关键标签) from django.conf.urls import url f ...

  6. KindEditor 和 xss过滤

    KindEditor   1.进入官网 2.下载 官网下载:http://kindeditor.net/down.php 本地下载:http://files.cnblogs.com/files/wup ...

  7. XSS过滤

    XSS过滤封装用法 封装到app01/form.py文件中进行验证 from django.forms import Form,widgets,fields class ArticleForm(For ...

  8. dedecms功能性函数封装(XSS过滤、编码、浏览器XSS hack、字符操作函数)

    dedecms虽然有诸多漏洞,但不可否认确实是一个很不错的内容管理系统(cms),其他也不乏很多功能实用性的函数,以下就部分列举,持续更新,不作过多说明.使用时需部分修改,你懂的 1.XSS过滤. f ...

  9. Asp.net Mvc中利用ValidationAttribute实现xss过滤

    在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...

  10. XSS过滤JAVA过滤器filter 防止常见SQL注入

    Java项目中XSS过滤器的使用方法. 简单介绍: XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩 ...

随机推荐

  1. C#生成Guid,SqlServer生成Guid

    https://www.cnblogs.com/che109/p/6808143.html工作中需要用到全球唯一标识符,在.net当中 微软已经为我们添加了此方法,我们只需要直接调用即可.代码如下: ...

  2. mysql 创建存储过程

    mysql 创建存储过程 实例一: CREATE PROCEDURE cp_test() BEGIN declare a int; declare b int; ; ; select a,b; END ...

  3. Codeforces Round #536 (Div. 2)

    前言 如您所见这又是一篇咕了的文章,直接咕了10天 好久没打CF了 所以还是个蓝名菜鸡 机房所有人都紫名及以上了,wtcl 这次前4题这么水虽然不知道为什么花了1h,结果不知道为什么搞到一半出锅了,后 ...

  4. 第28月第3天 c语言读写文件

    1. int ConfigIniFile::OpenFile( const char* szFileName ) { FILE *fp; size_t nLen; int nRet; CloseFil ...

  5. JDK自带的LinkedHashMap来实现LRU算法

    1 代码如下 public class LRULinkedHashMap<K, V> extends LinkedHashMap<K, V> { private final i ...

  6. 采用shell脚本定时清理Tomcat日志

    1 Shell脚本案例 删除超过30天的日志文件 #!/bin/bash log_path=/mnt/software/apache-tomcat-.M22/logs d=`date +%Y-%m-% ...

  7. 在eclipse中从cvs下载项目,再部署到tomcat常见错误!

    1.先调出cvs视图 如果cvs插件还未安装,下载一个: 安装cvs插件:将features和pluguns文件夹里面的内容分别复制到eclipse安装路径下面对应的features和pluguns文 ...

  8. java时间计算

  9. 【mmall】递归查询子节点并排重

    代码 @Override public ServerResponse getSelfAndChildrenCategory(Integer categoryId) { if (categoryId ! ...

  10. async get_event_loop

    以下论述转载自https://segmentfault.com/q/1010000007863971 答主论述清晰,很不错,可参考他的其他答案 首先,event loop 就是一个普通 Python ...