偶然间在向后台传递带单引号的参数值发现:单引号被转义成“& #39;”,不是正常的html符号转义,而是“&”后面多了一个空格,导致无法再被反转义。
后追踪代码发现项目中做了防XSS攻击处理导致的,代码如下:
private String cleanXSS(String value) {
//You'll need to remove the spaces from the html entities below
value = value.replaceAll("<", "& lt;").replaceAll(">", "& gt;");
value = value.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");
value = value.replaceAll("'", "& #39;");
value = value.replaceAll("eval\\((.*)\\)", "");
value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
value = value.replaceAll("script", "");
return value;
}
这段代码在网上的防XSS攻击文章里基本都一模一样,那么多文章连注释都不带变的。。。注释上貌似是说让去掉空格的吧,这么多人没改,搞的我也不敢确认这个注释是不是这个意思了。
这个情况在使用Guns的时候也有,在guns-base的pom文件中引入的kernel-core下cn.stylefeng.roses.core.xss.XssHttpServletRequestWrapper类,这里无法去掉的话只能自己再重写覆盖了。
<!--核心组件-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-core</artifactId>
</dependency>
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-validator</artifactId>
</dependency>