问题概述
项目后台使用的技术是Spring+Shiro+MVC,AppScan一共扫描出7种低危漏洞,以下是对这些漏洞的处理说明。
1、“Content-Security-Policy”头缺失
在网上查了关于这个响应头的说明,CSP相当于前台的白名单,用来限制网站内部一些资源获取的来源,如限制CSS、JS、图片或者第三方链接等。
CSP的设置可以在一定程度上限制XSS攻击,有2种方式可以设置。第一种通过设置HTTP响应头,另一种通过HTML的<meta>标签。
具体的设置和说明请参考: http://www.ruanyifeng.com/blog/2016/09/csp.html
我是在自定义的拦截器中加了CSP设置,后面2个关于响应头的设置也是在这里加的。
public class YourInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//your code
//……
//设置CSP
response.addHeader("Content-Security-Policy","default-src 'self' 'unsafe-inline' 'unsafe-eval';");
response.addHeader("X-Content-Type-Options","nosniff");
response.addHeader("X-XSS-Protection","1");
//your code
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//your code
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//your code
}
}
2、“X-Content-Type-Options”头缺失或不安全
响应头缺失,按照扫描要求设置,具体设置请参照第一点。如果不知道具体的属性值,可以参考官方的API或者去网上找一找别人的说明。
这是别人写的,大家有需要的话可以参考一下:
https://www.cnblogs.com/vekair/p/11233649.html
3、“X-XSS-Protection”头缺失或不安全
响应头缺失,按照扫描要求设置,具体设置请参照前2点。
4、查询中接受的主体参数
这个就是禁用GET请求,但是由于系统内部需要用到,所以只需要对携带数据传递的请求设置为POST方式。
5、启用了不安全的“OPTIONS”HTTP 方法
这个在网上有很多方法可以参考,如果服务器有Nginx,可以直接在Nginx中配置,如果没有可以配置在Tomcat上。我这里是配置在Tomcat上的。
Tomcat的web.xml文件进行修改(修改前请先记得备份哦)
<!-- 限制HTTP请求方法配置,开始 -->
<!-- 先注释掉原来的引入文件 -->
<!-- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> -->
<!-- 添加新的引入文件 -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://java.sun.com/xml/ns/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 过滤掉DELETE、HEAD、PUT等请求方式 -->
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>PATCH</http-method>
</web-resource-collection>
<auth-constraint></auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<!--限制HTTP请求方法配置,结束 -->
6、发现数据库错误模式
这个就是说不允许用户在前台看到有关数据库的报错,就是不能在前台看到下图这样类型的报错。
所以这里就对数据库后台的查询进行拦截,做一个统一的异常处理,使前台看不到这种报错,类似下图这种,具体的样式可以自己设计。
后端需要自定义一个异常处理类,继承HandlerExceptionResolver,重写resolveException()方法,在方法里面对异常信息进行处理即可。我这里直接简单写了几句话,返回给前台页面,加上了时间是便于排查问题。要记得把这个类注入进去。
@Component
public class WebExceptionResolver implements HandlerExceptionResolver {
private static transient Logger logger = LoggerFactory.getLogger(WebExceptionResolver.class);
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
logger.error("WebExceptionResolver:{}", ex);
// if json
boolean isJson = false;
HandlerMethod method = (HandlerMethod) handler;
ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class)