安全防护
This commit is contained in:
1 parent
1daf477899
commit
98657ace5b
25 files changed
+570
-4
No files matched your search
+109
@@ -0,0 +1,109 @@
|
||||
package com.ruoyi.system.security;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
import java.util.IdentityHashMap;
|
||||
import org.springframework.beans.factory.config.YamlProcessor;
|
||||
import org.springframework.boot.env.PropertySourceLoader;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||
import org.yaml.snakeyaml.representer.Representer;
|
||||
import org.yaml.snakeyaml.reader.UnicodeReader;
|
||||
import org.yaml.snakeyaml.error.YAMLException;
|
||||
|
||||
/**
|
||||
* 为 Boot 2.2 适配 SnakeYAML 2.x,保留 Spring 的 Map/列表扁平化及多文档 profile 行为。
|
||||
* 仅接受 YAML 标准数据类型;拒绝任意 Java 类型,限制别名、深度及输入量。
|
||||
* 通过 spring.factories 在读取 application*.yml 之前注册,不能依赖普通 @Bean。
|
||||
*/
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public final class SafeYamlPropertySourceLoader implements PropertySourceLoader {
|
||||
@Override
|
||||
public String[] getFileExtensions() {
|
||||
return new String[] {"yml", "yaml"};
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
|
||||
return new Processor().load(name, resource);
|
||||
}
|
||||
|
||||
private static final class Processor extends YamlProcessor {
|
||||
@Override
|
||||
protected Yaml createYaml() {
|
||||
LoaderOptions loader = new LoaderOptions();
|
||||
loader.setAllowDuplicateKeys(false);
|
||||
loader.setMaxAliasesForCollections(50);
|
||||
loader.setNestingDepthLimit(50);
|
||||
loader.setCodePointLimit(3 * 1024 * 1024);
|
||||
DumperOptions dumper = new DumperOptions();
|
||||
return new Yaml(new SafeConstructor(loader), new Representer(dumper), dumper, loader);
|
||||
}
|
||||
|
||||
private List<PropertySource<?>> load(String name, Resource resource) throws IOException {
|
||||
List<PropertySource<?>> sources = new ArrayList<>();
|
||||
// 不调用旧版 YamlProcessor.process:其 DEBUG 日志会打印整个配置 Map。
|
||||
// 只复用无日志的标准扁平化算法,配置秘密不能进入诊断输出。
|
||||
try (Reader reader = new UnicodeReader(resource.getInputStream())) {
|
||||
for (Object document : createYaml().loadAll(reader)) {
|
||||
if (document != null) {
|
||||
sources.add(new MapPropertySource(name + " (document #" + sources.size() + ")",
|
||||
new LinkedHashMap<>(getFlattenedMap(asMap(document)))));
|
||||
}
|
||||
}
|
||||
}
|
||||
return sources;
|
||||
}
|
||||
|
||||
private Map<String, Object> asMap(Object value) {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
Object normalized = convert(value, new IdentityHashMap<>(), 0, new int[1]);
|
||||
if (!(normalized instanceof Map)) {
|
||||
result.put("document", normalized);
|
||||
return result;
|
||||
}
|
||||
((Map<?, ?>) normalized).forEach((key, item) -> result.put((String) key, item));
|
||||
return result;
|
||||
}
|
||||
|
||||
private Object convert(Object value, IdentityHashMap<Object, Boolean> visiting,
|
||||
int depth, int[] visited) {
|
||||
// 别名可构造循环或指数展开的图,解析器的语法深度限制不能单独防住扁平化阶段。
|
||||
if (++visited[0] > 100000 || depth > 50) {
|
||||
throw new YAMLException("YAML 配置展开规模超过限制");
|
||||
}
|
||||
if (!(value instanceof Map) && !(value instanceof Collection)) return value;
|
||||
if (visiting.put(value, Boolean.TRUE) != null) {
|
||||
throw new YAMLException("YAML 配置不允许循环别名");
|
||||
}
|
||||
try {
|
||||
if (value instanceof Map) {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
((Map<?, ?>) value).forEach((key, item) -> result.put(
|
||||
key instanceof String ? (String) key : "[" + key + "]",
|
||||
convert(item, visiting, depth + 1, visited)));
|
||||
return result;
|
||||
}
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object item : (Collection<?>) value) {
|
||||
result.add(convert(item, visiting, depth + 1, visited));
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
visiting.remove(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user