feat(鉴权自检): 现网首报 5 条逐条定性——补"方法级已设防"一格与 4 条重定向壳
jar 60bd8430 上线后主机B 报出 API_GUARD_UNCOVERED count=5:
/、/admin、/admin.html、/login.html 都是 AdminConsoleController 的 sendRedirect 兜底(不读库、不回数据),
进 PUBLIC_BY_DESIGN;/api/client/credentials 的处理器第一行就是 adminAuthSupport.requireUser(request),
它是"第二道门已到位、只欠进第一道门(前缀名单)",不是匿名可达。
自检原本只认前缀名单这一道门,所以整格缺失会把已设防的端点报成欠账——报警不可信,比不报更糟。
新增 METHOD_GUARDED_PATHS 第五格,并要求每条由测试回到源码逐处理器方法体核对确实带 require*:
同一支抽取器在 /admin.html(纯重定向)上必须判"没有 require",
否则"整类里出现过 adminAuthSupport"就能喂饱这条判据、清单就成了自我豁免。
顺带修判据自己的两处取样错误:方法级正则在 @GetMapping({...}) 的数组里只抓第一条
(于是 /admin、/admin.html、/login.html 被误判成"没人映射"),以及根路径 "/" 被裁尾斜杠裁成空串。
This commit is contained in:
@@ -50,8 +50,31 @@ public class ApiGuardCoverageAuditor {
|
||||
"/api/version", // 客户端启动时拉最新版本号,必须先于登录
|
||||
"/api/tutorial", // 后台可维护的使用教程,公开内容
|
||||
"/api/kd-flow/verify", // 开店流程访问密码校验:它自己就是那道门
|
||||
// 下面四条都是 AdminConsoleController 的 302 兜底(response.sendRedirect 完事,
|
||||
// 不读库、不回数据),旧书签打进这些地址只会被送到 /admin-vue/。
|
||||
// 前缀匹配语义是 equals || startsWith(prefix + "/"),所以 "/" 只盖住根路径本身。
|
||||
"/",
|
||||
"/admin", // → /admin-vue/
|
||||
"/admin.html", // 旧入口兜底
|
||||
"/login.html", // 旧入口兜底
|
||||
"/error");
|
||||
|
||||
/**
|
||||
* 第二道门已到位、只欠进第一道门(前缀名单)的路径。
|
||||
*
|
||||
* <p>鉴权有两道:过滤器的前缀名单,和控制器方法体里的 {@code require*}。自检只看前者,
|
||||
* 所以后者设防、前缀没进名单的端点会被报成"匿名可达"。2026-10-08 现网首跑报出的 5 条里
|
||||
* 就有这一条:{@code ClientCredentialController.credentials:39} 第一行就是
|
||||
* {@code adminAuthSupport.requireUser(request)},拿到紫鸟口令的前提是登录态。
|
||||
*
|
||||
* <p>这一格**不是许可**:登记它等于承认"该前缀该进 USER_TOOL_PREFIXES 却还没进"。
|
||||
* 每条都由测试回到源码逐方法体核对确实带 {@code require*}(见
|
||||
* {@code ApiGuardCoverageAuditorTest#methodGuardedEntriesAreActuallyGatedInSource}),
|
||||
* 往里塞一条没设防的路径会红。
|
||||
*/
|
||||
static final Set<String> METHOD_GUARDED_PATHS = Set.of(
|
||||
"/api/client/credentials");
|
||||
|
||||
/**
|
||||
* 待收欠账:数据按 user_id 归属、却既不进守卫名单也没有 require* 的前缀。
|
||||
* 划掉它们的条件是"进 USER_TOOL_PREFIXES、并在 user-tool-guard-enabled 打开后回归 covered",
|
||||
@@ -90,8 +113,9 @@ public class ApiGuardCoverageAuditor {
|
||||
public void auditOnBoot() {
|
||||
Report report = audit(mappedPaths());
|
||||
if (report.uncovered().isEmpty()) {
|
||||
log.info("[api-guard-coverage] mapped={} covered={} public={} pending={} uncovered=0",
|
||||
report.mapped(), report.covered(), report.publicPaths().size(), report.pending().size());
|
||||
log.info("[api-guard-coverage] mapped={} covered={} public={} methodGuarded={} pending={} uncovered=0",
|
||||
report.mapped(), report.covered(), report.publicPaths().size(),
|
||||
report.methodGuarded().size(), report.pending().size());
|
||||
return;
|
||||
}
|
||||
log.warn("[api-guard-coverage] {} count={} endpoints={} —— 这些端点不在守卫前缀里、也不在公开/待收清单里,"
|
||||
@@ -104,12 +128,15 @@ public class ApiGuardCoverageAuditor {
|
||||
List<String> uncovered = new ArrayList<>();
|
||||
Set<String> publicPaths = new LinkedHashSet<>();
|
||||
Set<String> pending = new LinkedHashSet<>();
|
||||
Set<String> methodGuarded = new LinkedHashSet<>();
|
||||
int covered = 0;
|
||||
for (String path : paths) {
|
||||
if (matchesAny(path, AdminApiGuardFilter.guardedPrefixes())) {
|
||||
covered++;
|
||||
} else if (matchesAny(path, PUBLIC_BY_DESIGN)) {
|
||||
publicPaths.add(path);
|
||||
} else if (matchesAny(path, METHOD_GUARDED_PATHS)) {
|
||||
methodGuarded.add(path);
|
||||
} else if (matchesAny(path, PENDING_CONVERGENCE)) {
|
||||
pending.add(path);
|
||||
} else {
|
||||
@@ -117,7 +144,7 @@ public class ApiGuardCoverageAuditor {
|
||||
}
|
||||
}
|
||||
return new Report(paths.size(), covered, List.copyOf(publicPaths), List.copyOf(pending),
|
||||
List.copyOf(new LinkedHashSet<>(uncovered)));
|
||||
List.copyOf(methodGuarded), List.copyOf(new LinkedHashSet<>(uncovered)));
|
||||
}
|
||||
|
||||
private static boolean matchesAny(String path, Collection<String> prefixes) {
|
||||
@@ -137,6 +164,7 @@ public class ApiGuardCoverageAuditor {
|
||||
return paths;
|
||||
}
|
||||
|
||||
record Report(int mapped, int covered, List<String> publicPaths, List<String> pending, List<String> uncovered) {
|
||||
record Report(int mapped, int covered, List<String> publicPaths, List<String> pending,
|
||||
List<String> methodGuarded, List<String> uncovered) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user