Personal Telegram developer console for GitHub
phase2-20261005-y
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
| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import worker from '../src/index.js'; |
| 4 | |
| 5 | function makeReplayNamespace() { |
| 6 | const used = new Set(); |
| 7 | return { |
| 8 | idFromName(name) { |
| 9 | return String(name); |
| 10 | }, |
| 11 | get(id) { |
| 12 | return { |
| 13 | async fetch(request) { |
| 14 | const body = await request.json(); |
| 15 | const expiresAt = Number(body?.expiresAt); |
| 16 | if (!Number.isFinite(expiresAt)) { |
| 17 | return Response.json({ accepted: false, reason: 'invalid_expiry' }, { status: 400 }); |
| 18 | } |
| 19 | if (expiresAt < Date.now()) { |
| 20 | return Response.json({ accepted: false, reason: 'expired' }, { status: 410 }); |
| 21 | } |
| 22 | const key = String(id); |
| 23 | if (used.has(key)) { |
| 24 | return Response.json({ accepted: false, reason: 'already_used' }, { status: 409 }); |
| 25 | } |
| 26 | used.add(key); |
| 27 | return Response.json({ accepted: true }); |
| 28 | } |
| 29 | }; |
| 30 | } |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | const env = { |
| 35 | TELEGRAM_WEBHOOK_SECRET: 'right', |
| 36 | OWNER_TELEGRAM_ID: '123', |
| 37 | TELEGRAM_BOT_TOKEN: 'telegram-test-token', |
| 38 | GITHUB_TOKEN: 'github-test-token', |
| 39 | GITHUB_OWNER: 'davoodmehraban89', |
| 40 | APPROVAL_SIGNING_SECRET: 'approval-test-secret', |
| 41 | APPROVAL_REPLAY_GUARD: makeReplayNamespace() |
| 42 | }; |
| 43 | |
| 44 | function webhook(update) { |
| 45 | return new Request('https://example.test/telegram/webhook', { |
| 46 | method: 'POST', |
| 47 | headers: { |
| 48 | 'content-type': 'application/json', |
| 49 | 'X-Telegram-Bot-Api-Secret-Token': 'right' |
| 50 | }, |
| 51 | body: JSON.stringify(update) |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | function json(data, status = 200) { |
| 56 | return new Response(JSON.stringify(data), { |
| 57 | status, |
| 58 | headers: { 'content-type': 'application/json' } |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | function installMock() { |
| 63 | const telegramCalls = []; |
| 64 | const githubCalls = []; |
| 65 | const originalFetch = globalThis.fetch; |
| 66 | |
| 67 | globalThis.fetch = async (url, options = {}) => { |
| 68 | const value = String(url); |
| 69 | |
| 70 | if (value.startsWith('https://api.telegram.org/')) { |
| 71 | const method = value.split('/').at(-1); |
| 72 | const body = options.body ? JSON.parse(options.body) : {}; |
| 73 | telegramCalls.push({ method, body }); |
| 74 | return json({ ok: true, result: { message_id: body.message_id || 700, ...body } }); |
| 75 | } |
| 76 | |
| 77 | if (value === 'https://api.github.com/repositories/1001') { |
| 78 | githubCalls.push({ url: value, method: options.method || 'GET' }); |
| 79 | return json({ |
| 80 | id: 1001, |
| 81 | name: 'demo', |
| 82 | full_name: 'davoodmehraban89/demo', |
| 83 | private: true, |
| 84 | default_branch: 'main', |
| 85 | archived: false, |
| 86 | html_url: 'https://github.com/davoodmehraban89/demo' |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs/55/rerun-failed-jobs') { |
| 91 | githubCalls.push({ url: value, method: options.method || 'GET' }); |
| 92 | return new Response('', { status: 201 }); |
| 93 | } |
| 94 | |
| 95 | throw new Error(`Unexpected URL: ${value}`); |
| 96 | }; |
| 97 | |
| 98 | return { |
| 99 | telegramCalls, |
| 100 | githubCalls, |
| 101 | restore() { |
| 102 | globalThis.fetch = originalFetch; |
| 103 | } |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | async function sendUpdate(update, runtime = env) { |
| 108 | const response = await worker.fetch(webhook(update), runtime); |
| 109 | assert.equal(response.status, 200); |
| 110 | return response.json(); |
| 111 | } |
| 112 | |
| 113 | test('WRITE callback renders owner-bound approval and performs no GitHub write before approval', async () => { |
| 114 | const mock = installMock(); |
| 115 | try { |
| 116 | const menuMessage = { message_id: 700, chat: { id: 123 } }; |
| 117 | const result = await sendUpdate({ |
| 118 | callback_query: { |
| 119 | id: 'rerun-request', |
| 120 | from: { id: 123 }, |
| 121 | data: 'rerun:1001:55', |
| 122 | message: menuMessage |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | assert.equal(result.ok, true); |
| 127 | assert.equal(result.approvalRequired, true); |
| 128 | assert.equal(mock.githubCalls.length, 0, 'GitHub write must not execute before approval'); |
| 129 | |
| 130 | const edit = mock.telegramCalls.find(call => call.method === 'editMessageText'); |
| 131 | assert.ok(edit, 'approval request must edit the existing Telegram message'); |
| 132 | assert.match(edit.body.text, /تأیید|اجرا/); |
| 133 | |
| 134 | const approveButton = edit.body.reply_markup.inline_keyboard |
| 135 | .flat() |
| 136 | .find(button => String(button.callback_data || '').startsWith('approve:')); |
| 137 | assert.ok(approveButton, 'approval button must be present'); |
| 138 | assert.ok(new TextEncoder().encode(approveButton.callback_data).length <= 64); |
| 139 | } finally { |
| 140 | mock.restore(); |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | test('approve callback executes exactly once and replay is rejected', async () => { |
| 145 | const mock = installMock(); |
| 146 | try { |
| 147 | const menuMessage = { message_id: 701, chat: { id: 123 } }; |
| 148 | |
| 149 | await sendUpdate({ |
| 150 | callback_query: { |
| 151 | id: 'rerun-request', |
| 152 | from: { id: 123 }, |
| 153 | data: 'rerun:1001:55', |
| 154 | message: menuMessage |
| 155 | } |
| 156 | }); |
| 157 | |
| 158 | const approvalEdit = mock.telegramCalls |
| 159 | .filter(call => call.method === 'editMessageText') |
| 160 | .at(-1); |
| 161 | const approveButton = approvalEdit.body.reply_markup.inline_keyboard |
| 162 | .flat() |
| 163 | .find(button => String(button.callback_data || '').startsWith('approve:')); |
| 164 | assert.ok(approveButton); |
| 165 | |
| 166 | const first = await sendUpdate({ |
| 167 | callback_query: { |
| 168 | id: 'approve-once', |
| 169 | from: { id: 123 }, |
| 170 | data: approveButton.callback_data, |
| 171 | message: menuMessage |
| 172 | } |
| 173 | }); |
| 174 | assert.equal(first.ok, true); |
| 175 | |
| 176 | const writesAfterFirst = mock.githubCalls.filter(call => call.method === 'POST'); |
| 177 | assert.equal(writesAfterFirst.length, 1); |
| 178 | assert.match(writesAfterFirst[0].url, /rerun-failed-jobs$/); |
| 179 | |
| 180 | const replay = await sendUpdate({ |
| 181 | callback_query: { |
| 182 | id: 'approve-replay', |
| 183 | from: { id: 123 }, |
| 184 | data: approveButton.callback_data, |
| 185 | message: menuMessage |
| 186 | } |
| 187 | }); |
| 188 | assert.equal(replay.ok, false); |
| 189 | assert.match(String(replay.error || ''), /used|replay|approval/i); |
| 190 | |
| 191 | const writesAfterReplay = mock.githubCalls.filter(call => call.method === 'POST'); |
| 192 | assert.equal(writesAfterReplay.length, 1, 'replay must not perform a second GitHub write'); |
| 193 | } finally { |
| 194 | mock.restore(); |
| 195 | } |
| 196 | }); |
| 197 | |
| 198 | test('WRITE approval falls back to the configured Telegram webhook secret when no dedicated signing secret exists', async () => { |
| 199 | const mock = installMock(); |
| 200 | try { |
| 201 | const result = await sendUpdate({ |
| 202 | callback_query: { |
| 203 | id: 'fallback-signing-secret', |
| 204 | from: { id: 123 }, |
| 205 | data: 'rerun:1001:55', |
| 206 | message: { message_id: 702, chat: { id: 123 } } |
| 207 | } |
| 208 | }, { |
| 209 | ...env, |
| 210 | APPROVAL_SIGNING_SECRET: '' |
| 211 | }); |
| 212 | |
| 213 | assert.equal(result.ok, true); |
| 214 | assert.equal(result.approvalRequired, true); |
| 215 | assert.equal(mock.githubCalls.length, 0); |
| 216 | const edit = mock.telegramCalls.filter(call => call.method === 'editMessageText').at(-1); |
| 217 | assert.ok(edit.body.reply_markup.inline_keyboard.flat().some(button => String(button.callback_data || '').startsWith('approve:'))); |
| 218 | } finally { |
| 219 | mock.restore(); |
| 220 | } |
| 221 | }); |
| 222 | |
| 223 | test('approval lifecycle is audited without logging signed callback tokens', async () => { |
| 224 | const mock = installMock(); |
| 225 | const originalLog = console.log; |
| 226 | const logs = []; |
| 227 | console.log = (...args) => logs.push(args.map(String).join(' ')); |
| 228 | |
| 229 | try { |
| 230 | const menuMessage = { message_id: 703, chat: { id: 123 } }; |
| 231 | await sendUpdate({ |
| 232 | callback_query: { |
| 233 | id: 'audit-issue', |
| 234 | from: { id: 123 }, |
| 235 | data: 'rerun:1001:55', |
| 236 | message: menuMessage |
| 237 | } |
| 238 | }); |
| 239 | |
| 240 | const approvalEdit = mock.telegramCalls |
| 241 | .filter(call => call.method === 'editMessageText') |
| 242 | .at(-1); |
| 243 | const approveButton = approvalEdit.body.reply_markup.inline_keyboard |
| 244 | .flat() |
| 245 | .find(button => String(button.callback_data || '').startsWith('approve:')); |
| 246 | assert.ok(approveButton); |
| 247 | |
| 248 | await sendUpdate({ |
| 249 | callback_query: { |
| 250 | id: 'audit-verify', |
| 251 | from: { id: 123 }, |
| 252 | data: approveButton.callback_data, |
| 253 | message: menuMessage |
| 254 | } |
| 255 | }); |
| 256 | |
| 257 | await sendUpdate({ |
| 258 | callback_query: { |
| 259 | id: 'audit-reject', |
| 260 | from: { id: 123 }, |
| 261 | data: approveButton.callback_data, |
| 262 | message: menuMessage |
| 263 | } |
| 264 | }); |
| 265 | |
| 266 | const auditText = logs.filter(line => line.startsWith('nexa_audit ')).join('\n'); |
| 267 | assert.match(auditText, /"event":"approval_issued"/); |
| 268 | assert.match(auditText, /"event":"approval_verified"/); |
| 269 | assert.match(auditText, /"event":"approval_rejected"/); |
| 270 | assert.doesNotMatch(auditText, new RegExp(approveButton.callback_data.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))); |
| 271 | } finally { |
| 272 | console.log = originalLog; |
| 273 | mock.restore(); |
| 274 | } |
| 275 | }); |