简体   繁体   English

Java网站保护解决方案(尤其是XSS)

[英]Java website protection solutions (especially XSS)

I'm developing a web application, and facing some security problems. 我正在开发一个Web应用程序,并面临一些安全问题。

In my app users can send messages and see other's (a bulletin board like app). 在我的应用程序中,用户可以发送消息并查看其他人的消息(如应用程序之类的公告板)。 I'm validating all the form fields that users can send to my app. 我正在验证用户可以发送到我的应用程序的所有表单字段。

There are some very easy fields, like "nick name", that can be 6-10 alpabetical characters, or message sending time, which is sended to the users as a string, and then (when users ask for messages, that are "younger" or "older" than a date) I parse this with SimpleDateFormat (I'm developing in java, but my question is not related to only java). 有一些非常简单的字段,例如“昵称”,可以是6到10个字母字符,或者消息发送时间,它以字符串的形式发送给用户,然后(当用户要求消息时,“年轻” ”或“比日期晚”)我用SimpleDateFormat解析(我正在用Java开发,但我的问题不仅与Java有关)。

The big problem is the message field. 最大的问题是消息字段。 I can't restrict it to only alphabetical characters (upper or lowercase), because I have to deal with some often use characters like ",',/,{,} etc... (users would not be satisfied if the system didn't allow them to use these stuff) 我不能仅将其限制为字母字符(大写或小写),因为我不得不处理一些经常使用的字符,例如“,”,/,{,}等(如果系统不满足用户的要求,不允许他们使用这些东西)

According to this http://ha.ckers.org/xss.html , there are a lot of ways people can "hack" my site. 根据此http://ha.ckers.org/xss.html ,人们可以通过多种方式“入侵”我的网站。 But I'm wondering, is there any way I can do to prevent that? 但我想知道,有什么办法可以防止这种情况发生? Not all, because there is no 100% protection, but I'd like a solution that can protect my site. 不是全部,因为没有100%的保护,但是我想要一个可以保护我的网站的解决方案。

I'm using servlets on the server side, and jQuery, on the client side. 我在服务器端使用servlet,在客户端使用jQuery。 My app is "full" AJAX, so users open 1 JSP, then all the data is downloaded and rendered by jQuery using JSON. 我的应用程序是“完整的” AJAX,因此用户打开1个JSP,然后所有数据将由jQuery使用JSON下载并呈现。 (yeah, I know it's not "users-without-javascript" friendly, but it's 2010, right? :-) ) I know front end validation is not enough. (是的,我知道这对“ users-without-javascript”并不友好,但现在是2010年,对吗?:-))我知道前端验证是不够的。 I'd like to use 3 layer validation: - 1. front end, javascript validate the data, then send to the server - 2. server side, the same validation, if there is anything, that shouldn't be there (because of client side javascript), I BAN the user - 3. if there is anything that I wasn't able to catch earlier, the rendering process handle and render appropriately 我想使用3层验证:-1.前端,使用javascript验证数据,然后将其发送到服务器-2.服务器端,相同的验证,如果有的话,那不应该存在(因为客户端javascript),则禁止用户-3.如果有任何我之前无法捕获的内容,则呈现过程将进行处理并适当地进行呈现

Is there any "out of the box" solution, especially for java? 是否有任何“开箱即用”的解决方案,尤其是对于Java? Or other solution that I can use? 还是我可以使用的其他解决方案?

To minimize XSS attacks important thing is to encode any field data before putting it back on the page. 为了最大程度地减少XSS攻击,重要的是对任何字段数据进行编码,然后再将其放回到页面上。 Like change > to > and so on. 例如将>更改为>等。 This would never allow any malicious code to execute when being added to the page. 当添加到页面时,这绝不会允许任何恶意代码执行。

I think you are doing lot of right things by white listing the data you expect for different fields. 我认为您通过将您期望在不同字段中使用的数据列入白名单来做很多正确的事情。 Beyond that for fields which can allow other characters which can be problematic encoding would fix the issue for you. 除此之外,对于可以允许其他字符编码的字段,可能会为您解决问题。

Further since you are using Ajax it gives you some protection as people cannot override values in URL parameters etc. 此外,由于您使用的是Ajax,它可以为您提供一些保护,因为人们无法覆盖URL参数等中的值。

Look at the AntiSamy library. 查看AntiSamy库。 It allows you to define rulesets for your application, then run your user input through AntiSamy to clean it per your rules. 它允许您为应用程序定义规则集,然后通过AntiSamy运行用户输入以按照规则清除它。

The easiest way is to do a simple replacement for the following < with &lt; 最简单的方法是用&lt;替换以下<
> with &gt; >&gt;
' with \\' '与\\'

That will solve most database vulnerability. 这将解决大多数数据库漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM