django使用BeautifulSoup4库防止js的xss攻击

Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下:

Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。
它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。

这里我们再django中使用它来过滤用户提交文章中的script标签。用来防止用户编写JavaScript脚本攻击等。注意这个导入库与安装的库名略有差异。如果没有安装的话。安装命令为pip3 install beautifulsoup4

from bs4 import BeautifulSoup
def add_article(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        content = request.POST.get('content')
        category_id = request.POST.get('category')
        tag_id_list = request.POST.getlist('tag')
        soup = BeautifulSoup(content,'html.parser')
        #获取所有数据
        for tag in  soup.find_all():  #获取标签字符串所有的标签对象
            # print(tag.name)
            if tag.name =="script":
                #针对script标签,直接删除标签
                tag.decompose()
        #文章简介,先直接切取150个字符
        # desc = content[0:150]
        #2截取文本150个
        desc = soup.text[0:150] + "..."
        if content=="" or title =="":
            return redirect('/add/article/')
        else:
            Article_obj=models.Article.objects.create(
                title=title,
                content=str(soup),
                desc=desc,
                category_id=category_id,
                blog=request.user.blog
            )
            #文章和标签关系表,半自动因此需手动操作
            article_obj_list = []
            for i in tag_id_list:
                article_obj_list.append(models.Article_Tag(article=Article_obj,tag_id=i))  #生成对象并添加到列表

            #批量插入数据
            models.Article_Tag.objects.bulk_create(article_obj_list)
            #跳转到后台管理页面
            return redirect('/backend/')

    category_list = models.Category.objects.filter(blog=request.user.blog)
    tag_list = models.Tag.objects.filter(blog=request.user.blog)

    return render(request,'backend/add_article.html',locals())

猜你喜欢

转载自blog.csdn.net/qq_45701131/article/details/109202039