fix(protocol): keep forward previews valid
Keep forwarded cards readable when previews contain unsupported characters without changing the forwarded messages. Reject invalid resource references and record affected preview fields for troubleshooting. Refs #467
This commit is contained in:
2 files changed
+80
-7
No files matched your search
@@ -16,6 +16,7 @@
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { inflateSync } from 'zlib';
|
||||
import { subscribeLogs, type LogEntry } from '@snowluma/common/logger';
|
||||
|
||||
vi.mock('@snowluma/protocol/highway/image-upload', () => ({
|
||||
uploadImageMsgInfo: vi.fn(async () => new Uint8Array([7, 8, 9])),
|
||||
@@ -421,6 +422,53 @@ describe('element-builder / forward preview', () => {
|
||||
expect(xml).toContain('tSum="2"');
|
||||
});
|
||||
|
||||
it('normalizes only unsupported preview characters and records the affected fields', async () => {
|
||||
const logs: LogEntry[] = [];
|
||||
const unsubscribe = subscribeLogs(entry => logs.push(entry));
|
||||
const element = {
|
||||
type: 'forward' as const,
|
||||
resId: 'preview-controls',
|
||||
forwardSource: 'source\u0000',
|
||||
forwardSummary: 'summary\uFFFE',
|
||||
forwardPrompt: 'prompt\uFFFF',
|
||||
forwardNews: [{ text: 'a\u0001b\u000Bc\u001Fd\uD800e\uDC00f😀中文\t\n\r' }],
|
||||
};
|
||||
const original = structuredClone(element);
|
||||
try {
|
||||
const [elem] = await buildSendElems([element], { bridge: fakeBridge, groupId: 12345 });
|
||||
const xml = inflatePrefixedPayload(elem.richMsg!.template1!);
|
||||
expect(xml).toContain('brief="prompt�"');
|
||||
expect(xml).toContain('source�</title>');
|
||||
expect(xml).toContain('summary�</summary>');
|
||||
expect(xml).toContain('a�b�c�d�e�f😀中文	

</title>');
|
||||
expect(element).toEqual(original);
|
||||
expect(logs.filter(entry => entry.message.startsWith('forward preview characters normalized:'))
|
||||
.map(entry => entry.message)).toEqual([
|
||||
'forward preview characters normalized: field=source',
|
||||
'forward preview characters normalized: field=news[0]',
|
||||
'forward preview characters normalized: field=prompt',
|
||||
'forward preview characters normalized: field=summary',
|
||||
]);
|
||||
} finally {
|
||||
unsubscribe();
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects an unrepresentable resource reference instead of rewriting its identity', async () => {
|
||||
await expect(buildSendElems([{ type: 'forward', resId: 'res\u0001id' }], {
|
||||
bridge: fakeBridge, groupId: 12345,
|
||||
})).rejects.toMatchObject({ code: 'INVALID_FIELD', elementType: 'forward', field: 'resId' });
|
||||
});
|
||||
|
||||
it('preserves valid Unicode boundaries and whitespace in preview text', async () => {
|
||||
const text = '\u0009\u000A\u000D\u0020\uD7FF\uE000\uFFFD\u{10000}\u{10FFFF}';
|
||||
const [elem] = await buildSendElems([{ type: 'forward', resId: 'valid-unicode', forwardNews: [{ text }] }], {
|
||||
bridge: fakeBridge, groupId: 12345,
|
||||
});
|
||||
expect(inflatePrefixedPayload(elem.richMsg!.template1!))
|
||||
.toContain('	

 \uD7FF\uE000\uFFFD\u{10000}\u{10FFFF}</title>');
|
||||
});
|
||||
|
||||
it('threads forwardSource / forwardSummary / forwardPrompt / forwardNews / forwardTSum verbatim', async () => {
|
||||
const [elem] = await buildSendElems(
|
||||
[{
|
||||
|
||||
@@ -4,6 +4,7 @@ import type {
|
||||
} from '@snowluma/proto-defs/action';
|
||||
import type { Elem, GroupFileExtra, MarketFacePbReserve, MsgInfo, PokeExtra, QFaceExtra, QSmallFaceExtra } from '@snowluma/proto-defs/element';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { deflateSync } from 'zlib';
|
||||
import type { BridgeContext } from './bridge-context';
|
||||
@@ -22,6 +23,7 @@ import { uploadPttMsgInfo } from './highway/ptt-upload';
|
||||
import { uploadVideoMsgInfo } from './highway/video-upload';
|
||||
|
||||
type ProtoElem = Partial<Elem>;
|
||||
const log = createLogger('Protocol.Elements');
|
||||
|
||||
export interface SendContext {
|
||||
bridge: BridgeContext;
|
||||
@@ -271,7 +273,24 @@ function escapeXml(value: string): string {
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
.replace(/'/g, ''')
|
||||
.replace(/\t/g, '	')
|
||||
.replace(/\n/g, '
')
|
||||
.replace(/\r/g, '
');
|
||||
}
|
||||
|
||||
function xmlPreviewText(value: string): string {
|
||||
// Unicode mode preserves supplementary characters and replaces lone surrogates.
|
||||
// eslint-disable-next-line no-control-regex
|
||||
return value.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE\uFFFF]/gu, '\uFFFD');
|
||||
}
|
||||
|
||||
function escapeForwardPreview(value: string, field: string): string {
|
||||
const normalized = xmlPreviewText(value);
|
||||
if (normalized !== value) {
|
||||
log.debug('forward preview characters normalized: field=%s', field);
|
||||
}
|
||||
return escapeXml(normalized);
|
||||
}
|
||||
|
||||
function makeForwardElem(element: MessageElement): ProtoElem {
|
||||
@@ -303,20 +322,26 @@ function makeForwardElem(element: MessageElement): ProtoElem {
|
||||
// LightApp because their forwardUuid must match a piggyback actionCommand
|
||||
// in the outer long-message body.
|
||||
if (!element.forwardUuid) {
|
||||
const titles = news.map((item) =>
|
||||
'<title color="#777777" size="26">' + escapeXml(item.text ?? '') + '</title>',
|
||||
if (xmlPreviewText(resId) !== resId) {
|
||||
throw new MessageElementValidationError(
|
||||
'INVALID_FIELD', 'forward resId contains invalid characters', 'forward', 'resId',
|
||||
);
|
||||
}
|
||||
const escapedSource = escapeForwardPreview(source, 'source');
|
||||
const titles = news.map((item, index) =>
|
||||
'<title color="#777777" size="26">' + escapeForwardPreview(item.text ?? '', `news[${index}]`) + '</title>',
|
||||
).join('');
|
||||
const xml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?> "
|
||||
+ '<msg serviceID="35" templateID="1" action="viewMultiMsg"'
|
||||
+ ' brief="' + escapeXml(prompt) + '"'
|
||||
+ ' brief="' + escapeForwardPreview(prompt, 'prompt') + '"'
|
||||
+ ' m_fileName="' + escapeXml(uniseq) + '"'
|
||||
+ ' m_resid="' + escapeXml(resId) + '"'
|
||||
+ ' tSum="' + tSum + '" flag="3">'
|
||||
+ '<item layout="1"> '
|
||||
+ '<title color="#000000" size="34">' + escapeXml(source) + '</title>'
|
||||
+ '<title color="#000000" size="34">' + escapedSource + '</title>'
|
||||
+ titles
|
||||
+ ' <hr></hr> <summary color="#808080">' + escapeXml(summary) + '</summary>'
|
||||
+ '</item> <source name="' + escapeXml(source) + '"></source> </msg>';
|
||||
+ ' <hr></hr> <summary color="#808080">' + escapeForwardPreview(summary, 'summary') + '</summary>'
|
||||
+ '</item> <source name="' + escapedSource + '"></source> </msg>';
|
||||
return {
|
||||
richMsg: {
|
||||
template1: makeDeflatedPayload(xml),
|
||||
|
||||
Reference in new issue
Block a user