Personal Telegram developer console for GitHub

phase2-20261005-l
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
js 128 lines 3.4 KB
Raw
1 import test from "node:test";
2 import assert from "node:assert/strict";
3 import worker from "../src/index.js";
4
5 function telegramRequest(secret, update) {
6 const headers = { "content-type": "application/json" };
7 if (secret !== undefined) headers["X-Telegram-Bot-Api-Secret-Token"] = secret;
8
9 return new Request("https://example.com/telegram/webhook", {
10 method: "POST",
11 headers,
12 body: JSON.stringify(update),
13 });
14 }
15
16 test("webhook fails closed when TELEGRAM_WEBHOOK_SECRET is missing", async () => {
17 const req = telegramRequest(undefined, {
18 message: { text: "/status", from: { id: 123 }, chat: { id: 123 } },
19 });
20
21 let calls = 0;
22 globalThis.fetch = async () => {
23 calls += 1;
24 return new Response(JSON.stringify({ ok: true }), {
25 status: 200,
26 headers: { "content-type": "application/json" },
27 });
28 };
29
30 const res = await worker.fetch(req, {
31 OWNER_TELEGRAM_ID: "123",
32 TELEGRAM_BOT_TOKEN: "token",
33 });
34
35 assert.equal(res.status, 503);
36 assert.equal(calls, 0);
37 });
38
39 test("webhook rejects a wrong Telegram secret", async () => {
40 const req = telegramRequest("wrong", {
41 message: { text: "/status", from: { id: 123 }, chat: { id: 123 } },
42 });
43
44 let calls = 0;
45 globalThis.fetch = async () => {
46 calls += 1;
47 throw new Error("should not call network");
48 };
49
50 const res = await worker.fetch(req, {
51 TELEGRAM_WEBHOOK_SECRET: "right",
52 OWNER_TELEGRAM_ID: "123",
53 TELEGRAM_BOT_TOKEN: "token",
54 });
55
56 assert.equal(res.status, 403);
57 assert.equal(calls, 0);
58 });
59
60 test("unauthorized Telegram users receive no bot response", async () => {
61 const req = telegramRequest("right", {
62 message: { text: "/status", from: { id: 999 }, chat: { id: 999 } },
63 });
64
65 let calls = 0;
66 globalThis.fetch = async () => {
67 calls += 1;
68 return new Response(JSON.stringify({ ok: true }), {
69 status: 200,
70 headers: { "content-type": "application/json" },
71 });
72 };
73
74 const res = await worker.fetch(req, {
75 TELEGRAM_WEBHOOK_SECRET: "right",
76 OWNER_TELEGRAM_ID: "123",
77 TELEGRAM_BOT_TOKEN: "token",
78 });
79
80 assert.equal(res.status, 200);
81 assert.equal(calls, 0);
82 });
83
84 test("temporary setup endpoint is disabled after webhook registration", async () => {
85 const req = new Request("https://nexa-dev-bot.example.workers.dev/telegram/setup", {
86 method: "GET",
87 });
88
89 let calls = 0;
90 globalThis.fetch = async () => {
91 calls += 1;
92 throw new Error("setup endpoint must not call Telegram after registration");
93 };
94
95 const res = await worker.fetch(req, {
96 TELEGRAM_WEBHOOK_SECRET: "secret",
97 TELEGRAM_BOT_TOKEN: "token",
98 });
99
100 assert.equal(res.status, 404);
101 assert.equal(calls, 0);
102 });
103
104 test("temporary diagnostic command no longer exposes Telegram ID", async () => {
105 const req = telegramRequest("right", {
106 message: { text: "/diag", from: { id: 123 }, chat: { id: 123 } },
107 });
108
109 const messages = [];
110 globalThis.fetch = async (_url, options) => {
111 const payload = JSON.parse(options.body);
112 messages.push(payload.text);
113 return new Response(JSON.stringify({ ok: true }), {
114 status: 200,
115 headers: { "content-type": "application/json" },
116 });
117 };
118
119 const res = await worker.fetch(req, {
120 TELEGRAM_WEBHOOK_SECRET: "right",
121 OWNER_TELEGRAM_ID: "123",
122 TELEGRAM_BOT_TOKEN: "token",
123 });
124
125 assert.equal(res.status, 200);
126 assert.equal(messages.length, 1);
127 assert.doesNotMatch(messages[0], /Telegram ID|Authorization:|\b123\b/);
128 });