博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A XSS filter for Java EE web apps--转载
阅读量:7215 次
发布时间:2019-06-29

本文共 7626 字,大约阅读时间需要 25 分钟。

原文地址: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 at.

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  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 HTML
17.
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  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 . You may want to take a look at 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  . We do hope to get some public feedback in order to make this library as robust as it can be. 

转载地址:http://lhuym.baihongyu.com/

你可能感兴趣的文章
一个文科妹子走上前端开发不归路(干货分享)
查看>>
中国行政区划信息JS库china-location
查看>>
Vert.x 发送邮件
查看>>
苹果文档 UISearchController的介绍
查看>>
iOS计步器实例
查看>>
vue国际化-vue-i18n的配置
查看>>
Java引用计数与实现
查看>>
Broadcast源码分析
查看>>
create-react-app 2.0中使用antd(eject)
查看>>
logstash常用插件介绍
查看>>
Git命令
查看>>
如何写出优质干净的代码,这6个技巧你不能错过!
查看>>
某口腔app发现了不友善词汇(f*ckMobile)
查看>>
SAP S/4HANA生产订单创建时使用的工厂数据是从什么地方带出来的
查看>>
JavaScript的数据类型有哪些?
查看>>
如何只在IE上加载CSS样式表
查看>>
个人博客三|首页后台开发
查看>>
调用链系列四:调用链上下文传递
查看>>
在Windows下,用Hexo搭建博客
查看>>
刷前端面经笔记(十一)
查看>>