gpt4 book ai didi

spring - 在 Spring MVC Controller 中防止 XSS

转载 作者:行者123 更新时间:2023-12-04 14:27:49 24 4
gpt4 key购买 nike

您好,在我的项目中,Veracode 报告了一个 XSS 问题 CWE ID 80。在我的请求处理程序方法中:

@RequestMapping(value = "/Update.mvc")
public @ResponseBody String execute(@ModelAttribute UpdateForm updateForm, BindingResult result,
HttpServletRequest request, HttpServletResponse response) throws ActionException {
return executeAjax(updateForm, request, response, result);
}

所以 executeAjax 来自抽象类并且有不同的实现?在这些实现中,来自表单的用户输入被获取并被操纵以构造返回的字符串。

所以我的问题是:难道Veracode的假设是在实现中可以有XSS?还是一般的东西?- 如何防止这种情况?我总是使用转换输入数据,但它不会在用户输入时返回?- 那么如何预防呢?- 我是否必须从 HttpServiceRequest 中转义所有 header /请求参数?

编辑:我是否必须使用以下过滤器: SecurityWrapperRequest

最佳答案

您可以使用 XSSFilter 来转义所有请求参数。参见 here

public class XSSFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void destroy() {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(new XSSRequestWrapper((HttpServletRequest) request), response);
}

}

和包装器

import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class XSSRequestWrapper extends HttpServletRequestWrapper {

public XSSRequestWrapper(HttpServletRequest servletRequest) {
super(servletRequest);
}

@Override
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] = stripXSS(values[i]);
}

return encodedValues;
}

@Override
public String getParameter(String parameter) {
String value = super.getParameter(parameter);

return stripXSS(value);
}

@Override
public String getHeader(String name) {
String value = super.getHeader(name);
return stripXSS(value);
}

private String stripXSS(String value) {
if (value != null) {
// NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to
// avoid encoded attacks.
// value = ESAPI.encoder().canonicalize(value);

// Avoid null characters
value = value.replaceAll("", "");

// Avoid anything between script tags
Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");

// Avoid anything in a src='...' type of expression
scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");

scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");

// Remove any lonesome </script> tag
scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");

// Remove any lonesome <script ...> tag
scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");

// Avoid eval(...) expressions
scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");

// Avoid expression(...) expressions
scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");

// Avoid javascript:... expressions
scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");

// Avoid vbscript:... expressions
scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");

// Avoid onload= expressions
scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");
}
return value;
}
}

实际上,您可以将过滤器用作地下室并扩展它以向包装器添加任何所需的逻辑。

关于spring - 在 Spring MVC Controller 中防止 XSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41938010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com