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.
This commit is contained in:
DrSmoothl committed 2026-07-19 15:35:53 +08:00
1 parent 87bab6666b
commit 613b2b1e68
3 files changed
+54 -8

No files matched your search

@@ -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),
+3 -3
View File
@@ -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) {
@@ -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', () => {