feat(onebot): support private window shake messages
Encode poke message segments as private window shake messages while keeping the dedicated send_poke action unchanged. Restrict window shakes to a single ordinary private-chat segment. Reject group, temporary-session, mixed-message, and forwarded usage before identity lookup, media upload, or packet transmission. Add full outbound request coverage, OneBot action regressions, and live private-chat verification for issue #259. Closes #259
This commit is contained in:
15 files changed
+529
-44
No files matched your search
@@ -70,6 +70,43 @@ function decodeLongMsgRequest(rawBytes: Uint8Array): LongMsgResult {
|
||||
}
|
||||
|
||||
describe('actions/forward — file segment inside forward node', () => {
|
||||
it('rejects a recursive window shake before identity resolution or upload', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const resolveUserUid = vi.fn();
|
||||
const uploadPrivate = vi.fn();
|
||||
const sendRawPacket = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
resolveUserUid,
|
||||
sendRawPacket: sendRawPacket as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
uploadPrivate,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await expect(new ForwardApi(bridge as any).upload([{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'image', url: 'base64://AQID' }],
|
||||
innerForward: [{
|
||||
userUin: 10002,
|
||||
nickname: 'bob',
|
||||
elements: [{ type: 'poke', subType: 1 }],
|
||||
}],
|
||||
}], undefined, 67890)).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'poke',
|
||||
});
|
||||
|
||||
expect(resolveUserUid).not.toHaveBeenCalled();
|
||||
expect(uploadPrivate).not.toHaveBeenCalled();
|
||||
expect(buildSendElemsMock).not.toHaveBeenCalled();
|
||||
expect(sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('group forward sets forwardFake:true on the SendContext so transElem(24) is emitted', async () => {
|
||||
// Group case: the file element rides on elems[] as transElem(24).
|
||||
// The element-builder receives ctx.forwardFake=true so it knows to
|
||||
|
||||
@@ -75,6 +75,40 @@ describe('element-builder / rich card encoding', () => {
|
||||
});
|
||||
|
||||
describe('element-builder / commonElem.businessType per scene', () => {
|
||||
describe('private window shake', () => {
|
||||
it('encodes poke type 1 as CommonElem service 2 with the verified payload', async () => {
|
||||
const [elem] = await buildSendElems(
|
||||
[{ type: 'poke', subType: 1 }],
|
||||
{ bridge: fakeBridge, userUid: 'u_peer', scene: 'direct-private' },
|
||||
);
|
||||
|
||||
expect(commonElem(elem)).toMatchObject({
|
||||
serviceType: 2,
|
||||
businessType: 1,
|
||||
});
|
||||
expect(Array.from(commonElem(elem).pbElem)).toEqual([0x08, 0x01]);
|
||||
});
|
||||
|
||||
it('rejects mixed window-shake content before uploading an earlier media segment', async () => {
|
||||
vi.mocked(uploadImageMsgInfo).mockClear();
|
||||
|
||||
await expect(buildSendElems([
|
||||
{ type: 'image', url: 'file:///tmp/must-not-upload.png' },
|
||||
{ type: 'poke', subType: 1 },
|
||||
], {
|
||||
bridge: fakeBridge,
|
||||
userUid: 'u_peer',
|
||||
scene: 'direct-private',
|
||||
})).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'poke',
|
||||
message: expect.stringContaining('only segment'),
|
||||
});
|
||||
|
||||
expect(uploadImageMsgInfo).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('image', () => {
|
||||
it('c2c → businessType 10', async () => {
|
||||
const [elem] = await buildSendElems(
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import type { SendMessageRequest, SendMessageResponse } from '@snowluma/proto-defs/action';
|
||||
import type { PokeExtra } from '@snowluma/proto-defs/element';
|
||||
import { IdentityService } from '@snowluma/protocol/identity-service';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { Bridge } from '../src/bridge/bridge';
|
||||
|
||||
describe('private window shake wire request', () => {
|
||||
it('routes poke type 1 through direct C2C with the verified CommonElem payload', async () => {
|
||||
class TestBridge extends Bridge {
|
||||
capturedBody: Uint8Array | null = null;
|
||||
|
||||
override async resolveUserUid(): Promise<string> {
|
||||
throw new Error('window shake must not resolve media identity');
|
||||
}
|
||||
|
||||
override async sendRawPacket(command: string, body: Uint8Array): Promise<SendPacketResult> {
|
||||
expect(command).toBe('MessageSvc.PbSendMsg');
|
||||
this.capturedBody = body;
|
||||
return {
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<SendMessageResponse>({
|
||||
result: 0,
|
||||
privateSequence: 88,
|
||||
timestamp1: 1_710_000_000,
|
||||
})),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const bridge = new TestBridge(IdentityService.memory('10000'));
|
||||
await bridge.apis.message.sendPrivate(1787882683, [{ type: 'poke', subType: 1 }]);
|
||||
|
||||
expect(bridge.capturedBody).toBeInstanceOf(Uint8Array);
|
||||
const request = protobuf_decode<SendMessageRequest>(bridge.capturedBody!);
|
||||
expect(request.routingHead?.c2c).toMatchObject({ uin: 1787882683 });
|
||||
expect(request.routingHead?.grp ?? undefined).toBeUndefined();
|
||||
expect(request.routingHead?.grpTmp ?? undefined).toBeUndefined();
|
||||
expect(request.contentHead).toMatchObject({ type: 1, c2cCmd: 11 });
|
||||
expect(request.contentHead?.subType ?? 0).toBe(0);
|
||||
|
||||
const elems = request.messageBody?.richText?.elems ?? [];
|
||||
expect(elems).toHaveLength(1);
|
||||
expect(elems[0]?.commonElem).toMatchObject({ serviceType: 2, businessType: 1 });
|
||||
const payload = elems[0]?.commonElem?.pbElem;
|
||||
expect(payload).toBeInstanceOf(Uint8Array);
|
||||
expect(Array.from(payload!)).toEqual([0x08, 0x01]);
|
||||
expect(protobuf_decode<PokeExtra>(payload!)).toEqual({ type: 1 });
|
||||
});
|
||||
|
||||
it('rejects mixed direct-private content before resolving media identity', async () => {
|
||||
const bridge = new Bridge(IdentityService.memory('10000'));
|
||||
const resolveUserUid = vi.spyOn(bridge, 'resolveUserUid').mockResolvedValue('u_peer');
|
||||
|
||||
await expect(bridge.apis.message.sendPrivate(1787882683, [
|
||||
{ type: 'image', url: 'file:///tmp/must-not-resolve.png' },
|
||||
{ type: 'poke', subType: 1 },
|
||||
])).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'poke',
|
||||
});
|
||||
|
||||
expect(resolveUserUid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects temp-session window shakes before resolving the peer UID', async () => {
|
||||
const bridge = new Bridge(IdentityService.memory('10000'));
|
||||
const resolveUserUid = vi.spyOn(bridge, 'resolveUserUid').mockResolvedValue('u_peer');
|
||||
|
||||
await expect(bridge.apis.message.sendGroupTempMessage(
|
||||
1787882683,
|
||||
941657197,
|
||||
[{ type: 'poke', subType: 1 }],
|
||||
)).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'poke',
|
||||
});
|
||||
|
||||
expect(resolveUserUid).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user