Xss过滤

在表单填写的过程中我们就用到textarea,富文本编辑框,里面要用户输入相关的内容。如果有的人想要搞怪,在里面写一些js代码或者修改编辑的时候修改源代码,那提交上去之后就会使得页面显示不正确。这个时候我们就应该要在提交到数据库的时候进行过滤。把js等代码过滤掉或者转义。

python中有一个模块:beautifulsoup4

使用:

content='''
<h1>Xss过滤</h1>
<p><span style="width:100px;" name="haha">在表单填写的过程中我们就用到textarea,富文本编辑框,</span></p>
<p>
<strong>python中有一个模块:</strong>
<script>alert(111)</scripy>
</p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(content,"html.parser")
tag = soup.find("script")#找到要找的标签及内容
tag.hidden=True#隐藏标签
tag.clear()#清楚标签中的内容
span = soup.find('span') del span.attrs['style']#获得span里面的style标签 content = soup.decode()
print(content)

使用:

  • 首先引入模块from bs4 import BeautifulSoup
  • 创建对象,把要过滤的内容和解析器传入,python中有一个内置的解析器html.parser
  • 通过soup.find查找到相应的标签内容
  • 如果要删除标签就使用:tag.hidden=True
  • 如果想要把标签清除:tag.clear
  • 如果想要查找标签中的属性:span.attrs  ,获得一个字典,如:{'style': 'width:100px;', 'name': 'haha'}
  • 如果想要删除标签中某一个属性:del span.attrs['属性名']
  • 把对象转换成字符串形式:soup.decode()

上面是找到特殊标签,然后把特殊标签删除,那么我们能不能设置一个白名单,只允许名单内的标签通过,其他的都要过滤?

content='''
<h1>Xss过滤</h1>
<p><span style="width:100px;" name="haha">在表单填写的过程中我们就用到textarea,富文本编辑框,</span></p>
<p>
<strong>python中有一个模块:</strong>
<script>alert(111)</scripy>
</p>
'''
tags = ['p','strong']
from bs4 import BeautifulSoup
soup = BeautifulSoup(content,"html.parser")
for tag in soup.find_all():#tag.name就只标签的名字
if tag.name in tags:
continue
else:
tag.hidden = True
tag.clear()
content = soup.decode()
print(content)

这样我们得到的结果就是除了p标签和strong标签,都没有了

如果要还想要删除p标签中的class选择器,和strong中的id选择器:

 content='''
<h1>Xss过滤</h1>
<p class="c1" id="i1"><span style="width:100px;" name="haha">在表单填写的过程中我们就用到textarea,富文本编辑框,</span></p>
<p>
<strong class="c2" id="i2">python中有一个模块:</strong>
<script>alert(111)</scripy>
</p>
'''
# tags = ['p','strong']
tags = {
'p':{'class'},
'strong':{'id'}
}
from bs4 import BeautifulSoup
soup = BeautifulSoup(content,"html.parser")
for tag in soup.find_all():#tag.name就只标签的名字
if tag.name in tags:
input_attrs = tag.attrs#用户提交的标签的所有属性{'class':'c1','id':'i1'}
valid_attrs = tags[tag.name]#class
for k in list(input_attrs.keys()):#把字典变成list类型删除,否则直接在生成器中删除会报错
if k in valid_attrs:
continue
else:
del tag.attrs[k]
else:
tag.hidden = True
tag.clear()
content = soup.decode()
print(content)

这样就把id选择器和class选择器删除了,实现了标签级别和属性级别的同时过滤

之后我们要在用的话就可以把这个封装成一个类进行掉用那个

单例模式

永远用一个对象实例:

先看一个普通的单例模式:

class Foo(object):
instance=None
def __init__(self):
pass
@classmethod
def get_instance(cls):
if Foo.instance:
return Foo.instance
else:
Foo.instance=Foo()
return Foo.instance
def process(self):
return '' obj1 = Foo()
obj2 = Foo()
print(id(obj1),id(obj2))
obj1 = Foo.get_instance()
obj2 = Foo.get_instance()
print(id(obj1),id(obj2))

第二种方法,用__new__()实现:

class Foo(object):
instance=None
def __init__(self):
pass
def __new__(cls, *args, **kwargs):
if Foo.instance:
return Foo.instance
else:
Foo.instance = object.__new__(cls,*args,**kwargs)
return Foo.instance obj1 = Foo()
obj2 = Foo()
print(obj1,obj2)

Django---Xss过滤以及单例模式的更多相关文章

  1. django xss过滤

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

  2. xss过滤与单例模式(对象的实例永远用一个)

    kindeditor里面可以加入script代码,使用re可以过滤掉python有个专门的模块可以处理这种情况,beautifulsoup4 调用代码: content = XSSFilter().p ...

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

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

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

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

  5. KindEditor 和 xss过滤

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

  6. Django XSS攻击

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

  7. xss 过滤

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

  8. XSS过滤

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

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

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

随机推荐

  1. 优化--前端(全占课,未完成作业:);CDN; Http/2的设置(未完成)

    前端效能: 关键渲染路径:Google 文档 JavaScript 加载最佳化 让html和javascript同时渲染: 设置<script>的async或者defer属性(boolea ...

  2. Confluence 6 创建一个用户宏

    如果你想创建自定义的宏的话,用户宏能够帮你完成这个任务.这个可以在你系统中应用特定的操作,比如说应用自定义格式等. 用户用是在 Confluence 创建和和管理的,你需要有一定的编码基础才可以. 你 ...

  3. Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

    Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...

  4. NYOJ 720 DP+二分

    项目安排 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个 ...

  5. 升级OPENSSH 和 OPENSSL

    升级OPENSSH 和 OPENSSL   首先安装telnet服务,防止在操作过程中导致ssh远程中断   # 安装Telnetyum install telnet-server -y chkcon ...

  6. get方法传送中文乱码解决方法

    找到tomcat配置文件 server.xml 找到<Connector port="8080" .......  />   (......为配置文件中原来内容) 在最 ...

  7. 仿智能社官网:原生JS实现简单又酷炫的3D立方体时钟

    先放一下我做的效果:https://linrunzheng.github.io/3Dclock/3Dclock/new.html 至于3D立方体怎么做这里就不在阐述了,可以看一下我之前的博客. 这里默 ...

  8. 6-20 No Less Than X in BST(20 分)

    You are supposed to output, in decreasing order, all the elements no less than X in a binary search ...

  9. Java编程之Map中分拣思想。

    题目:给定一个字符串,求出字符串中每一个单词在字符串中出现的次数 旨意:map的分拣思想. 每一个key的包装类,存放出现的次数 /** * 作为包装类,用来存放英文单词,和该英文单词出现的次数 * ...

  10. ringojs 的包管理

    ringojs 集成了包管理目前有几种方式 ringo-admin rp ringo-admin 安装包 我们使用ringo-admin 安装rp ringo-admin install grob/r ...