fix(telegram): guard malformed native menu specs

This commit is contained in:
liuxiaopai-ai
2026-03-03 01:52:31 +08:00
committed by Peter Steinberger
parent ed55b63684
commit 0958d11478
3 changed files with 29 additions and 5 deletions

View File

@@ -60,6 +60,27 @@ describe("bot-native-command-menu", () => {
expect(result.issues).toEqual([]);
});
it("ignores malformed plugin specs without crashing", () => {
const malformedSpecs = [
{ name: "valid", description: " Works " },
{ name: "missing-description", description: undefined },
{ name: undefined, description: "Missing name" },
] as unknown as Parameters<typeof buildPluginTelegramMenuCommands>[0]["specs"];
const result = buildPluginTelegramMenuCommands({
specs: malformedSpecs,
existingCommands: new Set<string>(),
});
expect(result.commands).toEqual([{ command: "valid", description: "Works" }]);
expect(result.issues).toContain(
'Plugin command "/missing_description" is missing a description.',
);
expect(result.issues).toContain(
'Plugin command "/<unknown>" is invalid for Telegram (use a-z, 0-9, underscore; max 32 chars).',
);
});
it("deletes stale commands before setting new menu", async () => {
const callOrder: string[] = [];
const deleteMyCommands = vi.fn(async () => {

View File

@@ -15,8 +15,8 @@ export type TelegramMenuCommand = {
};
type TelegramPluginCommandSpec = {
name: string;
description: string;
name: unknown;
description: unknown;
};
function isBotCommandsTooMuchError(err: unknown): boolean {
@@ -54,14 +54,16 @@ export function buildPluginTelegramMenuCommands(params: {
const pluginCommandNames = new Set<string>();
for (const spec of specs) {
const normalized = normalizeTelegramCommandName(spec.name);
const rawName = typeof spec.name === "string" ? spec.name : "";
const normalized = normalizeTelegramCommandName(rawName);
if (!normalized || !TELEGRAM_COMMAND_NAME_PATTERN.test(normalized)) {
const invalidName = rawName.trim() ? rawName : "<unknown>";
issues.push(
`Plugin command "/${spec.name}" is invalid for Telegram (use a-z, 0-9, underscore; max 32 chars).`,
`Plugin command "/${invalidName}" is invalid for Telegram (use a-z, 0-9, underscore; max 32 chars).`,
);
continue;
}
const description = spec.description.trim();
const description = typeof spec.description === "string" ? spec.description.trim() : "";
if (!description) {
issues.push(`Plugin command "/${normalized}" is missing a description.`);
continue;