如何清理HTML代码以防止Java或JSP中的XSS攻击? [英] How to sanitize HTML code to prevent XSS attacks in Java or JSP?

查看:850
本文介绍了如何清理HTML代码以防止Java或JSP中的XSS攻击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于servlet的应用程序,我需要在其中提供一个消息传递系统。我很匆忙,所以我选择 CKEditor 来提供编辑功能,我目前直接插入生成的html显示所有消息的网页(消息存储在MySQL数据库中,fyi)。 CKEditor已经基于白名单过滤HTML,但是用户仍然可以使用POST请求注入恶意代码,所以这还不够。

I'm writing a servlet-based application in which I need to provide a messaging system. I'm in a rush, so I choose CKEditor to provide editing capabilities, and I currently insert the generated html directly in the web page displaying all messages (messages are stored in a MySQL databse, fyi). CKEditor already filters HTML based on a white list, but a user can still inject malicious code with a POST request, so this is not enough.

一个好的库已经存在通过过滤HTML标签来防止XSS攻击,但它是用PHP编写的: HTML Purifier

A good library already exists to prevent XSS attacks by filtering HTML tags, but it's written in PHP: HTML Purifier

那么,是否有类似的成熟库可以在Java中使用
基于白名单的简单字符串替换似乎不够,因为我也想过滤格式不正确的标签(这可能会改变页面的设计消息显示)。

So, is there a similar mature library that can be used in Java ? A simple string replacement based on a white list doesn't seem to be enough, since I'd like to filter malformed tags too (which could alter the design of the page on which the message is displayed).

如果没有,那我该怎么办? XML解析器看起来有点过分。

If there isn't, then how should I proceed? An XML parser seems overkill.

注意:在SO上有很多关于此的问题,但所有答案都是指过滤所有HTML标签:我想要保持有效的格式化标签。

推荐答案

你应该使用 AntiSamy 。 (这就是我所做的

这篇关于如何清理HTML代码以防止Java或JSP中的XSS攻击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