39)django-XSS 过滤
使用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 过滤的更多相关文章
- django xss过滤
django对于xss的过滤有其本身自带的safe等 但是如果通过jsonResponse返回再在前端加载,无法对XSS进行有效的过滤. 因此需自己写一个XSS过滤器,作为装饰器对request的GE ...
- python(Django之组合搜索、JSONP、XSS过滤 )
一.组合搜索 二.jsonp 三.xss过滤 一.组合搜索 首先,我们在做一个门户网站的时候,前端肯定是要进行搜索的,但是如果搜索的类型比较多的话,怎么做才能一目了然的,这样就引出了组合搜索的这个案例 ...
- Python开发【Django】:组合搜索、JSONP、XSS过滤
组合搜索 做博客后台时,需要根据文章的类型做不同的检索 1.简单实现 关联文件: from django.conf.urls import url from . import views urlpat ...
- Django XSS攻击
Django XSS攻击 XSS(cross-site scripting跨域脚本攻击)攻击是最常见的web攻击,其特点是“跨域”和“客户端执行”,XSS攻击分为三种: Reflected XSS(基 ...
- xss 过滤
一. xss过滤 用户通过Form获取展示在终端, 提交数据,Form验证里面加入xss验证(对用户提交的内容验证是否有关键标签) from django.conf.urls import url f ...
- KindEditor 和 xss过滤
KindEditor 1.进入官网 2.下载 官网下载:http://kindeditor.net/down.php 本地下载:http://files.cnblogs.com/files/wup ...
- XSS过滤
XSS过滤封装用法 封装到app01/form.py文件中进行验证 from django.forms import Form,widgets,fields class ArticleForm(For ...
- dedecms功能性函数封装(XSS过滤、编码、浏览器XSS hack、字符操作函数)
dedecms虽然有诸多漏洞,但不可否认确实是一个很不错的内容管理系统(cms),其他也不乏很多功能实用性的函数,以下就部分列举,持续更新,不作过多说明.使用时需部分修改,你懂的 1.XSS过滤. f ...
- Asp.net Mvc中利用ValidationAttribute实现xss过滤
在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...
- XSS过滤JAVA过滤器filter 防止常见SQL注入
Java项目中XSS过滤器的使用方法. 简单介绍: XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩 ...
随机推荐
- vertica系列:时间相关函数
-- * 注意: 本文的SQL是在 2017-09-14 测试的. 所以如果取当前日期, 结果为 2017-09-14* ------------------------------ 相关数据类型 - ...
- Android SVN上传项目
方式一: 1 工具栏 VCS ——import into Version Control - Share Project (SubVersion)(注意不要用import into SubVersio ...
- JS定义函数
一.定义函数的方法 (1)函数声明 (2)函数表达式 二.函数声明方法定义函数 function functionName(arg0, arg1, arg2) { // 函数体 } (1)FireFo ...
- Android开发最强模拟器Genymotion的安装及使用教程。附注释图详解
前沿 呵呵,笔者第一次在公开的博客网站写心得,想让自己的Android开发生涯留下点足迹,并且为自己做点笔记,如果该文章能帮到广大的Android小白朋友最好了(其实我也是一小白,(●'◡'●)) ...
- 在SQL注入中利用MySQL隐形的类型转换绕过WAF检测
web应用一般采用基于表单的身份验证方式(页面雏形如下图所示),处理逻辑就是将表单中提交的用户名和密码传递到后台数据库去查询,并根据查询结果判断是否通过身份验证.对于LAMP架构的web应用而言,处理 ...
- 小程序前端防止重复点击请求api的简陋方法
upload: function () { let that = this; let {uploadFlag} = that.data; if (that.data.uploadFlag) { ret ...
- 微信小程序总结
一.基础用法: <navigator url='跳转页面组件'></navigator>用法和a标签差不多 <text></text>文本标签 < ...
- Socket TCP客户端和服务器的实现
import java.io.*; import java.net.Inet4Address; import java.net.InetSocketAddress; import java.net.S ...
- #6284. 数列分块入门 8(区间询问等于一个数 cc 的元素,并将这个区间的所有元素改为 c)
题目链接:https://loj.ac/problem/6284 题目大意:中文题目 具体思路:还是和sqrt那个题的思路相同的,标记每一块的值是不是相同的,注意lazy下标的下放. AC代码: #i ...
- java知识点3
高级篇 新技术 Java 8 lambda表达式.Stream API. Java 9 Jigsaw.Jshell.Reactive Streams Java 10 局部变量类型推断.G1的并行Ful ...