From 2d70646e8d021ffbc98f37035878b6537fa07ab6 Mon Sep 17 00:00:00 2001 From: Evgeny Zislis Date: Fri, 20 Feb 2026 19:11:24 +0200 Subject: [PATCH] fix(cron): validate Telegram delivery targets to reject invalid formats Reject cron jobs with Telegram delivery targets using chatId/topicId format (e.g. "-10012345/6789") which are not supported. Allow valid formats: plain chat ID, colon delimiter (:), @username, and t.me URLs. Added validateTelegramDeliveryTarget() function and tests. --- src/cron/service.jobs.test.ts | 24 ++++++++++++++++-------- src/cron/service/jobs.ts | 12 ++++++++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/cron/service.jobs.test.ts b/src/cron/service.jobs.test.ts index 787e8f6ef81..c5c0632475b 100644 --- a/src/cron/service.jobs.test.ts +++ b/src/cron/service.jobs.test.ts @@ -153,7 +153,7 @@ describe("applyJobPatch", () => { expect(job.delivery).toEqual({ mode: "webhook", to: "https://example.invalid/trim" }); }); - it("rejects Telegram delivery with invalid target (slash delimiter)", () => { + it("rejects Telegram delivery with invalid target (chatId/topicId format)", () => { const job = createIsolatedAgentTurnJob("job-telegram-invalid", { mode: "announce", channel: "telegram", @@ -161,20 +161,28 @@ describe("applyJobPatch", () => { }); expect(() => applyJobPatch(job, { enabled: true })).toThrow( - 'Invalid Telegram delivery target "-10012345/6789". Use colon (:) as delimiter, not slash or other characters. Valid formats: -1001234567890, -1001234567890:123, -1001234567890:topic:123', + 'Invalid Telegram delivery target "-10012345/6789". Use colon (:) as delimiter for topics, not slash. Valid formats: -1001234567890, -1001234567890:123, -1001234567890:topic:123, @username, https://t.me/username', ); }); - it("rejects Telegram delivery with other invalid characters", () => { - const job = createIsolatedAgentTurnJob("job-telegram-invalid-space", { + it("accepts Telegram delivery with t.me URL", () => { + const job = createIsolatedAgentTurnJob("job-telegram-tme", { mode: "announce", channel: "telegram", - to: "-10012345 6789", + to: "https://t.me/mychannel", }); - expect(() => applyJobPatch(job, { enabled: true })).toThrow( - 'Invalid Telegram delivery target "-10012345 6789". Use colon (:) as delimiter, not slash or other characters. Valid formats: -1001234567890, -1001234567890:123, -1001234567890:topic:123', - ); + expect(() => applyJobPatch(job, { enabled: true })).not.toThrow(); + }); + + it("accepts Telegram delivery with t.me URL (no https)", () => { + const job = createIsolatedAgentTurnJob("job-telegram-tme-no-https", { + mode: "announce", + channel: "telegram", + to: "t.me/mychannel", + }); + + expect(() => applyJobPatch(job, { enabled: true })).not.toThrow(); }); it("accepts Telegram delivery with valid target (plain chat id)", () => { diff --git a/src/cron/service/jobs.ts b/src/cron/service/jobs.ts index dc403585574..db42b80ba54 100644 --- a/src/cron/service/jobs.ts +++ b/src/cron/service/jobs.ts @@ -83,15 +83,19 @@ export function assertSupportedJobSpec(job: Pick to.includes(char)); - if (hasInvalidChar) { - return `Invalid Telegram delivery target "${to}". Use colon (:) as delimiter, not slash or other characters. Valid formats: -1001234567890, -1001234567890:123, -1001234567890:topic:123`; + const trimmed = to.trim(); + if (TELEGRAM_TME_URL_REGEX.test(trimmed)) { + return undefined; + } + if (TELEGRAM_SLASH_TOPIC_REGEX.test(trimmed)) { + return `Invalid Telegram delivery target "${to}". Use colon (:) as delimiter for topics, not slash. Valid formats: -1001234567890, -1001234567890:123, -1001234567890:topic:123, @username, https://t.me/username`; } return undefined; }