fix(onebot): reject ambiguous video message combinations
Prevent ordinary messages and forwarded nodes from combining video content with visible sibling segments or additional videos. Invalid combinations now fail before parsing side effects, media work, or bridge sends, while exact empty text placeholders remain compatible with a single video. Cover group, private, temporary-session, CQ string, nested forward, cached forward, and protocol-builder paths. Preserve the existing standalone window-shake behavior and keep valid single-video messages unchanged.
This commit is contained in:
8 files changed
+371
-29
No files matched your search
@@ -2,12 +2,20 @@ import { describe, expect, it, vi } from 'vitest';
|
||||
import type { BridgeInterface } from '../../src/bridge/bridge-interface';
|
||||
import { ApiHandler, type ApiActionContext } from '../src/api-handler';
|
||||
import type { OneBotInstanceContext } from '../src/instance-context';
|
||||
import { deleteMessage, sendGroupMessage, setEssenceMessage } from '../src/modules/message-actions';
|
||||
import {
|
||||
deleteMessage,
|
||||
sendGroupMessage,
|
||||
sendPrivateMessage,
|
||||
setEssenceMessage,
|
||||
} from '../src/modules/message-actions';
|
||||
import type { MessageMeta } from '../src/types';
|
||||
|
||||
function instanceContext(sendGroup: ReturnType<typeof vi.fn>): OneBotInstanceContext {
|
||||
function instanceContext(
|
||||
sendGroup: ReturnType<typeof vi.fn>,
|
||||
sendPrivate: ReturnType<typeof vi.fn> = vi.fn(),
|
||||
): OneBotInstanceContext {
|
||||
const bridge = {
|
||||
apis: { message: { sendGroup } },
|
||||
apis: { message: { sendGroup, sendPrivate } },
|
||||
resolveUserUid: vi.fn(async () => 'u_peer'),
|
||||
} as unknown as BridgeInterface;
|
||||
|
||||
@@ -27,6 +35,14 @@ function instanceContext(sendGroup: ReturnType<typeof vi.fn>): OneBotInstanceCon
|
||||
}
|
||||
|
||||
describe('outbound message validation at the Action boundary', () => {
|
||||
const sendReceipt = {
|
||||
messageId: 1,
|
||||
sequence: 100,
|
||||
clientSequence: 0,
|
||||
random: 1,
|
||||
timestamp: 1700000000,
|
||||
};
|
||||
|
||||
it('returns BAD_REQUEST and performs no send when a later segment is unknown', async () => {
|
||||
const bridgeSend = vi.fn();
|
||||
const ref = instanceContext(bridgeSend);
|
||||
@@ -95,6 +111,140 @@ describe('outbound message validation at the Action boundary', () => {
|
||||
});
|
||||
expect(bridgeSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects video mixed with other visible content before sending', async () => {
|
||||
const bridgeSend = vi.fn(async () => sendReceipt);
|
||||
const ref = instanceContext(bridgeSend);
|
||||
const api = new ApiHandler({
|
||||
sendGroupMessage: (groupId, message, autoEscape) =>
|
||||
sendGroupMessage(ref, groupId, message, autoEscape),
|
||||
} as ApiActionContext);
|
||||
|
||||
const response = await api.handle('send_group_msg', {
|
||||
group_id: 12345,
|
||||
message: [
|
||||
{ type: 'video', data: { file: 'https://example.com/video.mp4' } },
|
||||
{ type: 'text', data: { text: 'must not become hidden content' } },
|
||||
],
|
||||
});
|
||||
|
||||
expect(response).toMatchObject({
|
||||
status: 'failed',
|
||||
retcode: 1400,
|
||||
wording: expect.stringContaining('video'),
|
||||
});
|
||||
expect(bridgeSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects mixed video content before resolving a later segment', async () => {
|
||||
const bridgeSend = vi.fn(async () => sendReceipt);
|
||||
const getGroupRecommendArk = vi.fn(async () => '{"app":"contact"}');
|
||||
const ref = instanceContext(bridgeSend);
|
||||
(ref.bridge.apis as any).contacts = { getGroupRecommendArk };
|
||||
const api = new ApiHandler({
|
||||
sendGroupMessage: (groupId, message, autoEscape) =>
|
||||
sendGroupMessage(ref, groupId, message, autoEscape),
|
||||
} as ApiActionContext);
|
||||
|
||||
const response = await api.handle('send_group_msg', {
|
||||
group_id: 12345,
|
||||
message: [
|
||||
{ type: 'video', data: { file: 'https://example.com/video.mp4' } },
|
||||
{ type: 'contact', data: { type: 'group', id: 67890 } },
|
||||
],
|
||||
});
|
||||
|
||||
expect(response).toMatchObject({ status: 'failed', retcode: 1400 });
|
||||
expect(getGroupRecommendArk).not.toHaveBeenCalled();
|
||||
expect(bridgeSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects multiple video segments before sending', async () => {
|
||||
const bridgeSend = vi.fn(async () => sendReceipt);
|
||||
const ref = instanceContext(bridgeSend);
|
||||
const api = new ApiHandler({
|
||||
sendGroupMessage: (groupId, message, autoEscape) =>
|
||||
sendGroupMessage(ref, groupId, message, autoEscape),
|
||||
} as ApiActionContext);
|
||||
|
||||
const response = await api.handle('send_group_msg', {
|
||||
group_id: 12345,
|
||||
message: [
|
||||
{ type: 'video', data: { file: 'https://example.com/first.mp4' } },
|
||||
{ type: 'video', data: { file: 'https://example.com/second.mp4' } },
|
||||
],
|
||||
});
|
||||
|
||||
expect(response).toMatchObject({
|
||||
status: 'failed',
|
||||
retcode: 1400,
|
||||
wording: expect.stringContaining('video'),
|
||||
});
|
||||
expect(bridgeSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects hidden content after a video in CQ string form', async () => {
|
||||
const bridgeSend = vi.fn(async () => sendReceipt);
|
||||
const ref = instanceContext(bridgeSend);
|
||||
const api = new ApiHandler({
|
||||
sendGroupMessage: (groupId, message, autoEscape) =>
|
||||
sendGroupMessage(ref, groupId, message, autoEscape),
|
||||
} as ApiActionContext);
|
||||
|
||||
const response = await api.handle('send_group_msg', {
|
||||
group_id: 12345,
|
||||
message: '[CQ:video,file=https://example.com/video.mp4]hidden text',
|
||||
});
|
||||
|
||||
expect(response).toMatchObject({ status: 'failed', retcode: 1400 });
|
||||
expect(bridgeSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects video mixed with private-chat content before sending', async () => {
|
||||
const privateSend = vi.fn(async () => sendReceipt);
|
||||
const ref = instanceContext(vi.fn(), privateSend);
|
||||
const api = new ApiHandler({
|
||||
sendPrivateMessage: (userId, message, autoEscape, tempGroupId) =>
|
||||
sendPrivateMessage(ref, userId, message, autoEscape, tempGroupId),
|
||||
} as ApiActionContext);
|
||||
|
||||
const response = await api.handle('send_private_msg', {
|
||||
user_id: 67890,
|
||||
message: [
|
||||
{ type: 'video', data: { file: 'https://example.com/video.mp4' } },
|
||||
{ type: 'text', data: { text: 'must not become hidden content' } },
|
||||
],
|
||||
});
|
||||
|
||||
expect(response).toMatchObject({
|
||||
status: 'failed',
|
||||
retcode: 1400,
|
||||
wording: expect.stringContaining('only segment'),
|
||||
});
|
||||
expect(privateSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps exact empty text placeholders compatible with a single video', async () => {
|
||||
const bridgeSend = vi.fn(async () => sendReceipt);
|
||||
const ref = instanceContext(bridgeSend);
|
||||
const api = new ApiHandler({
|
||||
sendGroupMessage: (groupId, message, autoEscape) =>
|
||||
sendGroupMessage(ref, groupId, message, autoEscape),
|
||||
} as ApiActionContext);
|
||||
|
||||
const response = await api.handle('send_group_msg', {
|
||||
group_id: 12345,
|
||||
message: [
|
||||
{ type: 'text', data: { text: '' } },
|
||||
{ type: 'video', data: { file: 'https://example.com/video.mp4' } },
|
||||
{ type: 'text', data: { text: '' } },
|
||||
],
|
||||
});
|
||||
|
||||
expect(response).toMatchObject({ status: 'ok', retcode: 0 });
|
||||
expect(bridgeSend).toHaveBeenCalledOnce();
|
||||
expect(bridgeSend.mock.calls[0]?.[1]).toMatchObject([{ type: 'video' }]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('server-sequence action validation', () => {
|
||||
|
||||
Reference in new issue
Block a user