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 { NexaDevBot as LegacyNexaDevBot } from './bot-legacy.js'; |
| 2 | import { TotpVerifier } from './core/totp.js'; |
| 3 | |
| 4 | const escapeHtml = (value) => |
| 5 | String(value ?? '') |
| 6 | .replaceAll('&', '&') |
| 7 | .replaceAll('<', '<') |
| 8 | .replaceAll('>', '>'); |
| 9 | |
| 10 | function destructiveSourceCallback(payload = {}) { |
| 11 | if (payload.actionId === 'cloudflare.worker.deploy_version') { |
| 12 | const versionId = String(payload.input?.versionId || ''); |
| 13 | if (!versionId) throw new Error('Destructive approval target is invalid'); |
| 14 | return `cfdeploy:${versionId}`; |
| 15 | } |
| 16 | if (payload.actionId === 'github.pull_request.merge') { |
| 17 | const repoId = Number(payload.input?.repoId); |
| 18 | const prNumber = Number(payload.input?.prNumber); |
| 19 | const headSha = String(payload.input?.headSha || ''); |
| 20 | if ( |
| 21 | !Number.isSafeInteger(repoId) || |
| 22 | !Number.isSafeInteger(prNumber) || |
| 23 | !/^[a-f0-9]{40}$/i.test(headSha) |
| 24 | ) { |
| 25 | throw new Error('Destructive approval target is invalid'); |
| 26 | } |
| 27 | return `mergepr:${repoId.toString(36)}:${prNumber.toString(36)}:${headSha}`; |
| 28 | } |
| 29 | throw new Error('Destructive approval action is not supported'); |
| 30 | } |
| 31 | |
| 32 | function secondFactorFailureMarkup(payload = {}) { |
| 33 | if (payload.actionId === 'cloudflare.worker.deploy_version') { |
| 34 | return { |
| 35 | inline_keyboard: [[{ text: '↩️ نسخهها', callback_data: 'cf:versions' }]] |
| 36 | }; |
| 37 | } |
| 38 | return { |
| 39 | inline_keyboard: [[{ text: '↩️ پروژهها', callback_data: 'menu:repos' }]] |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | export class NexaDevBot extends LegacyNexaDevBot { |
| 44 | constructor(env = {}) { |
| 45 | super(env); |
| 46 | this.destructiveTotp = new TotpVerifier({ |
| 47 | secret: env.DESTRUCTIVE_TOTP_SECRET || '' |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | async handleCallbackQuery(query) { |
| 52 | const data = query?.data || 'menu:main'; |
| 53 | const ownerId = query?.from?.id; |
| 54 | const chatId = query?.message?.chat?.id; |
| 55 | const messageId = query?.message?.message_id; |
| 56 | |
| 57 | if ( |
| 58 | data.startsWith('approve:') && |
| 59 | ownerId !== undefined && |
| 60 | ownerId !== null |
| 61 | ) { |
| 62 | const token = data.slice(8); |
| 63 | let preview = null; |
| 64 | try { |
| 65 | preview = await this.approvals.verify(token, { |
| 66 | ownerId: String(ownerId) |
| 67 | }); |
| 68 | } catch {} |
| 69 | |
| 70 | if (preview?.risk === 'DESTRUCTIVE') { |
| 71 | let callbackAnswered = true; |
| 72 | try { |
| 73 | await this.telegram.answerCallbackQuery(query.id); |
| 74 | } catch { |
| 75 | callbackAnswered = false; |
| 76 | } |
| 77 | |
| 78 | if (!this.destructiveTotp.isConfigured()) { |
| 79 | const error = 'Destructive second factor (TOTP) is not configured'; |
| 80 | this.audit.log({ |
| 81 | event: 'second_factor_unavailable', |
| 82 | pluginId: preview.pluginId, |
| 83 | actionId: preview.actionId, |
| 84 | risk: 'DESTRUCTIVE', |
| 85 | outcome: 'rejected' |
| 86 | }); |
| 87 | const res = await this.telegram.editMessageText( |
| 88 | chatId, |
| 89 | messageId, |
| 90 | `⚠️ <b>عملیات متوقف شد</b>\nعامل دوم TOTP برای عملیات DESTRUCTIVE پیکربندی نشده است.\nهیچ تغییری اجرا نشد.`, |
| 91 | secondFactorFailureMarkup(preview) |
| 92 | ); |
| 93 | return { |
| 94 | ok: false, |
| 95 | secondFactorRequired: true, |
| 96 | approvalRejected: true, |
| 97 | error, |
| 98 | callbackAnswered, |
| 99 | edited: true, |
| 100 | res |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | try { |
| 105 | await this.replayStore.setSecondFactorPending({ |
| 106 | ownerId: String(ownerId), |
| 107 | token, |
| 108 | expiresAt: preview.expiresAt, |
| 109 | attempts: 0 |
| 110 | }); |
| 111 | this.audit.log({ |
| 112 | event: 'second_factor_required', |
| 113 | pluginId: preview.pluginId, |
| 114 | actionId: preview.actionId, |
| 115 | risk: 'DESTRUCTIVE', |
| 116 | outcome: 'pending' |
| 117 | }); |
| 118 | const res = await this.telegram.editMessageText( |
| 119 | chatId, |
| 120 | messageId, |
| 121 | '🔐 <b>عامل دوم لازم است</b>\nApproval تلگرام معتبر است، اما عملیات هنوز اجرا نشده.\n\nکد ۶ رقمی برنامه Authenticator را به شکل زیر ارسال کنید:\n<code>/totp 123456</code>\n\nکد یا Secret در Audit ذخیره نمیشود.', |
| 122 | secondFactorFailureMarkup(preview) |
| 123 | ); |
| 124 | return { |
| 125 | ok: true, |
| 126 | secondFactorRequired: true, |
| 127 | actionId: preview.actionId, |
| 128 | risk: preview.risk, |
| 129 | callbackAnswered, |
| 130 | edited: true, |
| 131 | res |
| 132 | }; |
| 133 | } catch (error) { |
| 134 | const res = await this.telegram.editMessageText( |
| 135 | chatId, |
| 136 | messageId, |
| 137 | `⚠️ <b>عامل دوم آماده نشد</b>\nهیچ تغییر DESTRUCTIVE اجرا نشد.\n\n${escapeHtml(error?.message || 'Second factor unavailable')}`, |
| 138 | secondFactorFailureMarkup(preview) |
| 139 | ); |
| 140 | return { |
| 141 | ok: false, |
| 142 | secondFactorRequired: true, |
| 143 | approvalRejected: true, |
| 144 | error: error?.message, |
| 145 | callbackAnswered, |
| 146 | edited: true, |
| 147 | res |
| 148 | }; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (data.startsWith('cfdeploy:')) { |
| 154 | let callbackAnswered = true; |
| 155 | try { |
| 156 | await this.telegram.answerCallbackQuery(query.id); |
| 157 | } catch { |
| 158 | callbackAnswered = false; |
| 159 | } |
| 160 | try { |
| 161 | const routed = await this.router.dispatchCallback(data, { |
| 162 | isOwner: true, |
| 163 | ownerId: String(ownerId ?? '') |
| 164 | }); |
| 165 | if ( |
| 166 | !routed?.requiresApproval || |
| 167 | routed.actionId !== 'cloudflare.worker.deploy_version' |
| 168 | ) |
| 169 | throw new Error( |
| 170 | routed?.reason || 'Cloudflare approval route unavailable' |
| 171 | ); |
| 172 | if (ownerId === undefined || ownerId === null) |
| 173 | throw new Error('Approval owner is required'); |
| 174 | const token = await this.approvals.issue({ |
| 175 | pluginId: routed.pluginId, |
| 176 | actionId: routed.actionId, |
| 177 | risk: routed.risk, |
| 178 | ownerId: String(ownerId), |
| 179 | input: routed.approval.input |
| 180 | }); |
| 181 | const callback = `approve:${token}`; |
| 182 | if (callback.length > 64) |
| 183 | throw new Error('Approval callback exceeds Telegram limit'); |
| 184 | const text = `⚠️ <b>تأیید نهایی Cloudflare لازم است</b>\nVersion: <code>${routed.approval.input.versionId}</code>\n\nTraffic این Worker روی نسخه انتخابشده قرار میگیرد. پس از این تأیید، عامل دوم TOTP نیز لازم است.`; |
| 185 | const markup = { |
| 186 | inline_keyboard: [ |
| 187 | [ |
| 188 | { |
| 189 | text: '✅ ادامه تأیید Deploy / Rollback', |
| 190 | callback_data: callback |
| 191 | } |
| 192 | ], |
| 193 | [{ text: '↩️ انصراف', callback_data: 'cf:versions' }] |
| 194 | ] |
| 195 | }; |
| 196 | const res = await this.telegram.editMessageText( |
| 197 | chatId, |
| 198 | messageId, |
| 199 | text, |
| 200 | markup |
| 201 | ); |
| 202 | return { |
| 203 | ok: true, |
| 204 | approvalRequired: true, |
| 205 | actionId: routed.actionId, |
| 206 | risk: routed.risk, |
| 207 | callbackAnswered, |
| 208 | edited: true, |
| 209 | res |
| 210 | }; |
| 211 | } catch (error) { |
| 212 | const text = `⚠️ <b>Cloudflare Deploy آماده نشد</b>\n${escapeHtml(error?.message || 'Unavailable')}`; |
| 213 | const res = await this.telegram.editMessageText( |
| 214 | chatId, |
| 215 | messageId, |
| 216 | text, |
| 217 | { |
| 218 | inline_keyboard: [ |
| 219 | [{ text: '↩️ نسخهها', callback_data: 'cf:versions' }] |
| 220 | ] |
| 221 | } |
| 222 | ); |
| 223 | return { |
| 224 | ok: false, |
| 225 | error: error?.message, |
| 226 | callbackAnswered, |
| 227 | edited: true, |
| 228 | res |
| 229 | }; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (data.startsWith('mergepr:')) { |
| 234 | let callbackAnswered = true; |
| 235 | try { |
| 236 | await this.telegram.answerCallbackQuery(query.id); |
| 237 | } catch { |
| 238 | callbackAnswered = false; |
| 239 | } |
| 240 | try { |
| 241 | const routed = await this.router.dispatchCallback(data, { |
| 242 | isOwner: true, |
| 243 | ownerId: String(ownerId ?? '') |
| 244 | }); |
| 245 | if ( |
| 246 | !routed?.requiresApproval || |
| 247 | routed.actionId !== 'github.pull_request.merge' |
| 248 | ) |
| 249 | throw new Error(routed?.reason || 'Merge approval route unavailable'); |
| 250 | if (ownerId === undefined || ownerId === null) |
| 251 | throw new Error('Approval owner is required'); |
| 252 | const token = await this.approvals.issue({ |
| 253 | pluginId: routed.pluginId, |
| 254 | actionId: routed.actionId, |
| 255 | risk: routed.risk, |
| 256 | ownerId: String(ownerId), |
| 257 | input: routed.approval.input |
| 258 | }); |
| 259 | const callback = `approve:${token}`; |
| 260 | if (callback.length > 64) |
| 261 | throw new Error('Approval callback exceeds Telegram limit'); |
| 262 | const text = `⚠️ <b>تأیید نهایی Merge لازم است</b>\nPR #${routed.approval.input.prNumber}\nHead: <code>${routed.approval.input.headSha.slice(0, 12)}</code>\n\nMerge فقط برای همین Head و فقط یکبار قابل اجراست. پس از این تأیید، عامل دوم TOTP نیز لازم است.`; |
| 263 | const markup = { |
| 264 | inline_keyboard: [ |
| 265 | [{ text: '✅ ادامه تأیید Merge', callback_data: callback }], |
| 266 | [ |
| 267 | { |
| 268 | text: '↩️ انصراف', |
| 269 | callback_data: routed.approval.cancelCallback |
| 270 | } |
| 271 | ] |
| 272 | ] |
| 273 | }; |
| 274 | const res = await this.telegram.editMessageText( |
| 275 | chatId, |
| 276 | messageId, |
| 277 | text, |
| 278 | markup |
| 279 | ); |
| 280 | return { |
| 281 | ok: true, |
| 282 | approvalRequired: true, |
| 283 | actionId: routed.actionId, |
| 284 | risk: routed.risk, |
| 285 | callbackAnswered, |
| 286 | edited: true, |
| 287 | res |
| 288 | }; |
| 289 | } catch (error) { |
| 290 | const text = `⚠️ <b>Merge آماده نشد</b>\n${escapeHtml(error?.message || 'Merge unavailable')}`; |
| 291 | const res = await this.telegram.editMessageText( |
| 292 | chatId, |
| 293 | messageId, |
| 294 | text, |
| 295 | { |
| 296 | inline_keyboard: [ |
| 297 | [{ text: '↩️ پروژهها', callback_data: 'menu:repos' }] |
| 298 | ] |
| 299 | } |
| 300 | ); |
| 301 | return { |
| 302 | ok: false, |
| 303 | error: error?.message, |
| 304 | callbackAnswered, |
| 305 | edited: true, |
| 306 | res |
| 307 | }; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return super.handleCallbackQuery(query); |
| 312 | } |
| 313 | |
| 314 | async handleMessage(message) { |
| 315 | const rawText = |
| 316 | typeof message?.text === 'string' ? message.text.trim() : ''; |
| 317 | if (!rawText.startsWith('/totp')) return super.handleMessage(message); |
| 318 | |
| 319 | const chatId = message?.chat?.id; |
| 320 | const ownerId = message?.from?.id; |
| 321 | if (!chatId || ownerId === undefined || ownerId === null) { |
| 322 | return { |
| 323 | ok: false, |
| 324 | secondFactorRejected: true, |
| 325 | error: 'Second-factor owner context is missing' |
| 326 | }; |
| 327 | } |
| 328 | |
| 329 | if (!this.destructiveTotp.isConfigured()) { |
| 330 | const error = 'Destructive second factor (TOTP) is not configured'; |
| 331 | const res = await this.telegram.sendMessage( |
| 332 | chatId, |
| 333 | '⚠️ <b>عامل دوم پیکربندی نشده است</b>\nهیچ عملیات DESTRUCTIVE اجرا نشد.' |
| 334 | ); |
| 335 | return { ok: false, secondFactorRejected: true, error, res }; |
| 336 | } |
| 337 | |
| 338 | const match = rawText.match(/^\/totp\s+(\d{6})$/); |
| 339 | if (!match) { |
| 340 | const res = await this.telegram.sendMessage( |
| 341 | chatId, |
| 342 | '⚠️ فرمت کد درست نیست. فقط به شکل <code>/totp 123456</code> ارسال کنید.' |
| 343 | ); |
| 344 | return { |
| 345 | ok: false, |
| 346 | secondFactorRejected: true, |
| 347 | error: 'Invalid TOTP format', |
| 348 | res |
| 349 | }; |
| 350 | } |
| 351 | |
| 352 | let pending; |
| 353 | try { |
| 354 | pending = await this.replayStore.getSecondFactorPending({ |
| 355 | ownerId: String(ownerId) |
| 356 | }); |
| 357 | } catch (error) { |
| 358 | const res = await this.telegram.sendMessage( |
| 359 | chatId, |
| 360 | '⚠️ وضعیت عامل دوم در دسترس نیست؛ عملیات متوقف شد.' |
| 361 | ); |
| 362 | return { |
| 363 | ok: false, |
| 364 | secondFactorRejected: true, |
| 365 | error: error?.message || 'Second-factor state unavailable', |
| 366 | res |
| 367 | }; |
| 368 | } |
| 369 | |
| 370 | if (!pending) { |
| 371 | const res = await this.telegram.sendMessage( |
| 372 | chatId, |
| 373 | '⚠️ درخواست DESTRUCTIVE منتظر عامل دوم پیدا نشد. از ابتدا یک Approval تازه ایجاد کنید.' |
| 374 | ); |
| 375 | return { |
| 376 | ok: false, |
| 377 | secondFactorRejected: true, |
| 378 | error: 'Second-factor challenge is unavailable', |
| 379 | res |
| 380 | }; |
| 381 | } |
| 382 | |
| 383 | const verifiedStep = await this.destructiveTotp.verifyStep(match[1]); |
| 384 | if (!verifiedStep) { |
| 385 | const attempts = Number(pending.attempts || 0) + 1; |
| 386 | if (attempts >= 5) { |
| 387 | await this.replayStore.clearSecondFactorPending({ |
| 388 | ownerId: String(ownerId) |
| 389 | }); |
| 390 | } else { |
| 391 | await this.replayStore.setSecondFactorPending({ |
| 392 | ownerId: String(ownerId), |
| 393 | token: pending.token, |
| 394 | expiresAt: pending.expiresAt, |
| 395 | attempts |
| 396 | }); |
| 397 | } |
| 398 | this.audit.log({ |
| 399 | event: 'second_factor_rejected', |
| 400 | risk: 'DESTRUCTIVE', |
| 401 | outcome: 'rejected', |
| 402 | reason: attempts >= 5 ? 'attempt_limit' : 'invalid_code' |
| 403 | }); |
| 404 | const res = await this.telegram.sendMessage( |
| 405 | chatId, |
| 406 | attempts >= 5 |
| 407 | ? '⚠️ <b>عامل دوم رد شد</b>\nتعداد تلاش مجاز تمام شد. برای ادامه Approval تازه بسازید.' |
| 408 | : '⚠️ <b>کد TOTP معتبر نیست</b>\nهیچ تغییری اجرا نشد.' |
| 409 | ); |
| 410 | return { |
| 411 | ok: false, |
| 412 | secondFactorRejected: true, |
| 413 | error: 'Invalid TOTP code', |
| 414 | res |
| 415 | }; |
| 416 | } |
| 417 | |
| 418 | let payload; |
| 419 | try { |
| 420 | const preview = await this.approvals.verify(pending.token, { |
| 421 | ownerId: String(ownerId), |
| 422 | risk: 'DESTRUCTIVE' |
| 423 | }); |
| 424 | // Claim the owner/step independently of the action Approval. Store no OTP. |
| 425 | const claimedStep = await this.replayStore.consume({ |
| 426 | ownerId: String(ownerId), |
| 427 | nonce: `second-factor-step:${verifiedStep.counter}`, |
| 428 | expiresAt: verifiedStep.expiresAt |
| 429 | }); |
| 430 | if (!claimedStep) throw new Error('TOTP step already used'); |
| 431 | payload = await this.approvals.consume(pending.token, { |
| 432 | ownerId: String(ownerId), |
| 433 | pluginId: preview.pluginId, |
| 434 | actionId: preview.actionId, |
| 435 | risk: 'DESTRUCTIVE' |
| 436 | }); |
| 437 | await this.replayStore.clearSecondFactorPending({ |
| 438 | ownerId: String(ownerId) |
| 439 | }); |
| 440 | this.audit.log({ |
| 441 | event: 'second_factor_verified', |
| 442 | pluginId: payload.pluginId, |
| 443 | actionId: payload.actionId, |
| 444 | risk: 'DESTRUCTIVE', |
| 445 | outcome: 'verified' |
| 446 | }); |
| 447 | |
| 448 | const routed = await this.router.dispatchCallback( |
| 449 | destructiveSourceCallback(payload), |
| 450 | { |
| 451 | isOwner: true, |
| 452 | ownerId: String(ownerId), |
| 453 | approved: true |
| 454 | } |
| 455 | ); |
| 456 | if (!routed || routed.requiresApproval || routed.denied) |
| 457 | throw new Error('Approved destructive action could not be dispatched'); |
| 458 | |
| 459 | const res = await this.telegram.sendMessage( |
| 460 | chatId, |
| 461 | routed.text, |
| 462 | routed.reply_markup |
| 463 | ); |
| 464 | return { |
| 465 | ok: true, |
| 466 | approved: true, |
| 467 | secondFactorVerified: true, |
| 468 | actionId: payload.actionId, |
| 469 | res |
| 470 | }; |
| 471 | } catch (error) { |
| 472 | try { |
| 473 | await this.replayStore.clearSecondFactorPending({ |
| 474 | ownerId: String(ownerId) |
| 475 | }); |
| 476 | } catch {} |
| 477 | const res = await this.telegram.sendMessage( |
| 478 | chatId, |
| 479 | `⚠️ <b>عملیات DESTRUCTIVE اجرا نشد</b>\nهیچ Success ساختگی ثبت نمیشود.\n\n${escapeHtml(error?.message || 'Approval rejected')}`, |
| 480 | secondFactorFailureMarkup(payload || {}) |
| 481 | ); |
| 482 | return { |
| 483 | ok: false, |
| 484 | secondFactorRejected: true, |
| 485 | error: error?.message || 'Destructive approval rejected', |
| 486 | res |
| 487 | }; |
| 488 | } |
| 489 | } |
| 490 | } |