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)的缩 ...
随机推荐
- Windows 查看某个端口号是否被占用
Ø 前言 在 Windows 下很多系统或服务都需要使用独立的端口号,实现网络数据传输,如果需要知道某个端口号是否被占用,就可以使用下面步骤了. 1. 首先打开命令窗口,Windows + R ...
- getservbyname和getservbyport
一.getservbyname函数原型 #include <netdb.h> struct servent *getservbyname(const char *servname, con ...
- 使用select的str_cli函数的实现
void str_cli(FILE *fp, int sockfd) { int maxfdp1; fd_set rset; char sendline[MAXLINE], recvline[MAXL ...
- ****** 三十 ******、软设笔记【计算机体系结构】-循环冗余校验码(CRC)
循环冗余校验码(CRC) 广泛地在网络通信及磁盘存储时采用. 1.多项式 在循环冗余校验(CRC)码中,无一例外地要提到多项式的概念.一个二进制数可以以一个多项式来表示.如1011表示为多项式X ...
- win10编译caffe调用matlab接口
参考 https://www.cnblogs.com/njust-ycc/p/5776286.html https://www.cnblogs.com/heately/p/7922521.html
- Python 17 web框架&Django
本节内容 1.html里面的正则表达式 2.web样式简介 3.Django创建工程 Html里的正则表达式 test 用来判断字符串是否符合规定的正则 rep.test('....') ...
- docker remote api 的安全隐患
开启docker的api,首先要知道docker的守护进程daemon,在下认为daemon作为Client和service连接的一个桥梁,负责代替将client的请求传递给service端. 默认情 ...
- Django学习手册 - ORM数据类型
DOM 字段/参数 配置格式: Module.字段(参数) 常用的字段归纳: 数字 models.AutoField() 自增列(int),必须设置为主键 models.IntegerField() ...
- 基于html2canvas实现网页保存为图片及图片清晰度优化
一.实现HTML页面保存为图片 1.1 已知可行方案 现有已知能够实现网页保存为图片的方案包括: 方案1:将DOM改写为canvas,然后利用canvas的toDataURL方法实现将DOM输出为包含 ...
- 你必须知道的几种java容器(集合类)
一.基本概念 Java容器类类库的用途是“持有对象”,并将其划分为两个不同的概念: 1)Collection:一个独立元素的序列,这些元素都服从一条或者多条规则. List必须按照插入的顺序保存元素, ...