⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(api): harden telegram integration config handling
Tinaabc1454160a
diff --git a/apps/api/src/plugins/telegram/config.ts b/apps/api/src/plugins/telegram/config.tsindex fbc26cfa8..58022b8e4 100644--- a/apps/api/src/plugins/telegram/config.ts+++ b/apps/api/src/plugins/telegram/config.ts@@ -13,7 +13,7 @@ export type TelegramEventKey = (typeof telegramEventKeys)[number];const telegramBotTokenSchema = v.pipe(v.string(),- v.regex(/^\d+:[A-Za-z0-9_-]{20,}$/, "Enter a valid Telegram bot token"),+ v.regex(/^\d{8,10}:[A-Za-z0-9_-]{35}$/, "Enter a valid Telegram bot token"),);const telegramChatIdSchema = v.pipe(@@ -55,7 +55,6 @@ export function normalizeTelegramConfig(): TelegramConfig {return {...config,- chatId: config.chatId,threadId:typeof config.threadId === "number" && Number.isFinite(config.threadId)? config.threadIddiff --git a/apps/api/src/plugins/telegram/events.ts b/apps/api/src/plugins/telegram/events.tsindex 4ea6915e5..077c08a94 100644--- a/apps/api/src/plugins/telegram/events.ts+++ b/apps/api/src/plugins/telegram/events.ts@@ -18,7 +18,7 @@ import type {} from "../types";import { postToTelegram } from "./client";import type { TelegramConfig, TelegramEventKey } from "./config";-import { normalizeTelegramConfig } from "./config";+import { normalizeTelegramConfig, validateTelegramConfig } from "./config";type TelegramEventData = {taskTitle: string;@@ -206,6 +206,18 @@ async function runTelegramHandler(featureKey: TelegramEventKey,buildMessage: () => TelegramMessageContent,): Promise<void> {+ const validation = await validateTelegramConfig(context.config);+ if (!validation.valid) {+ console.error("Invalid Telegram plugin config; skipping event dispatch", {+ errors: validation.errors,+ config: context.config,+ featureKey,+ projectId: event.projectId,+ taskId: event.taskId,+ });+ return;+ }+const config = normalizeTelegramConfig(context.config as TelegramConfig);if (!isEnabled(config, featureKey)) return;diff --git a/apps/api/src/telegram-integration/controllers/telegram-controller.ts b/apps/api/src/telegram-integration/controllers/telegram-controller.tsindex 75311bfe2..5933ad543 100644--- a/apps/api/src/telegram-integration/controllers/telegram-controller.ts+++ b/apps/api/src/telegram-integration/controllers/telegram-controller.ts@@ -1,10 +1,13 @@import { and, eq } from "drizzle-orm";+import { HTTPException } from "hono/http-exception";+import * as v from "valibot";import db from "../../database";import { integrationTable } from "../../database/schema";import {defaultTelegramEvents,normalizeTelegramConfig,type TelegramConfig,+ telegramConfigSchema,} from "../../plugins/telegram/config";function maskBotToken(value: string): string {@@ -18,17 +21,39 @@ function maskBotToken(value: string): string {return `${prefix}:${maskedSuffix}`;}-export function toResponse(integration: {+type TelegramIntegrationRecord = {id: string;projectId: string;config: string;isActive: boolean | null;createdAt: Date;updatedAt: Date;-}) {- const config = normalizeTelegramConfig(- JSON.parse(integration.config) as TelegramConfig,- );+};++export function parseTelegramIntegrationConfig(+ integration: Pick<TelegramIntegrationRecord, "config" | "id" | "projectId">,+): TelegramConfig {+ try {+ const parsed = v.parse(+ telegramConfigSchema,+ JSON.parse(integration.config),+ );+ return normalizeTelegramConfig(parsed);+ } catch (error) {+ console.error("Failed to parse Telegram integration config", {+ error,+ integrationId: integration.id,+ projectId: integration.projectId,+ rawConfig: integration.config,+ });+ throw new HTTPException(500, {+ message: "Stored Telegram integration configuration is invalid",+ });+ }+}++export function toResponse(integration: TelegramIntegrationRecord) {+ const config = parseTelegramIntegrationConfig(integration);return {id: integration.id,diff --git a/apps/api/src/telegram-integration/index.ts b/apps/api/src/telegram-integration/index.tsindex 99ece5551..e41a8e1c6 100644--- a/apps/api/src/telegram-integration/index.ts+++ b/apps/api/src/telegram-integration/index.ts@@ -7,12 +7,14 @@ import db from "../database";import { integrationTable } from "../database/schema";import {normalizeTelegramConfig,- type TelegramConfig,validateTelegramConfig,} from "../plugins/telegram/config";import { telegramIntegrationSchema } from "../schemas";import { workspaceAccess } from "../utils/workspace-access-middleware";-import { getTelegramIntegration } from "./controllers/telegram-controller";+import {+ getTelegramIntegration,+ parseTelegramIntegrationConfig,+} from "./controllers/telegram-controller";const telegramIntegration = new Hono<{Variables: {@@ -49,6 +51,9 @@ telegramIntegration"application/json": { schema: resolver(telegramIntegrationSchema) },},},+ 404: {+ description: "Telegram integration not found",+ },},}),validator("param", v.object({ projectId: v.string() })),@@ -56,6 +61,11 @@ telegramIntegrationasync (c) => {const { projectId } = c.req.valid("param");const integration = await getTelegramIntegration(projectId);+ if (!integration) {+ throw new HTTPException(404, {+ message: "Telegram integration not found",+ });+ }return c.json(integration);},)@@ -171,12 +181,16 @@ telegramIntegration});}- const currentConfig = normalizeTelegramConfig(- JSON.parse(existing.config) as TelegramConfig,- );+ const currentConfig = parseTelegramIntegrationConfig(existing);+ const nextBotToken =+ "botToken" in body+ ? (body.botToken?.trim() ?? "")+ : currentConfig.botToken;+ const nextChatId =+ "chatId" in body ? (body.chatId?.trim() ?? "") : currentConfig.chatId;const nextConfig = normalizeTelegramConfig({- botToken: body.botToken?.trim() || currentConfig.botToken,- chatId: body.chatId?.trim() || currentConfig.chatId,+ botToken: nextBotToken,+ chatId: nextChatId,threadId:body.threadId === undefined? currentConfig.threadId