Personal Telegram developer console for GitHub
security: restore fail-closed Telegram webhook boundary
davoodmehraban89 committed
Oct 2, 2026 at 22:09 UTC
4aaa9660db5531a39dd8eaf093daf2d841d3c43e
1 file changed
+49
-24
src/index.js
+49
-24
@@ -1,58 +1,83 @@
1
import { NexaDevBot } from './bot.js';
2
3
+const jsonResponse = (payload, status = 200) => new Response(JSON.stringify(payload), {
4
+ status,
5
+ headers: { 'Content-Type': 'application/json; charset=utf-8' }
6
+});
7
+
8
+function senderIdFromUpdate(update) {
9
+ return update?.message?.from?.id ?? update?.callback_query?.from?.id ?? null;
10
+}
11
+
12
+function isOwnerUpdate(update, env) {
13
+ if (!env.OWNER_TELEGRAM_ID) return false;
14
+ const senderId = senderIdFromUpdate(update);
15
+ return senderId !== null && String(senderId) === String(env.OWNER_TELEGRAM_ID);
16
+}
17
+
18
/**
4
- * Cloudflare Worker Export & Universal HTTP Handler
19
+ * Cloudflare Worker entrypoint.
20
+ * Security invariants:
21
+ * - Telegram webhook requests must carry the configured secret header.
22
+ * - Telegram updates are owner-only and unauthorized senders are ignored silently.
23
+ * - Missing security configuration fails closed.
24
*/
25
export default {
26
async fetch(request, env = {}, ctx = {}) {
27
const url = new URL(request.url);
28
10
- // Health check & root route
29
if (request.method === 'GET') {
30
if (url.pathname === '/health' || url.pathname === '/') {
13
- return new Response(JSON.stringify({
31
+ return jsonResponse({
32
status: 'online',
33
bot: 'Nexa Dev Bot',
16
- version: '1.1.0',
17
- mode: 'Cloudflare Worker Webhook',
18
- uptime: '100% SLA'
19
- }), {
20
- status: 200,
21
- headers: { 'Content-Type': 'application/json' }
34
+ version: '1.1.1',
35
+ mode: 'Cloudflare Worker Webhook'
36
});
37
}
38
39
if (url.pathname === '/status') {
40
const bot = new NexaDevBot(env);
41
const statusView = bot.menu.getServiceStatusView();
28
- return new Response(JSON.stringify(statusView), {
29
- status: 200,
30
- headers: { 'Content-Type': 'application/json' }
31
- });
42
+ return jsonResponse(statusView);
43
}
44
45
return new Response('Not Found', { status: 404 });
46
}
47
37
- // Webhook POST from Telegram
38
- if (request.method === 'POST') {
48
+ if (request.method === 'POST' && url.pathname === '/telegram/webhook') {
49
+ if (!env.TELEGRAM_WEBHOOK_SECRET) {
50
+ return new Response('Webhook secret not configured', { status: 503 });
51
+ }
52
+
53
+ const suppliedSecret = request.headers.get('X-Telegram-Bot-Api-Secret-Token');
54
+ if (suppliedSecret !== env.TELEGRAM_WEBHOOK_SECRET) {
55
+ return new Response('Forbidden', { status: 403 });
56
+ }
57
+
58
+ if (!env.OWNER_TELEGRAM_ID) {
59
+ return new Response('Owner not configured', { status: 503 });
60
+ }
61
+
62
try {
63
const update = await request.json();
64
+
65
+ if (!isOwnerUpdate(update, env)) {
66
+ return jsonResponse({ ok: true, ignored: true });
67
+ }
68
+
69
const bot = new NexaDevBot(env);
70
const result = await bot.handleUpdate(update);
43
-
44
- return new Response(JSON.stringify(result), {
45
- status: 200,
46
- headers: { 'Content-Type': 'application/json' }
47
- });
71
+ return jsonResponse(result);
72
} catch (err) {
49
- return new Response(JSON.stringify({ ok: false, error: err.message }), {
50
- status: 400,
51
- headers: { 'Content-Type': 'application/json' }
52
- });
73
+ return jsonResponse({ ok: false, error: err.message }, 400);
74
}
75
}
76
77
+ if (request.method === 'POST') {
78
+ return new Response('Not Found', { status: 404 });
79
+ }
80
+
81
return new Response('Method Not Allowed', { status: 405 });
82
}
83
};