xss攻擊的修復方案:
1.對輸入的數據進行HTML轉義,使其不會識別為可執行腳本,例如:
String result = HtmlUtils.htmlEscape(source);
2.根據白名單的標簽和屬性對數據進行過濾,對可執行的腳本進行清除,例如:
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
String result = Jsoup.clean(source, Whitelist.basic());
//默認的基礎白名單配置
public static Whitelistbasic(){
return new Whitelist()
.addTags(
"a","b","blockquote","br","cite","code","dd","dL","dt","em",
"i","Li","oL","p","pre","q","small","span","strike","strong","sub"
"sup","U","uL")
.addAttributes(tag:"a", ...attributes:"href")
.addAttributes(tag:”bLockquote", ...attributes:"cite")
.addAttributes(tag:"q", ...attritoutes:"cite")
.addProtocols(tag:"a", attribute:"href", ...potocols:"ftp", "http", "https", "mailto")
.addProtocols(tag:"bLockquote", attribute:"cite", ...protocolo:"http", "https")
.addProtocols(tag:"cite",attribute:"cite", ...protocols:"http", "https")
.addEnforcedAttribute(tag:"a",attribute:"rel",value:"nofollow")
;
}