Personal Telegram developer console for GitHub
fix(hardening): avoid cross-request global kill switch state
davoodmehraban89 committed
Oct 5, 2026 at 13:25 UTC
78638239d5391ae3f17e11b0c4c801aff7dbfbf1
1 file changed
+2
-4
src/index.js
+2
-4
@@ -1,11 +1,9 @@
1
-import { GuidedNexaDevBot } from './guide-bot.js';
1
+import { KillSwitchGuidedNexaDevBot } from './kill-switch-bot.js';
2
import { DurableWorkspaceStoreClient } from './core/workspace-store.js';
3
-import { DurableKillSwitchStoreClient } from './core/kill-switch-store.js';
4
-import { KillSwitch } from './core/kill-switch.js';
3
import { handleWorkspaceEditorRequest, workspaceEditorPageResponse } from './workspace-editor.js';
4
export { ApprovalReplayGuard } from './durable/approval-replay-guard.js';
5
export { WorkspaceStore } from './durable/workspace-store.js';
6
export { IncidentStore } from './durable/incident-store.js';
7
export { AutomationStore } from './durable/automation-store.js';
8
export { KillSwitchStore } from './durable/kill-switch-store.js';
11
-const jsonResponse=(p,s=200)=>new Response(JSON.stringify(p),{status:s,headers:{'Content-Type':'application/json; charset=utf-8'}});const publicHealthPayload=()=>({status:'online',bot:'Nexa Dev Bot',version:'1.1.1',mode:'Cloudflare Worker Webhook'});const senderIdFromUpdate=u=>u?.message?.from?.id??u?.callback_query?.from?.id??null;const isOwnerUpdate=(u,e)=>!!e.OWNER_TELEGRAM_ID&&senderIdFromUpdate(u)!==null&&String(senderIdFromUpdate(u))===String(e.OWNER_TELEGRAM_ID);function safeErrorMessage(error,env={}){let m=String(error?.message||'Unknown error');for(const s of[env.TELEGRAM_BOT_TOKEN,env.TELEGRAM_WEBHOOK_SECRET,env.GITHUB_TOKEN,env.CLOUDFLARE_API_TOKEN,env.APPROVAL_SIGNING_SECRET,env.DESTRUCTIVE_TOTP_SECRET]){const v=String(s||'');if(v)m=m.split(v).join('[REDACTED]');}return m;}export default{async fetch(request,env={}){globalThis.__NEXA_KILL_SWITCH__=new KillSwitch({store:new DurableKillSwitchStoreClient({namespace:env.KILL_SWITCH_STORE})});const url=new URL(request.url);if(request.method==='GET'&&url.pathname==='/workspace/editor')return workspaceEditorPageResponse();if(url.pathname==='/workspace/api/editor'&&(request.method==='GET'||request.method==='PUT'))return handleWorkspaceEditorRequest(request,{workspaces:new DurableWorkspaceStoreClient({namespace:env.WORKSPACE_STORE}),botToken:env.TELEGRAM_BOT_TOKEN||'',ownerId:env.OWNER_TELEGRAM_ID||''});if(request.method==='GET'){if(['/health','/','/status'].includes(url.pathname))return jsonResponse(publicHealthPayload());return new Response('Not Found',{status:404});}if(request.method==='POST'&&url.pathname==='/telegram/webhook'){if(!env.TELEGRAM_WEBHOOK_SECRET)return new Response('Webhook secret not configured',{status:503});if(request.headers.get('X-Telegram-Bot-Api-Secret-Token')!==env.TELEGRAM_WEBHOOK_SECRET)return new Response('Forbidden',{status:403});if(!env.OWNER_TELEGRAM_ID)return new Response('Owner not configured',{status:503});if(!env.TELEGRAM_BOT_TOKEN)return new Response('Telegram bot token not configured',{status:503});try{const u=await request.json();if(!isOwnerUpdate(u,env))return jsonResponse({ok:true,ignored:true});return jsonResponse(await new GuidedNexaDevBot(env).handleUpdate(u));}catch(e){console.error('telegram_webhook_error',safeErrorMessage(e,env));return jsonResponse({ok:false,error:'Telegram webhook processing failed'},400);}}return new Response(request.method==='POST'?'Not Found':'Method Not Allowed',{status:request.method==='POST'?404:405});}};
9
+const jsonResponse=(p,s=200)=>new Response(JSON.stringify(p),{status:s,headers:{'Content-Type':'application/json; charset=utf-8'}});const publicHealthPayload=()=>({status:'online',bot:'Nexa Dev Bot',version:'1.1.1',mode:'Cloudflare Worker Webhook'});const senderIdFromUpdate=u=>u?.message?.from?.id??u?.callback_query?.from?.id??null;const isOwnerUpdate=(u,e)=>!!e.OWNER_TELEGRAM_ID&&senderIdFromUpdate(u)!==null&&String(senderIdFromUpdate(u))===String(e.OWNER_TELEGRAM_ID);function safeErrorMessage(error,env={}){let m=String(error?.message||'Unknown error');for(const s of[env.TELEGRAM_BOT_TOKEN,env.TELEGRAM_WEBHOOK_SECRET,env.GITHUB_TOKEN,env.CLOUDFLARE_API_TOKEN,env.APPROVAL_SIGNING_SECRET,env.DESTRUCTIVE_TOTP_SECRET]){const v=String(s||'');if(v)m=m.split(v).join('[REDACTED]');}return m;}export default{async fetch(request,env={}){const url=new URL(request.url);if(request.method==='GET'&&url.pathname==='/workspace/editor')return workspaceEditorPageResponse();if(url.pathname==='/workspace/api/editor'&&(request.method==='GET'||request.method==='PUT'))return handleWorkspaceEditorRequest(request,{workspaces:new DurableWorkspaceStoreClient({namespace:env.WORKSPACE_STORE}),botToken:env.TELEGRAM_BOT_TOKEN||'',ownerId:env.OWNER_TELEGRAM_ID||''});if(request.method==='GET'){if(['/health','/','/status'].includes(url.pathname))return jsonResponse(publicHealthPayload());return new Response('Not Found',{status:404});}if(request.method==='POST'&&url.pathname==='/telegram/webhook'){if(!env.TELEGRAM_WEBHOOK_SECRET)return new Response('Webhook secret not configured',{status:503});if(request.headers.get('X-Telegram-Bot-Api-Secret-Token')!==env.TELEGRAM_WEBHOOK_SECRET)return new Response('Forbidden',{status:403});if(!env.OWNER_TELEGRAM_ID)return new Response('Owner not configured',{status:503});if(!env.TELEGRAM_BOT_TOKEN)return new Response('Telegram bot token not configured',{status:503});try{const u=await request.json();if(!isOwnerUpdate(u,env))return jsonResponse({ok:true,ignored:true});return jsonResponse(await new KillSwitchGuidedNexaDevBot(env).handleUpdate(u));}catch(e){console.error('telegram_webhook_error',safeErrorMessage(e,env));return jsonResponse({ok:false,error:'Telegram webhook processing failed'},400);}}return new Response(request.method==='POST'?'Not Found':'Method Not Allowed',{status:request.method==='POST'?404:405});}};