fix(protocol): harden history response decoding
Share packet response validation across group and private history fetches so transport failures and empty responses cannot masquerade as empty pages. Reject malformed group records and mismatched private-peer responses while preserving explicit QQ control-message filters. Mark fetched contexts as historical so old invite cards cannot overwrite live approval state. Add wire-level regression coverage for failed responses, malformed entries, peer mismatches, ordering, and live-only identity observations. Refs #280.
This commit is contained in:
7 files changed
+204
-25
No files matched your search
@@ -2,6 +2,7 @@
|
||||
// tags) and the fetched private-history decode path (each returned PushMsgBody
|
||||
// re-uses the regular friend decoder).
|
||||
|
||||
import { deflateSync } from 'node:zlib';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
@@ -11,7 +12,10 @@ import type {
|
||||
SsoGetRoamMsg,
|
||||
SsoGetRoamMsgResponse,
|
||||
} from '@snowluma/proto-defs/get-c2c-msg';
|
||||
import type { PushMsgBody } from '@snowluma/proto-defs/message';
|
||||
import type { IdentityService } from '../../src/identity-service';
|
||||
import { buildContextFromMessage } from '../../src/msg-push/context';
|
||||
import { decodeFriendMessage } from '../../src/msg-push/decoders/friend-message';
|
||||
import {
|
||||
SSO_GET_C2C_MSG_CMD,
|
||||
SSO_GET_ROAM_MSG_CMD,
|
||||
@@ -25,6 +29,28 @@ function okResult(data: Uint8Array): SendPacketResult {
|
||||
return { success: true, gotResponse: true, responseData: data } as SendPacketResult;
|
||||
}
|
||||
|
||||
function inviteCardMessage(sequence: number): PushMsgBody {
|
||||
const json = JSON.stringify({
|
||||
app: 'com.tencent.qun.invite',
|
||||
jumpUrl: `mqqapi://group/invite_join?groupcode=12345&msgseq=${sequence}`,
|
||||
});
|
||||
const compressed = deflateSync(Buffer.from(json));
|
||||
const data = new Uint8Array(compressed.length + 1);
|
||||
data[0] = 1;
|
||||
data.set(compressed, 1);
|
||||
return {
|
||||
responseHead: { fromUin: 222, forward: { friendName: 'Bob' } },
|
||||
contentHead: {
|
||||
msgType: 166,
|
||||
sequence,
|
||||
ntMsgSeq: sequence,
|
||||
timestamp: 1_700_000_000 + sequence,
|
||||
msgId: sequence,
|
||||
},
|
||||
body: { richText: { elems: [{ lightApp: { data } }] } },
|
||||
};
|
||||
}
|
||||
|
||||
describe('fetchC2cMessageRange / SsoGetC2cMsg', () => {
|
||||
it('sends the right command and request field tags', async () => {
|
||||
let captured: { cmd: string; body: Uint8Array } | null = null;
|
||||
@@ -129,6 +155,55 @@ describe('fetchC2cMessageRange / SsoGetC2cMsg', () => {
|
||||
expect(await fetchC2cMessageRange(guardSender, identity, 10001, 'u_x', 200, 100)).toEqual([]); // start > end
|
||||
expect(sent).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects a response attributed to a different private peer', async () => {
|
||||
const response = protobuf_encode<SsoGetC2cMsgResponse>({
|
||||
friendUid: 'u_other',
|
||||
messages: [],
|
||||
});
|
||||
await expect(fetchC2cMessageRange(
|
||||
{ sendRawPacket: async () => okResult(response) },
|
||||
identity,
|
||||
10001,
|
||||
'u_friend',
|
||||
100,
|
||||
120,
|
||||
)).rejects.toThrow('friend uid mismatch');
|
||||
});
|
||||
|
||||
it('does not replay live-only invite observations from older history', async () => {
|
||||
const remembered: Array<[number, number]> = [];
|
||||
const observingIdentity = {
|
||||
findFriend: () => undefined,
|
||||
rememberGroupInviteCardSequence: (groupUin: number, sequence: number) => {
|
||||
remembered.push([groupUin, sequence]);
|
||||
},
|
||||
} as unknown as IdentityService;
|
||||
|
||||
const live = buildContextFromMessage(
|
||||
inviteCardMessage(222),
|
||||
10001,
|
||||
observingIdentity,
|
||||
false,
|
||||
);
|
||||
expect(live).not.toBeNull();
|
||||
decodeFriendMessage(live!);
|
||||
|
||||
const historyResponse = protobuf_encode<SsoGetC2cMsgResponse>({
|
||||
friendUid: 'u_friend',
|
||||
messages: [inviteCardMessage(111)],
|
||||
});
|
||||
await fetchC2cMessageRange(
|
||||
{ sendRawPacket: async () => okResult(historyResponse) },
|
||||
observingIdentity,
|
||||
10001,
|
||||
'u_friend',
|
||||
100,
|
||||
120,
|
||||
);
|
||||
|
||||
expect(remembered).toEqual([[12345, 222]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchC2cRoamMessagePage / SsoGetRoamMsg', () => {
|
||||
|
||||
Reference in new issue
Block a user