采用bs4过滤的方式防xss跨站脚本
2020-5-13 22:52 作者:Mr.刘 | 黑客技术 | 标签: 采用bs4过滤的方式防xss跨站脚本
采用bs4过滤的方式防xss跨站脚本
from bs4 import BeautifulSoup
class XSSFilter(object):
__instance = None
def __init__(self):
# XSS白名单
self.valid_tags = {
"font": ['color', 'size', 'face', 'style'],
'b': [],
'div': [],
"span": [],
}
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 class="sister" id="link2">Lacie</a> and
<a 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)
文章作者:Mr.刘 作者QQ:905690245
文章地址:https://www.blog.qixiwangluo.com:443/?post=125
版权所有 © 转载时必须以链接形式注明作者和原始出处!
北京时间
网站资料
- 日志数量:155 篇
- 评论数量:3 条
- 微语数量:3 条
- 建站日期:2018.03.28
- 运行时间:239611445 秒
网站微语
-
没有网络安全,就没有国家安全.
最新微语
- 黑客不在乎技术的高与低.
Mr.刘 : 03月31日 00:12
- 而在乎她对这种技术的执着追求.
Mr.刘 : 03月31日 00:12
- 谦虚谨慎,不骄不躁.
Mr.刘 : 03月31日 00:11
分类
最新文章
热门文章
位卑勿敢忘忧国1937.12.13
八一.军网 www.81.cn
招聘广告
微信扫一扫赞助本站




发表评论: