一. xss过滤

用户通过Form获取展示在终端, 提交数据,Form验证里面加入xss验证(对用户提交的内容验证是否有关键标签)
from django.conf.urls import url
from django.contrib import admin
from app01 import views urlpatterns = [ #写文章
url(r'^test/', views.test),
#查看写的文章
url(r'^see/', views.see), ]

urls.py

from django.shortcuts import render,HttpResponse
from app01 import models from app01.forms import ArticleForm CONIENT = "" def test(request):
if request.method == "GET":
obj = ArticleForm()
return render(request,"test.html",{"obj":obj})
else:
obj = ArticleForm(request.POST)
if obj.is_valid():
content = obj.cleaned_data["content"]
global CONIENT
CONIENT = content
print(content)
return HttpResponse("...") def see(request):
return render(request,"see.html",{"con":CONIENT})

views.py

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="POST" action="/test/" novalidate>
{% csrf_token %} <p>
文章标题
{{ obj.title }}
</p> <div>
<div>文章内容</div>
<div>
{{ obj.content|safe }}
</div>
</div>
<input type="submit" value="提交">
</form>
<script src="/static/css/kindeditor-4.1.10/kindeditor-all.js"></script> <script>
KindEditor.create("#id1",{
width:"200px",
height:"300px",
{# //items:['source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',#}
// 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
// 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
// 'superscript', 'clearhtml', 'quickformat'],
//noDisableItems:['source', '|', 'undo'], //保留某些item
//designMode:false //其它注释 //resizeType 改变窗口大小
uploadJson:"/upload_img.html", //上传文件
extraFileUploadParams:{ //上传文件时携带token
"csrfmiddlewaretoken":"{{ csrf_token }}"
}
})
</script> </body>
</html>

test.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ con | safe }}
</body>
</html>

see.html

from django import forms
from django.forms import fields,Form
from django.forms import widgets class ArticleForm(Form):
title = fields.CharField(max_length=64)
content = fields.CharField(
widget=widgets.Textarea(attrs={"id":"id1"})
) def clean_content(self): valid_tag = {
"p": ["class", "id"],
"img": ["src"],
"div": ["class"],
} from bs4 import BeautifulSoup
old = self.cleaned_data["content"]
soup = BeautifulSoup(old, "html.parser") tags = soup.find_all()
for tag in tags:
if tag.name not in valid_tag:
tag.decompose()
if tag.attrs:
# print(tag.attrs) #获取所有标签的属性
for k in list(tag.attrs.keys()):
if k not in valid_tag[tag.name]:
del tag.attrs[k]
content_str = soup.decode() return content_str

forms.py

二.以后用法

#根据上面修改
from django import forms
from django.forms import fields,Form
from django.forms import widgets class ArticleForm(Form):
title = fields.CharField(max_length=64)
content = fields.CharField(
widget=widgets.Textarea(attrs={"id":"id1"})
) def clean_content(self):
old = self.cleaned_data["content"]
from utils.xss import xss return xss(old)

forms.py

from bs4 import BeautifulSoup

def xss(old):

    valid_tag = {
"p": ["class", "id"],
"img": ["src"],
"div": ["class"],
} soup = BeautifulSoup(old, "html.parser") tags = soup.find_all()
for tag in tags:
if tag.name not in valid_tag:
tag.decompose()
if tag.attrs:
# print(tag.attrs) #获取所有标签的属性
for k in list(tag.attrs.keys()):
if k not in valid_tag[tag.name]:
del tag.attrs[k]
content_str = soup.decode() return content_str

utils/xss.py

  

xss 过滤的更多相关文章

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

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

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

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

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

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

  4. 如何在springboot项目中进行XSS过滤

    简单介绍 XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意 ...

  5. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

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

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

  7. 04: 使用BeautifulSoup封装的xss过滤模块

    目录: 1.1 xss攻击简介 1.2 xss攻击解决方法 1.1 xss攻击简介返回顶部 1.简介 1. 跨站脚本(cross site script)为了避免与样式css混淆,所以简称为XSS. ...

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

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

  9. Bypass xss过滤的测试方法

    0x00 背景 本文来自于<Modern Web Application Firewalls Fingerprinting and Bypassing XSS Filters>其中的byp ...

随机推荐

  1. Docker学习笔记二:Docker常用命令及提升拉取镜像的速度

    一.Docker命令: 1.docker images   //命令用来查看docker中所包含的镜像信息 2.docker ps -a    //命令用来查看docker中所包含所有容器信息(运行状 ...

  2. WildFly8(JBoss)默认web服务器-------Undertow

    Java微服务框架之Undertow 一.Undertow简介: Undertow 是红帽公司(RedHat)的开源产品,是 WildFly8(JBoos) 默认的 Web 服务器. 官网API给出一 ...

  3. 【WPF】PopupColorEdit 的使用

    一.前言        PopupColorEdit 是 dev中一个常用的调色盘控件,它的Color属性返回的是一个System.Windows.Media.Color对象,而不是System.Dr ...

  4. mysql权限管理,用户管理

    1 创建用户 mysql> truncate table user; //先删除所有用户 mysql> CREATE USER  'paris'@'localhost' IDENTIFIE ...

  5. 【CF375C】Circling Round Treasures

    Portal --> CF375C Solution 一个有趣的事情:题目中有很大的篇幅在介绍如何判断一个位置在不在所围的多边形中 那么..给了方法当然就是要用啊 ​ 首先是不能包含\('B'\ ...

  6. 【arc068E】Snuke Line

    Portal -->arc068E (温馨提示:那啥..因为各种奇怪的我也不知道的原因这题的题号在某度上面显示出来是agc007F...然而下面是arc068E的题解qwq给大家带来不便之处真是 ...

  7. K8S Link

    https://www.cnblogs.com/linuxk/p/9783510.html https://www.cnblogs.com/fengzhihai/p/9851470.html

  8. libc、glibc与gcc

    转http://blog.163.com/dragon_sjl@126/blog/static/100473339201107101517380/ 1.gcc(gnu collect compiler ...

  9. HTML5增强的表单

    form元素a.用来定义一个表单,是建立表单的基础元素(就类似定义表格的table)b.表单的其他元素包含在form元素中,其主要子元素有:input/button/select......form元 ...

  10. ZOJ 3774 Fibonacci的K次方和

    In mathematics, Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers of the f ...