xss漏洞和csrf漏洞防护

xss防护:

一、尽可能少将域名的domain设为域名的根下面,减小分站xss漏洞对主站的影响;php

二、对输入的数据进行过滤检查:css

  
  
  
  
  1. public static String htmlSpecialChars(final String s) { 
  2.       String result = s; 
  3.       result = regexReplace("&", "&", result); 
  4.       result = regexReplace("\"", """, result); 
  5.       result = regexReplace("<", "&lt;", result); 
  6.       result = regexReplace(">", "&gt;", result); 
  7.       return result; 
  8.   } 

注意:CSS的行为方式也会有JavaScript的执行:html

<style type="text/css" >
#content { height: expression(alert('test xss') ); }
</style>java

若是要支持html能够使用这个过滤器(附件,开源的)web

  
  
  
  
  1. 例子 
  2. final ArrayList<Attribute> span_atts = new ArrayList<Attribute>(); 
  3. Map<String, Pattern> allowedAttrValues = new HashMap<String, Pattern>(); 
  4. allowedAttrValues.put(“color”, Pattern.compile(“(#([0-9a-fA-F]{6}|[0-9a-fA-F]{3}))”)); 
  5. allowedAttrValues.put(“font-weight”, Pattern.compile(“bold”)); 
  6. allowedAttrValues.put(“text-align”, Pattern.compile(“(center|right|justify)”)); 
  7. allowedAttrValues.put(“font-style”, Pattern.compile(“italic”)); 
  8. allowedAttrValues.put(“text-decoration”, Pattern.compile(“underline”)); 
  9. allowedAttrValues.put(“margin-left”, Pattern.compile(“[0-9]+px”)); 
  10. allowedAttrValues.put(“text-align”, Pattern.compile(“center”)); 
  11. span_atts.add(new Attribute(“style”, allowedAttrValues)); 
  12. vAllowed.put(“span”, span_atts); 
  13. final ArrayList<Attribute> div_atts = new ArrayList<Attribute>(); 
  14. div_atts.add(new Attribute(“class”)); 
  15. div_atts.add(new Attribute(“align”)); 
  16. vAllowed.put(“div”, div_atts); 
  17. * 2. 调用相似这样的函数String outHtml = HetaoBlogXssHTMLFilter.filter(sourceHtmlString); 

三、针对图片的上传须要检测是不是正确的图片格式是不是伪格式 ,图片服务器尽可能不开启程序(java,php,.net)功能或对图片格式不作程序解析;express

防护CSRF:

    在Web应用程序侧防护CSRF漏洞,通常都是利用referer判断输入端的url来源、或使用token或者使用JavaScript看不见的验证码;服务器

相关文章
相关标签/搜索