I am trying to add custom XSS protection by creating a RequestWrapper extending HttpServletRequestWrapper in an XSS filter. The below code provides XSS protection to: 1. Request Params 2. Payload.
public class XssRequestWrapper extends HttpServletRequestWrapper {
XssRequestWrapper(HttpServletRequest request) {
super(request);
}
@Override
public String getQueryString() {
/*
Custom XSS logic
*/
}
@Override
public String getParameterMap() {
/*
Custom XSS logic
*/
}
@Override
public String getParameterValues() {
/*
Custom XSS logic
*/
}
}
But when I configure my REST Api with @RequestBody Annotation in my controller, the overridden getParameterValues is not invoked. Instead, getInputStream is invoked, which results in the addition of the following:
@Override
public ServletInputStream getInputStream() throws IOException {
/*
Custom XSS logic
*/
}
Is there any better/ideal way to provide XSS protection to data passed via @RequestBody annotation?
Edit: Solution: https://www.baeldung.com/spring-reading-httpservletrequest-multiple-times Since I was using ContentCachingRequestWrapper in one of my previous filters, I was unable to use the same and hence went forward with the above solution. By Caching the request, I was able to read it multiple times and perform XSS check over the cached content.