* feat(proto-defs): add flash-transfer OIDB proto types QQ 闪传(FlashTransfer / fileset)的 protobuf 类型定义: - FlashFileEntry(0x93d2/0x93d3 文件条目) - 0x93cf 申请 fileSet / 0x93d0 commit / 0x93db 完成 / 0x93d1 设状态 - 0x93d3 文件集详情 / 0x93d4 下载 URL(含主文件 fileId + 缩略图) - 0x12a9 sub=100/103/200 prepare/apply/get-download - sliceupload body + Sha1StateV - 0x9407 删除 / 0x9427 重命名 / 0x93d7 发送 注册到 oidb-actions/index.ts,package.json 加 exports。 * feat(protocol): add flash-transfer OIDB services + sha1-stream 闪传协议层(OIDB namespace,复用 invokeOidb/OidbCallSpec): - apply-fileset / commit-file / complete-fileset / set-status(上传流程) - prepare-upload (0x12a9_100) / apply-upload (0x12a9_103)(sliceupload 凭证) - get-fileset-detail (0x93d3) / list-filesets (0x93d2) / get-download-url (0x93d4) - get-flash-download (0x12a9_200,主文件下载直链) - delete-file (0x9407) / rename-file (0x9427,sub=0+uinForm) / send-flash (0x93d7) - sha1-stream:SHA1 中间 state 计算器(sliceupload f107.f6 需要,Node crypto 不支持) - byte-oracle 单测(apply/prepare/get-download-url/send-flash)+ sha1-stream 单测 package.json 加 exports。 * feat(core): add FlashTransferApi facade FlashTransferApi 编排闪传全流程,复用 OIDB 基础设施(invokeOidb)+ computeHashes/loadBinarySource/fetch: - createFlashTask:统一走 sub=100/103+sliceupload(含缩略图),不走小文件 PUT (PUT 不上报主文件 sha1,fileset 卡在未完成;QQ 客户端小文件也走 sliceupload) - buildFileId:主文件 appid=14901(进 f14.f1),缩略图 png=14903/jpg=14902 - uploadThumbnail:随机纯色 PNG 占位(手写编码,不引入图像库),关联主文件下载入口 - getFilesetInfo/getFlashFileList/getShareLink/getFlashFileUrl/downloadFileset - sendFlashMsg(私聊 uid / 群聊 groupId)/ getFilesetIdByCode(网页解析) - deleteFlashFile / renameFlashFile / listFilesets 注册到 ApiHub(flashTransfer 字段)+ export。 * feat(onebot): add flash-transfer actions OneBot action(extended.ts)10 个: create_flash_task / get_fileset_info / get_flash_file_list / get_flash_file_url / get_share_link / download_fileset / send_flash_msg(私聊 user_id / 群聊 group_id)/ get_fileset_id / rename_flash_file / delete_flash_file MCP catalog(生成文件)同步新增 action 元数据。 extended-actions 接线单测覆盖。 * fix(onebot): align flash file list field name with NapCat flashFileInfoToJson 的文件大小字段从 file_size 改为 size,对齐 NapCat get_flash_file_list 响应(NapCat 用 size)。get_fileset_info 的 file_list 复用同一映射,一并生效。便于 NapCat 客户端 drop-in 迁移。 * feat(proto-defs): support multi-file commit in flash-transfer 0x93d0 commit 的 f4 改为 repeated,一个请求携带 fileset 内全部文件条目。 FlashCommitFileInfo 的 f3/f5/f6/f7/f10/f12 用 pb_optional,强制 0 值上 wire (服务端多文件时严格校验字段存在,省略 0 会导致后续文件不计入 fileset)。 * feat(protocol): parameterize flash-transfer upload for multi-file CommitFile.Params 改为 entries 数组,serialize 输出 repeated f4,每条 f6=文件序号。 PrepareUpload/ApplyUpload 加 fileIndex + formatCode 参数:filesetWrap.f4 用 fileIndex (与 commit f6 对齐),f7 用 formatCode(mp4=2 非 26),f9=1。 * feat(core): multi-file flash-transfer upload createFlashTask 支持多文件共用一个 fileset。关键改动: - ApplyFileset 多文件时 fileName 用「<首文件名>等N个文件」、fileSize 用总和, 服务端据此判定 fileset 为多文件,commit 的后续 entry 才被计入 - 一次性 CommitFile 提交所有文件元数据(f4 repeated) - 两阶段上传:先全部 prepare+apply 注册 fileId,再全部 sliceupload 落盘 - 拆分原 uploadLargeFile 为 prepareAndApply + sliceuploadFile - fileTypeCode mp4 formatCode 26→2(与 commit f7/filesetWrap.f7 一致) * feat(onebot): multi-file create_flash_task + list_filesets action create_flash_task 的 files 参数支持 string 或 string[],多文件共用一个 fileset。 新增 list_filesets action(facade.listFilesets 之前未暴露)。MCP catalog 同步更新 files schema(anyOf string/array)与 list_filesets 条目。 * fix(flash-transfer): multi-file download by fileIndex 0x93d4 响应 f1.f3 是 repeated fileInfo(多文件 fileset 每个文件一条,f6=序号、 f14=主文件 fileId),之前定义成单个导致多文件只能拿到第一个文件。 FlashDownloadEntry.fileInfo 改 pb_repeated,GetDownloadUrl 返回所有文件元信息, download_fileset 按 fileIndex 选中目标文件走 0x12a9 sub=200 拿直链。 * test(protocol): inline byte-oracle hex fixtures 两个 flash-transfer byte-oracle 测试原先用 readFileSync 读仓库外的 .claude/reverse/scripts/proto/*.hex(本地逆向笔记目录),CI 上不存在导致失败。 把 hex 内容内联成常量,测试自包含。 * fix(flash-transfer): address review feedback 1. get_flash_file_url 接 file_index 参数并在 action 透传(之前漏接,多文件永远返回首文件) 2. getFileDownload 越界 fileIndex 不再静默回退首文件,找不到返回 null 3. postSliceupload status 缺失不再当成功,非 success 一律抛错 4. computeSha1StateV 改 O(n) 增量 update(之前每片 reset 重算整个前缀,O(n²)) 5. get_flash_file_url 找不到文件抛错(之前返回空串当成功) 6. create_flash_task files 数组拒绝空字符串
191 lines
8.4 KiB
TypeScript
191 lines
8.4 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { createHash } from 'node:crypto';
|
||
import { Sha1Stream, computeSha1StateV } from '@snowluma/protocol/highway/sha1-stream';
|
||
|
||
// SHA1 标准填充:msg + 0x80 + 0x00...(k 个) + 8 字节大端 bit 长度,总长为 64 倍数。
|
||
// 用于把任意输入变成「已 finalize」的完整 block 序列——这样 Sha1Stream 对它逐 block
|
||
// transform 后取大端 state,应当等于 Node crypto 对原始 msg 的 digest。
|
||
function sha1Pad(msg: Uint8Array): Uint8Array {
|
||
const msgLen = msg.length;
|
||
const k = (56 - ((msgLen + 1) % 64) + 64) % 64;
|
||
const padded = new Uint8Array(msgLen + 1 + k + 8);
|
||
padded.set(msg, 0);
|
||
padded[msgLen] = 0x80;
|
||
const view = new DataView(padded.buffer, padded.byteOffset + padded.byteLength - 8, 8);
|
||
view.setBigUint64(0, BigInt(msgLen) * 8n, false); // 大端 bit 长度
|
||
return padded;
|
||
}
|
||
|
||
// padding oracle 的核心:Node crypto 的 SHA1 digest 内部就是对 padding 后的输入逐 block
|
||
// transform、取最终 state 的大端表示。Sha1Stream 自行实现 transform 且不 finalize,
|
||
// 因此对「已 padding 的输入」transform 后大端输出必须等于 digest。Node crypto 走
|
||
// OpenSSL,与本实现完全独立,是验证 transform 正确性的可靠参照。
|
||
function expectSha1StreamMatchesCrypto(msg: Uint8Array): void {
|
||
const expected = createHash('sha1').update(Buffer.from(msg)).digest();
|
||
const stream = new Sha1Stream();
|
||
stream.update(sha1Pad(msg));
|
||
const bigEndian = stream.hash(false);
|
||
expect(Buffer.from(bigEndian).equals(expected)).toBe(true);
|
||
}
|
||
|
||
describe('Sha1Stream transform (padding oracle vs Node crypto)', () => {
|
||
it('matches for empty input (single padded block)', () => {
|
||
expectSha1StreamMatchesCrypto(Buffer.alloc(0));
|
||
});
|
||
|
||
it('matches for "abc" (single padded block)', () => {
|
||
expectSha1StreamMatchesCrypto(Buffer.from('abc', 'utf8'));
|
||
});
|
||
|
||
it('matches across the 56-byte boundary (forces a second padded block)', () => {
|
||
// 56 字节:0x80 后只剩 0 字节给 zeros 不够放 8 字节长度,padding 溢出到第二块。
|
||
expectSha1StreamMatchesCrypto(Buffer.alloc(56, 0x41));
|
||
});
|
||
|
||
it('matches for exactly one block (64 bytes → two padded blocks)', () => {
|
||
expectSha1StreamMatchesCrypto(Buffer.alloc(64, 0x42));
|
||
});
|
||
|
||
it('matches for multi-block input (200 bytes → four padded blocks)', () => {
|
||
expectSha1StreamMatchesCrypto(Buffer.alloc(200, 0x43));
|
||
});
|
||
|
||
it('matches for pseudo-random multi-block input', () => {
|
||
const data = Buffer.alloc(313, 0);
|
||
for (let i = 0; i < data.length; i++) data[i] = (i * 1103515245 + 12345) & 0xff;
|
||
expectSha1StreamMatchesCrypto(data);
|
||
});
|
||
});
|
||
|
||
describe('Sha1Stream update chunk-equivalence', () => {
|
||
// update 内部维护 64 字节 buffer + 128 位 bit 计数;分块喂入必须与一次性喂入
|
||
// 产生相同的内部 state。切分点覆盖 block 边界附近以暴露 buffer/count 管理 bug。
|
||
it('two-chunk split at various boundaries yields the same state as one update', () => {
|
||
const data = Buffer.alloc(200, 0x44);
|
||
const full = new Sha1Stream();
|
||
full.update(data);
|
||
const fullHash = Buffer.from(full.hash(true));
|
||
for (const split of [0, 1, 32, 63, 64, 65, 127, 128, 129, 199]) {
|
||
const s = new Sha1Stream();
|
||
s.update(data.subarray(0, split));
|
||
s.update(data.subarray(split));
|
||
expect(Buffer.from(s.hash(true)).equals(fullHash)).toBe(true);
|
||
}
|
||
});
|
||
|
||
it('three-chunk split yields the same state as one update', () => {
|
||
const data = Buffer.alloc(300, 0x45);
|
||
const full = new Sha1Stream();
|
||
full.update(data);
|
||
const fullHash = Buffer.from(full.hash(true));
|
||
const s = new Sha1Stream();
|
||
s.update(data.subarray(0, 100));
|
||
s.update(data.subarray(100, 200));
|
||
s.update(data.subarray(200));
|
||
expect(Buffer.from(s.hash(true)).equals(fullHash)).toBe(true);
|
||
});
|
||
|
||
it('empty updates do not change the state', () => {
|
||
const data = Buffer.alloc(130, 0x46);
|
||
const full = new Sha1Stream();
|
||
full.update(data);
|
||
const fullHash = Buffer.from(full.hash(true));
|
||
const s = new Sha1Stream();
|
||
s.update(Buffer.alloc(0));
|
||
s.update(data);
|
||
s.update(Buffer.alloc(0));
|
||
expect(Buffer.from(s.hash(true)).equals(fullHash)).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe('Sha1Stream hash endianness', () => {
|
||
// hash(true) 小端与 hash(false) 大端是同一组 5 个 state word 的不同字节序表示。
|
||
// 结合上面的 padding oracle(大端 == digest),即可间接确认小端中间 state 的值。
|
||
it('little-endian is the per-word byte-swap of big-endian', () => {
|
||
const s = new Sha1Stream();
|
||
s.update(Buffer.alloc(64, 0x47));
|
||
const le = Buffer.from(s.hash(true));
|
||
const be = Buffer.from(s.hash(false));
|
||
expect(le.length).toBe(20);
|
||
for (let i = 0; i < 5; i++) {
|
||
expect(le.readUInt32LE(i * 4)).toBe(be.readUInt32BE(i * 4));
|
||
}
|
||
});
|
||
|
||
it('reset restores the initial SHA1 state', () => {
|
||
const s = new Sha1Stream();
|
||
s.update(Buffer.alloc(200, 0x48));
|
||
s.reset();
|
||
const initial = Buffer.from(s.hash(false));
|
||
const expected = Buffer.from([
|
||
0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89,
|
||
0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76,
|
||
0xc3, 0xd2, 0xe1, 0xf0,
|
||
]);
|
||
expect(initial.equals(expected)).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe('computeSha1StateV', () => {
|
||
const SLICE = 1024 * 1024; // 1 MB,与上传链路一致,且是 64 的倍数
|
||
|
||
it('returns one 20-byte state per slice', () => {
|
||
const data = Buffer.alloc(SLICE + 100, 0x49); // 2 片
|
||
const states = computeSha1StateV(data, 2, SLICE);
|
||
expect(states.length).toBe(2);
|
||
for (const st of states) expect(st.length).toBe(20);
|
||
});
|
||
|
||
// 末片定义为「标准整文件 SHA1(有 finalize/padding)」,直接对齐 Node crypto。
|
||
it('last slice equals the standard full-file SHA1 digest', () => {
|
||
const data = Buffer.alloc(SLICE + 100, 0x4a);
|
||
const states = computeSha1StateV(data, 2, SLICE);
|
||
const expected = createHash('sha1').update(data).digest();
|
||
expect(Buffer.from(states[1]).equals(expected)).toBe(true);
|
||
});
|
||
|
||
it('last slice equals full-file SHA1 for a three-slice file', () => {
|
||
const data = Buffer.alloc(SLICE * 2 + 1, 0x4b);
|
||
const states = computeSha1StateV(data, 3, SLICE);
|
||
expect(Buffer.from(states[2]).equals(createHash('sha1').update(data).digest())).toBe(true);
|
||
});
|
||
|
||
// 非末片是「累积 SHA1 中间 state(不 finalize,小端)」。若误用标准 finalize
|
||
// 计算,会得到 file[0:slice] 的完整 digest,与本实现必然不等——以此锁定
|
||
// 「中间 state 而非独立片 digest」的语义。
|
||
it('non-last slice is NOT the finalized digest of the prefix', () => {
|
||
const data = Buffer.alloc(SLICE + 100, 0x4c);
|
||
const states = computeSha1StateV(data, 2, SLICE);
|
||
const prefixDigest = createHash('sha1').update(data.subarray(0, SLICE)).digest();
|
||
expect(Buffer.from(states[0]).equals(prefixDigest)).toBe(false);
|
||
});
|
||
|
||
// 非末片中间 state 的正确性来自 transform(padding oracle 已验)与 update
|
||
// 分块等价(已验)。这里再做一次直接对照:computeSha1StateV 对首片的处理
|
||
// 等价于手动 reset + update(prefix) + 小端输出。
|
||
it('non-last slice equals manual Sha1Stream reset+update of the prefix', () => {
|
||
const data = Buffer.alloc(SLICE + 100, 0x4d);
|
||
const states = computeSha1StateV(data, 2, SLICE);
|
||
const manual = new Sha1Stream();
|
||
manual.update(data.subarray(0, SLICE));
|
||
expect(Buffer.from(states[0]).equals(Buffer.from(manual.hash(true)))).toBe(true);
|
||
});
|
||
|
||
// 单片文件只有末片,直接是整文件 digest。
|
||
it('single-slice file has only the finalized digest', () => {
|
||
const data = Buffer.alloc(100, 0x4e);
|
||
const states = computeSha1StateV(data, 1, SLICE);
|
||
expect(states.length).toBe(1);
|
||
expect(Buffer.from(states[0]).equals(createHash('sha1').update(data).digest())).toBe(true);
|
||
});
|
||
|
||
// 中间 state 与「把它当末片算出的 digest」必须不同——这是 sliceupload 服务端
|
||
// 校验「累积 state 而非独立片 digest」的核心区分点。
|
||
it('non-last slice differs from a single-slice digest of the same prefix', () => {
|
||
const data = Buffer.alloc(SLICE + 100, 0x4f);
|
||
const multiFirst = computeSha1StateV(data, 2, SLICE)[0];
|
||
const singleAsLast = computeSha1StateV(data.subarray(0, SLICE), 1, SLICE)[0];
|
||
expect(Buffer.from(multiFirst).equals(Buffer.from(singleAsLast))).toBe(false);
|
||
});
|
||
});
|