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
| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { NexaDevBot } from '../src/bot.js'; |
| 4 | import { makeReplayNamespace } from './helpers/replay-namespace.js'; |
| 5 | |
| 6 | const TOTP_SECRET = 'JBSWY3DPEHPK3PXP'; |
| 7 | |
| 8 | function decodeBase32(value) { |
| 9 | const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; |
| 10 | let bits = ''; |
| 11 | for (const ch of value.replace(/=+$/g, '').toUpperCase()) { |
| 12 | const index = alphabet.indexOf(ch); |
| 13 | if (index < 0) throw new Error('invalid base32'); |
| 14 | bits += index.toString(2).padStart(5, '0'); |
| 15 | } |
| 16 | const out = []; |
| 17 | for (let i = 0; i + 8 <= bits.length; i += 8) |
| 18 | out.push(Number.parseInt(bits.slice(i, i + 8), 2)); |
| 19 | return new Uint8Array(out); |
| 20 | } |
| 21 | |
| 22 | async function currentTotp(secret, now = Date.now()) { |
| 23 | const counter = Math.floor(now / 30_000); |
| 24 | const buffer = new ArrayBuffer(8); |
| 25 | const view = new DataView(buffer); |
| 26 | view.setUint32(0, Math.floor(counter / 2 ** 32)); |
| 27 | view.setUint32(4, counter >>> 0); |
| 28 | const key = await crypto.subtle.importKey( |
| 29 | 'raw', |
| 30 | decodeBase32(secret), |
| 31 | { name: 'HMAC', hash: 'SHA-1' }, |
| 32 | false, |
| 33 | ['sign'] |
| 34 | ); |
| 35 | const digest = new Uint8Array(await crypto.subtle.sign('HMAC', key, buffer)); |
| 36 | const offset = digest[digest.length - 1] & 0x0f; |
| 37 | const binary = |
| 38 | ((digest[offset] & 0x7f) << 24) | |
| 39 | (digest[offset + 1] << 16) | |
| 40 | (digest[offset + 2] << 8) | |
| 41 | digest[offset + 3]; |
| 42 | return String(binary % 1_000_000).padStart(6, '0'); |
| 43 | } |
| 44 | |
| 45 | function makeBot(secret = TOTP_SECRET) { |
| 46 | const bot = new NexaDevBot({ |
| 47 | DESTRUCTIVE_TOTP_SECRET: secret, |
| 48 | APPROVAL_REPLAY_GUARD: makeReplayNamespace() |
| 49 | }); |
| 50 | const payload = { |
| 51 | v: 1, |
| 52 | pluginId: 'github', |
| 53 | actionId: 'github.pull_request.merge', |
| 54 | risk: 'DESTRUCTIVE', |
| 55 | ownerId: '123', |
| 56 | input: { repoId: 7, prNumber: 39, headSha: 'a'.repeat(40) }, |
| 57 | issuedAt: Date.now(), |
| 58 | expiresAt: Date.now() + 120_000, |
| 59 | nonce: 'nonce123' |
| 60 | }; |
| 61 | const pending = new Map(); |
| 62 | let consumeCount = 0; |
| 63 | let executeCount = 0; |
| 64 | |
| 65 | bot.approvals.verify = async () => payload; |
| 66 | bot.approvals.consume = async () => { |
| 67 | consumeCount += 1; |
| 68 | return payload; |
| 69 | }; |
| 70 | bot.replayStore.setSecondFactorPending = async ({ |
| 71 | ownerId, |
| 72 | token, |
| 73 | expiresAt |
| 74 | }) => { |
| 75 | pending.set(String(ownerId), { token, expiresAt }); |
| 76 | return true; |
| 77 | }; |
| 78 | bot.replayStore.getSecondFactorPending = async ({ ownerId }) => |
| 79 | pending.get(String(ownerId)) || null; |
| 80 | bot.replayStore.clearSecondFactorPending = async ({ ownerId }) => { |
| 81 | pending.delete(String(ownerId)); |
| 82 | return true; |
| 83 | }; |
| 84 | bot.router.dispatchCallback = async (_data, context) => { |
| 85 | if (context?.approved) executeCount += 1; |
| 86 | return { text: 'executed', reply_markup: { inline_keyboard: [] } }; |
| 87 | }; |
| 88 | bot.telegram.answerCallbackQuery = async () => ({ ok: true }); |
| 89 | bot.telegram.editMessageText = async ( |
| 90 | _chatId, |
| 91 | _messageId, |
| 92 | text, |
| 93 | replyMarkup |
| 94 | ) => ({ ok: true, result: { text, reply_markup: replyMarkup } }); |
| 95 | bot.telegram.sendMessage = async (_chatId, text, replyMarkup) => ({ |
| 96 | ok: true, |
| 97 | result: { text, reply_markup: replyMarkup } |
| 98 | }); |
| 99 | |
| 100 | return { |
| 101 | bot, |
| 102 | get consumeCount() { |
| 103 | return consumeCount; |
| 104 | }, |
| 105 | get executeCount() { |
| 106 | return executeCount; |
| 107 | } |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | test('DESTRUCTIVE approval fails closed when second-factor secret is not configured', async () => { |
| 112 | const harness = makeBot(''); |
| 113 | const result = await harness.bot.handleUpdate({ |
| 114 | callback_query: { |
| 115 | id: 'approve-missing-factor', |
| 116 | from: { id: 123 }, |
| 117 | data: 'approve:signed-token', |
| 118 | message: { message_id: 900, chat: { id: 123 } } |
| 119 | } |
| 120 | }); |
| 121 | |
| 122 | assert.equal(result.ok, false); |
| 123 | assert.equal( |
| 124 | harness.consumeCount, |
| 125 | 0, |
| 126 | 'approval must not be consumed without second factor' |
| 127 | ); |
| 128 | assert.equal( |
| 129 | harness.executeCount, |
| 130 | 0, |
| 131 | 'DESTRUCTIVE provider action must not execute without second factor' |
| 132 | ); |
| 133 | assert.match(String(result.error || ''), /second factor|totp|configured/i); |
| 134 | }); |
| 135 | |
| 136 | test('DESTRUCTIVE approval requires valid TOTP before consuming approval or executing provider mutation', async () => { |
| 137 | const harness = makeBot(); |
| 138 | |
| 139 | const first = await harness.bot.handleUpdate({ |
| 140 | callback_query: { |
| 141 | id: 'approve-first-factor', |
| 142 | from: { id: 123 }, |
| 143 | data: 'approve:signed-token', |
| 144 | message: { message_id: 901, chat: { id: 123 } } |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | assert.equal(first.ok, true); |
| 149 | assert.equal(first.secondFactorRequired, true); |
| 150 | assert.equal(harness.consumeCount, 0); |
| 151 | assert.equal(harness.executeCount, 0); |
| 152 | |
| 153 | const wrong = await harness.bot.handleUpdate({ |
| 154 | message: { |
| 155 | message_id: 902, |
| 156 | chat: { id: 123 }, |
| 157 | from: { id: 123 }, |
| 158 | text: '/totp 000000' |
| 159 | } |
| 160 | }); |
| 161 | assert.equal(wrong.ok, false); |
| 162 | assert.equal(wrong.secondFactorRejected, true); |
| 163 | assert.equal(harness.consumeCount, 0); |
| 164 | assert.equal(harness.executeCount, 0); |
| 165 | |
| 166 | const code = await currentTotp(TOTP_SECRET); |
| 167 | const accepted = await harness.bot.handleUpdate({ |
| 168 | message: { |
| 169 | message_id: 903, |
| 170 | chat: { id: 123 }, |
| 171 | from: { id: 123 }, |
| 172 | text: `/totp ${code}` |
| 173 | } |
| 174 | }); |
| 175 | assert.equal(accepted.ok, true); |
| 176 | assert.equal(accepted.secondFactorVerified, true); |
| 177 | assert.equal(harness.consumeCount, 1); |
| 178 | assert.equal(harness.executeCount, 1); |
| 179 | }); |
| 180 | |
| 181 | test('TOTP code and signed approval token are never written to audit output', async () => { |
| 182 | const harness = makeBot(); |
| 183 | const originalLog = console.log; |
| 184 | const logs = []; |
| 185 | console.log = (...args) => logs.push(args.map(String).join(' ')); |
| 186 | try { |
| 187 | await harness.bot.handleUpdate({ |
| 188 | callback_query: { |
| 189 | id: 'approve-audit-factor', |
| 190 | from: { id: 123 }, |
| 191 | data: 'approve:signed-token', |
| 192 | message: { message_id: 904, chat: { id: 123 } } |
| 193 | } |
| 194 | }); |
| 195 | const code = await currentTotp(TOTP_SECRET); |
| 196 | await harness.bot.handleUpdate({ |
| 197 | message: { |
| 198 | message_id: 905, |
| 199 | chat: { id: 123 }, |
| 200 | from: { id: 123 }, |
| 201 | text: `/totp ${code}` |
| 202 | } |
| 203 | }); |
| 204 | const output = logs.join('\n'); |
| 205 | assert.doesNotMatch(output, new RegExp(code)); |
| 206 | assert.doesNotMatch(output, /signed-token/); |
| 207 | } finally { |
| 208 | console.log = originalLog; |
| 209 | } |
| 210 | }); |