fix: xss 启用后编辑器上传图片错误
parent
bbb27df5e9
commit
d7bec143fd
@ -0,0 +1,14 @@
|
|||||||
|
package cn.iocoder.yudao.framework.web.core.clean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对 html 文本中的有 Xss 风险的数据进行清理
|
||||||
|
*/
|
||||||
|
public interface XssCleaner {
|
||||||
|
/**
|
||||||
|
* 清理有 Xss 风险的文本
|
||||||
|
*
|
||||||
|
* @param html 原 html
|
||||||
|
* @return 清理后的 html
|
||||||
|
*/
|
||||||
|
String clean(String html);
|
||||||
|
}
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
package cn.iocoder.yudao.framework.web.core.json;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.web.config.XssProperties;
|
||||||
|
import cn.iocoder.yudao.framework.web.core.clean.XssCleaner;
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.core.JsonToken;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XSS过滤 jackson 反序列化器
|
||||||
|
*
|
||||||
|
* 参考 ballcat 实现
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class XssStringJsonDeserializer extends StringDeserializer {
|
||||||
|
|
||||||
|
private final XssCleaner xssCleaner;
|
||||||
|
private final XssProperties xssProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
if (p.hasToken(JsonToken.VALUE_STRING)) {
|
||||||
|
return getCleanText(p.getText());
|
||||||
|
}
|
||||||
|
JsonToken t = p.currentToken();
|
||||||
|
// [databind#381]
|
||||||
|
if (t == JsonToken.START_ARRAY) {
|
||||||
|
return _deserializeFromArray(p, ctxt);
|
||||||
|
}
|
||||||
|
// need to gracefully handle byte[] data, as base64
|
||||||
|
if (t == JsonToken.VALUE_EMBEDDED_OBJECT) {
|
||||||
|
Object ob = p.getEmbeddedObject();
|
||||||
|
if (ob == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (ob instanceof byte[]) {
|
||||||
|
return ctxt.getBase64Variant().encode((byte[]) ob, false);
|
||||||
|
}
|
||||||
|
// otherwise, try conversion using toString()...
|
||||||
|
return ob.toString();
|
||||||
|
}
|
||||||
|
// 29-Jun-2020, tatu: New! "Scalar from Object" (mostly for XML)
|
||||||
|
if (t == JsonToken.START_OBJECT) {
|
||||||
|
return ctxt.extractScalarFromObject(p, this, _valueClass);
|
||||||
|
}
|
||||||
|
// allow coercions for other scalar types
|
||||||
|
// 17-Jan-2018, tatu: Related to [databind#1853] avoid FIELD_NAME by ensuring it's
|
||||||
|
// "real" scalar
|
||||||
|
if (t.isScalarValue()) {
|
||||||
|
String text = p.getValueAsString();
|
||||||
|
return getCleanText(text);
|
||||||
|
}
|
||||||
|
return (String) ctxt.handleUnexpectedToken(_valueClass, p);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getCleanText(String text) {
|
||||||
|
if (text == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return xssProperties.isEnable() ? xssCleaner.clean(text) : text;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
package cn.iocoder.yudao.framework.web.core.json;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.web.config.XssProperties;
|
||||||
|
import cn.iocoder.yudao.framework.web.core.clean.XssCleaner;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XSS过滤 jackson 序列化器
|
||||||
|
*
|
||||||
|
* 参考 ballcat 实现
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class XssStringJsonSerializer extends JsonSerializer<String> {
|
||||||
|
|
||||||
|
private final XssCleaner xssCleaner;
|
||||||
|
private final XssProperties xssProperties;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<String> handledType() {
|
||||||
|
return String.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
|
||||||
|
throws IOException {
|
||||||
|
if (value != null) {
|
||||||
|
// 开启 Xss 才进行处理
|
||||||
|
if (xssProperties.isEnable()) {
|
||||||
|
value = xssCleaner.clean(value);
|
||||||
|
}
|
||||||
|
jsonGenerator.writeString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue