简体   繁体   English

跨站点脚本和过滤器请求asp.net

[英]Cross Site Scripting & filter request asp.net

i need to write a generic routine which will filter cross site scripting related character. 我需要编写一个通用例程,该例程将过滤跨站点脚本相关的字符。 i found one that is in java. 我发现一个在Java中。 i dont know java. 我不懂Java。

here is java code 这是java代码

package com.greatwebguy.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class CrossScriptingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
}
public void destroy() {
    this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
    chain.doFilter(new RequestWrapper((HttpServletRequest) request), response);
}

} }

package com.greatwebguy.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public final class RequestWrapper extends HttpServletRequestWrapper {
public RequestWrapper(HttpServletRequest servletRequest) {
    super(servletRequest);
}
public String[] getParameterValues(String parameter) {
  String[] values = super.getParameterValues(parameter);
  if (values==null)  {
              return null;
      }
  int count = values.length;
  String[] encodedValues = new String[count];
  for (int i = 0; i < count; i++) {
             encodedValues[i] = cleanXSS(values[i]);
   }
  return encodedValues;
}
public String getParameter(String parameter) {
      String value = super.getParameter(parameter);
      if (value == null) {
             return null;
              }
      return cleanXSS(value);
}
public String getHeader(String name) {
    String value = super.getHeader(name);
    if (value == null)
        return null;
    return cleanXSS(value);
}
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;
}

} }

<filter>
<filter-name>XSS</filter-name>
<display-name>XSS</display-name>
<description></description>
<filter-class>com.greatwebguy.filter.CrossScriptingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XSS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

the url from where i got this code http://greatwebguy.com/programming/java/simple-cross-site-scripting-xss-servlet-filter/ 我从那里获得此代码的网址http://greatwebguy.com/programming/java/simple-cross-site-scripting-xss-servlet-filter/

anyone can give me the tips to convert the code to asp.net & c# or write class from scratch which mimic the above functionality. 任何人都可以给我提示,以将代码转换为asp.net&c#,或者从头开始编写模仿上述功能的类。 thanks. 谢谢。

Cross-site scripting can be avoided easily by ensuring that any user submitted content is HTML encoded prior to being rendered in the page. 通过确保任何用户提交的内容在呈现在页面中之前都经过HTML编码,可以轻松避免跨站点脚本编写。 Usually this would be achieved by consistently using the <%: instead of the <%= ASPX tag to ensure that encoding is applied to the content. 通常,这可以通过始终使用<%:而不是<%= ASPX标记来实现,以确保将编码应用于内容。 Attempting to "clean" input is doomed to failure. 尝试“清除”输入注定会失败。 Rendering that input correctly is all that is required. 只需正确渲染该输入即可。

MSDN has a good article ' How to prevent Cross-Site scripting in ASP.NET ' MSDN上有一篇很好的文章“ 如何防止ASP.NET中跨站点脚本编写

You don't need half of that (Java) code to achieve the same result in asp.net. 您不需要这些(Java)代码的一半就可以在asp.net中获得相同的结果。

Edit: as per Spenders recommendation here's another link, this time for MVC 编辑:根据Spenders的建议,这是MVC的另一个链接

Note: I'd still read the first article though for understanding the principles. 注意:尽管如此,我仍然会读第一篇文章以了解这些原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM