Commit c943233ee56d00d74d41a3fa8dbf85626f10cbdd
1 parent
d636391a
bugfix:修复 Xss 开启时,忽略的 URL 针对 RequestBody 还是会 XSS 过滤的问题
Showing
2 changed files
with
27 additions
and
2 deletions
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/xss/config/YudaoXssAutoConfiguration.java
| @@ -44,9 +44,11 @@ public class YudaoXssAutoConfiguration implements WebMvcConfigurer { | @@ -44,9 +44,11 @@ public class YudaoXssAutoConfiguration implements WebMvcConfigurer { | ||
| 44 | @ConditionalOnMissingBean(name = "xssJacksonCustomizer") | 44 | @ConditionalOnMissingBean(name = "xssJacksonCustomizer") |
| 45 | @ConditionalOnBean(ObjectMapper.class) | 45 | @ConditionalOnBean(ObjectMapper.class) |
| 46 | @ConditionalOnProperty(value = "yudao.xss.enable", havingValue = "true") | 46 | @ConditionalOnProperty(value = "yudao.xss.enable", havingValue = "true") |
| 47 | - public Jackson2ObjectMapperBuilderCustomizer xssJacksonCustomizer(XssCleaner xssCleaner) { | 47 | + public Jackson2ObjectMapperBuilderCustomizer xssJacksonCustomizer(XssProperties properties, |
| 48 | + PathMatcher pathMatcher, | ||
| 49 | + XssCleaner xssCleaner) { | ||
| 48 | // 在反序列化时进行 xss 过滤,可以替换使用 XssStringJsonSerializer,在序列化时进行处理 | 50 | // 在反序列化时进行 xss 过滤,可以替换使用 XssStringJsonSerializer,在序列化时进行处理 |
| 49 | - return builder -> builder.deserializerByType(String.class, new XssStringJsonDeserializer(xssCleaner)); | 51 | + return builder -> builder.deserializerByType(String.class, new XssStringJsonDeserializer(properties, pathMatcher, xssCleaner)); |
| 50 | } | 52 | } |
| 51 | 53 | ||
| 52 | /** | 54 | /** |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/xss/core/json/XssStringJsonDeserializer.java
| 1 | package cn.iocoder.yudao.framework.xss.core.json; | 1 | package cn.iocoder.yudao.framework.xss.core.json; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | ||
| 4 | +import cn.iocoder.yudao.framework.xss.config.XssProperties; | ||
| 3 | import cn.iocoder.yudao.framework.xss.core.clean.XssCleaner; | 5 | import cn.iocoder.yudao.framework.xss.core.clean.XssCleaner; |
| 4 | import com.fasterxml.jackson.core.JsonParser; | 6 | import com.fasterxml.jackson.core.JsonParser; |
| 5 | import com.fasterxml.jackson.core.JsonToken; | 7 | import com.fasterxml.jackson.core.JsonToken; |
| 6 | import com.fasterxml.jackson.databind.DeserializationContext; | 8 | import com.fasterxml.jackson.databind.DeserializationContext; |
| 7 | import com.fasterxml.jackson.databind.deser.std.StringDeserializer; | 9 | import com.fasterxml.jackson.databind.deser.std.StringDeserializer; |
| 10 | +import jakarta.servlet.http.HttpServletRequest; | ||
| 8 | import lombok.AllArgsConstructor; | 11 | import lombok.AllArgsConstructor; |
| 9 | import lombok.extern.slf4j.Slf4j; | 12 | import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.springframework.util.PathMatcher; | ||
| 10 | 14 | ||
| 11 | import java.io.IOException; | 15 | import java.io.IOException; |
| 12 | 16 | ||
| @@ -20,10 +24,29 @@ import java.io.IOException; | @@ -20,10 +24,29 @@ import java.io.IOException; | ||
| 20 | @AllArgsConstructor | 24 | @AllArgsConstructor |
| 21 | public class XssStringJsonDeserializer extends StringDeserializer { | 25 | public class XssStringJsonDeserializer extends StringDeserializer { |
| 22 | 26 | ||
| 27 | + /** | ||
| 28 | + * 属性 | ||
| 29 | + */ | ||
| 30 | + private final XssProperties properties; | ||
| 31 | + /** | ||
| 32 | + * 路径匹配器 | ||
| 33 | + */ | ||
| 34 | + private final PathMatcher pathMatcher; | ||
| 35 | + | ||
| 23 | private final XssCleaner xssCleaner; | 36 | private final XssCleaner xssCleaner; |
| 24 | 37 | ||
| 25 | @Override | 38 | @Override |
| 26 | public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | 39 | public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { |
| 40 | + // 1. 白名单 URL 的处理 | ||
| 41 | + HttpServletRequest request = ServletUtils.getRequest(); | ||
| 42 | + if (request != null) { | ||
| 43 | + String uri = ServletUtils.getRequest().getRequestURI(); | ||
| 44 | + if (properties.getExcludeUrls().stream().anyMatch(excludeUrl -> pathMatcher.match(excludeUrl, uri))) { | ||
| 45 | + return p.getText(); | ||
| 46 | + } | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + // 2. 真正使用 xssCleaner 进行过滤 | ||
| 27 | if (p.hasToken(JsonToken.VALUE_STRING)) { | 50 | if (p.hasToken(JsonToken.VALUE_STRING)) { |
| 28 | return xssCleaner.clean(p.getText()); | 51 | return xssCleaner.clean(p.getText()); |
| 29 | } | 52 | } |