防止XSS脚本注入-前端、后端
作者 | 时间 |
---|---|
雨中星辰 | 2020-09-10 |
xss是什么
跨站脚本攻击(XSS),是目前最普遍的Web应用安全漏洞。这类漏洞能够使得攻击者嵌入恶意脚本代码到正常用户会访问到的页面中,当正常用户访问该页面时,则可导致嵌入的恶意脚本代码的执行,从而达到恶意攻击用户的目的。
xss脚本注入演示
- 通过表单,先添加一个数据,其中一条数据为脚本
- 刷新页面
从截图看,注入的脚本已经执行了。
防止xss脚本注入的原理
将参数中的特殊字符进行转换
处理前为:
<script>alert(1);</script>
处理后为:
<script>alert(1);</script>
后台springboot 防止xss注入配置
-
添加依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.8</version> </dependency>
- 添加xss过滤器
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
//用于将html参数转义
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
public XssHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
}
@Override
public String getQueryString() {
return StringEscapeUtils.escapeHtml4(super.getQueryString());
}
@Override
public String getParameter(String name) {
return StringEscapeUtils.escapeHtml4(super.getParameter(name));
}
@Override
public String[] getParameterValues(String name) {
String[] values = super.getParameterValues(name);
if (ArrayUtils.isEmpty(values)) {
return values;
}
int length = values.length;
String[] escapeValues = new String[length];
for (int i = 0; i < length; i++) {
escapeValues[i] = StringEscapeUtils.escapeHtml4(values[i]);
}
return escapeValues;
}
}
- JSON字符串请求参数处理
实现Jackson反序列化方法,将参数值转义处理
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringEscapeUtils;
import java.io.IOException;
public class XssJacksonDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
return StringEscapeUtils.escapeHtml4(jsonParser.getText());
}
}
- SON字符串响应结果处理
实现Jackson序列化方法,将参数值转义处理
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import java.io.IOException;
@Slf4j
public class XssJacksonSerializer extends JsonSerializer<String> {
@Override
public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(StringEscapeUtils.escapeHtml4(s));
}
}
- xxs拦截器
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Slf4j
@WebFilter(filterName = "xssFilter", urlPatterns = "/*", asyncSupported = true)
public class XssFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
XssHttpServletRequestWrapper xssHttpServletRequestWrapper = new XssHttpServletRequestWrapper(request);
filterChain.doFilter(xssHttpServletRequestWrapper, servletResponse);
}
/**
* springboot默认的json库为jackson,将其配置xss
* @param builder
* @return
*/
@Bean
@Primary
public ObjectMapper xssObjectMapper(Jackson2ObjectMapperBuilder builder) {
//解析器
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
//注册xss解析器
SimpleModule xssModule = new SimpleModule("XssStringJsonSerializer");
xssModule.addSerializer(new XssJacksonSerializer());
xssModule.addDeserializer(String.class,new XssJacksonDeserializer());
objectMapper.registerModule(xssModule);
//返回
return objectMapper;
}
}
- 启动类添加@ServletComponentScan注解,扫描使用servlet注解的类,启用 XssFilter
前端vue 防止xss脚本注入
vue防止xss注入
- 尽量使用插值表达式
{{}}
,它会把要显示的内容转为字符串。 - 如果使用v-html,要保证来自服务端的渲染数据都是安全的。
- 在使用第三方UI组件库的的时候,要检查一下它们渲染页面的方式,是否使用了v-html
引用:
https://springboot.plus/guide/xss.html#%E5%90%8E%E5%8F%B0%E5%A4%84%E7%90%86