HTML Sanitizer 기능 추가

This commit is contained in:
KNKIM 2021-12-21 15:41:16 +09:00
parent d035e8e056
commit 11b8e77034
2 changed files with 36 additions and 0 deletions

View File

@ -385,6 +385,13 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>3.3.2</version> <version>3.3.2</version>
</dependency> </dependency>
<!-- OWASP HTML Sanitizer -->
<dependency>
<groupId>com.googlecode.owasp-java-html-sanitizer</groupId>
<artifactId>owasp-java-html-sanitizer</artifactId>
<version>20211018.2</version>
</dependency>
</dependencies> </dependencies>

View File

@ -13,6 +13,8 @@ import java.util.Base64.Encoder;
import java.util.Locale; import java.util.Locale;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.owasp.html.HtmlPolicyBuilder;
import org.owasp.html.PolicyFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -383,6 +385,33 @@ public class StringUtil extends StringUtils {
return value; return value;
} }
/**
* XSS 등의 공격으로 부터 보안성 유지를 위해서 HTML 허용된 태그와 속성만으로 HTML을 재구성하여 리턴한다.
*
* @param html
* @return
*/
private static String sanitizeHtml(String html) {
PolicyFactory policy = new HtmlPolicyBuilder()
.allowAttributes("src", "align", "title").onElements("img")
.allowAttributes("href", "title").onElements("a")
.allowAttributes("class", "height", "width", "style").globally()
.allowUrlProtocols("http","https","mailto","tel")
.allowElements(
"a", "label",
"h1", "h2", "h3", "h4", "h5", "h6",
"p", "i", "b", "u", "strong", "em", "small", "big", "pre", "code",
"cite", "samp", "sub", "sup", "strike", "center", "blockquote",
"hr", "br", "col", "font", "span", "div", "img",
"ul", "ol", "li", "dd", "dt", "dl", "tbody", "thead", "tfoot",
"table", "td", "th", "tr", "colgroup", "fieldset", "legend"
)
.toFactory();
return policy.sanitize(html);
}
// Tag 화이트 리스트 ( 허용할 태그 등록 ) // Tag 화이트 리스트 ( 허용할 태그 등록 )
static String[] whiteListTag = { "<p>","</p>","<br />" }; static String[] whiteListTag = { "<p>","</p>","<br />" };