控制好输入/输出
php可以用以下函数来防御
htmlspecialchars();
htmlentities();
HTMLPurifier.auto.php插件 http://htmlpurifier.org/download
RemoveXss函数 https://gist.github.com/aligundogdu/1839051
python库
>>> import cgi
>>> cgi.escape('<script>&"', quote=True)
'<script>&"'
>>> HTMLParser.unescape.__func__(HTMLParser, '<script>&"')
u'<script>&"'
import bleach
from bleach.sanitizer import ALLOWED_TAGS,ALLOWED_ATTRIBUTES
@require_http_methods(['POST'])defmessage(request):# 从客户端中获取提交的数据
content = request.POST.get('content')
# 在默认的允许标签中添加img标签
tags = ALLOWED_TAGS + ['img']
# 在默认的允许属性中添加src属性
attributes = {**ALLOWED_ATTRIBUTES,'img':['src']}
# 对提交的数据进行过滤
cleaned_content=bleach.clean(content,tags=tags,attributes=attributes)
# 保存到数据库中
Message.objects.create(content=cleaned_content)
return redirect(reverse('index'))
pom.xml
首先添加一个jar包:commons-lang-2.5.jar ,然后在后台调用这些函数:
StringEscapeUtils.escapeHtml(string);
StringEscapeUtils.escapeJavaScript(string);
StringEscapeUtils.escapeSql(string);
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
或者自己编写一个XSSFilter静态类,可以直接调用
设置Httponly
不要动态输出用户的输入
不要使用eval函数解析json,用JSON.parse()
前后端分离
eval('(' + jsonstr + ')') #这种方式 是不严谨的解析
JSON.parse()
https://github.com/leizongmin/js-xss
<script src="https://rawgit.com/leizongmin/js-xss/master/dist/xss.js"></script>
<script>
// 使用函数名 filterXSS,用法一样
var html = filterXSS('<script>alert("xss");</scr' + 'ipt>');
alert(html);
</script>
参考:
http://blog.evalshell.com/2020/12/20/风炫安全web安全学习第二十七节课-xss的防御措施/
手机扫一扫
移动阅读更方便
你可能感兴趣的文章