mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-11-18 08:40:29 +00:00
SSRF 地址校验补全(GHSA-5g5g / GHSA-2m5w / GHSA-hw5p)
- 补拦 0.0.0.0 及 IPv6 内嵌本机/链路本地地址(NAT64、6to4、Teredo、IPv4 兼容/映射) - 新增 jeecg.firewall.allowLocalAddress(默认 true,false 时拦截内网)、localAddressWhiteList(IP/网段/域名/地址:端口,命中即放行,云元数据除外) - 访问本服务自身(本机地址 + 本服务端口)放行 - 校验逻辑抽到 LocalAddressCheckUtils,SsrfFileTypeFilter.checkSsrfHttpUrl 保留并委托 - 匿名 AI 附件存储地址改为按主机名+端口精确比对,下沉为 CommonUtils.extractStorageRelativePath Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f2b5192669
commit
f7eb9f38dc
@ -606,4 +606,116 @@ public class CommonUtils {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 从完整 URL 中提取本系统存储服务(OSS staticDomain/endpoint、MinIO)里的相对路径,非已配置存储的地址返回 null。
|
||||
* 按主机名 + 端口精确比对(不区分大小写),带用户信息(user@host)或路径含 .. 的地址一律不认,
|
||||
* 防止 http://存储地址@内网IP/、在查询串里拼存储地址等前缀匹配绕过;可用于"只允许引用本系统存储文件"的场景(如匿名附件)
|
||||
*
|
||||
* @param url 完整地址
|
||||
* @return 存储内相对路径(不带开头 /,MinIO 已去掉 bucket),非本系统存储返回 null
|
||||
* @author scott
|
||||
* @since 2026-10-27 【GHSA-hw5p-65q6-qcxr】由 AiragChatServiceImpl 下沉,供其他模块复用
|
||||
*/
|
||||
public static String extractStorageRelativePath(String url) {
|
||||
if (oConvertUtils.isEmpty(url)) {
|
||||
return null;
|
||||
}
|
||||
java.net.URI target = parseUri(url);
|
||||
// 带用户信息(user@host)的地址一律不认,防止 http://存储地址@内网IP/ 这类写法
|
||||
if (target == null || target.getHost() == null || target.getRawUserInfo() != null) {
|
||||
return null;
|
||||
}
|
||||
String targetPath = oConvertUtils.getString(target.getPath(), "");
|
||||
if (targetPath.contains("..")) {
|
||||
return null;
|
||||
}
|
||||
// 匹配 OSS staticDomain(如 https://jeecgdev.oss-cn-beijing.aliyuncs.com)
|
||||
java.net.URI ossDomain = parseUri(OssBootUtil.getStaticDomain());
|
||||
if (isSameServer(ossDomain, target)) {
|
||||
return stripBasePath(ossDomain, targetPath);
|
||||
}
|
||||
// 匹配 OSS endpoint(如 oss-cn-beijing.aliyuncs.com,访问地址为 bucket.endpoint)
|
||||
String ossEndpoint = OssBootUtil.getEndPoint();
|
||||
if (oConvertUtils.isNotEmpty(ossEndpoint)) {
|
||||
String endpointHost = ossEndpoint.replaceFirst("(?i)^https?://", "").replaceAll("/.*$", "").toLowerCase();
|
||||
String host = target.getHost().toLowerCase();
|
||||
if (host.equals(endpointHost) || host.endsWith("." + endpointHost)) {
|
||||
return targetPath.startsWith("/") ? targetPath.substring(1) : targetPath;
|
||||
}
|
||||
}
|
||||
// 匹配 MinIO URL(如 http://192.168.1.100:9000/)
|
||||
java.net.URI minioUrl = parseUri(MinioUtil.getMinioUrl());
|
||||
if (isSameServer(minioUrl, target)) {
|
||||
String path = stripBasePath(minioUrl, targetPath);
|
||||
if (path == null) {
|
||||
return null;
|
||||
}
|
||||
// 跳过 bucket 名称
|
||||
int slashIdx = path.indexOf('/');
|
||||
return slashIdx >= 0 ? path.substring(slashIdx + 1) : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析地址,格式错误返回 null
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-27 【GHSA-hw5p-65q6-qcxr】存储地址按主机名比对
|
||||
*/
|
||||
private static java.net.URI parseUri(String url) {
|
||||
if (oConvertUtils.isEmpty(url)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return new java.net.URI(url.trim());
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 两个地址是否指向同一服务:主机名相同(不区分大小写)且端口相同(未写端口按协议默认端口)
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-27 【GHSA-hw5p-65q6-qcxr】存储地址按主机名比对
|
||||
*/
|
||||
private static boolean isSameServer(java.net.URI base, java.net.URI target) {
|
||||
if (base == null || base.getHost() == null) {
|
||||
return false;
|
||||
}
|
||||
return base.getHost().equalsIgnoreCase(target.getHost()) && effectivePort(base) == effectivePort(target);
|
||||
}
|
||||
|
||||
/**
|
||||
* 地址的实际端口,未写端口时按协议取默认值
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-27 【GHSA-hw5p-65q6-qcxr】存储地址按主机名比对
|
||||
*/
|
||||
private static int effectivePort(java.net.URI uri) {
|
||||
if (uri.getPort() != -1) {
|
||||
return uri.getPort();
|
||||
}
|
||||
return "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉存储地址自身的路径前缀,返回不带开头 / 的相对路径;不在该前缀下返回 null
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-27 【GHSA-hw5p-65q6-qcxr】存储地址按主机名比对
|
||||
*/
|
||||
private static String stripBasePath(java.net.URI base, String targetPath) {
|
||||
String basePath = oConvertUtils.getString(base.getPath(), "");
|
||||
if (basePath.endsWith("/")) {
|
||||
basePath = basePath.substring(0, basePath.length() - 1);
|
||||
}
|
||||
if (!basePath.isEmpty() && !(targetPath.equals(basePath) || targetPath.startsWith(basePath + "/"))) {
|
||||
return null;
|
||||
}
|
||||
String path = targetPath.substring(basePath.length());
|
||||
return path.startsWith("/") ? path.substring(1) : path;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,423 @@
|
||||
package org.jeecg.common.util.filter;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.common.exception.JeecgBootException;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.jeecg.config.JeecgBaseConfig;
|
||||
import org.jeecg.config.vo.Firewall;
|
||||
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* SSRF 出站地址校验工具:
|
||||
* <ul>
|
||||
* <li>访问本服务自身(本机地址 + 本服务监听端口)放行;</li>
|
||||
* <li>命中白名单 jeecg.firewall.localAddressWhiteList 即放行:域名、IP、CIDR 网段、地址:端口(只放行该端口);</li>
|
||||
* <li>链路本地(含云元数据 169.254.169.254)、0.0.0.0/8、[::] 始终拒绝,写进白名单也不放行;</li>
|
||||
* <li>本机其他端口拒绝;内网段(10/172.16/192.168/100.64/fc00::/7)默认放行,allowLocalAddress=false 时拒绝;</li>
|
||||
* <li>IPv6 过渡地址(NAT64、6to4、Teredo、IPv4兼容/映射)按内嵌 IPv4 判断。</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-27 【GHSA-5g5g-43q2-g87v】【GHSA-2m5w-w7v9-qwx7】【GHSA-hw5p-65q6-qcxr】从 SsrfFileTypeFilter 抽取,专门用于 SSRF 校验
|
||||
*/
|
||||
@Slf4j
|
||||
public class LocalAddressCheckUtils {
|
||||
|
||||
private LocalAddressCheckUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验HTTP(S) URL,防止SSRF攻击(最小化拦截,只挡真正危险的目标)。
|
||||
* 规则:
|
||||
* 1. 仅允许 http / https 协议;
|
||||
* 2. 解析主机IP,拒绝 loopback(127.x / ::1)、link-local(169.254.x,含云元数据 169.254.169.254 / fe80:)、0.0.0.0/8、[::],
|
||||
* 以及内嵌这些地址的 IPv6 过渡地址(NAT64、6to4、Teredo、IPv4兼容/映射);
|
||||
* 3. 访问本服务自身(本机地址 + 本服务监听端口)放行。
|
||||
* 4. 命中白名单 jeecg.firewall.localAddressWhiteList(域名、IP、CIDR 网段、地址:端口)即放行,链路本地、0.0.0.0 除外。
|
||||
* 注意:RFC1918 私网段(10/172.16/192.168)默认允许通过,兼容企业内网 MinIO/OSS/文件服务等合法用途;
|
||||
* 配置 jeecg.firewall.allowLocalAddress=false 后拦截。
|
||||
*
|
||||
* @param fileUrl HTTP(S) URL
|
||||
* @author scott
|
||||
* @since 2026-10-27 【GHSA-5g5g-43q2-g87v】由 SsrfFileTypeFilter.checkSsrfHttpUrl 迁移
|
||||
*/
|
||||
public static void checkUrl(String fileUrl) {
|
||||
if (StringUtils.isBlank(fileUrl)) {
|
||||
throw new JeecgBootException("非法URL:地址为空");
|
||||
}
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI(fileUrl);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new JeecgBootException("非法URL:格式错误");
|
||||
}
|
||||
String scheme = uri.getScheme();
|
||||
if (scheme == null || !(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
|
||||
throw new JeecgBootException("非法URL:仅允许 http / https 协议");
|
||||
}
|
||||
String host = uri.getHost();
|
||||
if (StringUtils.isBlank(host)) {
|
||||
throw new JeecgBootException("非法URL:主机名为空");
|
||||
}
|
||||
// 去掉 IPv6 的中括号
|
||||
if (host.startsWith("[") && host.endsWith("]")) {
|
||||
host = host.substring(1, host.length() - 1);
|
||||
}
|
||||
try {
|
||||
checkAddresses(host, uri);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new JeecgBootException("非法URL:主机名无法解析");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析主机并校验所有解析结果,不合法时抛出 JeecgBootException
|
||||
*
|
||||
* @param host 主机名(IPv6 已去掉中括号)
|
||||
* @param uri 目标地址(用于取端口判断是否访问本服务自身)
|
||||
* @throws UnknownHostException 主机名无法解析
|
||||
*/
|
||||
private static void checkAddresses(String host, URI uri) throws UnknownHostException {
|
||||
Firewall firewall = getFirewall();
|
||||
boolean blockPrivate = firewall != null && Boolean.FALSE.equals(firewall.getAllowLocalAddress());
|
||||
List<String> whiteList = firewall == null ? null : firewall.getLocalAddressWhiteList();
|
||||
InetAddress[] addresses = InetAddress.getAllByName(host);
|
||||
// 访问本服务自身(如本地存储文件 http://localhost:8080/jeecg-boot/sys/common/static/xx.png)不拦截
|
||||
if (isSelfServiceAddress(addresses, uri)) {
|
||||
return;
|
||||
}
|
||||
int port = getPort(uri);
|
||||
// 命中白名单即放行(域名、IP、网段、地址:端口),与开关无关;链路本地(云元数据)、0.0.0.0 仍拒绝
|
||||
if (isInWhiteList(host, addresses, port, whiteList)) {
|
||||
for (InetAddress addr : addresses) {
|
||||
if (isNeverAllowedAddress(addr)) {
|
||||
throw new JeecgBootException("非法URL:禁止访问链路本地或任意地址 " + addr.getHostAddress());
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
String hint = ",如需放行请在 jeecg.firewall.localAddressWhiteList 中配置该地址,或 地址:端口(如 " + (host.contains(":") ? "[" + host + "]" : host) + ":" + port + ")";
|
||||
for (InetAddress addr : addresses) {
|
||||
if (addr.isLoopbackAddress()) {
|
||||
throw new JeecgBootException("非法URL:禁止访问本机或链路本地地址 " + addr.getHostAddress() + hint);
|
||||
}
|
||||
checkDangerousAddress(addr, addr);
|
||||
if (blockPrivate && isPrivateAddress(addr)) {
|
||||
throw new JeecgBootException("非法URL:禁止访问内网地址 " + addr.getHostAddress() + hint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否命中白名单:命中"地址:端口",或每个解析结果都命中域名/IP/CIDR 网段条目
|
||||
*/
|
||||
private static boolean isInWhiteList(String host, InetAddress[] addresses, int port, List<String> whiteList) {
|
||||
if (whiteList == null || whiteList.isEmpty() || addresses.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (isInHostPortWhiteList(addresses, port, whiteList)) {
|
||||
return true;
|
||||
}
|
||||
for (InetAddress addr : addresses) {
|
||||
if (!isInLocalAddressWhiteList(host, addr, whiteList)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 目标端口,未写端口时 https 为 443、其余为 80
|
||||
*/
|
||||
private static int getPort(URI uri) {
|
||||
return uri.getPort() != -1 ? uri.getPort() : ("https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否命中白名单中的"地址:端口"条目:端口一致,且目标解析结果全部属于该条目地址的解析结果(写 localhost:9999 时 127.0.0.1:9999 同样命中)
|
||||
*/
|
||||
private static boolean isInHostPortWhiteList(InetAddress[] addresses, int port, List<String> whiteList) {
|
||||
if (whiteList == null || whiteList.isEmpty() || addresses.length == 0) {
|
||||
return false;
|
||||
}
|
||||
for (String entry : whiteList) {
|
||||
if (StringUtils.isBlank(entry)) {
|
||||
continue;
|
||||
}
|
||||
String[] hostPort = parseHostPort(entry.trim());
|
||||
if (hostPort == null || Integer.parseInt(hostPort[1]) != port) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
List<InetAddress> allowed = Arrays.asList(InetAddress.getAllByName(hostPort[0]));
|
||||
if (Arrays.stream(addresses).allMatch(allowed::contains)) {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("SSRF 白名单配置无法解析,已忽略: {}", entry);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析"地址:端口"条目:host:port 或 [IPv6]:port,返回 {host, port};IP、CIDR、域名、不带中括号的 IPv6 返回 null
|
||||
*/
|
||||
private static String[] parseHostPort(String item) {
|
||||
if (item.startsWith("[")) {
|
||||
int end = item.indexOf("]:");
|
||||
return end > 0 && item.substring(end + 2).matches("\\d{1,5}") ? new String[]{item.substring(1, end), item.substring(end + 2)} : null;
|
||||
}
|
||||
int idx = item.indexOf(':');
|
||||
if (idx <= 0 || idx != item.lastIndexOf(':') || item.contains("/") || !item.substring(idx + 1).matches("\\d{1,5}")) {
|
||||
return null;
|
||||
}
|
||||
return new String[]{item.substring(0, idx), item.substring(idx + 1)};
|
||||
}
|
||||
|
||||
/**
|
||||
* 白名单也不能放行的地址:链路本地(含云元数据 169.254.169.254)、0.0.0.0/8、[::],IPv6 过渡地址按内嵌 IPv4 判断
|
||||
*/
|
||||
private static boolean isNeverAllowedAddress(InetAddress addr) {
|
||||
// ::1 字节上形如 IPv4 兼容地址(内嵌 0.0.0.1),属于本机回环,不按内嵌地址判断
|
||||
if (addr.isLoopbackAddress()) {
|
||||
return false;
|
||||
}
|
||||
byte[] b = addr.getAddress();
|
||||
if (addr.isLinkLocalAddress() || addr.isAnyLocalAddress() || (b.length == 4 && b[0] == 0)) {
|
||||
return true;
|
||||
}
|
||||
for (InetAddress embedded : getEmbeddedIpv4(addr)) {
|
||||
if (isNeverAllowedAddress(embedded)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验单个地址:本机、链路本地(含云元数据 169.254.169.254)、0.0.0.0/8、[::] 一律拒绝;
|
||||
* IPv6 过渡地址(NAT64、6to4、Teredo、IPv4兼容/映射)取出内嵌的 IPv4 再按同样规则校验
|
||||
*
|
||||
* @param addr 当前校验的地址
|
||||
* @param original 原始解析地址(用于提示)
|
||||
*/
|
||||
private static void checkDangerousAddress(InetAddress addr, InetAddress original) {
|
||||
byte[] b = addr.getAddress();
|
||||
boolean thisNetwork = b.length == 4 && b[0] == 0;
|
||||
if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isAnyLocalAddress() || thisNetwork) {
|
||||
throw new JeecgBootException("非法URL:禁止访问本机或链路本地地址 " + original.getHostAddress());
|
||||
}
|
||||
for (InetAddress embedded : getEmbeddedIpv4(addr)) {
|
||||
checkDangerousAddress(embedded, original);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取出 IPv6 过渡地址中内嵌的 IPv4 地址,非过渡地址返回空列表
|
||||
* <ul>
|
||||
* <li>IPv4兼容 ::a.b.c.d、IPv4映射 ::ffff:a.b.c.d</li>
|
||||
* <li>NAT64 64:ff9b::/96、64:ff9b:1::/48(取末 32 位)</li>
|
||||
* <li>6to4 2002::/16(第 3~6 字节)</li>
|
||||
* <li>Teredo 2001:0::/32(服务器地址第 5~8 字节;客户端地址末 32 位按位取反)</li>
|
||||
* </ul>
|
||||
*/
|
||||
private static List<InetAddress> getEmbeddedIpv4(InetAddress addr) {
|
||||
List<InetAddress> result = new ArrayList<>();
|
||||
if (!(addr instanceof Inet6Address)) {
|
||||
return result;
|
||||
}
|
||||
byte[] b = addr.getAddress();
|
||||
try {
|
||||
boolean first10Zero = true;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (b[i] != 0) {
|
||||
first10Zero = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
boolean compatOrMapped = first10Zero && ((b[10] == 0 && b[11] == 0) || (b[10] == (byte) 0xff && b[11] == (byte) 0xff));
|
||||
boolean nat64 = b[0] == 0x00 && b[1] == 0x64 && b[2] == (byte) 0xff && b[3] == (byte) 0x9b;
|
||||
if (compatOrMapped || nat64) {
|
||||
result.add(InetAddress.getByAddress(Arrays.copyOfRange(b, 12, 16)));
|
||||
} else if (b[0] == 0x20 && b[1] == 0x02) {
|
||||
result.add(InetAddress.getByAddress(Arrays.copyOfRange(b, 2, 6)));
|
||||
} else if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x00 && b[3] == 0x00) {
|
||||
result.add(InetAddress.getByAddress(Arrays.copyOfRange(b, 4, 8)));
|
||||
byte[] client = Arrays.copyOfRange(b, 12, 16);
|
||||
for (int i = 0; i < client.length; i++) {
|
||||
client[i] = (byte) ~client[i];
|
||||
}
|
||||
result.add(InetAddress.getByAddress(client));
|
||||
}
|
||||
} catch (UnknownHostException ignored) {
|
||||
// 4 字节数组不会触发
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否内网地址:10/8、172.16/12、192.168/16、100.64/10(运营商级NAT)、fc00::/7(IPv6唯一本地地址),IPv6 过渡地址按内嵌 IPv4 判断
|
||||
*/
|
||||
private static boolean isPrivateAddress(InetAddress addr) {
|
||||
byte[] b = addr.getAddress();
|
||||
if (addr.isSiteLocalAddress()) {
|
||||
return true;
|
||||
}
|
||||
if (b.length == 4 && (b[0] & 0xff) == 100 && (b[1] & 0xc0) == 64) {
|
||||
return true;
|
||||
}
|
||||
if (b.length == 16 && (b[0] & 0xfe) == 0xfc) {
|
||||
return true;
|
||||
}
|
||||
for (InetAddress embedded : getEmbeddedIpv4(addr)) {
|
||||
if (isPrivateAddress(embedded)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单个地址是否命中白名单的域名/IP/CIDR 条目:域名精确匹配(不区分大小写)、IP 精确匹配或 CIDR 网段匹配
|
||||
*/
|
||||
private static boolean isInLocalAddressWhiteList(String host, InetAddress addr, List<String> whiteList) {
|
||||
if (whiteList == null || whiteList.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
byte[] target = addr.getAddress();
|
||||
for (String entry : whiteList) {
|
||||
if (StringUtils.isBlank(entry)) {
|
||||
continue;
|
||||
}
|
||||
String item = entry.trim();
|
||||
// "地址:端口" 条目由 isInHostPortWhiteList 处理
|
||||
if (parseHostPort(item) != null) {
|
||||
continue;
|
||||
}
|
||||
if (item.equalsIgnoreCase(host)) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
String ip = item;
|
||||
int prefix = -1;
|
||||
if (item.contains("/")) {
|
||||
ip = item.substring(0, item.indexOf('/'));
|
||||
prefix = Integer.parseInt(item.substring(item.indexOf('/') + 1).trim());
|
||||
}
|
||||
// 只按字面量 IP 解析,非 IP 条目(域名)跳过,避免触发 DNS 查询
|
||||
if (!ip.matches("^[0-9.]+$") && !ip.contains(":")) {
|
||||
continue;
|
||||
}
|
||||
byte[] net = InetAddress.getByName(ip).getAddress();
|
||||
if (net.length != target.length) {
|
||||
continue;
|
||||
}
|
||||
int bits = prefix < 0 ? net.length * 8 : prefix;
|
||||
if (matchPrefix(net, target, bits)) {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("SSRF 白名单配置无法解析,已忽略: {}", item);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个地址的前 bits 位是否相同
|
||||
*/
|
||||
private static boolean matchPrefix(byte[] net, byte[] target, int bits) {
|
||||
int fullBytes = bits / 8;
|
||||
for (int i = 0; i < fullBytes; i++) {
|
||||
if (net[i] != target[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int rest = bits % 8;
|
||||
if (rest == 0) {
|
||||
return true;
|
||||
}
|
||||
int mask = (0xff << (8 - rest)) & 0xff;
|
||||
return (net[fullBytes] & mask) == (target[fullBytes] & mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否访问本服务自身:解析结果全部为本机地址(回环或本机网卡 IP),且端口等于本服务实际监听端口。
|
||||
* 端口取自服务器自身(请求连接器端口或 server.port),不读请求头,无法伪造;本机其他端口(Redis、数据库等)仍按规则拦截
|
||||
*/
|
||||
private static boolean isSelfServiceAddress(InetAddress[] addresses, URI uri) {
|
||||
if (addresses == null || addresses.length == 0) {
|
||||
return false;
|
||||
}
|
||||
int targetPort = uri.getPort() != -1 ? uri.getPort() : ("https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80);
|
||||
Integer selfPort = getSelfServerPort();
|
||||
if (selfPort == null || selfPort != targetPort) {
|
||||
return false;
|
||||
}
|
||||
for (InetAddress addr : addresses) {
|
||||
if (!isLocalMachineAddress(addr)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否本机地址:回环地址,或绑定在本机某块网卡上的 IP(0.0.0.0、组播不算)
|
||||
*/
|
||||
private static boolean isLocalMachineAddress(InetAddress addr) {
|
||||
if (addr.isLoopbackAddress()) {
|
||||
return true;
|
||||
}
|
||||
if (addr.isAnyLocalAddress() || addr.isMulticastAddress()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return NetworkInterface.getByInetAddress(addr) != null;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 本服务实际监听端口:有请求上下文时取连接器端口,否则读 server.port(默认 8080),获取失败返回 null
|
||||
*/
|
||||
private static Integer getSelfServerPort() {
|
||||
try {
|
||||
jakarta.servlet.http.HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
|
||||
if (request != null) {
|
||||
return request.getLocalPort();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// 无请求上下文(定时任务、异步线程等)
|
||||
}
|
||||
try {
|
||||
return SpringContextUtils.getApplicationContext().getEnvironment().getProperty("server.port", Integer.class, 8080);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取平台安全配置,非 Spring 环境(如单元测试)返回 null,按默认值处理
|
||||
*/
|
||||
private static Firewall getFirewall() {
|
||||
try {
|
||||
JeecgBaseConfig config = SpringContextUtils.getBean(JeecgBaseConfig.class);
|
||||
return config == null ? null : config.getFirewall();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,10 +7,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -321,49 +317,17 @@ public class SsrfFileTypeFilter {
|
||||
}
|
||||
}
|
||||
|
||||
//update-begin---author:zhangdaihao ---date:2026-04-15 for:【issues/9553】修复二次SSRF漏洞,对HTTP下载URL进行安全校验-----------
|
||||
/**
|
||||
* 校验HTTP(S) URL,防止SSRF攻击(最小化拦截,只挡真正危险的目标)。
|
||||
* 规则:
|
||||
* 1. 仅允许 http / https 协议;
|
||||
* 2. 解析主机IP,拒绝 loopback(127.x / ::1)和 link-local(169.254.x,含云元数据 169.254.169.254 / fe80:);
|
||||
* 注意:RFC1918 私网段(10/172.16/192.168)允许通过,兼容企业内网 MinIO/OSS/文件服务等合法用途。
|
||||
/**
|
||||
* 【issues/9553】修复二次SSRF漏洞,对HTTP下载URL进行安全校验
|
||||
* 2026-04-15
|
||||
*
|
||||
* 校验HTTP(S) URL,防止SSRF攻击,规则见 {@link LocalAddressCheckUtils#checkUrl(String)}
|
||||
*
|
||||
* @param fileUrl HTTP(S) URL
|
||||
*/
|
||||
public static void checkSsrfHttpUrl(String fileUrl) {
|
||||
if (StringUtils.isBlank(fileUrl)) {
|
||||
throw new JeecgBootException("非法URL:地址为空");
|
||||
}
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI(fileUrl);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new JeecgBootException("非法URL:格式错误");
|
||||
}
|
||||
String scheme = uri.getScheme();
|
||||
if (scheme == null || !(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
|
||||
throw new JeecgBootException("非法URL:仅允许 http / https 协议");
|
||||
}
|
||||
String host = uri.getHost();
|
||||
if (StringUtils.isBlank(host)) {
|
||||
throw new JeecgBootException("非法URL:主机名为空");
|
||||
}
|
||||
// 去掉 IPv6 的中括号
|
||||
if (host.startsWith("[") && host.endsWith("]")) {
|
||||
host = host.substring(1, host.length() - 1);
|
||||
}
|
||||
try {
|
||||
for (InetAddress addr : InetAddress.getAllByName(host)) {
|
||||
if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
|
||||
throw new JeecgBootException("非法URL:禁止访问本机或链路本地地址 " + addr.getHostAddress());
|
||||
}
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
throw new JeecgBootException("非法URL:主机名无法解析");
|
||||
}
|
||||
LocalAddressCheckUtils.checkUrl(fileUrl);
|
||||
}
|
||||
//update-end---author:zhangdaihao ---date:2026-04-15 for:【issues/9553】修复二次SSRF漏洞,对HTTP下载URL进行安全校验-----------
|
||||
|
||||
/**
|
||||
* 批量校验文件路径安全性(逗号分隔的多个文件路径)
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package org.jeecg.config.vo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 平台安全配置
|
||||
*
|
||||
@ -40,6 +42,24 @@ public class Firewall {
|
||||
private Boolean enableStrongPwd = false;
|
||||
//update-end---author:wangshuai ---date:2026-06-29 for:【QQYUN-16619】三级等保密码强度开关-----------
|
||||
|
||||
/**
|
||||
* 是否允许访问本地网络地址(内网 10/8、172.16/12、192.168/16、100.64/10、fc00::/7):按URL下载文件时(网络图片上传、AI附件、知识库网页等)生效。
|
||||
* 默认 true 允许,兼容企业内网 MinIO/文件服务;设为 false 后拦截,白名单除外。本机其他端口、链路本地(含云元数据)、0.0.0.0 始终拦截,不受此开关影响。
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-26 【GHSA-5g5g-43q2-g87v】【GHSA-hw5p-65q6-qcxr】SSRF 内网段可配置拦截
|
||||
*/
|
||||
private Boolean allowLocalAddress = true;
|
||||
|
||||
/**
|
||||
* 本地地址白名单:allowLocalAddress=false 时仍允许访问的内网地址,支持 IP、CIDR 网段(如 10.0.1.0/24)、域名(精确匹配);
|
||||
* 写"地址:端口"(如 localhost:9999)只放行该端口,本机地址也可按端口放行,不受 allowLocalAddress 开关影响
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-26 【GHSA-5g5g-43q2-g87v】【GHSA-hw5p-65q6-qcxr】SSRF 内网段可配置拦截
|
||||
*/
|
||||
private List<String> localAddressWhiteList;
|
||||
|
||||
// /**
|
||||
// * 表字典安全模式(white:白名单——配置了白名单的表才能通过表字典方式访问,black:黑名单——配置了黑名单的表不允许表字典方式访问)
|
||||
// */
|
||||
@ -103,4 +123,20 @@ public class Firewall {
|
||||
this.enableStrongPwd = enableStrongPwd;
|
||||
}
|
||||
//update-end---author:wangshuai ---date:2026-06-29 for:【QQYUN-16619】三级等保密码强度开关-----------
|
||||
|
||||
public Boolean getAllowLocalAddress() {
|
||||
return allowLocalAddress;
|
||||
}
|
||||
|
||||
public void setAllowLocalAddress(Boolean allowLocalAddress) {
|
||||
this.allowLocalAddress = allowLocalAddress;
|
||||
}
|
||||
|
||||
public List<String> getLocalAddressWhiteList() {
|
||||
return localAddressWhiteList;
|
||||
}
|
||||
|
||||
public void setLocalAddressWhiteList(List<String> localAddressWhiteList) {
|
||||
this.localAddressWhiteList = localAddressWhiteList;
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,7 +26,6 @@ import org.jeecg.common.system.api.ISysBaseAPI;
|
||||
import org.jeecg.common.system.util.JwtUtil;
|
||||
import org.jeecg.common.util.*;
|
||||
import org.jeecg.common.util.filter.SsrfFileTypeFilter;
|
||||
import org.jeecg.common.util.oss.OssBootUtil;
|
||||
import org.jeecg.config.AiChatConfig;
|
||||
import org.jeecg.config.AiRagConfigBean;
|
||||
import org.jeecg.config.JeecgBaseConfig;
|
||||
@ -2546,46 +2545,14 @@ public class AiragChatServiceImpl implements IAiragChatService {
|
||||
*/
|
||||
private void checkAnonymousFileRef(String fileRef) {
|
||||
String airag = "airag/";
|
||||
String relativePath = extractStorageRelativePath(fileRef);
|
||||
//update-begin---author:scott ---date:20261027 for:【GHSA-hw5p-65q6-qcxr】存储地址识别逻辑下沉到 CommonUtils.extractStorageRelativePath(按主机名+端口精确比对),删除本类私有方法-----------
|
||||
String relativePath = CommonUtils.extractStorageRelativePath(fileRef);
|
||||
//update-end---author:scott ---date:20261027 for:【GHSA-hw5p-65q6-qcxr】存储地址识别逻辑下沉到 CommonUtils.extractStorageRelativePath(按主机名+端口精确比对),删除本类私有方法-----------
|
||||
if (relativePath == null || !relativePath.startsWith(airag)) {
|
||||
log.warn("匿名请求文件路径不在允许范围内: {}", fileRef);
|
||||
throw new JeecgBootException("匿名聊天不支持远程文件引用,请直接上传文件");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从完整URL中提取存储服务的相对路径,非已配置存储的URL返回null
|
||||
*/
|
||||
private String extractStorageRelativePath(String url) {
|
||||
if (oConvertUtils.isEmpty(url)) {
|
||||
return null;
|
||||
}
|
||||
// 匹配 OSS staticDomain(如 https://jeecgdev.oss-cn-beijing.aliyuncs.com)
|
||||
String ossDomain = OssBootUtil.getStaticDomain();
|
||||
if (oConvertUtils.isNotEmpty(ossDomain) && url.toLowerCase().startsWith(ossDomain.toLowerCase())) {
|
||||
String path = url.substring(ossDomain.length());
|
||||
return path.startsWith("/") ? path.substring(1) : path;
|
||||
}
|
||||
// 匹配 OSS endpoint(如 oss-cn-beijing.aliyuncs.com)
|
||||
String ossEndpoint = OssBootUtil.getEndPoint();
|
||||
if (oConvertUtils.isNotEmpty(ossEndpoint) && url.toLowerCase().contains(ossEndpoint.toLowerCase())) {
|
||||
int idx = url.indexOf(ossEndpoint);
|
||||
String afterEndpoint = url.substring(idx + ossEndpoint.length());
|
||||
int slashIdx = afterEndpoint.indexOf('/');
|
||||
return slashIdx >= 0 ? afterEndpoint.substring(slashIdx + 1) : null;
|
||||
}
|
||||
// 匹配 MinIO URL(如 http://192.168.1.100:9000/)
|
||||
String minioUrl = MinioUtil.getMinioUrl();
|
||||
if (oConvertUtils.isNotEmpty(minioUrl) && url.toLowerCase().startsWith(minioUrl.toLowerCase())) {
|
||||
String path = url.substring(minioUrl.length());
|
||||
if (path.startsWith("/")) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
// 跳过 bucket 名称
|
||||
int slashIdx = path.indexOf('/');
|
||||
return slashIdx >= 0 ? path.substring(slashIdx + 1) : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
//update-end---author:wangshuai ---date:2026-06-16 for:【issues/9672】匿名请求文件引用安全校验-----------
|
||||
}
|
||||
|
||||
@ -268,6 +268,14 @@ jeecg:
|
||||
is-concurrent: true
|
||||
# 是否开启三级等保强密码校验(true 开启强密码模式,false 使用简单密码规则)
|
||||
enableStrongPwd: false
|
||||
# 是否允许访问本地网络地址(内网):默认 true 允许,兼容内网 MinIO/文件服务;设为 false 后,按URL下载文件(网络图片上传、AI附件、知识库网页抓取等)禁止访问内网地址,白名单除外。本机其他端口、云元数据地址始终拦截
|
||||
# 集成积木报表时,设为 false 后积木的 API 数据集、API 字典等出站请求同样拦截内网地址,白名单与积木 localAddressWhiteList 合并生效
|
||||
allowLocalAddress: true
|
||||
# 白名单:写了就放行,与开关无关(云元数据、0.0.0.0 除外);支持 IP、CIDR 网段、域名、地址:端口(推荐,只放行该端口,如微服务网关 localhost:9999);公网地址无需配置
|
||||
#localAddressWhiteList:
|
||||
# - 192.168.1.100
|
||||
# - 10.0.1.0/24
|
||||
# - localhost:9999
|
||||
# 签名密钥串(前后端要一致,正式发布请自行修改)
|
||||
signatureSecret: dd05f1c54d63749eda95f9fa6d49v442a
|
||||
#签名拦截接口
|
||||
|
||||
@ -272,6 +272,14 @@ jeecg:
|
||||
is-concurrent: true
|
||||
# 是否开启三级等保强密码校验(true 开启强密码模式,false 使用简单密码规则)
|
||||
enableStrongPwd: false
|
||||
# 是否允许访问本地网络地址(内网):默认 true 允许,兼容内网 MinIO/文件服务;设为 false 后,按URL下载文件(网络图片上传、AI附件、知识库网页抓取等)禁止访问内网地址,白名单除外。本机其他端口、云元数据地址始终拦截
|
||||
# 集成积木报表时,设为 false 后积木的 API 数据集、API 字典等出站请求同样拦截内网地址,白名单与积木 localAddressWhiteList 合并生效
|
||||
allowLocalAddress: true
|
||||
# 白名单:写了就放行,与开关无关(云元数据、0.0.0.0 除外);支持 IP、CIDR 网段、域名、地址:端口(推荐,只放行该端口,如微服务网关 localhost:9999);公网地址无需配置
|
||||
#localAddressWhiteList:
|
||||
# - 192.168.1.100
|
||||
# - 10.0.1.0/24
|
||||
# - localhost:9999
|
||||
# 签名密钥串(前后端要一致,正式发布请自行修改)
|
||||
signatureSecret: dd05f1c54d63749eda95f9fa6d49v442a
|
||||
#签名拦截接口
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user