前言
Spring XSS 过滤器是用于防御 跨站脚本攻击(Cross-Site Scripting, XSS) 的核心安全组件。它工作在 Web 请求到达控制器之前和响应离开控制器之后,对数据进行净化和转义。
核心作用:
-
拦截恶意输入: 在 HTTP 请求到达应用程序控制器之前,扫描请求参数(
GET
,POST
)、请求头(如User-Agent
,Referer
)、Cookies 和请求体(如application/x-www-form-urlencoded
,multipart/form-data
,application/json
)中的内容。 -
净化/转义输入: 识别并清除或转义请求数据中包含的潜在恶意脚本代码(如
<script>
,javascript:
,onerror=
,eval(
等)。目的是将危险的 HTML/JS 标签和属性转换为安全的文本表示,使其在浏览器中仅作为纯文本显示,而不会被解析执行。 -
净化/转义输出(可选): 某些高级过滤器也能在 HTTP 响应发送回客户端之前,对响应内容(如动态生成的 HTML、JSON 中的字符串值)进行转义处理,提供另一层防御(但通常更推荐在视图层处理输出转义)。
-
防止恶意脚本注入: 通过上述处理,有效阻止攻击者将恶意脚本注入到网页中。这些脚本一旦被其他用户浏览器加载执行,可能导致会话劫持、敏感数据窃取、页面篡改、重定向到钓鱼网站等严重后果。
创建过滤器
public class XssFilter implements Filter {
public void init(FilterConfig config)
throws ServletException
{}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper((HttpServletRequest)request);
chain.doFilter(xssRequest, response);
}
public void destroy() {}
}
public class XssHttpServletRequestWrapper
extends HttpServletRequestWrapper
{
HttpServletRequest orgRequest;
private static final HTMLFilter htmlFilter = new HTMLFilter();
public XssHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
this.orgRequest = request;
}
public ServletInputStream getInputStream() throws IOException {
if (!"application/json".equalsIgnoreCase(super.getHeader("Content-Type"))) {
return super.getInputStream();
}
String json = IOUtils.toString(super.getInputStream(), "utf-8");
if (StringUtils.isBlank(json)) {
return super.getInputStream();
}
json = xssEncode(json);
final ByteArrayInputStream bais = new ByteArrayInputStream(json.getBytes("utf-8"));
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read(){
return bais.read();
}
};
}
public String getParameter(String name)
{
String value = super.getParameter(xssEncode(name));
if (StringUtils.isNotBlank(value)) {
value = xssEncode(value);
}
return value;
}
public String[] getParameterValues(String name)
{
String[] parameters = super.getParameterValues(name);
if ((parameters == null) || (parameters.length == 0)) {
return null;
}
for (int i = 0; i < parameters.length; i++) {
parameters[i] = xssEncode(parameters[i]);
}
return parameters;
}
public Map<String, String[]> getParameterMap()
{
Map<String, String[]> map = new LinkedHashMap();
Map<String, String[]> parameters = super.getParameterMap();
for (String key : parameters.keySet())
{
String[] values = (String[])parameters.get(key);
for (int i = 0; i < values.length; i++) {
values[i] = xssEncode(values[i]);
}
map.put(key, values);
}
return map;
}
public String getHeader(String name)
{
String value = super.getHeader(xssEncode(name));
if (StringUtils.isNotBlank(value)) {
value = xssEncode(value);
}
return value;
}
public HttpServletRequest getOrgRequest()
{
return this.orgRequest;
}
public static HttpServletRequest getOrgRequest(HttpServletRequest request)
{
if ((request instanceof XssHttpServletRequestWrapper)) {
return ((XssHttpServletRequestWrapper)request).getOrgRequest();
}
return request;
}
private String xssEncode(String s)
{
if ((s == null) || (s.isEmpty())) {
return s;
}
StringBuilder sb = new StringBuilder(s.length() + 16);
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
switch (c)
{
case '>':
sb.append(65310);
break;
case '<':
sb.append(65308);
break;
default:
sb.append(c);
}
}
return htmlFilter.filter(sb.toString());
}
public void processUrlEncoder(StringBuilder sb, String s, int index)
{
if (s.length() >= index + 2)
{
if ((s.charAt(index + 1) == '3') && ((s.charAt(index + 2) == 'c') || (s.charAt(index + 2) == 'C')))
{
sb.append(65308);
return;
}
if ((s.charAt(index + 1) == '6') && (s.charAt(index + 2) == '0'))
{
sb.append(65308);
return;
}
if ((s.charAt(index + 1) == '3') && ((s.charAt(index + 2) == 'e') || (s.charAt(index + 2) == 'E')))
{
sb.append(65310);
return;
}
if ((s.charAt(index + 1) == '6') && (s.charAt(index + 2) == '2'))
{
sb.append(65310);
return;
}
}
sb.append(s.charAt(index));
}
}
Html过滤
public final class HTMLFilter
{
private static final int REGEX_FLAGS_SI = 34;
private static final Pattern P_COMMENTS = Pattern.compile("<!--(.*?)-->", 32);
private static final Pattern P_COMMENT = Pattern.compile("^!--(.*)--$", 34);
private static final Pattern P_TAGS = Pattern.compile("<(.*?)>", 32);
private static final Pattern P_END_TAG = Pattern.compile("^/([a-z0-9]+)", 34);
private static final Pattern P_START_TAG = Pattern.compile("^([a-z0-9]+)(.*?)(/?)$", 34);
private static final Pattern P_QUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)=([\"'])(.*?)\\2", 34);
private static final Pattern P_UNQUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)(=)([^\"\\s']+)", 34);
private static final Pattern P_PROTOCOL = Pattern.compile("^([^:]+):", 34);
private static final Pattern P_ENTITY = Pattern.compile("&#(\\d+);?");
private static final Pattern P_ENTITY_UNICODE = Pattern.compile("&#x([0-9a-f]+);?");
private static final Pattern P_ENCODE = Pattern.compile("%([0-9a-f]{2});?");
private static final Pattern P_VALID_ENTITIES = Pattern.compile("&([^&;]*)(?=(;|&|$))");
private static final Pattern P_VALID_QUOTES = Pattern.compile("(>|^)([^<]+?)(<|$)", 32);
private static final Pattern P_END_ARROW = Pattern.compile("^>");
private static final Pattern P_BODY_TO_END = Pattern.compile("<([^>]*?)(?=<|$)");
private static final Pattern P_XML_CONTENT = Pattern.compile("(^|>)([^<]*?)(?=>)");
private static final Pattern P_STRAY_LEFT_ARROW = Pattern.compile("<([^>]*?)(?=<|$)");
private static final Pattern P_STRAY_RIGHT_ARROW = Pattern.compile("(^|>)([^<]*?)(?=>)");
private static final Pattern P_AMP = Pattern.compile("&");
private static final Pattern P_QUOTE = Pattern.compile("<");
private static final Pattern P_LEFT_ARROW = Pattern.compile("<");
private static final Pattern P_RIGHT_ARROW = Pattern.compile(">");
private static final Pattern P_BOTH_ARROWS = Pattern.compile("<>");
private static final ConcurrentMap<String, Pattern> P_REMOVE_PAIR_BLANKS = new ConcurrentHashMap();
private static final ConcurrentMap<String, Pattern> P_REMOVE_SELF_BLANKS = new ConcurrentHashMap();
private final Map<String, List<String>> vAllowed;
private final Map<String, Integer> vTagCounts = new HashMap();
private final String[] vSelfClosingTags;
private final String[] vNeedClosingTags;
private final String[] vDisallowed;
private final String[] vProtocolAtts;
private final String[] vAllowedProtocols;
private final String[] vRemoveBlanks;
private final String[] vAllowedEntities;
private final boolean stripComment;
private final boolean encodeQuotes;
private boolean vDebug = false;
private final boolean alwaysMakeTags;
public HTMLFilter()
{
this.vAllowed = new HashMap();
ArrayList<String> a_atts = new ArrayList();
a_atts.add("href");
a_atts.add("target");
this.vAllowed.put("a", a_atts);
ArrayList<String> img_atts = new ArrayList();
img_atts.add("src");
img_atts.add("width");
img_atts.add("height");
img_atts.add("alt");
this.vAllowed.put("img", img_atts);
ArrayList<String> no_atts = new ArrayList();
this.vAllowed.put("b", no_atts);
this.vAllowed.put("strong", no_atts);
this.vAllowed.put("i", no_atts);
this.vAllowed.put("em", no_atts);
this.vSelfClosingTags = new String[] { "img" };
this.vNeedClosingTags = new String[] { "a", "b", "strong", "i", "em" };
this.vDisallowed = new String[0];
this.vAllowedProtocols = new String[] { "http", "mailto", "https" };
this.vProtocolAtts = new String[] { "src", "href" };
this.vRemoveBlanks = new String[] { "a", "b", "strong", "i", "em" };
this.vAllowedEntities = new String[] { "amp", "gt", "lt", "quot" };
this.stripComment = true;
this.encodeQuotes = true;
this.alwaysMakeTags = true;
}
public HTMLFilter(boolean debug)
{
this();
this.vDebug = debug;
}
public HTMLFilter(Map<String, Object> conf)
{
assert (conf.containsKey("vAllowed")) : "configuration requires vAllowed";
assert (conf.containsKey("vSelfClosingTags")) : "configuration requires vSelfClosingTags";
assert (conf.containsKey("vNeedClosingTags")) : "configuration requires vNeedClosingTags";
assert (conf.containsKey("vDisallowed")) : "configuration requires vDisallowed";
assert (conf.containsKey("vAllowedProtocols")) : "configuration requires vAllowedProtocols";
assert (conf.containsKey("vProtocolAtts")) : "configuration requires vProtocolAtts";
assert (conf.containsKey("vRemoveBlanks")) : "configuration requires vRemoveBlanks";
assert (conf.containsKey("vAllowedEntities")) : "configuration requires vAllowedEntities";
this.vAllowed = Collections.unmodifiableMap((HashMap)conf.get("vAllowed"));
this.vSelfClosingTags = ((String[])conf.get("vSelfClosingTags"));
this.vNeedClosingTags = ((String[])conf.get("vNeedClosingTags"));
this.vDisallowed = ((String[])conf.get("vDisallowed"));
this.vAllowedProtocols = ((String[])conf.get("vAllowedProtocols"));
this.vProtocolAtts = ((String[])conf.get("vProtocolAtts"));
this.vRemoveBlanks = ((String[])conf.get("vRemoveBlanks"));
this.vAllowedEntities = ((String[])conf.get("vAllowedEntities"));
this.stripComment = (conf.containsKey("stripComment") ? ((Boolean)conf.get("stripComment")).booleanValue() : true);
this.encodeQuotes = (conf.containsKey("encodeQuotes") ? ((Boolean)conf.get("encodeQuotes")).booleanValue() : true);
this.alwaysMakeTags = (conf.containsKey("alwaysMakeTags") ? ((Boolean)conf.get("alwaysMakeTags")).booleanValue() : true);
}
private void reset()
{
this.vTagCounts.clear();
}
private void debug(String msg)
{
if (this.vDebug) {
Logger.getAnonymousLogger().info(msg);
}
}
public static String chr(int decimal)
{
return String.valueOf((char)decimal);
}
public static String htmlSpecialChars(String s)
{
String result = s;
result = regexReplace(P_AMP, "&", result);
result = regexReplace(P_QUOTE, """, result);
result = regexReplace(P_LEFT_ARROW, "<", result);
result = regexReplace(P_RIGHT_ARROW, ">", result);
return result;
}
public String filter(String input)
{
reset();
String s = input;
debug("************************************************");
debug(" INPUT: " + input);
s = escapeComments(s);
debug(" escapeComments: " + s);
s = balanceHTML(s);
debug(" balanceHTML: " + s);
s = checkTags(s);
debug(" checkTags: " + s);
s = processRemoveBlanks(s);
debug("processRemoveBlanks: " + s);
s = validateEntities(s);
debug(" validateEntites: " + s);
debug("************************************************\n\n");
return s;
}
public boolean isAlwaysMakeTags()
{
return this.alwaysMakeTags;
}
public boolean isStripComments()
{
return this.stripComment;
}
private String escapeComments(String s)
{
Matcher m = P_COMMENTS.matcher(s);
StringBuffer buf = new StringBuffer();
if (m.find())
{
String match = m.group(1);
m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->"));
}
m.appendTail(buf);
return buf.toString();
}
private String balanceHTML(String s)
{
if (this.alwaysMakeTags)
{
s = regexReplace(P_END_ARROW, "", s);
s = regexReplace(P_BODY_TO_END, "<$1>", s);
s = regexReplace(P_XML_CONTENT, "$1<$2", s);
}
else
{
s = regexReplace(P_STRAY_LEFT_ARROW, "<$1", s);
s = regexReplace(P_STRAY_RIGHT_ARROW, "$1$2><", s);
s = regexReplace(P_BOTH_ARROWS, "", s);
}
return s;
}
private String checkTags(String s)
{
Matcher m = P_TAGS.matcher(s);
StringBuffer buf = new StringBuffer();
String replaceStr;
while (m.find())
{
replaceStr = m.group(1);
replaceStr = processTag(replaceStr);
m.appendReplacement(buf, Matcher.quoteReplacement(replaceStr));
}
m.appendTail(buf);
s = buf.toString();
for (String key : this.vTagCounts.keySet()) {
for (int ii = 0; ii < ((Integer)this.vTagCounts.get(key)).intValue(); ii++) {
s = s + "</" + key + ">";
}
}
return s;
}
private String processRemoveBlanks(String s)
{
String result = s;
for (String tag : this.vRemoveBlanks)
{
if (!P_REMOVE_PAIR_BLANKS.containsKey(tag)) {
P_REMOVE_PAIR_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?></" + tag + ">"));
}
result = regexReplace((Pattern)P_REMOVE_PAIR_BLANKS.get(tag), "", result);
if (!P_REMOVE_SELF_BLANKS.containsKey(tag)) {
P_REMOVE_SELF_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?/>"));
}
result = regexReplace((Pattern)P_REMOVE_SELF_BLANKS.get(tag), "", result);
}
return result;
}
private static String regexReplace(Pattern regex_pattern, String replacement, String s)
{
Matcher m = regex_pattern.matcher(s);
return m.replaceAll(replacement);
}
private String processTag(String s)
{
Matcher m = P_END_TAG.matcher(s);
if (m.find())
{
String name = m.group(1).toLowerCase();
if ((allowed(name)) &&
(!inArray(name, this.vSelfClosingTags)) &&
(this.vTagCounts.containsKey(name)))
{
this.vTagCounts.put(name, Integer.valueOf(((Integer)this.vTagCounts.get(name)).intValue() - 1));
return "</" + name + ">";
}
}
m = P_START_TAG.matcher(s);
if (m.find())
{
String name = m.group(1).toLowerCase();
String body = m.group(2);
String ending = m.group(3);
if (allowed(name))
{
String params = "";
Matcher m2 = P_QUOTED_ATTRIBUTES.matcher(body);
Matcher m3 = P_UNQUOTED_ATTRIBUTES.matcher(body);
List<String> paramNames = new ArrayList();
List<String> paramValues = new ArrayList();
while (m2.find())
{
paramNames.add(m2.group(1));
paramValues.add(m2.group(3));
}
while (m3.find())
{
paramNames.add(m3.group(1));
paramValues.add(m3.group(3));
}
for (int ii = 0; ii < paramNames.size(); ii++)
{
String paramName = ((String)paramNames.get(ii)).toLowerCase();
String paramValue = (String)paramValues.get(ii);
if (allowedAttribute(name, paramName))
{
if (inArray(paramName, this.vProtocolAtts)) {
paramValue = processParamProtocol(paramValue);
}
params = params + " " + paramName + "=\"" + paramValue + "\"";
}
}
if (inArray(name, this.vSelfClosingTags)) {
ending = " /";
}
if (inArray(name, this.vNeedClosingTags)) {
ending = "";
}
if ((ending == null) || (ending.length() < 1))
{
if (this.vTagCounts.containsKey(name)) {
this.vTagCounts.put(name, Integer.valueOf(((Integer)this.vTagCounts.get(name)).intValue() + 1));
} else {
this.vTagCounts.put(name, Integer.valueOf(1));
}
}
else {
ending = " /";
}
return "<" + name + params + ending + ">";
}
return "";
}
m = P_COMMENT.matcher(s);
if ((!this.stripComment) && (m.find())) {
return "<" + m.group() + ">";
}
return "";
}
private String processParamProtocol(String s)
{
s = decodeEntities(s);
Matcher m = P_PROTOCOL.matcher(s);
if (m.find())
{
String protocol = m.group(1);
if (!inArray(protocol, this.vAllowedProtocols))
{
s = "#" + s.substring(protocol.length() + 1, s.length());
if (s.startsWith("#//")) {
s = "#" + s.substring(3, s.length());
}
}
}
return s;
}
private String decodeEntities(String s)
{
StringBuffer buf = new StringBuffer();
Matcher m = P_ENTITY.matcher(s);
while (m.find())
{
String match = m.group(1);
int decimal = Integer.decode(match).intValue();
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
}
m.appendTail(buf);
s = buf.toString();
buf = new StringBuffer();
m = P_ENTITY_UNICODE.matcher(s);
while (m.find())
{
String match = m.group(1);
int decimal = Integer.valueOf(match, 16).intValue();
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
}
m.appendTail(buf);
s = buf.toString();
buf = new StringBuffer();
m = P_ENCODE.matcher(s);
while (m.find())
{
String match = m.group(1);
int decimal = Integer.valueOf(match, 16).intValue();
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
}
m.appendTail(buf);
s = buf.toString();
s = validateEntities(s);
return s;
}
private String validateEntities(String s)
{
StringBuffer buf = new StringBuffer();
Matcher m = P_VALID_ENTITIES.matcher(s);
while (m.find())
{
String one = m.group(1);
String two = m.group(2);
m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two)));
}
m.appendTail(buf);
return encodeQuotes(buf.toString());
}
private String encodeQuotes(String s)
{
if (this.encodeQuotes)
{
StringBuffer buf = new StringBuffer();
Matcher m = P_VALID_QUOTES.matcher(s);
while (m.find())
{
String one = m.group(1);
String two = m.group(2);
String three = m.group(3);
m.appendReplacement(buf, Matcher.quoteReplacement(one + regexReplace(P_QUOTE, """, two) + three));
}
m.appendTail(buf);
return buf.toString();
}
return s;
}
private String checkEntity(String preamble, String term)
{
return "&" + preamble;
}
private boolean isValidEntity(String entity)
{
return inArray(entity, this.vAllowedEntities);
}
private static boolean inArray(String s, String[] array)
{
for (String item : array) {
if ((item != null) && (item.equals(s))) {
return true;
}
}
return false;
}
private boolean allowed(String name)
{
return ((this.vAllowed.isEmpty()) || (this.vAllowed.containsKey(name))) && (!inArray(name, this.vDisallowed));
}
private boolean allowedAttribute(String name, String paramName)
{
return (allowed(name)) && ((this.vAllowed.isEmpty()) || (((List)this.vAllowed.get(name)).contains(paramName)));
}
}
项目如果使用的富文本编辑器,记得接收后转回去
public static String decodeXXS(String html){
html = html.replaceAll("65308","<");
html = html.replaceAll("65310",">");
html = html.replaceAll("&","&");
html = html.replaceAll(""","<");
html = html.replaceAll("<","<");
html = html.replaceAll(">",">");
return html;
}
SpringMvc 在web.xml配置过滤器
<!-- Xss -->
<filter>
<filter-name>XssFilter</filter-name>
<filter-class>com.qc.config.filter.XssFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XssFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Springboot
@Configuration
public class WebSecurityConfig {
@Bean
public FilterRegistrationBean<XssFilter> xssFilterRegistration() {
FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new XssFilter());
registration.addUrlPatterns("/*"); // 过滤所有请求,根据需要调整
registration.setName("xssFilter");
registration.setOrder(1); // 设置优先级,确保在Spring Security等过滤器之前
return registration;
}
}