fix(护栏自证): 切片桩不冒充产品端点;版本闸门少一处异常控制流,把 catch 棘轮记回原基线

把这期后端护栏类并进"实跑档"以后,一次 mvn 就当夜逼出两处真红。两处都是测试件/我自己
上一笔提交的问题,不是产品逻辑错,但都必须改代码而不是改基线:

1) 上一版切片的桩控制器写了 GET /api/admin/users,与真 AdminUserController#listUsers
   撞在同一条完整映射上。MappedPathProbe 把整棵 main 树的映射枚举进同一个
   RequestMappingHandlerMapping,于是 ApiGuardCoverageBreachTest 先炸 Ambiguous mapping
   (2 errors);把桩路径改成 /api/client/slice-ping、/api/admin/slice-probe 后又暴露第二层:
   测试桩本身被当成"新增未设防端点"进判定 ⇒ 探针改为不登记测试夹具里的控制器
   (按最外层类名 Test/Tests/IT 排除,main 树里这种命名的真控制器实测 0 个)。
   分母守卫照旧有效:mapped.size() > 300 那条断言仍在,排掉桩不会把清单清成空的。

2) CatchSilenceRatchetTest 红的是我自己 9d762c55(20:25 提交)里那处
   `catch (NumberFormatException overflow) { return null; }`:total 1002→1003、
   不重抛 559→560。也就是说**那笔提交是带着一支红的护栏类交进去的**——上一轮我只把切片
   纳入实跑分母,没把这支棘轮一起跑,所以"7 例绿"的读数过期了。
   修法不是抬基线:版本号的单段长度超过 18 位本来就该按"判不出来"处理,于是用
   MAX_SEGMENT_DIGITS 上限替掉 try/catch,判定走向逐字不变(仍是 return null ⇒ 不拒),
   total 与不重抛都记回 1002/559,验收基线一格没动。RETHROW 下限 388、logOnly 172、
   empty 56 三档本轮都没碰。
   顺手改掉失败文案里的错算式(原写"= 验收基线 1000 + 1 处",实际那 2 处各自有出处)。

版本闸门本身仍未上线(现网 jar 是 12:3x 那版,9d762c55 在 20:25)⇒ 本改动零现网影响。

