Java学习笔记26:webx安全配置问题

来源:互联网 发布:软件开发合同 编辑:程序博客网 时间:2024/05/23 11:06

1、默认拦截器

即使你不加说明,BasicRequestContext也总是会启用一个默认的interceptor实

现:ResponseHeaderSecurityFilter。这个类实现了下列功能:

• 避免header name和value中出现CRLF字符 ── 在header中嵌入CRLF(回车换行)字符

是一种常见的攻击手段。攻击者嵌入CRLF以后,使服务器对HTTP请求发生错误判断,从而执行攻击者的恶意代码。事实上,现在的servlet引擎如tomcat已经可以防御这种攻击。但作为框架,并不能依赖于特定的servlet引擎,所以加上这个额外的安全检查,确保万无一失。

• 将status message用HTML entity编码重写 ── 通常status message会被显示在HTML页面中。攻击者可以利用这一点在页面中嵌入恶意代码。将status message以HTML entity编码重写以后,就可以避免这个问题。

• 限制cookie的总大小 ── 过大的cookie可能使WEB服务器拒绝响应请求。攻击者同样可以利用这一点使用户无法正常访问网站。限制cookie的总大小可以部分地解决这种危机。

如果需要,你可以对ResponseHeaderSecurityFilter指定一些参数。

例 7.2. 配置ResponseHeaderSecurityFilter(/WEB-INF/webx.xml)

<request-contexts:interceptors

xmlns="http://www.alibaba.com/schema/services/request-contexts/basic/interceptors">

<interceptor class="...Interceptor1" />

<interceptor class="...Interceptor2" />

<response-header-security-filter maxSetCookieSize="5K" />

</request-contexts:interceptors>

2、过滤参数

出于安全的考虑,<parser>还支持对输入参数进行过滤。请看示例:

例 7.31. 配置过滤参数

  1. <parser> 
  2.  
  3. <filters> 
  4.  
  5. <parser-filters:uploaded-file-whitelist extensions="jpg, gif, png" /> 
  6.  
  7. </filters> 
  8.  
  9. </parser> 

上面的配置将会禁止文件名后缀不在列表中的文件被上传到服务器上。如果做得更好一点,你甚至可以对上传文件进行病毒扫描。

目前,<parser>支持两种过滤器接口:ParameterValueFilter和UploadedFileFilter。前者用来对普通的参数值进行过滤(例如排除可能造成攻击的HTML代码);后者用来对上传文件的file item对象进行过滤,就像刚才的uploaded-file-whitelist的例子。

3、增加Fasttext的宏

在webx-component-and-root.xml中配置

  1. <!-- Template渲染服务。 --> 
  2.     <services:template 
  3.         xmlns="http://www.alibaba.com/schema/services/template/engines" 
  4.         searchExtensions="true"> 
  5.         <velocity-engine templateEncoding="GBK" 
  6.             strictReference="false" path="/${component}/templates"> 
  7.             <global-macros> 
  8.                 <name>global/*Macros.vm</name> 
  9.                 <name>global/*macros.vm</name> 
  10.                 <name>classpath/unictemplate/macros.vm</name> 
  11.             </global-macros> 
  12.             <plugins> 
  13.                 <!-- <vm-plugins:escape-support defaultEscape="html"> 
  14.                     <vm-plugins:noescape> 
  15.                         <vm-plugins:if-matches pattern="^control\." /> 
  16.                         <vm-plugins:if-matches pattern="^screen_placeholder" /> 
  17.                         <vm-plugins:if-matches pattern="^stringEscapeUtil\.escape" /> 
  18.                         <vm-plugins:if-matches pattern="^csrfToken\.(get)?hiddenField" /> 
  19.                     </vm-plugins:noescape> 
  20.                 </vm-plugins:escape-support> --> 
  21.                 <vm-plugins:fasttext-support /> 
  22.                 <vm-plugins:renderable-support /> 
  23.             </plugins> 
  24.         </velocity-engine> 

注意:

(1)b2b使用的是fasttext解决方案,而不用eacape,所以添加<vm-plugins:fasttext-support /> 

(2)为了解决二房库的宏引用问题,使用  <name>classpath/unictemplate/macros.vm</name> 进行配置

顺便说说资源的load,在webx-component-and-root.xml中配置的资源,例如:

  1. <global-macros> 
  2.                 <name>global/*Macros.vm</name> 
  3.                 <name>global/*macros.vm</name> 
  4.                 <name>classpath/unictemplate/macros.vm</name> 
  5.             </global-macros> 

他们都是在中相应的classloader去加载的,这些loader在resources.xml中进行了说明,比如:res-loaders:classpath-loader, res-loaders:webapp-loader