Personal Telegram developer console for GitHub

feat(cp4): enforce destructive Cloudflare Safe Approval

davoodmehraban89 committed Oct 5, 2026 at 06:07 UTC efa039178b49ecbb0d74e7268681018a5e7c3d02
1 file changed +34 -1
src/bot.js
+34 -1
@@ -9,7 +9,22 @@ export class NexaDevBot extends LegacyNexaDevBot {
9 if (data.startsWith('approve:') && ownerId !== undefined && ownerId !== null) {
10 const token = data.slice(8);
11 let preview = null;
12 - try { preview = await this.approvals.verify(token, { ownerId: String(ownerId), pluginId: 'github' }); } catch {}
12 + try { preview = await this.approvals.verify(token, { ownerId: String(ownerId) }); } catch {}
13 + if (preview?.actionId === 'cloudflare.worker.deploy_version') {
14 + let callbackAnswered = true; try { await this.telegram.answerCallbackQuery(query.id); } catch { callbackAnswered = false; }
15 + try {
16 + const payload = await this.approvals.consume(token, { ownerId: String(ownerId), pluginId: 'cloudflare', actionId: 'cloudflare.worker.deploy_version', risk: 'DESTRUCTIVE' });
17 + const source = `cfdeploy:${payload.input.versionId}`;
18 + const routed = await this.router.dispatchCallback(source, { isOwner: true, ownerId: String(ownerId), approved: true });
19 + if (!routed || routed.requiresApproval || routed.denied) throw new Error('Approved Cloudflare action could not be dispatched');
20 + const res = await this.telegram.editMessageText(chatId, messageId, routed.text, routed.reply_markup);
21 + return { ok: true, approved: true, actionId: payload.actionId, callbackAnswered, edited: true, res };
22 + } catch (error) {
23 + const text = `⚠️ <b>Cloudflare Deploy اجرا نشد</b>\nهیچ تغییر تأییدنشده‌ای انجام نشد.\n\n${String(error?.message || 'Approval rejected').replaceAll('&','&amp;').replaceAll('<','&lt;').replaceAll('>','&gt;')}`;
24 + const res = await this.telegram.editMessageText(chatId, messageId, text, { inline_keyboard: [[{ text: '↩️ نسخه‌ها', callback_data: 'cf:versions' }]] });
25 + return { ok: false, approvalRejected: true, error: error?.message, callbackAnswered, edited: true, res };
26 + }
27 + }
28 if (preview?.actionId === 'github.pull_request.merge') {
29 let callbackAnswered = true; try { await this.telegram.answerCallbackQuery(query.id); } catch { callbackAnswered = false; }
30 try {
@@ -26,6 +41,24 @@ export class NexaDevBot extends LegacyNexaDevBot {
41 }
42 }
43 }
44 + if (data.startsWith('cfdeploy:')) {
45 + let callbackAnswered = true; try { await this.telegram.answerCallbackQuery(query.id); } catch { callbackAnswered = false; }
46 + try {
47 + const routed = await this.router.dispatchCallback(data, { isOwner: true, ownerId: String(ownerId ?? '') });
48 + if (!routed?.requiresApproval || routed.actionId !== 'cloudflare.worker.deploy_version') throw new Error(routed?.reason || 'Cloudflare approval route unavailable');
49 + if (ownerId === undefined || ownerId === null) throw new Error('Approval owner is required');
50 + const token = await this.approvals.issue({ pluginId: routed.pluginId, actionId: routed.actionId, risk: routed.risk, ownerId: String(ownerId), input: routed.approval.input });
51 + const callback = `approve:${token}`; if (callback.length > 64) throw new Error('Approval callback exceeds Telegram limit');
52 + const text = `⚠️ <b>تأیید نهایی Cloudflare لازم است</b>\nVersion: <code>${routed.approval.input.versionId}</code>\n\nTraffic این Worker روی نسخه انتخاب‌شده قرار می‌گیرد.`;
53 + const markup = { inline_keyboard: [[{ text: '✅ تأیید Deploy / Rollback', callback_data: callback }],[{ text: '↩️ انصراف', callback_data: 'cf:versions' }]] };
54 + const res = await this.telegram.editMessageText(chatId, messageId, text, markup);
55 + return { ok: true, approvalRequired: true, actionId: routed.actionId, risk: routed.risk, callbackAnswered, edited: true, res };
56 + } catch (error) {
57 + const text = `⚠️ <b>Cloudflare Deploy آماده نشد</b>\n${String(error?.message || 'Unavailable').replaceAll('&','&amp;').replaceAll('<','&lt;').replaceAll('>','&gt;')}`;
58 + const res = await this.telegram.editMessageText(chatId, messageId, text, { inline_keyboard: [[{ text: '↩️ نسخه‌ها', callback_data: 'cf:versions' }]] });
59 + return { ok: false, error: error?.message, callbackAnswered, edited: true, res };
60 + }
61 + }
62 if (data.startsWith('mergepr:')) {
63 let callbackAnswered = true; try { await this.telegram.answerCallbackQuery(query.id); } catch { callbackAnswered = false; }
64 try {