Personal Telegram developer console for GitHub
feat: require durable replay guard for write approvals
davoodmehraban89 committed
Oct 3, 2026 at 06:33 UTC
dec38d6b62a16ca15fe0a7599ac3a2e3166763ab
1 file changed
+12
-33
src/bot.js
+12
-33
@@ -4,6 +4,7 @@ import { MenuManager } from './menu.js';
4
import { PluginRegistry } from './core/plugin-registry.js';
5
import { ActionPolicy } from './core/action-policy.js';
6
import { ApprovalService } from './core/approval.js';
7
+import { DurableApprovalReplayStore } from './core/durable-replay-store.js';
8
import { AuditLogger } from './core/audit.js';
9
import { PluginRouter } from './core/router.js';
10
import { createGitHubPlugin } from './plugins/github/plugin.js';
@@ -48,8 +49,12 @@ export class NexaDevBot {
49
this.telegram = new TelegramClient(env.TELEGRAM_BOT_TOKEN || '');
50
this.github = new GitHubService(env.GITHUB_TOKEN || '', env.GITHUB_OWNER || 'davoodmehraban89');
51
this.menu = new MenuManager(this.github);
52
+ this.replayStore = new DurableApprovalReplayStore({
53
+ namespace: env.APPROVAL_REPLAY_GUARD
54
+ });
55
this.approvals = new ApprovalService({
52
- secret: env.APPROVAL_SIGNING_SECRET || env.TELEGRAM_WEBHOOK_SECRET || ''
56
+ secret: env.APPROVAL_SIGNING_SECRET || env.TELEGRAM_WEBHOOK_SECRET || '',
57
+ replayStore: this.replayStore
58
});
59
60
this.githubPlugin = createGitHubPlugin({
@@ -73,24 +78,10 @@ export class NexaDevBot {
78
});
79
}
80
76
- /**
77
- * Process incoming Telegram webhook update.
78
- * Owner authorization is enforced at the Worker boundary before this bot is
79
- * constructed, so routed actions use an owner-authorized internal context.
80
- */
81
async handleUpdate(update) {
82
if (!update) return { ok: false, error: 'Empty update' };
83
-
84
- // 1. Handle Inline Keyboard Callback Queries (In-Place Message Editing)
85
- if (update.callback_query) {
86
- return this.handleCallbackQuery(update.callback_query);
87
- }
88
-
89
- // 2. Handle Text Messages and Commands
90
- if (update.message) {
91
- return this.handleMessage(update.message);
92
- }
93
-
83
+ if (update.callback_query) return this.handleCallbackQuery(update.callback_query);
84
+ if (update.message) return this.handleMessage(update.message);
85
return { ok: true, ignored: true };
86
}
87
@@ -102,8 +93,6 @@ export class NexaDevBot {
93
const messageId = message?.message_id;
94
const ownerId = query.from?.id;
95
105
- // Callback acknowledgement is best-effort. A stale/expired callback must not
106
- // block rendering and editing the existing menu message.
96
let callbackAnswered = true;
97
try {
98
await this.telegram.answerCallbackQuery(callbackId);
@@ -117,9 +106,7 @@ export class NexaDevBot {
106
107
try {
108
if (data.startsWith('approve:')) {
120
- if (ownerId === undefined || ownerId === null) {
121
- throw new Error('Approval owner is required');
122
- }
109
+ if (ownerId === undefined || ownerId === null) throw new Error('Approval owner is required');
110
const token = data.slice('approve:'.length);
111
let payload;
112
try {
@@ -172,8 +159,6 @@ export class NexaDevBot {
159
};
160
}
161
175
- // GitHub callbacks pass through the plugin policy boundary. Non-GitHub
176
- // callbacks remain on the existing compatibility router in Foundation v1.
162
const routed = await this.router.dispatchCallback(data, { isOwner: true });
163
if (routed?.requiresApproval) {
164
const approval = routed.approval;
@@ -182,8 +167,9 @@ export class NexaDevBot {
167
if (!approval || !Number.isSafeInteger(Number(repoId)) || !Number.isSafeInteger(Number(runId))) {
168
throw new Error('Approval metadata is missing');
169
}
185
- if (ownerId === undefined || ownerId === null) {
186
- throw new Error('Approval owner is required');
170
+ if (ownerId === undefined || ownerId === null) throw new Error('Approval owner is required');
171
+ if (!this.approvals.isReplayProtected()) {
172
+ throw new Error('Durable approval replay protection is not configured');
173
}
174
175
const token = await this.approvals.issue({
@@ -234,8 +220,6 @@ export class NexaDevBot {
220
}
221
222
const view = routed || await this.menu.renderByCallback(data);
237
-
238
- // Edit the existing message in-place to prevent chat flooding
223
const res = await this.telegram.editMessageText(
224
chatId,
225
messageId,
@@ -245,7 +229,6 @@ export class NexaDevBot {
229
230
return { ok: true, edited: true, callbackAnswered, res };
231
} catch (err) {
248
- // Ignore 'message is not modified' error in Telegram
232
if (err.message && err.message.includes('message is not modified')) {
233
return { ok: true, unmodified: true, callbackAnswered };
234
}
@@ -259,14 +242,12 @@ export class NexaDevBot {
242
243
if (!chatId) return { ok: false, error: 'Missing chat_id' };
244
262
- // Command: /start or /menu
245
if (text === '/start' || text === '/menu' || text.startsWith('/start')) {
246
const view = this.menu.getMainMenuView();
247
const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup);
248
return { ok: true, command: 'start', res };
249
}
250
269
- // GitHub shortcut command routed through the plugin kernel.
251
if (text === '/repos') {
252
const routed = await this.router.dispatchCommand(text, { isOwner: true });
253
if (routed?.requiresApproval || routed?.denied || !routed) {
@@ -281,14 +262,12 @@ export class NexaDevBot {
262
return { ok: true, command: 'repos', res };
263
}
264
284
- // Command: /status
265
if (text === '/status') {
266
const view = this.menu.getServiceStatusView();
267
const res = await this.telegram.sendMessage(chatId, view.text, view.reply_markup);
268
return { ok: true, command: 'status', res };
269
}
270
291
- // Default response: Offer interactive menu
271
const defaultText = `💡 برای دسترسی به منوهای پروژهها و ابزارهای Nexa Dev، دکمه زیر را لمس کنید:`;
272
const view = {
273
text: defaultText,