fix(security): 网关基址头 X_GATEWAY_BASE_PATH 只接受纯路径

validateGatewayBasePath 原用解码后的 uri.getPath() 重建基址,%3f/%23 解码出的
? # 会被带进去,可篡改 OpenAPI 中继的转发目标。改为拒绝查询串和片段,
路径走字符白名单并禁止 ..,补充对应单元测试。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
scott 2026-10-24 05:15:41 -04:00
parent 59cd9c28f9
commit ff9b67f4ba
2 changed files with 56 additions and 2 deletions

View File

@ -431,15 +431,25 @@ public class CommonUtils {
if (oConvertUtils.isEmpty(host)) {
return null;
}
//update-begin---author:jeecg---for:网关基址头只接受纯路径,拒绝查询串/片段/路径穿越,防止篡改转发目标---
if (uri.getRawQuery() != null || uri.getRawFragment() != null) {
return null;
}
StringBuilder sb = new StringBuilder();
sb.append(scheme.toLowerCase()).append("://").append(host);
if (uri.getPort() != -1) {
sb.append(":").append(uri.getPort());
}
if (uri.getPath() != null && !uri.getPath().isEmpty()) {
sb.append(uri.getPath());
String path = uri.getPath();
if (path != null && !path.isEmpty()) {
// 仅允许常规路径字符,拒绝解码后出现的 ? # 及路径穿越 ..
if (!path.matches("[A-Za-z0-9/_.\\-]*") || path.contains("..")) {
return null;
}
sb.append(path);
}
return sb.toString();
//update-end---author:jeecg---for:网关基址头只接受纯路径---
} catch (Exception e) {
return null;
}

View File

@ -164,6 +164,50 @@ public class Issue9695_OpenApiSsrfHeaderInjectionTest {
}
}
// ==================================================================
// 二·补:网关头只接受纯路径 —— 拒绝查询串/片段/路径穿越
// 漏洞:validateGatewayBasePath 原用 uri.getPath()(解码后)重建基址,未拒绝
// 解码后出现的 ? / #,攻击者传 X_GATEWAY_BASE_PATH: http://127.0.0.1:8080/jeecg-boot%3f
// (%3f 解码为 ?)即可篡改转发目标。修复:拒绝 rawQuery/rawFragment,且 path 走字符白名单、禁止 ..
// ==================================================================
@Nested
@DisplayName("网关头纯路径校验 —— 拒绝查询串/片段/路径穿越")
class GatewayHeaderPathOnly {
@Test
@DisplayName("【漏洞复现】编码问号 %3f 篡改转发目标 → 被拒绝返回 null")
void encodedQuestionMarkRejected() {
// 实测利用载荷:%3f 解码为 ?,原逻辑会把基址变为 http://127.0.0.1:8080/jeecg-boot? 打到上下文根
assertNull(CommonUtils.validateGatewayBasePathForDomain("http://127.0.0.1:8080/jeecg-boot%3f"),
"解码后路径含 ? 必须被拒绝");
assertNull(CommonUtils.validateGatewayBasePathForDomain("http://127.0.0.1:8080/jeecg-boot%23frag"),
"解码后路径含 # 必须被拒绝");
}
@Test
@DisplayName("字面量查询串 ? 与片段 # 被拒绝 → 返回 null")
void rawQueryAndFragmentRejected() {
assertNull(CommonUtils.validateGatewayBasePathForDomain("http://127.0.0.1:8080/jeecg-boot?x=1"));
assertNull(CommonUtils.validateGatewayBasePathForDomain("http://127.0.0.1:8080/jeecg-boot#frag"));
}
@Test
@DisplayName("路径穿越 .. 被拒绝 → 返回 null")
void pathTraversalRejected() {
assertNull(CommonUtils.validateGatewayBasePathForDomain("http://127.0.0.1:8080/jeecg-boot/../actuator"));
}
@Test
@DisplayName("正常纯路径基址仍被采纳(不误伤合法网关头)")
void plainPathStillAccepted() {
assertEquals("http://127.0.0.1:8080/jeecg-boot",
CommonUtils.validateGatewayBasePathForDomain("http://127.0.0.1:8080/jeecg-boot"));
assertEquals("http://10.0.0.5:9999/jeecg-boot",
CommonUtils.validateGatewayBasePathForDomain("http://10.0.0.5:9999/jeecg-boot"));
}
}
// ==================================================================
// 三、SSRF 兜底:checkInternalUrl 仅放行内网地址
// ==================================================================