fix(protocol): align system message dedup identity

Match QQ NT's reverse-engineered peer, sequence, and random key so duplicate system events cannot bypass filtering through a different outer package type.

Fixes #266
This commit is contained in:
DrSmoothl committed 2026-07-28 23:59:56 +08:00
1 parent 19e3135e63
commit 3d7c4f5dc3
2 files changed
+22 -12

No files matched your search

+12 -9
View File
@@ -21,13 +21,16 @@ import type { MsgPushHead } from './context';
* LLOneBot) receive events *after* this dedup; SnowLuma reads the raw OlPush
* *before* it, so we replicate it here.
*
* We dedup at the push level (drop the whole duplicate, like NT) using the
* fields already on the head plus `fromUin` for peer scoping (a stand-in for
* peerUid — `msg_seq` can be per-conversation, so the peer keeps two groups'
* pushes from colliding; `random`/`msgId` makes the collision astronomically
* unlikely anyway). A push with no server identity (`sequence` or `msgId` 0) is
* never deduped — without a real per-message id we cannot distinguish a true
* duplicate from two distinct events, and dropping then would be a regression.
* We dedup at the push level (drop the whole duplicate, like NT) using
* `sequence` + `msgId` and `fromUin` for peer scoping (a stand-in for peerUid —
* `msg_seq` can be per-conversation, so the peer keeps two groups' pushes from
* colliding; `random`/`msgId` makes the collision astronomically unlikely
* anyway). `msgType` and `subType` are deliberately absent: they are not part
* of QQ NT's global key, and one server system message may arrive through
* different outer package types (#266). A push with no server identity
* (`sequence` or `msgId` 0) is never deduped — without a real per-message id we
* cannot distinguish a true duplicate from two distinct events, and dropping
* then would be a regression.
*/
export class SysMsgDedup {
private readonly seen = new Set<string>();
@@ -45,9 +48,9 @@ export class SysMsgDedup {
* Returns true if a system push with this identity was already seen (caller
* should drop it); otherwise records it and returns false.
*/
seenDuplicate(head: Pick<MsgPushHead, 'msgType' | 'subType' | 'sequence' | 'msgId'>, fromUin: number): boolean {
seenDuplicate(head: Pick<MsgPushHead, 'sequence' | 'msgId'>, fromUin: number): boolean {
if (head.sequence === 0 || head.msgId === 0) return false;
const key = `${head.msgType}:${head.subType}:${fromUin}:${head.sequence}:${head.msgId}`;
const key = `${fromUin}:${head.sequence}:${head.msgId}`;
if (this.seen.has(key)) return true;
const evicted = this.ring[this.cursor];
if (evicted !== undefined) this.seen.delete(evicted);
@@ -53,23 +53,30 @@ function onlineDevicesPush(deviceName: string): PacketInfo {
}
describe('SysMsgDedup', () => {
it('flags the second push with the same (msgType, subType, fromUin, seq, msgId)', () => {
it('flags the second push with the same (peer, seq, msgId)', () => {
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);
});
it('treats a different seq, msgId, type, or peer as a distinct push', () => {
it('treats a different seq, msgId, or peer as a distinct push', () => {
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, msgType: 34 }, 700)).toBe(false);
expect(d.seenDuplicate(base, 701)).toBe(false);
});
it('[#266] follows QQ NT and dedups one identity across outer package types', () => {
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);
});
it('never dedups a push with no server identity (seq or msgId 0)', () => {
const d = new SysMsgDedup();
expect(d.seenDuplicate({ msgType: 33, subType: 0, sequence: 0, msgId: 0 }, 700)).toBe(false);