gpt4 book ai didi

用于 XSS 保护的请求正文中的 Spring Boot 转义字符

转载 作者:行者123 更新时间:2023-12-05 06:36:00 25 4
gpt4 key购买 nike

我正在尝试使用这样的 XSSFilter 来保护我的 spring boot 应用程序:

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);
}

}

和包装器:

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

return encodedValues;
}

private String replaceXSSCharacters(String value) {
if (value == null) {
return null;
}

return value
.replace("&","&#38;")
.replace("<", "&#60;")
.replace(">","&#62;")
.replace("\"","&#34;")
.replace("'","&#39;");
}

@Override
public String getParameter(String parameter) {
return replaceXSSCharacters(super.getParameter(parameter));
}

@Override
public String getHeader(String name) {
return replaceXSSCharacters(super.getHeader(name));
}

}

问题是,它只保护请求参数和 header ,而不保护请求主体,有时我的 Controller 使用@RequestBody 接收数据。

所以,如果我向我的 Controller 提交一个像这样的 json:

{"name":"<script>alert('hello!')</script>"}

name 属性中的 html 字符没有像我需要的那样被转义。我怎样才能逃脱 RequestBody?

编辑:这与“重复”问题不同。我的问题非常具体。如何转义请求正文中的字符。

最佳答案

我用自定义类解决了:

@Configuration
public class AntiXSSConfig {

@Autowired()
public void configeJackson(ObjectMapper mapper) {
mapper.getFactory().setCharacterEscapes(new HTMLCharacterEscapes());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

public static class HTMLCharacterEscapes extends JsonpCharacterEscapes {

@Override
public int[] getEscapeCodesForAscii() {
int[] asciiEscapes = CharacterEscapes.standardAsciiEscapesForJSON();
// and force escaping of a few others:
asciiEscapes['<'] = CharacterEscapes.ESCAPE_CUSTOM;
asciiEscapes['>'] = CharacterEscapes.ESCAPE_CUSTOM;
asciiEscapes['&'] = CharacterEscapes.ESCAPE_CUSTOM;
asciiEscapes['"'] = CharacterEscapes.ESCAPE_CUSTOM;
asciiEscapes['\''] = CharacterEscapes.ESCAPE_CUSTOM;
return asciiEscapes;
}

@Override
public SerializableString getEscapeSequence(int ch) {
switch (ch) {
case '&' : return new SerializedString("&#38;");
case '<' : return new SerializedString("&#60;");
case '>' : return new SerializedString("&#62;");
case '\"' : return new SerializedString("&#34;");
case '\'' : return new SerializedString("&#39;");
default : return super.getEscapeSequence(ch);
}
}
}
}

它涵盖了所有情况。

关于用于 XSS 保护的请求正文中的 Spring Boot 转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49439020/

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