fix(protocol): scope system dedup to native routes
Derive QQ NT's peerUid_chatType_msg_seq_random identity from its reverse-engineered static routing table before event dispatch. Unknown routes and incomplete identities fail open, while C2C and group peers use their native namespaces. Refines #266
This commit is contained in:
3 files changed
+235
-103
No files matched your search
@@ -5,8 +5,9 @@
|
||||
// global key whose per-message discriminators are msg_seq (contentHead field 5
|
||||
// = head.sequence) and random (contentHead field 4 = head.msgId); kernel-based
|
||||
// bots see events only after that dedup. SnowLuma reads the raw OlPush, so
|
||||
// parseMsgPush must replicate the drop for system pushes — but only when given
|
||||
// a dedup tracker (the live path), and never for chat messages.
|
||||
// parseMsgPush must replicate the drop for routes in QQ NT's static system-
|
||||
// message table, but only when given a dedup tracker (the live path). Ordinary
|
||||
// chat routes remain outside that table even when their outer msgType matches.
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { PacketInfo } from '@snowluma/common/protocol-types';
|
||||
@@ -14,7 +15,12 @@ import { protobuf_encode } from '@snowluma/proton';
|
||||
import type { PushMsg, PushMsgBody } from '@snowluma/proto-defs/message';
|
||||
import type { GroupChange, OnlineDeviceNotify } from '@snowluma/proto-defs/notify';
|
||||
import type { IdentityService } from '../../src/identity-service';
|
||||
import { parseMsgPush, SysMsgDedup } from '../../src/msg-push';
|
||||
import {
|
||||
deriveSysMsgDedupIdentity,
|
||||
parseMsgPush,
|
||||
SysMsgDedup,
|
||||
type SysMsgDedupIdentity,
|
||||
} from '../../src/msg-push';
|
||||
|
||||
const identity = { findUinByUid: () => null, findFriend: () => undefined } as unknown as IdentityService;
|
||||
|
||||
@@ -40,6 +46,14 @@ function memberIncreasePush(opts: { groupId: number; memberUin: number; sequence
|
||||
});
|
||||
}
|
||||
|
||||
function c2cPacket(msgType: number, subType: number): PacketInfo {
|
||||
return pushPacket({
|
||||
responseHead: { fromUin: 10001, fromUid: 'u_x' },
|
||||
contentHead: { msgType, subType, sequence: 900, timestamp: 1781540572, msgId: 7 },
|
||||
body: { richText: { elems: [{ text: { str: 'hi' } }] } },
|
||||
});
|
||||
}
|
||||
|
||||
function onlineDevicesPush(deviceName: string): PacketInfo {
|
||||
return pushPacket({
|
||||
responseHead: { fromUin: 2000000001, fromUid: '' },
|
||||
@@ -52,47 +66,135 @@ function onlineDevicesPush(deviceName: string): PacketInfo {
|
||||
});
|
||||
}
|
||||
|
||||
function dedupIdentity(overrides: Partial<SysMsgDedupIdentity> = {}): SysMsgDedupIdentity {
|
||||
return {
|
||||
peerUid: '700',
|
||||
chatType: 2,
|
||||
sequence: 500,
|
||||
random: 12345,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function deriveIdentity(opts: {
|
||||
msgType: number;
|
||||
subType: number;
|
||||
sequence?: number;
|
||||
msgId?: number;
|
||||
fromUin?: number;
|
||||
fromUid?: string;
|
||||
}) {
|
||||
return deriveSysMsgDedupIdentity({
|
||||
head: {
|
||||
msgType: opts.msgType,
|
||||
subType: opts.subType,
|
||||
c2cCmd: 0,
|
||||
sequence: opts.sequence ?? 500,
|
||||
ntMsgSeq: 0,
|
||||
timestamp: 1781540572,
|
||||
msgId: opts.msgId ?? 12345,
|
||||
},
|
||||
fromUin: opts.fromUin ?? 700,
|
||||
fromUid: opts.fromUid ?? 'u_peer',
|
||||
});
|
||||
}
|
||||
|
||||
describe('deriveSysMsgDedupIdentity', () => {
|
||||
it.each([
|
||||
[528, 290],
|
||||
[166, 75],
|
||||
[166, 129],
|
||||
[166, 131],
|
||||
[166, 133],
|
||||
[166, 135],
|
||||
[167, 133],
|
||||
])('maps native C2C route %d/%d to fromUid and chatType 1', (msgType, subType) => {
|
||||
expect(deriveIdentity({ msgType, subType, fromUin: 700, fromUid: 'u_peer' })).toEqual({
|
||||
peerUid: 'u_peer',
|
||||
chatType: 1,
|
||||
sequence: 500,
|
||||
random: 12345,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[732, 12],
|
||||
[732, 16],
|
||||
[732, 20],
|
||||
[33, 0],
|
||||
[34, 0],
|
||||
[85, 0],
|
||||
])('maps native group route %d/%d to decimal fromUin and chatType 2', (msgType, subType) => {
|
||||
expect(deriveIdentity({ msgType, subType, fromUin: 700, fromUid: 'ignored' })).toEqual({
|
||||
peerUid: '700',
|
||||
chatType: 2,
|
||||
sequence: 500,
|
||||
random: 12345,
|
||||
});
|
||||
});
|
||||
|
||||
it('[#266] fails open for an outer package pair absent from QQ NT’s static table', () => {
|
||||
expect(deriveIdentity({ msgType: 528, subType: 99 })).toBeNull();
|
||||
});
|
||||
|
||||
it('fails open when the native sequence, random, or route-specific peer is missing', () => {
|
||||
expect(deriveIdentity({ msgType: 33, subType: 0, sequence: 0 })).toBeNull();
|
||||
expect(deriveIdentity({ msgType: 33, subType: 0, msgId: 0 })).toBeNull();
|
||||
expect(deriveIdentity({ msgType: 33, subType: 0, fromUin: 0 })).toBeNull();
|
||||
expect(deriveIdentity({ msgType: 528, subType: 290, fromUid: '' })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('SysMsgDedup', () => {
|
||||
it('flags the second push with the same (peer, seq, msgId)', () => {
|
||||
it('flags the second push with the same native global key', () => {
|
||||
const d = new SysMsgDedup();
|
||||
const head = { msgType: 33, subType: 0, sequence: 500, msgId: 12345 };
|
||||
expect(d.seenDuplicate(head, 700)).toBe(false);
|
||||
expect(d.seenDuplicate(head, 700)).toBe(true);
|
||||
expect(d.seenDuplicate(dedupIdentity())).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity())).toBe(true);
|
||||
});
|
||||
|
||||
it('treats a different seq, msgId, or peer as a distinct push', () => {
|
||||
it('treats a different sequence, random, peer, or chat type as distinct', () => {
|
||||
const d = new SysMsgDedup();
|
||||
const base = { msgType: 33, subType: 0, sequence: 500, msgId: 12345 };
|
||||
expect(d.seenDuplicate(base, 700)).toBe(false);
|
||||
expect(d.seenDuplicate({ ...base, sequence: 501 }, 700)).toBe(false);
|
||||
expect(d.seenDuplicate({ ...base, msgId: 99999 }, 700)).toBe(false);
|
||||
expect(d.seenDuplicate(base, 701)).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity())).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ sequence: 501 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ random: 99999 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ peerUid: '701' }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ chatType: 1 }))).toBe(false);
|
||||
});
|
||||
|
||||
it('[#266] follows QQ NT and dedups one identity across outer package types', () => {
|
||||
it('[#266] dedups different listed outer routes that derive the same native identity', () => {
|
||||
const d = new SysMsgDedup();
|
||||
const first = { msgType: 33, subType: 0, sequence: 500, msgId: 12345 };
|
||||
const alternateWrapper = { msgType: 528, subType: 99, sequence: 500, msgId: 12345 };
|
||||
expect(d.seenDuplicate(first, 700)).toBe(false);
|
||||
expect(d.seenDuplicate(alternateWrapper, 700)).toBe(true);
|
||||
const first = deriveIdentity({ msgType: 166, subType: 133 });
|
||||
const alternateRoute = deriveIdentity({ msgType: 167, subType: 133 });
|
||||
expect(first).not.toBeNull();
|
||||
expect(alternateRoute).toEqual(first);
|
||||
expect(d.seenDuplicate(first!)).toBe(false);
|
||||
expect(d.seenDuplicate(alternateRoute!)).toBe(true);
|
||||
});
|
||||
|
||||
it('never dedups a push with no server identity (seq or msgId 0)', () => {
|
||||
it('[#266] does not record an unlisted outer route', () => {
|
||||
const d = new SysMsgDedup();
|
||||
expect(d.seenDuplicate({ msgType: 33, subType: 0, sequence: 0, msgId: 0 }, 700)).toBe(false);
|
||||
expect(d.seenDuplicate({ msgType: 33, subType: 0, sequence: 0, msgId: 0 }, 700)).toBe(false);
|
||||
expect(d.seenDuplicate({ msgType: 33, subType: 0, sequence: 5, msgId: 0 }, 700)).toBe(false);
|
||||
expect(d.seenDuplicate({ msgType: 33, subType: 0, sequence: 0, msgId: 5 }, 700)).toBe(false);
|
||||
const unlisted = deriveIdentity({ msgType: 528, subType: 99 });
|
||||
const listed = deriveIdentity({ msgType: 528, subType: 290 });
|
||||
expect(unlisted).toBeNull();
|
||||
expect(listed).not.toBeNull();
|
||||
expect(d.seenDuplicate(listed!)).toBe(false);
|
||||
});
|
||||
|
||||
it('never dedups a manually supplied identity without a server discriminator or peer', () => {
|
||||
const d = new SysMsgDedup();
|
||||
expect(d.seenDuplicate(dedupIdentity({ sequence: 0 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ sequence: 0 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ random: 0 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ peerUid: '' }))).toBe(false);
|
||||
});
|
||||
|
||||
it('evicts the oldest key once capacity is exceeded (bounded memory)', () => {
|
||||
const d = new SysMsgDedup(2);
|
||||
const k = (sequence: number) => ({ msgType: 33, subType: 0, sequence, msgId: 1 });
|
||||
expect(d.seenDuplicate(k(1), 1)).toBe(false); // [1]
|
||||
expect(d.seenDuplicate(k(2), 1)).toBe(false); // [1,2]
|
||||
expect(d.seenDuplicate(k(3), 1)).toBe(false); // [2,3] — evicts 1
|
||||
expect(d.seenDuplicate(k(1), 1)).toBe(false); // 1 was evicted → seen as new again
|
||||
expect(d.seenDuplicate(k(3), 1)).toBe(true); // 3 still tracked
|
||||
expect(d.seenDuplicate(dedupIdentity({ sequence: 1, random: 1 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ sequence: 2, random: 1 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ sequence: 3, random: 1 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ sequence: 1, random: 1 }))).toBe(false);
|
||||
expect(d.seenDuplicate(dedupIdentity({ sequence: 3, random: 1 }))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -124,15 +226,22 @@ describe('parseMsgPush — system-push dedup (#137)', () => {
|
||||
expect(b[0]).toMatchObject({ userUin: 222 });
|
||||
});
|
||||
|
||||
it('does not dedup chat messages, even with an identical (seq, msgId)', () => {
|
||||
it('[#266] dedups a listed C2C route before its outer type is decoded as a chat message', () => {
|
||||
const dedup = new SysMsgDedup();
|
||||
const chat = (): PacketInfo => pushPacket({
|
||||
responseHead: { fromUin: 10001, fromUid: 'u_x' },
|
||||
contentHead: { msgType: 166, subType: 0, sequence: 900, timestamp: 1781540572, msgId: 7 },
|
||||
body: { richText: { elems: [{ text: { str: 'hi' } }] } },
|
||||
});
|
||||
expect(parseMsgPush(chat(), identity, dedup)).toHaveLength(1);
|
||||
expect(parseMsgPush(chat(), identity, dedup)).toHaveLength(1);
|
||||
expect(parseMsgPush(c2cPacket(166, 133), identity, dedup)).toHaveLength(1);
|
||||
expect(parseMsgPush(c2cPacket(166, 133), identity, dedup)).toEqual([]);
|
||||
});
|
||||
|
||||
it('[#266] records a listed undecoded route before a matching listed route is dispatched', () => {
|
||||
const dedup = new SysMsgDedup();
|
||||
expect(parseMsgPush(c2cPacket(167, 133), identity, dedup)).toEqual([]);
|
||||
expect(parseMsgPush(c2cPacket(166, 133), identity, dedup)).toEqual([]);
|
||||
});
|
||||
|
||||
it('does not dedup ordinary chat messages outside the native system-message table', () => {
|
||||
const dedup = new SysMsgDedup();
|
||||
expect(parseMsgPush(c2cPacket(166, 0), identity, dedup)).toHaveLength(1);
|
||||
expect(parseMsgPush(c2cPacket(166, 0), identity, dedup)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('does not dedup replacement state snapshots with the same server identity', () => {
|
||||
|
||||
Reference in new issue
Block a user