△/SecureCoding
크로스 사이트 스크립트 공격 취약점: DOM
KkingKkang
2022. 11. 18. 13:51
728x90
- 외부에서 입력되는 스크립트 문자열이 웹페이지 생성에 사용되면 생성된 웹페이지를 열람하는 사용자에게 피해를 입힐 수 있다.
- JSP의 document.write()메소드와 같이 JSP의 DOM 객체 출력을 수행하는 메소드의 인자값으로 외부의 입력을 사용할 경우 위험한 문자를 제거하는 과정이 수행되어야 한다.
1. 특수 문자 치환
String name = request.getParameter("name");
if( name != null ) {
name = name.replaceAll("<","<");
name = name.replaceAll(">",">");
name = name.replaceAll("&","&");
name = name.replaceAll("\"",""");
name = name.replaceAll("\'","'");
name = name.replaceAll("/"","/");
} else {
return;
}
2. owasp 에서 제공하는 esapi 함수 중 ESAPI.encoder().encodeForHTMLAttribute를 이용하여 외부에서 입력받은 인자값을 검증 후 사용
String eid = request.getParameter("eid");
String safeEID = ESAPI.encoder().encodeForHTMLAttribute(eid);
3. HTMLInputFilter().filter 메소드를 이용해 문자열을 필터링하고 있다.
import com.josephoconnell.html.HTMLInputFilter;
....
private String filter(String input) {
String clean = new HTMLInputFilter().filter(input);
return clean;
}
http://josephoconnell.com/java/xss-html-filter/
XSS HTML Filter: A Java library for protecting against cross site scripting
HTML filtering utility for Java This utility is a single class, HTMLInputFilter, which can be used to parse user-submitted input and sanitize it against potential cross site scripting attacks, malicious html, or simply badly formed html. This version, writ
josephoconnell.com
728x90