一、尽可能少将域名的domain设为域名的根下面,减小分站xss漏洞对主站的影响;php
二、对输入的数据进行过滤检查:css
- public static String htmlSpecialChars(final String s) {
- String result = s;
- result = regexReplace("&", "&", result);
- result = regexReplace("\"", """, result);
- result = regexReplace("<", "<", result);
- result = regexReplace(">", ">", result);
- return result;
- }
注意:CSS的行为方式也会有JavaScript的执行:html
<style type="text/css" >
#content { height: expression(alert('test xss') ); }
</style>java
若是要支持html能够使用这个过滤器(附件,开源的)web
- 例子
- {
- final ArrayList<Attribute> span_atts = new ArrayList<Attribute>();
- Map<String, Pattern> allowedAttrValues = new HashMap<String, Pattern>();
- allowedAttrValues.put(“color”, Pattern.compile(“(#([0-9a-fA-F]{6}|[0-9a-fA-F]{3}))”));
- allowedAttrValues.put(“font-weight”, Pattern.compile(“bold”));
- allowedAttrValues.put(“text-align”, Pattern.compile(“(center|right|justify)”));
- allowedAttrValues.put(“font-style”, Pattern.compile(“italic”));
- allowedAttrValues.put(“text-decoration”, Pattern.compile(“underline”));
- allowedAttrValues.put(“margin-left”, Pattern.compile(“[0-9]+px”));
- allowedAttrValues.put(“text-align”, Pattern.compile(“center”));
- span_atts.add(new Attribute(“style”, allowedAttrValues));
- vAllowed.put(“span”, span_atts);
- }
- {
- final ArrayList<Attribute> div_atts = new ArrayList<Attribute>();
- div_atts.add(new Attribute(“class”));
- div_atts.add(new Attribute(“align”));
- vAllowed.put(“div”, div_atts);
- }
- * 2. 调用相似这样的函数String outHtml = HetaoBlogXssHTMLFilter.filter(sourceHtmlString);
三、针对图片的上传须要检测是不是正确的图片格式是不是伪格式 ,图片服务器尽可能不开启程序(java,php,.net)功能或对图片格式不作程序解析;express
在Web应用程序侧防护CSRF漏洞,通常都是利用referer判断输入端的url来源、或使用token或者使用JavaScript看不见的验证码;服务器