fix(protocol): integrate styled group card normalization
Keep the full control-character range limited to decoded styled-card text. Preserve member-level error isolation and cover whitespace, Unicode text, cached output, and restart behavior. Refs #680
This commit is contained in:
4 files changed
+92
-8
No files matched your search
@@ -52,6 +52,13 @@ field is present.
|
||||
|
||||
## Group member cards
|
||||
|
||||
Styled cards are decoded before their text is normalized. Within successfully
|
||||
decoded UTF-8 text, SnowLuma removes C0 control characters (`U+0000`–`U+001F`)
|
||||
except tab, line feed and carriage return, and removes DEL (`U+007F`). Spaces,
|
||||
Unicode formatting characters and emoji sequences are preserved. Raw plain text
|
||||
and explicitly literal display names retain strict control-character validation;
|
||||
invalid UTF-8 is rejected in both formats.
|
||||
|
||||
`get_group_member_list` and `get_group_member_info` keep returning member data
|
||||
when an individual group card cannot be decoded. The last valid card for that
|
||||
member in the same group and account is retained; if none is known, the OneBot
|
||||
|
||||
@@ -150,7 +150,21 @@ describe('group card failures through roster, cache and OneBot output', () => {
|
||||
};
|
||||
const transport = sendPages(bridge, [
|
||||
firstPage,
|
||||
{ members: [{ uin: { uin: 66, uid: 'u_66' } }] },
|
||||
{
|
||||
members: [
|
||||
{ uin: { uin: 66, uid: 'u_66' } },
|
||||
{
|
||||
uin: { uin: 77, uid: 'u_77' },
|
||||
memberName: 'styled member',
|
||||
memberCard: {
|
||||
memberCard: Buffer.from(
|
||||
'0a090a0724c7bfc480440e0a071205202014c6951000',
|
||||
'hex',
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
const output = await getGroupMemberList(bridge, groupId, noCache);
|
||||
@@ -161,6 +175,7 @@ describe('group card failures through roster, cache and OneBot output', () => {
|
||||
[44, ''],
|
||||
[55, ''],
|
||||
[66, ''],
|
||||
[77, ' ƕ'],
|
||||
]);
|
||||
expect(output[0]).toMatchObject({ nickname: 'fresh-11', role: 'admin' });
|
||||
expect(output[5]).toMatchObject({ is_robot: true });
|
||||
@@ -177,6 +192,9 @@ describe('group card failures through roster, cache and OneBot output', () => {
|
||||
expect(await getGroupMemberInfo(bridge, groupId, 11)).toMatchObject({
|
||||
card: 'last valid card',
|
||||
});
|
||||
expect(await getGroupMemberInfo(bridge, groupId, 77)).toMatchObject({
|
||||
card: ' ƕ',
|
||||
});
|
||||
expect(transport.tokens).toEqual(['', 'page-2']);
|
||||
await bridge.dispose();
|
||||
|
||||
|
||||
@@ -4,11 +4,12 @@ import { protobuf_decode } from '@snowluma/proton';
|
||||
|
||||
const log = createLogger('Protocol.GroupMemberCard');
|
||||
const utf8 = new TextDecoder('utf-8', { fatal: true });
|
||||
// eslint-disable-next-line no-control-regex -- C0 controls except tab/LF/CR, plus DEL.
|
||||
const cardControls = /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g;
|
||||
|
||||
/** Keep ordinary text whitespace, but never publish binary framing as a name. */
|
||||
export function isBinaryCardText(text: string): boolean {
|
||||
// eslint-disable-next-line no-control-regex -- These are the binary framing bytes we must detect.
|
||||
return /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(text);
|
||||
return text.search(cardControls) !== -1;
|
||||
}
|
||||
|
||||
function utf8Text(bytes: Uint8Array): string {
|
||||
@@ -57,9 +58,9 @@ export function decodeGroupMemberCard(bytes?: Uint8Array): string {
|
||||
const text = parts.flatMap((part) =>
|
||||
part.control == null && part.text != null ? [part.text] : [],
|
||||
);
|
||||
// QQ hides control characters embedded in a text part (e.g. 0x14) and shows the rest.
|
||||
// eslint-disable-next-line no-control-regex -- Same set as isBinaryCardText.
|
||||
return utf8Text(Buffer.concat(text)).replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '');
|
||||
// Structured text can contain display controls (#680). Normalize the whole
|
||||
// control set after strict UTF-8 decoding, preserving whitespace and Unicode formatting.
|
||||
return utf8Text(Buffer.concat(text)).replace(cardControls, '');
|
||||
}
|
||||
|
||||
/** A malformed optional display name must not discard the message itself. */
|
||||
|
||||
@@ -1,16 +1,27 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { subscribeLogs } from '@snowluma/common/logger';
|
||||
import type { PushMsg } from '@snowluma/proto-defs/message';
|
||||
import type { PushMsg, RichGroupCard } from '@snowluma/proto-defs/message';
|
||||
import type { OidbBase, OidbSvcTrpcTcp0xFE7_3Response } from '@snowluma/proto-defs/oidb';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import { IdentityService } from '../src/identity-service';
|
||||
import type { GroupMemberInfo } from '../src/qq-info';
|
||||
import { MSG_PUSH_CMD, parseMsgPushOrThrow } from '../src/msg-push';
|
||||
import { FetchGroupMemberListPage } from '../src/oidb-services/contacts/fetch-group-member-list-page';
|
||||
import { decodeGroupMemberCard, isBinaryCardText } from '../src/group-member-card';
|
||||
|
||||
const groupId = 42;
|
||||
const uin = 123;
|
||||
const hex = (s: string) => Buffer.from(s, 'hex');
|
||||
const controlCodes = [
|
||||
...Array.from({ length: 0x20 }, (_, code) => code).filter(
|
||||
(code) => ![9, 10, 13].includes(code),
|
||||
),
|
||||
0x7f,
|
||||
];
|
||||
|
||||
function styledCard(text: Uint8Array): Uint8Array {
|
||||
return protobuf_encode<RichGroupCard>({ parts: [{ text }] });
|
||||
}
|
||||
|
||||
async function fetchMember(card: Uint8Array) {
|
||||
const responseData = Buffer.from(
|
||||
@@ -118,6 +129,43 @@ describe('group member card wire formats', () => {
|
||||
},
|
||||
);
|
||||
|
||||
it.each(controlCodes.map((code) => [code.toString(16).padStart(2, '0'), code] as const))(
|
||||
'cleans 0x%s only in decoded card text and keeps raw text strict',
|
||||
async (_label, code) => {
|
||||
const raw = Buffer.from(` 甲${String.fromCodePoint(code)}乙🌙 `);
|
||||
const card = styledCard(raw);
|
||||
const member = await fetchMember(card);
|
||||
expect(member.card).toBe(' 甲乙🌙 ');
|
||||
expect(push(withCachedMember({ ...member, card: 'outdated' }), card)).toMatchObject({
|
||||
senderNick: 'nickname',
|
||||
senderCard: ' 甲乙🌙 ',
|
||||
});
|
||||
expect(() => decodeGroupMemberCard(raw)).toThrow('control characters');
|
||||
// Detection also serves repeated cache reads; it must not depend on regex state.
|
||||
expect(isBinaryCardText(raw.toString())).toBe(true);
|
||||
expect(isBinaryCardText(raw.toString())).toBe(true);
|
||||
expect(isBinaryCardText(member.card!)).toBe(false);
|
||||
},
|
||||
);
|
||||
|
||||
it('preserves whitespace, Unicode formatting and combining characters', async () => {
|
||||
const text = ' \t甲\n乙\r 👩\u200d💻 ❤️\u200b\u2060 e\u0301 \u0085 ';
|
||||
const member = await fetchMember(styledCard(Buffer.from(text)));
|
||||
expect(member.card).toBe(text);
|
||||
expect(decodeGroupMemberCard(Buffer.from(text))).toBe(text);
|
||||
expect(isBinaryCardText(text)).toBe(false);
|
||||
});
|
||||
|
||||
it('treats decoded text containing only non-display controls as an empty card', async () => {
|
||||
const card = styledCard(Buffer.from(controlCodes));
|
||||
const member = await fetchMember(card);
|
||||
expect(member.card).toBe('');
|
||||
expect(push(withCachedMember({ ...member, card: 'outdated' }), card)).toMatchObject({
|
||||
senderNick: 'nickname',
|
||||
senderCard: '',
|
||||
});
|
||||
});
|
||||
|
||||
it('treats a control-only card as empty even when a previous card was cached', async () => {
|
||||
const control = hex('0a030a01ff1000');
|
||||
const member = await fetchMember(control);
|
||||
@@ -137,7 +185,7 @@ describe('group member card wire formats', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each(['0a051202ff00', '616207'])(
|
||||
it.each(['0a051202ff00', '616207', '0a031201ff', '0a0512036114ff'])(
|
||||
'keeps a member with unreadable card %s and logs its context',
|
||||
async (encoded) => {
|
||||
const damaged = hex(encoded);
|
||||
@@ -149,6 +197,7 @@ describe('group member card wire formats', () => {
|
||||
});
|
||||
|
||||
try {
|
||||
expect(() => decodeGroupMemberCard(damaged)).toThrow('group card');
|
||||
expect(await fetchMember(damaged)).toMatchObject({
|
||||
uin,
|
||||
nickname: 'nickname',
|
||||
@@ -176,4 +225,13 @@ describe('group member card wire formats', () => {
|
||||
elements: [{ type: 'text', text: 'hello' }],
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps literal display names strict when they contain a styled-card control character', async () => {
|
||||
const identity = withCachedMember(await fetchMember(Buffer.from('existing card')));
|
||||
expect(push(identity, Buffer.from('literal\x14name'), 2)).toMatchObject({
|
||||
senderNick: 'nickname',
|
||||
senderCard: 'existing card',
|
||||
elements: [{ type: 'text', text: 'hello' }],
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user