fix(telegram): use Array.from instead of spread for code point length validation (#111443)

This commit is contained in:
weiqinl
2026-08-01 23:33:12 +08:00
committed by GitHub
parent c8e6ee5306
commit ba2827f706
2 changed files with 74 additions and 2 deletions

View File

@@ -49,7 +49,7 @@ export async function editForumTopicTelegram(
if (nameProvided && !trimmedName) {
throw new Error("Telegram forum topic name is required");
}
if (trimmedName && trimmedName.length > 128) {
if (trimmedName && Array.from(trimmedName).length > 128) {
throw new Error("Telegram forum topic name must be 128 characters or fewer");
}
const iconProvided = opts.iconCustomEmojiId !== undefined;
@@ -177,7 +177,7 @@ export async function createForumTopicTelegram(
throw new Error("Forum topic name is required");
}
const trimmedName = name.trim();
if (trimmedName.length > 128) {
if (Array.from(trimmedName).length > 128) {
throw new Error("Forum topic name must be 128 characters or fewer");
}

View File

@@ -826,6 +826,31 @@ describe("sendMessageTelegram", () => {
});
});
it.each([
["65 emoji", "😀".repeat(65)],
["128 emoji", "😀".repeat(128)],
["128 mixed emoji and ASCII characters", "😀".repeat(64) + "a".repeat(64)],
["128 CJK characters", "界".repeat(128)],
])("accepts %s forum topic names by Unicode code points", async (_label, name) => {
loadConfig.mockReturnValue({
channels: {
telegram: {
botToken: "tok",
},
},
});
botApi.editForumTopic.mockResolvedValue(true);
await editForumTopicTelegram("-1001234567890", 271, {
cfg: TELEGRAM_TEST_CFG,
token: "tok",
accountId: "default",
name,
});
expect(botApi.editForumTopic).toHaveBeenCalledWith("-1001234567890", 271, { name });
});
it("strips topic suffixes before editing a Telegram forum topic", async () => {
loadConfig.mockReturnValue({
channels: {
@@ -5106,6 +5131,24 @@ describe("createForumTopicTelegram", () => {
});
}
it.each([
["65 emoji", "🎃".repeat(65)],
["128 emoji", "🎃".repeat(128)],
["128 mixed emoji and ASCII characters", "🎃".repeat(64) + "a".repeat(64)],
["128 CJK characters", "界".repeat(128)],
])("accepts %s forum topic names by Unicode code points", async (_label, name) => {
const createForumTopic = vi.fn().mockResolvedValue({ message_thread_id: 400, name });
const api = { createForumTopic } as unknown as Bot["api"];
await createForumTopicTelegram("-1001234567890", name, {
cfg: TELEGRAM_TEST_CFG,
token: "tok",
api,
});
expect(createForumTopic).toHaveBeenCalledWith("-1001234567890", name, undefined);
});
it("rejects an invalid topic name before creating a Telegram client", async () => {
botCtorSpy.mockClear();
@@ -5117,5 +5160,34 @@ describe("createForumTopicTelegram", () => {
).rejects.toThrow("Forum topic name is required");
expect(botCtorSpy).not.toHaveBeenCalled();
});
it.each([
["129 ASCII characters", "a".repeat(129)],
["129 emoji", "🎃".repeat(129)],
["19 multi-code-point emoji graphemes", "👨‍👩‍👧‍👦".repeat(19)],
])("rejects %s exceeding 128 Unicode code points on create and edit", async (_label, name) => {
const createForumTopic = vi.fn();
const editForumTopic = vi.fn();
const api = { createForumTopic, editForumTopic } as unknown as Bot["api"];
await expect(
createForumTopicTelegram("-1001234567890", name, {
cfg: TELEGRAM_TEST_CFG,
token: "tok",
api,
}),
).rejects.toThrow("128 characters or fewer");
await expect(
editForumTopicTelegram("-1001234567890", 271, {
cfg: TELEGRAM_TEST_CFG,
token: "tok",
api,
name,
}),
).rejects.toThrow("128 characters or fewer");
expect(createForumTopic).not.toHaveBeenCalled();
expect(editForumTopic).not.toHaveBeenCalled();
});
});
/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */