feat(更新链路): web_config 加 sha256(V142),/api/version/latest 回带包摘要

客户端自更新过去只验 zip 结构:桶里的包被换掉时,结构完整的恶意 zip 会直接交给
update.exe 解压执行。摘要必须由服务端给出才与下载来源无关——能替换对象的人替换不了这个接口。

- V142 加 sha256 列(可空、不回填历史行,列缺失时客户端行为不变);
- /latest、公开列表、后台上传响应都回带摘要;multipart 上传按真正落盘的字节算;
- 直传确认路径不重算摘要(那要把 100MB 对象拉回来),这类行留空由发布脚本回填。

盘点文档同步:V1..V142、141 个文件、后续从 V143 取号。
This commit is contained in:
2026-10-03 15:41:49 +08:00
parent 37592a3b62
commit 4b7405895d
5 changed files with 141 additions and 5 deletions
@@ -10,6 +10,7 @@ import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.mock.web.MockMultipartFile;
import java.time.LocalDateTime;
import java.util.List;
@@ -163,4 +164,61 @@ class SoftwareVersionServiceTest {
verify(versionMapper, never()).markLatestById(any());
}
@Test
void latestResponseCarriesThePackageDigest() {
SoftwareVersionEntity withDigest = row(1, "4.0.43", true, 0);
withDigest.setSha256("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
when(versionMapper.selectOne(any())).thenReturn(withDigest);
Map<String, Object> result = service().latestSoftwareVersion();
// 客户端拿它比对下载到的 zip:这是更新链路唯一的、换包的人改不动的锚点
assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
result.get("sha256"));
}
@Test
void legacyRowWithoutDigestReportsEmptyStringNotNull() {
when(versionMapper.selectOne(any())).thenReturn(row(1, "4.0.32", true, 0));
Map<String, Object> result = service().latestSoftwareVersion();
// 空串 = 「服务端还没登记这个包的摘要」,客户端据此退回旧的只验 zip 结构;
// null 会被 JSON 省略掉,客户端就分不清"没有摘要"和"服务端是旧版本没这个字段"
assertEquals("", result.get("sha256"));
}
@Test
void sha256HexMatchesTheKnownVector() throws Exception {
java.io.File file = java.io.File.createTempFile("digest-vector-", ".zip");
try {
java.nio.file.Files.write(file.toPath(), new byte[]{'a', 'b', 'c'});
assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
SoftwareVersionService.sha256Hex(file), "\"abc\" 的 SHA-256 是公开向量");
} finally {
file.delete();
}
}
@Test
void multipartUploadStoresTheDigestOfTheUploadedBytes() throws Exception {
// 摘要必须算在真正落到对象存储的那份字节上:先 transferTo 再 digest,顺序反了就锚错对象
MockMultipartFile upload = new MockMultipartFile("file", "4.0.44.zip",
"application/zip", new byte[]{'a', 'b', 'c'});
String objectKey = SoftwareVersionService.STORAGE_PATH_PREFIX + "4.0.44.zip";
when(ossStorageService.uploadSoftwareVersionPackage(any(), eq(objectKey)))
.thenReturn("https://oss.aishufu.top/" + objectKey);
ArgumentCaptor<SoftwareVersionEntity> inserted = ArgumentCaptor.forClass(SoftwareVersionEntity.class);
when(versionMapper.insert(inserted.capture())).thenAnswer(invocation -> {
((SoftwareVersionEntity) invocation.getArgument(0)).setId(300L);
return 1;
});
service().uploadSoftwareVersion("4.0.44", upload);
assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
inserted.getValue().getSha256());
}
}