Created
May 6, 2014 11:58
-
-
Save anonymous/0045ef4df99b31b43daa to your computer and use it in GitHub Desktop.
Improved workaround for struts exploit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ParamFilter implements Filter { | |
private Pattern pattern; | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { | |
pattern = Pattern.compile(filterConfig.getInitParameter("excludeParams")); | |
} | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | |
chain.doFilter(new ParamFilteredRequest(request, pattern), response); | |
} | |
@Override | |
public void destroy() { | |
} | |
/** */ | |
private static class ParamFilteredRequest extends HttpServletRequestWrapper { | |
private final Pattern pattern; | |
public ParamFilteredRequest(ServletRequest request, Pattern pattern) { | |
super((HttpServletRequest)request); | |
this.pattern = pattern; | |
} | |
@Override | |
public Enumeration<String> getParameterNames() { | |
List<String> requestParameterNames = Collections.list(super.getParameterNames()); | |
List<String> finalParameterNames = new ArrayList<>(); | |
for (String parameterName: requestParameterNames) { | |
if (!pattern.matcher(parameterName).matches()) { | |
finalParameterNames.add(parameterName); | |
} | |
} | |
return Collections.enumeration(finalParameterNames); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment