A XSS filter for Java EE web apps--转载
原文地址:http://java.dzone.com/articles/xss-filter-java-ee-web-apps
Cross Site Scripting, or XSS, is a fairly common vector used to attack web sites. It involves user generated code being redisplayed by a website with all the privileges and security rights that a browser assigns to code originating from the current host. If the user code is something like <script>doEvil();</script>, then you have a problem.
OWASP is an organisation that provides guidance on web security, and they have a page that provides a suggested method for avoiding XSS in JavaEE web app. You can read this document athttps://www.owasp.org/index.php/How_to_add_validation_logic_to_HttpServletRequest.
The library being demonstrated here is based off the ideas presented in that article, but fleshed out to be more flexible and easy to deploy. We call this library the (unimaginatively named) Parameter Validation Filter, or PVF.
PVF is implemented as a Servlet filter that intercepts requests to web pages, runs submitted parameters through a configurable sequence of validation rules, and either sanitises the parameters before they are sent through to the web application, or returns a HTTP error code if validation errors were detected.
We have made the following assumptions when developing this library:
- Client side validation will prevent legitimate users from submitting invalid data.
- The PVF library should prevent further processing if invalid data is submitted in the majority of cases.
- Occasionally it might be appropriate to sanitise submitted data, but any sanitisation should be trivial (like the removal of whitespace).
To make use of the PVF library, you’ll need to add it to your project. This artifact is currently in the Sonatype staging repo, so you'll need to add that repo to your Maven config. See http://stackoverflow.com/questions/13945757/how-do-you-import-a-maven-dependency-from-sonatype-org for details.
1.<dependency>2.<groupId>com.matthewcasperson</groupId>3.<artifactId>parameter_validation_filter</artifactId>4.<version>LATEST</version>5.</dependency>The filter then needs to be added to the web.xml file with the following settings. You may want to configure the url-pattern to match the pages that you actually want to protect.
01.<filter>02.<filter-name>ParameterValidationFilter</filter-name>03.<filter-class>com.matthewcasperson.validation.filter.ParameterValidationFilter</filter-class>04.<init-param>05.<param-name>configFile</param-name>06.<param-value>/WEB-INF/xml/pvf.xml</param-value>07.</init-param>08.</filter>09.<filter-mapping>10.<filter-name>ParameterValidationFilter</filter-name>11.<url-pattern>*.jsp</url-pattern>12.</filter-mapping>Finally you need to create a file called WEB-INF/xml/pvf.xml. This file defines the custom validation rules applied to the parameters being sent to your web applications.
01.<?xml version="1.0" encoding="UTF-8" standalone="yes"?>02.<!-- ParameterValidationChainDatabase is always the document element -->03.<ParameterValidationChainDatabase>04.<!--05.Enforcing mode needs to be set to true to return a HTTP error code if validation fails.06.If set to false, validation errors are logged but ignored.07.-->08.<EnforcingMode>true</EnforcingMode>09. 10.<!-- We always have a single ParameterValidationChains element under the parent -->11. 12.<ParameterValidationChains>13. 14.<!-- Each chain of validation rules is contained in a ParameterValidationDefinition element -->15.<!--16.This chain apply some global validation rules. If anyone supplies encoded or params with HTML17.characters, it will fail.18.-->19.<ParameterValidationDefinition>20.<!-- This is the list of validation classes that should be applied to matching parameters -->21.<ParameterValidationRuleList>22.<ParameterValidationRule>23.<!-- This is the fully qualified name of the class used to apply the validation rule -->24.<!-- All input fields are to be trimmed of excess whitespace -->25.<validationRuleName>com.matthewcasperson.validation.ruleimpl.TrimTextValidationRule</validationRuleName>26.</ParameterValidationRule>27.<ParameterValidationRule>28.<!-- No parameters are expected to already be encoded -->29.<validationRuleName>com.matthewcasperson.validation.ruleimpl.FailIfNotCanonicalizedValidationRule</validationRuleName>30.</ParameterValidationRule>31.<ParameterValidationRule>32.<!-- No parameters are expected to contain html -->33.<validationRuleName>com.matthewcasperson.validation.ruleimpl.FailIfContainsHTMLValidationRule</validationRuleName>34.</ParameterValidationRule>35.</ParameterValidationRuleList>36.<!-- This is a regex that defines which parameteres will be validated by the classes above -->37.<paramNamePatternString>.*</paramNamePatternString>38.<!-- This is a regex that defines which URLs will be validated by the classes above -->39.<requestURIPatternString>.*</requestURIPatternString>40.<!--41.Setting this to false means the paramNamePatternString has to match the param name.42.Setting it to true would mean that paramNamePatternString would have to *not* match the param name.43.--> 44.<paramNamePatternNegated>false</paramNamePatternNegated>45.<!--46.Setting this to false means the requestURIPatternString has to match the uri.47.Setting it to true would mean that requestURIPatternString would have to *not* match the uri name.48.-->49.<requestURIPatternNegated>false</requestURIPatternNegated>50.</ParameterValidationDefinition> 51.</ParameterValidationChains>52.</ParameterValidationChainDatabase>The XML has been commented to make it easier to understand, but there are a few interesting elements:
- paramNamePatternString, which has been configured to enable the validation chain to match all parameters
- requestURIPatternString, which has been configured to enable the chain to match all URIs
- The three elements called validationRuleName, which reference the full class name of the validation rules that will be applied to each parameter passed into our web application
Although this is a simple example, the three validation rules that have been implemented (TrimTextValidationRule, FailIfNotCanonicalizedValidationRule and FailIfContainsHTMLValidationRule) are quite effective at preventing a malicious user from submitting parameters that contain XSS code.
The first rule, TrimTextValidationRule, simply strips away any whitespace on either side of the parameter. This uses the trim() function any developer should be familiar with.
The second rule, FailIfNotCanonicalizedValidationRule, will prevent further processing if the supplied parameter has already been encoded. No legitimate user will have a need to supply text like %3Cscript%3EdoEvil()%3B%3C%2Fscript%3E, so any time encoded text is found we simply return with a HTTP 400 error code. This rule makes use of the ESAPI library supplied by OWASP.
Like the second rule, the third rule will prevent further processing if the supplied parameter has any special HTML characters. If you would like your customers to be able to pass through characters like &, this rule is too broad. However, it is almost always valid to block special HTML characters.
If you want to see how effective this simple validation chain is, check out the live demo at http://pvftest-matthewcasperson.rhcloud.com/. You may want to take a look athttps://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet to find some XSS patterns that are often used to bypass XSS filters.
Moving forward we will be looking to implement more targeted validation rules, especially those that can’t be easily implemented as regex matches (like making sure a date if after today, or that a number is between two values etc).
If you have any suggestions, or find any bugs, feel free to fork the code from our GitHub repo . We do hope to get some public feedback in order to make this library as robust as it can be.
A XSS filter for Java EE web apps--转载的更多相关文章
- 严重: Exception starting filter struts2 java.lang.NullPointerException (转载)
严重: Exception starting filter struts2 java.lang.NullPointerException at com.opensymphony.xwork2.util ...
- 各种容器与服务器的区别与联系:Servlet容器、WEB容器、Java EE容器、应用服务器、WEB服务器、Java EE服务器
1.容器与服务器的联系 如上图,我们先来看下容器与服务器的联系:容器是位于应用程序/组件和服务器平台之间的接口集合,使得应用程序/组件可以方便部署到服务器上运行. 2.各种容器的区别/联系 2-1.容 ...
- 各种容器与服务器的区别与联系 Servlet容器 WEB容器 Java EE容器 应用服务器 WEB服务器 Java EE服务器
转自:https://blog.csdn.net/tjiyu/article/details/53148174 各种容器与服务器的区别与联系 Servlet容器 WEB容器 Java EE容器 应用服 ...
- Java EE 学习总结
1.Java EE WEB 工程项目文件结构 组成:静态HTML页.Servlet.JSP和其他相关的class: 每个组件在WEB应用中都有固定的存放目录. WEB应用的配置信息存放在web.xml ...
- 【JAVA EE企业级开发四步走完全攻略】
本文是J2EE企业级开发四步走完全攻略索引,因内容比较广泛,涉及整个JAVA EE开发相关知识,这是一个长期的计划,单个发blog比较零散,所以整理此索引,决定以后每发一季JAVA EE blog后会 ...
- 微服务:Java EE的拯救者还是掘墓人?
有人认为,微服务的大行其道是在给Java EE下达死刑判决书.也有人认为,Java EE已死的论调可笑至极.读者朋友,你们怎么看? 引言 有人说,Java确实过于臃肿,经常“小题大做”.但PHP.No ...
- java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter /struts2.1.3以前版本和之后版本区别/新版Eclipse找不到Java EE Module Dependencies选项
严重: Exception starting filter struts2java.lang.ClassNotFoundException: org.apache.struts2.dispatcher ...
- Java EE 学习(6):IDEA + maven + spring 搭建 web(2)- 配置 Spring
参考:https://my.oschina.net/gaussik/blog/513353 注:此文承接上一文:Java EE 学习(5):IDEA + maven + spring 搭建 web(1 ...
- Java EE 和 Java Web
什么是 Java Web 应用程序? Java Web 应用程序会生成包含各种类型的标记语言(HTML 和 XML 等)和动态内容的交互式 Web 页.它通常由 Web 组件组成(如 JavaServ ...
随机推荐
- 多线程程序设计学习(6)Producer-Consumer模式
Producer-Consumer[生产消费者模式]一:Producer-Consumer pattern的参与者--->产品(蛋糕)--->通道(传递蛋糕的桌子)--->生产者线程 ...
- memcached单点故障与负载均衡
在上文中,主要教大家如何搭建在windows IIS 7.5下搭建php环境,使用常见的两种memcached性能监视工具.通过自己动手实践,观察监控工具上数据,相信大家对于memcached的了解 ...
- HDU 5624 KK's Reconstruction 最小生成树
题意:这是bc round 71 div 1 的 1004 直接去看中文题意 分析: 首先,一种合法方案对应了原图的一棵生成树. 我们知道,最小生成树有一个性质是最大边最小. 因此,我们可以枚举生成树 ...
- 安装GNS3-0.8.6-all-in-one时language里没有选项
初次安装使用GNS3,安装的版本是GNS3-0.8.6-all-in-one,本人也是菜鸟,安装时都是一路 Next,结果安装后运行出现了这样的问题,如图 language里是没有选项的,解决方法 把 ...
- 【译】 AWK教程指南 9读取命令行上的参数
大部分的应用程序都允许使用者在命令之后增加一些选择性的参数.执行awk时这些参数大部分用于指定数据文件文件名,有时希望在程序中能从命令行上得到一些其它用途的数据.本小节中将叙述如何在awk程序中取用这 ...
- 问题:关于坛友的一个js轮播效果的实现
需求:点击向前按钮进行向前翻页,向后按钮进行向后翻页,点击中间蓝色小圆圈可以来回自由切换 我的大概思路:先默认显示一个div 然后在原位置在隐藏一个div 给按钮添加click事件,转到下一个时 ...
- 你真的知道HTML吗?
经过几次面试当中,被问及到最基础的东西,没想到回答不上来,有点蛋痛,今天特地的复习了一下!! 内容: 1.Doctype(文档类型)的作用是什么?有多少文档类型? 2.浏览器标准模式和怪异模式之间的区 ...
- Matlab GUI界面
做SVD的时候,看学姐的demo,用到了matlab的GUI,感兴趣就自己学了一下: 从简单的例子说起吧. 创建Matlab GUI界面通常有两种方式: 1,使用 .m 文件直接动态添加控件 ...
- JavaScript:子ウィンドウから親ウィンドウを再読み込みさせる方法
ことの起こり Webの画面では.新規入力をしようとすると.別ウィンドウ=子ウィンドウが開いて入力し.登録ボタンを押すと.子ウィンドウが閉じる仕組みがある. 子ウィンドウが閉じるだけなら問題ないが.一覧 ...
- HDU4862-Jump(最大流量最大费用流)
题意:有n*m的格子,每一个格子包含一个数字,0-9.你初始的能量为0,你可以玩k次,每一个你可以选择你现在的格子的正下方或者正右方的任意一个格子跳,但必须是之前没有跳过的格子.每玩一次你都可以跳任意 ...