前台RequestPayload传值 后台RequestBody 接收不到
问题描述 字段值为standard_model的药具规格 后台接受值为null
问题定位:xss拦截
scriptPattern = Pattern.compile("and", Pattern.CASE_INSENSITIVE);
input = scriptPattern.matcher(input).replaceAll("");
后台进行了xss拦截 过滤器的原因 对and字符进行了过滤 导致参数standard_model变成了stard_model
由此后台取值出现问题
此类问题在之后漏洞修复的时候要注意
附:正确解决方法
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
HttpServletRequest orgRequest = null;
private boolean isIncludeRichText = false;
public static Pattern pattern_select = Pattern.compile("select",Pattern.CASE_INSENSITIVE);
public static Pattern pattern_insert = Pattern.compile("insert",Pattern.CASE_INSENSITIVE);
public static Pattern pattern_update = Pattern.compile("update",Pattern.CASE_INSENSITIVE);
public static Pattern pattern_drop = Pattern.compile("drop",Pattern.CASE_INSENSITIVE);
public static Pattern pattern_delete = Pattern.compile("delete",Pattern.CASE_INSENSITIVE);
public static Pattern pattern_truncate = Pattern.compile("truncate",Pattern.CASE_INSENSITIVE);
public static Pattern pattern_or = Pattern.compile("or",Pattern.CASE_INSENSITIVE);
public static Pattern pattern_and = Pattern.compile("and",Pattern.CASE_INSENSITIVE);
public static Pattern pattern_script = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
public static Pattern pattern_src = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public XssHttpServletRequestWrapper(HttpServletRequest request, boolean isIncludeRichText) {
super(request);
orgRequest = request;
this.isIncludeRichText = isIncludeRichText;
}
@Override
public ServletInputStream getInputStream() throws IOException {
if (!super.getHeader("Content-Type").equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE)) {
return super.getInputStream();
}
String json = IOUtils.toString(super.getInputStream(), "utf-8");
if (StringUtils.isBlank(json)) {
return super.getInputStream();
}
json = xssEncode(json,0);
final ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes());
return new ServletInputStream() {
@Override
public boolean isFinished() {
return true;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
return bis.read();
}
};
}
@Override
public String getParameter(String name) {
Boolean flag = ("content".equals(name) || name.endsWith("WithHtml"));
if (flag && !isIncludeRichText) {
return super.getParameter(name);
}
name = JsoupUtil.clean(name);
String value = super.getParameter(name);
if (StringUtils.isNotBlank(value)) {
value = xssEncode(value,1);
}
return value;
}
@Override
public String[] getParameterValues(String name) {
String[] arr = super.getParameterValues(name);
if (arr != null) {
for (int i = 0; i < arr.length; i++) {
arr[i] = xssEncode(arr[i],1);
}
}
return arr;
}
@Override
public String getHeader(String name) {
name = JsoupUtil.clean(name);
String value = super.getHeader(name);
if (StringUtils.isNotBlank(value)) {
value = xssEncode(value,0);
}
return value;
}
private String xssEncode(String input,Integer type) {
input = pattern_script.matcher(input).replaceAll("");
input = pattern_src.matcher(input).replaceAll("");
Pattern scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("<script(.*?)>",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("eval\\((.*?)\\)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("expression\\((.*?)\\)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("onload(.*?)=",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("<svg(.*?)>",Pattern.CASE_INSENSITIVE);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("onerror(.*?)=",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("alert\\((.*?)\\)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
scriptPattern = Pattern.compile("prompt\\((.*?)\\)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
if(type==1){
scriptPattern = Pattern.compile("[`~!@#$%^&*()+=|''\\\\[\\\\]<>/?~!@#¥%……&*()——+|‘;:”“’。,、?]",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
input = scriptPattern.matcher(input).replaceAll("");
input = pattern_and.matcher(input).replaceAll("");
input = pattern_or.matcher(input).replaceAll("");
input = pattern_delete.matcher(input).replaceAll("");
input = pattern_drop.matcher(input).replaceAll("");
input = pattern_select.matcher(input).replaceAll("");
input = pattern_insert.matcher(input).replaceAll("");
input = pattern_update.matcher(input).replaceAll("");
}
return input;
}
public HttpServletRequest getOrgRequest() {
return orgRequest;
}
public static HttpServletRequest getOrgRequest(HttpServletRequest req) {
if (req instanceof XssHttpServletRequestWrapper) {
return ((XssHttpServletRequestWrapper) req).getOrgRequest();
}
return req;
}
}
|