From 613b2b1e6876fd344dd6a01a550e773dbab58d18 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 19 Jul 2026 15:35:53 +0800 Subject: [PATCH] fix(proton): decode extended uint32 field varints Consume complete well-formed value varints before retaining their low 32 bits, matching QQ push behavior without relaxing tag or length parsing. Add generator and end-to-end group reaction regressions, including truncated and malformed varint coverage. Refs #242. --- .../onebot/tests/group-msg-emoji-like.test.ts | 30 +++++++++++++++++++ packages/proton/src/codegen/generator.ts | 6 ++-- .../proton/test/__tests__/generator.test.ts | 26 ++++++++++++---- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/packages/onebot/tests/group-msg-emoji-like.test.ts b/packages/onebot/tests/group-msg-emoji-like.test.ts index 7ab8807d..6fa7563e 100644 --- a/packages/onebot/tests/group-msg-emoji-like.test.ts +++ b/packages/onebot/tests/group-msg-emoji-like.test.ts @@ -138,6 +138,36 @@ describe('decodeEvent0x2DC subType=16 — GroupMsgEmojiLike', () => { }); }); + it('decodes a full PushMsg whose envelope sequence carries high uint32 bits', () => { + const content = buildReactContent({ + groupUin: BigInt(GROUP_ID), + field13: 35, + groupReactionData: { + data: { + data: { + groupReactionTarget: { seq: 4242n }, + groupReactionDataContent: { + code: '76', count: 1, operatorUid: 'u_operator_xxx', type: 1, + }, + }, + }, + }, + }); + const packet = buildPushPacket(content, { + msgId: 123n, + sequence: 0x1_0000_0007n, + }); + + const [event] = parseMsgPush(packet, makeIdentity()) as GroupMsgEmojiLikeEvent[]; + expect(event).toMatchObject({ + kind: 'group_msg_emoji_like', + groupId: GROUP_ID, + msgSeq: 4242, + emojiId: '76', + }); + expect(buildContext(packet, makeIdentity())?.head.sequence).toBe(7); + }); + it('accepts a sign-extended negative ContentHead field 4 without losing its low 32 bits', () => { const packet = buildPushPacket(new Uint8Array(0), { msgId: encodeSignedInt32Varint(-1), diff --git a/packages/proton/src/codegen/generator.ts b/packages/proton/src/codegen/generator.ts index 784dcf81..b00b70bc 100644 --- a/packages/proton/src/codegen/generator.ts +++ b/packages/proton/src/codegen/generator.ts @@ -119,9 +119,9 @@ function __readVarint64(data, offset, end) { } function __readVarint32Value(data, offset, end) { const [value, next] = __readVarint64(data, offset, end); - if (value > 0xffffffffn && value < 0xffffffff80000000n) { - throw new Error('protobuf uint32 field varint overflow'); - } + // QQ-compatible protobuf runtimes consume a complete, well-formed uint32 + // value varint and retain its low 32 bits. Keep tags and lengths on + // __readVarint32 so malformed framing still fails before it affects bounds. return [Number(BigInt.asUintN(32, value)), next]; } function __skipVarint(data, offset, end) { diff --git a/packages/proton/test/__tests__/generator.test.ts b/packages/proton/test/__tests__/generator.test.ts index ae6b46f7..a7ff6f10 100644 --- a/packages/proton/test/__tests__/generator.test.ts +++ b/packages/proton/test/__tests__/generator.test.ts @@ -169,7 +169,7 @@ interface Int32Msg { ])).value).toBe(-1); }); - it('accepts sign-extended 10-byte values for uint_32 fields without accepting real overflow', () => { + it('retains the low 32 bits of extended uint_32 field varints', () => { const { dec } = makeRoundTripFromSchema( `interface Uint32Msg { value: pb<1, uint_32>; }`, 'Uint32Msg', @@ -183,12 +183,28 @@ interface Int32Msg { 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, ])).value).toBe(0xfffffffe); - // 2^32 is neither uint32 nor a sign-extended negative int32, so accepting - // ten-byte compatibility must not turn genuine overflow into truncation. - expect(() => dec(Uint8Array.from([ + // QQ-compatible uint32 readers consume up to a full 64-bit varint and + // retain its low 32 bits. Some push-envelope sequences use that wire + // representation, so values with non-zero high bits must remain decodable. + expect(dec(Uint8Array.from([ 0x08, 0x80, 0x80, 0x80, 0x80, 0x10, - ]))).toThrow(/uint32 field varint overflow/); + ])).value).toBe(0); + expect(dec(Uint8Array.from([ + 0x08, + 0x87, 0x80, 0x80, 0x80, 0x10, + ])).value).toBe(7); + + // Compatibility applies only to complete uint64 varints. Truncated input + // and values wider than 64 bits must still fail loudly. + expect(() => dec(Uint8Array.from([ + 0x08, + 0x80, + ]))).toThrow(/truncated uint64 varint/); + expect(() => dec(Uint8Array.from([ + 0x08, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02, + ]))).toThrow(/uint64 varint overflow/); }); it('rejects malformed length-delimited fields without leaving parent bounds', () => {