Personal Telegram developer console for GitHub

feat/studio-g0-foundation
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 206 lines 6.74 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { KillSwitchGuidedNexaDevBot } from '../src/kill-switch-bot.js';
4
5 const OWNER = '42';
6 const syntheticToken = (prefix) => `${prefix}synthetic_noncredential_fixture`;
7 const ownerMessage = (text, extra = {}) => ({
8 message_id: 17,
9 from: { id: Number(OWNER) },
10 chat: { id: Number(OWNER), type: 'private' },
11 text,
12 ...extra
13 });
14
15 function guardedBot({ ownerId = OWNER, failures = {} } = {}) {
16 const bot = new KillSwitchGuidedNexaDevBot({
17 OWNER_TELEGRAM_ID: ownerId,
18 TELEGRAM_BOT_TOKEN: 'synthetic-bot-credential'
19 });
20 const effects = [];
21 bot.telegram.fetchFn = async (url, options) => {
22 const method = new URL(url).pathname.split('/').at(-1);
23 const payload = JSON.parse(options.body);
24 effects.push({ method, payload });
25 if (failures[method]) throw new Error(failures[method]);
26 return Response.json({
27 ok: true,
28 result: method === 'sendMessage' ? { message_id: 18 } : true
29 });
30 };
31 return { bot, effects };
32 }
33
34 test('all GitHub token prefixes delete the message before a Persian revocation warning', async (t) => {
35 for (const prefix of [
36 'github_pat_',
37 'ghp_',
38 'gho_',
39 'ghu_',
40 'ghs_',
41 'ghr_'
42 ]) {
43 await t.test(prefix, async () => {
44 const { bot, effects } = guardedBot();
45 const text = syntheticToken(prefix);
46 const result = await bot.handleUpdate({ message: ownerMessage(text) });
47
48 assert.equal(result.secretRejected, true);
49 assert.deepEqual(
50 effects.map(({ method }) => method),
51 ['deleteMessage', 'sendMessage']
52 );
53 assert.deepEqual(effects[0].payload, { chat_id: 42, message_id: 17 });
54 assert.match(effects[1].payload.text, /توکن در چت قرار گرفت/);
55 assert.match(effects[1].payload.text, /باید در GitHub باطل شود/);
56 assert.match(
57 effects[1].payload.text,
58 /https:\/\/github\.com\/settings\//
59 );
60 assert.equal(JSON.stringify({ effects, result }).includes(text), false);
61 assert.equal(result.deleted, true);
62 assert.equal(result.warned, true);
63 });
64 }
65 });
66
67 test('token interception precedes pending workspace reads, file download, routing and audit', async () => {
68 const { bot, effects } = guardedBot();
69 const reached = [];
70 bot.workspaceStore = {
71 isConfigured: () => true,
72 async getPendingInput() {
73 reached.push('pending');
74 return { workspaceId: 'W000000001', mode: 'draft_file' };
75 }
76 };
77 bot.telegram.downloadTextFile = async () => {
78 reached.push('download');
79 return { text: 'safe fixture', byteLength: 12 };
80 };
81 bot.router.dispatchMessage = async () => {
82 reached.push('dispatch');
83 return { text: 'generic response' };
84 };
85 bot.audit.log = () => reached.push('audit');
86
87 const result = await bot.handleUpdate({
88 message: ownerMessage(` \n${syntheticToken('github_pat_')}`, {
89 document: { file_id: 'synthetic-file', file_size: 12 }
90 })
91 });
92
93 assert.deepEqual(reached, []);
94 assert.equal(result.secretRejected, true);
95 assert.equal(effects.length, 2);
96 });
97
98 test('direct message handling also rejects tokens before pending input', async () => {
99 const { bot } = guardedBot();
100 let pendingReads = 0;
101 bot.workspaceStore = {
102 isConfigured: () => true,
103 async getPendingInput() {
104 pendingReads++;
105 return null;
106 }
107 };
108
109 const result = await bot.handleMessage(ownerMessage(syntheticToken('ghp_')));
110
111 assert.equal(pendingReads, 0);
112 assert.equal(result.secretRejected, true);
113 });
114
115 test('edited token messages are intercepted before the generic message dispatcher', async () => {
116 const { bot, effects } = guardedBot();
117 let dispatched = false;
118 bot.handleMessage = async () => {
119 dispatched = true;
120 return { ok: true, generic: true };
121 };
122
123 const result = await bot.handleUpdate({
124 edited_message: ownerMessage(syntheticToken('gho_'))
125 });
126
127 assert.equal(result.secretRejected, true);
128 assert.equal(dispatched, false);
129 assert.deepEqual(
130 effects.map(({ method }) => method),
131 ['deleteMessage', 'sendMessage']
132 );
133 });
134
135 test('document captions beginning with a token are rejected before upload processing', async () => {
136 const { bot, effects } = guardedBot();
137 const result = await bot.handleUpdate({
138 message: ownerMessage(undefined, {
139 caption: syntheticToken('ghs_'),
140 document: { file_id: 'synthetic-file', file_size: 12 }
141 })
142 });
143
144 assert.equal(result.secretRejected, true);
145 assert.equal(effects[0].method, 'deleteMessage');
146 });
147
148 test('a failed delete still warns without claiming deletion or echoing a raw provider error', async () => {
149 const text = syntheticToken('github_pat_');
150 const { bot, effects } = guardedBot({
151 failures: { deleteMessage: `provider included ${text}` }
152 });
153 const result = await bot.handleUpdate({ message: ownerMessage(text) });
154
155 assert.equal(result.secretRejected, true);
156 assert.equal(result.deleted, false);
157 assert.equal(result.warned, true);
158 assert.equal(effects[1].method, 'sendMessage');
159 assert.match(effects[1].payload.text, /حذف.*تأیید نشد/);
160 assert.equal(JSON.stringify({ effects, result }).includes(text), false);
161 });
162
163 test('warning failures return only metadata and never fall back to a generic handler', async () => {
164 const text = syntheticToken('ghu_');
165 const { bot, effects } = guardedBot({
166 failures: { sendMessage: `provider included ${text}` }
167 });
168 let dispatched = false;
169 bot.handleMessage = async () => {
170 dispatched = true;
171 return { ok: true };
172 };
173 const result = await bot.handleUpdate({ message: ownerMessage(text) });
174
175 assert.equal(result.secretRejected, true);
176 assert.equal(result.warned, false);
177 assert.equal(result.deleted, true);
178 assert.equal(dispatched, false);
179 assert.equal(JSON.stringify({ effects, result }).includes(text), false);
180 });
181
182 test('secret messages cannot cross the configured owner boundary or run without an owner', async () => {
183 for (const ownerId of [OWNER, '']) {
184 const { bot, effects } = guardedBot({ ownerId });
185 const result = await bot.handleUpdate({
186 message: ownerMessage(syntheticToken('ghr_'), { from: { id: 99 } })
187 });
188 assert.equal(result.ignored, true);
189 assert.deepEqual(effects, []);
190 }
191 });
192
193 test('ordinary messages keep their existing dispatch and ordinary edits remain ignored', async () => {
194 const { bot, effects } = guardedBot();
195 const ordinary = await bot.handleUpdate({ message: ownerMessage('/help') });
196 assert.equal(ordinary.ok, true);
197 assert.equal(ordinary.secretRejected, undefined);
198 assert.equal(effects.length, 1);
199 assert.equal(effects[0].method, 'sendMessage');
200
201 const edited = await bot.handleUpdate({
202 edited_message: ownerMessage('/help')
203 });
204 assert.deepEqual(edited, { ok: true, ignored: true });
205 assert.equal(effects.length, 1);
206 });