Anti cross-site scripting (XSS) filter for Java web apps

本文介绍了一个为Java web应用程序编写的安全改进型跨站脚本攻击(XSS)过滤器,它能从请求参数中移除可疑字符串,防止攻击。该过滤器作为链中第一个过滤器配置,适用于捕获所有站点请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Anti cross-site scripting (XSS) filter for Java web apps

Here is a good and simple anti cross-site scripting (XSS) filter written for Java web applications. What it basically does is remove all suspicious strings from request parameters before returning them to the application. It’s an improvement over  my previous post on the topic.
You should configure it as the first filter in your chain (web.xml) and it’s generally a good idea to let it catch every request made to your site.
The actual implementation consists of two classes, the actual filter is quite simple, it wraps the HTTP request object in a specialized HttpServletRequestWrapper that will perform our filtering.
01 public class XSSFilter implements Filter {
02  
03     @Override
04     public void init(FilterConfig filterConfig) throws ServletException {
05     }
06  
07     @Override
08     public void destroy() {
09     }
10  
11     @Override
12     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
13         throws IOException, ServletException {
14         chain.doFilter(new XSSRequestWrapper((HttpServletRequest) request), response);
15     }
16  
17 }

The wrapper overrides the getParameterValues(), getParameter() and getHeader() methods to execute the filtering before returning the desired field to the caller. The actual XSS checking and striping is performed in the stripXSS() private method.

01 import java.util.regex.Pattern;
02 import javax.servlet.http.HttpServletRequest;
03 import javax.servlet.http.HttpServletRequestWrapper;
04  
05 public class XSSRequestWrapper extends HttpServletRequestWrapper {
06  
07     public XSSRequestWrapper(HttpServletRequest servletRequest) {
08         super(servletRequest);
09     }
10  
11     @Override
12     public String[] getParameterValues(String parameter) {
13         String[] values = super.getParameterValues(parameter);
14  
15         if (values == null) {
16             return null;
17         }
18  
19         int count = values.length;
20         String[] encodedValues = new String[count];
21         for (int i = 0; i < count; i++) {
22             encodedValues[i] = stripXSS(values[i]);
23         }
24  
25         return encodedValues;
26     }
27  
28     @Override
29     public String getParameter(String parameter) {
30         String value = super.getParameter(parameter);
31  
32         return stripXSS(value);
33     }
34  
35     @Override
36     public String getHeader(String name) {
37         String value = super.getHeader(name);
38         return stripXSS(value);
39     }
40  
41     private String stripXSS(String value) {
42         if (value != null) {
43             // NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to
44             // avoid encoded attacks.
45             // value = ESAPI.encoder().canonicalize(value);
46  
47             // Avoid null characters
48             value = value.replaceAll("""");
49  
50             // Avoid anything between script tags
51             Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
52             value = scriptPattern.matcher(value).replaceAll("");
53  
54             // Avoid anything in a src='...' type of expression
55             scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
56             value = scriptPattern.matcher(value).replaceAll("");
57  
58             scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
59             value = scriptPattern.matcher(value).replaceAll("");
60  
61             // Remove any lonesome </script> tag
62             scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
63             value = scriptPattern.matcher(value).replaceAll("");
64  
65             // Remove any lonesome <script ...> tag
66             scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
67             value = scriptPattern.matcher(value).replaceAll("");
68  
69             // Avoid eval(...) expressions
70             scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
71             value = scriptPattern.matcher(value).replaceAll("");
72  
73             // Avoid expression(...) expressions
74             scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
75             value = scriptPattern.matcher(value).replaceAll("");
76  
77             // Avoid javascript:... expressions
78             scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
79             value = scriptPattern.matcher(value).replaceAll("");
80  
81             // Avoid vbscript:... expressions
82             scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
83             value = scriptPattern.matcher(value).replaceAll("");
84  
85             // Avoid onload= expressions
86             scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
87             value = scriptPattern.matcher(value).replaceAll("");
88         }
89         return value;
90     }
91 }

Notice the comment about the ESAPI library, I strongly recommend you check it out and try to include it in your projects.

If you want to dig deeper on the topic I suggest you check out the OWASP page about XSS and RSnake’s XSS (Cross Site Scripting) Cheat Sheet.

Reference: Stronger anti cross-site scripting (XSS) filter for Java web apps from our JCG partner Ricardo Zuasti at the Ricardo Zuasti’s blog blog.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值