From e61f05c98cfd2cef067f3303965b543097d733eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Mon, 2 Nov 2026 21:14:21 +0800 Subject: [PATCH] =?UTF-8?q?test(library):=20=E8=B7=A8SHA=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E4=BF=9D=E7=95=99=E4=B8=BA=E6=8B=92=E7=BB=9D?= =?UTF-8?q?RED?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 同协议批次下换一个合法 40 位 SHA 不能被 epoch 相等证明;激活与使用点都必须 拒绝。当前实现仍会接受(compatible_upgrade=true),此提交先把期望钉在拒绝上, 作为修复前的 RED 证据;同 SHA 正例与同 SHA 错 display 拒绝一并保留。 --- ...ary_restore_dispatch_final_binding_test.go | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/internal/platform/postgres/library_restore_dispatch_final_binding_test.go b/internal/platform/postgres/library_restore_dispatch_final_binding_test.go index fd7727b..6ac906f 100644 --- a/internal/platform/postgres/library_restore_dispatch_final_binding_test.go +++ b/internal/platform/postgres/library_restore_dispatch_final_binding_test.go @@ -223,24 +223,42 @@ VALUES ($1,$2,'internal',$3,'instance',$4::jsonb,'closed','needs_restore','','', t.Fatalf("rejected evidence created %d activation rows", n) } - // 5) 合法兼容升级:不同完整 SHA、同一协议批次 ⇒ 允许,但必须显式记成升级。 - compat := base() - compat.ProtocolBuild = "0.1.999-compatible" - compat.ProtocolBuildSHA = strings.Repeat("a", 40) - if err := activate(compat); err != nil { - t.Fatalf("compatible upgrade evidence: %v", err) + // 5) 同一协议批次、任意 40 位 SHA:epoch 相等不能替代构建身份证明,必须拒绝。 + // 原实现只凭 epoch==3 就接受任何 SHA(compatible_upgrade=true),换一个 SHA 即可 + // 绕开同 SHA display 绑定;本轮关闭这条无证据的跨 SHA 自动兼容。 + evidence = base() + evidence.ProtocolBuild = version.DisplayVersion() + evidence.ProtocolBuildSHA = strings.Repeat("b", 40) + if err := activate(evidence); err == nil || !strings.Contains(err.Error(), "cross-sha") { + t.Fatalf("same-epoch unknown build sha was accepted: %v", err) + } + + // 6) 同 SHA、错 display:运行二进制身份必须自洽。 + evidence = base() + evidence.ProtocolBuild = "0.1.999-wrong" + if err := activate(evidence); err == nil || !strings.Contains(err.Error(), "display") { + t.Fatalf("same-sha wrong display = %v", err) + } + if n := count(); n != 0 { + t.Fatalf("rejected evidence created %d activation rows", n) + } + + // 7) 同 SHA、同 display:唯一允许的绑定;台账不得再声明 compatible_upgrade。 + if err := activate(base()); err != nil { + t.Fatalf("same-sha activation: %v", err) } var stored string if err := pool.QueryRow(ctx, `SELECT evidence::text FROM library_index_restore_dispatch_activation WHERE target_identity=$1`, target).Scan(&stored); err != nil { t.Fatal(err) } compact := strings.ReplaceAll(stored, " ", "") - if !strings.Contains(compact, `"compatible_upgrade":true`) || - !strings.Contains(compact, `"system_identifier":"`+compat.GovernanceAuthority.SystemIdentifier+`"`) { - t.Fatalf("activation record does not state the verified authority/upgrade: %s", stored) + if !strings.Contains(compact, `"compatible_upgrade":false`) || + !strings.Contains(compact, `"protocol_build_sha":"`+version.CommitSHA+`"`) || + !strings.Contains(compact, `"system_identifier":"`+base().GovernanceAuthority.SystemIdentifier+`"`) { + t.Fatalf("activation record does not state the exact build/authority: %s", stored) } - // 使用点正例:同一兼容批次下真实窗口可以准备/取得许可。 + // 使用点正例:与激活相同的 SHA 下真实窗口可以准备/取得许可。 windowOp := seedOperationJob(t, store, pool, "internal", "libsync-ds01-authority", "sync", now) if _, err := store.BeginLibraryAttempt(ctx, "libsync-ds01-authority", "worker-a#q1#0", 10*time.Minute, now); err != nil { t.Fatal(err) @@ -270,6 +288,10 @@ VALUES ($1,$2,'internal',$3,'instance',$4::jsonb,'closed','needs_restore','','', "target-ds01-tamper-sha": func(v map[string]any) { v["protocol_build_sha"] = "unknown" }, + "target-ds01-tamper-cross-sha": func(v map[string]any) { + // 同一协议批次下换一个合法 40 位 SHA:使用点必须与激活点同样拒绝。 + v["protocol_build_sha"] = strings.Repeat("c", 40) + }, } for tamperTarget, mutate := range tamperTargets { var record map[string]any