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:
DrSmoothl committed 2026-08-05 23:55:55 +08:00
1 parent 2ac0b92859
commit 27c527976b
8 files changed
+371 -29

No files matched your search

+8
View File
@@ -10,6 +10,7 @@ import type { BridgeContext } from './bridge-context';
import { sysFaceStore } from './sys-face-store';
import type { MessageElement } from './events';
import {
assertVideoSendPolicy,
assertWindowShakeSendPolicy,
assertValidMessageElements,
MessageElementValidationError,
@@ -458,6 +459,13 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
// preserves all-or-nothing semantics: a malformed later segment cannot make
// an earlier media segment perform side effects before the send is rejected.
assertValidMessageElements(elements, 'W');
// Canonical MessageElements cannot contain empty text. OneBot's explicit
// empty-text compatibility placeholders are removed before this boundary,
// so the array length here is the effective on-wire segment count.
assertVideoSendPolicy(
elements.filter((element) => element.type === 'video').length,
elements.length,
);
assertWindowShakeSendPolicy(
elements.filter((element) => element.type === 'poke').length,
elements.length,
+27
View File
@@ -67,6 +67,33 @@ export function assertWindowShakeSendPolicy(
}
}
/**
* QQ clients treat a video as a standalone message: sibling elements remain
* on the wire but are not rendered, and additional videos cannot be opened.
* Reject that ambiguous wire shape instead of reporting a successful send
* whose visible content differs from the caller's request.
*/
export function assertVideoSendPolicy(
videoCount: number,
effectiveSegmentCount: number,
): void {
if (videoCount === 0) return;
if (videoCount !== 1) {
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
'message element "video" may appear only once in a message',
'video',
);
}
if (effectiveSegmentCount !== 1) {
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
'message element "video" must be the only segment in a message',
'video',
);
}
}
type ElementField<T extends MessageElementType> = Exclude<keyof MessageElementOf<T>, 'type'> & string;
export interface ElementSpec<T extends MessageElementType> {