1. CrossScriptingFilter 필터 파일
2. RequestWrapper 필터링을 실행할 파일
3. web.xml 에서 세팅
1. 기본 컨트롤 클래스
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class CrossScriptingFilter implements Filter {
public FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void destroy() {
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(new RequestWrapper((HttpServletRequest) request), response);
}
}
2. 치환 동작 메서드
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class RequestWrapper extends HttpServletRequestWrapper {
public RequestWrapper(HttpServletRequest servletRequest) {
super(servletRequest);
}
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] = cleanXSS(values[i]);
}
return encodedValues;
}
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
if (value == null) {
return null;
}
return cleanXSS(value);
}
public String getHeader(String name) {
String value = super.getHeader(name);
if (value == null)
return null;
return cleanXSS(value);
}
private String cleanXSS(String value) {
//You'll need to remove the spaces from the html entities below
value = value.replaceAll("<", "& lt;").replaceAll(">", "& gt;");
/*value = value.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");*/
value = value.replaceAll("'", "& #39;");
value = value.replaceAll("eval\\((.*)\\)", "");
value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
value = value.replaceAll("script", "");
value = value.replaceAll(";", "& #59;");
value = value.replaceAll("-", "& #45;");
return value;
}
}
3.web.xml
<filter>
<filter-name>XSS</filter-name>
<filter-class>클래스파일 경로.CrossScriptingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XSS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
[참고]
https://epthffh.tistory.com/entry/XSS-필터크로스-사이트-스크립트Encoding-필터 [물고기 개발자의 블로그]
'DEV' 카테고리의 다른 글
FP(Function Point,기능점수) 작성 중 헷갈리던 것 정리 (0) | 2020.07.27 |
---|---|
[PDF.js] 웹페이지에서 PDF 열기 (0) | 2020.07.02 |
[eclipse] 심각: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener (0) | 2020.06.29 |
sitemesh 설정 (exclude 처리 포함) (0) | 2020.06.18 |
[PostgreSQL] 정규식으로 문자 제거 (0) | 2020.06.18 |