实跑:mvn -o test -Dtest=ClientVersionGuardContextSliceTest,ApiGuardCoverageBreachTest,
ApiGuardCoverageAuditorTest,ApiGuardCoverageAuditorWiringTest,CatchSilenceRatchetTest,
ClientVersionGuardFilterTest,BuildInfoLoggerTest
⇒ Tests run: 53, Failures: 0, Errors: 0 / BUILD SUCCESS(逐类 6/10/8/2/7/12/8)。
This commit is contained in:
2026-10-09 00:23:14 +08:00
parent 4e154e5318
commit 97ca6810f7
4 changed files with 33 additions and 12 deletions
@@ -203,6 +203,12 @@ public class ClientVersionGuardFilter extends OncePerRequestFilter {
* 解析点分数字版本:{@code 4.0.46} → {@code [4,0,46]};任何一段非数字(含空串、{@code 4.x})返回 null。
* 故意不做"取前缀数字"这类容错:猜出来的版本号会被当成合法装机统计进去。
*/
/** 单段允许的最大位数:Long.MAX 是 19 位,取 18 位保证 {@code parseLong} 永不溢出。
* 超过它就按「判不出来」走 {@code null}(不拒),与原先包 try/catch 时的走向一致;
* 少一处异常控制流是为了让 catch 静默棘轮记回原基线,不是为了改判定。
*/
private static final int MAX_SEGMENT_DIGITS = 18;
static long[] parse(String version) {
if (version == null) {
return null;
@@ -218,11 +224,10 @@ public class ClientVersionGuardFilter extends OncePerRequestFilter {
if (part.isEmpty() || !isAllDigits(part)) {
return null;
}
try {
segments[i] = Long.parseLong(part);
} catch (NumberFormatException overflow) {
if (part.length() > MAX_SEGMENT_DIGITS) {
return null;
}
segments[i] = Long.parseLong(part);
}
return segments;
}
@@ -137,7 +137,7 @@ class CatchSilenceRatchetTest {
Result r = tree();
assertTrue(r.total() <= STRICT_TOTAL,
() -> "catch 总数超过 " + STRICT_TOTAL + "(= 验收基线 " + GIVEN_TOTAL
+ " + 本轮拒起闸门新增的那 1 处重抛型 catch):现在是 " + r.total()
+ " 再加上 STRICT_TOTAL 常量上方逐处记名的两处评审增量):现在是 " + r.total()
+ "。新增 catch 块要么别加,要么同批减掉一处同形状的");
assertTrue(r.count(Kind.RETHROW) >= RETHROW_FLOOR,
() -> "重抛型 catch 跌到 " + r.count(Kind.RETHROW) + ",低于下限 " + RETHROW_FLOOR
@@ -69,16 +69,22 @@ class ClientVersionGuardContextSliceTest {
static class SliceConfig {
}
/**
* 桩路径必须留在受管前缀 {@code /api/client} 内(闸门按前缀决定判不判),但不许复用真控制器的
* 完整映射:同一个容器里两条相同映射是 {@code Ambiguous mapping},而 {@code ApiGuardCoverageBreachTest}
* 用 {@code MappedPathProbe} 把整棵 main 树的映射枚举进同一个 {@code RequestMappingHandlerMapping}——
* 上一版这里写 {@code GET /api/admin/users},就把那支用例连带打成 2 errors(真凶是测试件,不是产品码)。
*/
@RestController
static class SlicePingController {
@PostMapping("/api/client/ping")
@PostMapping("/api/client/slice-ping")
String clientPing(@RequestBody(required = false) String body,
@RequestHeader(value = "X-Client-Version", required = false) String version) {
return "PONG:" + (version == null ? "-" : version);
}
@GetMapping("/api/admin/users")
@GetMapping("/api/admin/slice-probe")
String adminPath() {
return "ADMIN";
}
@@ -95,7 +101,7 @@ class ClientVersionGuardContextSliceTest {
@DisplayName("properties 里的地板值真被 @Value 绑定(纯 mock 用例证不到这一层)")
void floorComesFromProperties() throws Exception {
// 4.0.10 < 4.0.40:若属性没绑进闸门,minSupportedVersion 会是空串 ⇒ 闸门不配置 ⇒ 放行到 controller
MvcResult result = mockMvc.perform(post("/api/client/ping")
MvcResult result = mockMvc.perform(post("/api/client/slice-ping")
.contentType(MediaType.APPLICATION_JSON)
.header("X-Client-Version", "4.0.10")
.content("{}"))
@@ -111,7 +117,7 @@ class ClientVersionGuardContextSliceTest {
@Test
@DisplayName("版本达标时过滤器放行,请求真的走到 controller")
void allowedVersionReachesController() throws Exception {
mockMvc.perform(post("/api/client/ping")
mockMvc.perform(post("/api/client/slice-ping")
.contentType(MediaType.APPLICATION_JSON)
.header("X-Client-Version", "4.0.46")
.content("{}"))
@@ -122,7 +128,7 @@ class ClientVersionGuardContextSliceTest {
@Test
@DisplayName("未上报版本默认不拒(存量装机不受影响),仍走到 controller")
void unreportedIsSparedByDefault() throws Exception {
mockMvc.perform(post("/api/client/ping").contentType(MediaType.APPLICATION_JSON).content("{}"))
mockMvc.perform(post("/api/client/slice-ping").contentType(MediaType.APPLICATION_JSON).content("{}"))
.andExpect(status().isOk())
.andExpect(content().string("PONG:-"));
}
@@ -130,7 +136,7 @@ class ClientVersionGuardContextSliceTest {
@Test
@DisplayName("非客户端路径不参与判定(后台与内部令牌通道零影响)")
void nonClientPathUntouched() throws Exception {
mockMvc.perform(get("/api/admin/users").header("X-Client-Version", "0.0.1"))
mockMvc.perform(get("/api/admin/slice-probe").header("X-Client-Version", "0.0.1"))
.andExpect(status().isOk())
.andExpect(content().string("ADMIN"));
}
@@ -138,7 +144,7 @@ class ClientVersionGuardContextSliceTest {
@Test
@DisplayName("拒绝形态是 200 + success=false + code 4012(与全仓业务拒绝口径一致)")
void rejectionKeepsAppConvention() throws Exception {
MvcResult result = mockMvc.perform(post("/api/client/ping")
MvcResult result = mockMvc.perform(post("/api/client/slice-ping")
.contentType(MediaType.APPLICATION_JSON)
.header("X-Client-Version", "3.9.9")
.content("{}"))
@@ -88,8 +88,18 @@ final class MappedPathProbe {
scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
List<Class<?>> out = new ArrayList<>();
scanner.findCandidateComponents(BASE_PACKAGE).forEach(cd -> {
String binary = cd.getBeanClassName();
// 测试夹具里的 @RestController(切片桩)不是产品端点:登记进来会让映射清单凭空多出
// 真树里没有的条目,桩与真控制器撞同一条完整路径时还会当场抛 Ambiguous mapping
// (上一版切片写 GET /api/admin/users,就是把探针宿主用例打成 2 errors 的原因)。
int dollar = binary.indexOf('$');
String outermost = dollar < 0 ? binary : binary.substring(0, dollar);
if (outermost.endsWith("Test") || outermost.endsWith("Tests")
|| outermost.endsWith("IT")) {
return;
}
try {
Class<?> type = Class.forName(cd.getBeanClassName());
Class<?> type = Class.forName(binary);
boolean handler = AnnotatedElementUtils.hasAnnotation(type, Controller.class)
|| AnnotatedElementUtils.hasAnnotation(type, RequestMapping.class);
// @ControllerAdvice / @RestControllerAdvice 不是 handler,MVC 不会为它登记映射