@goodandready/dsh-messenger-gateway 0.4.39 → 0.4.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -151,6 +151,9 @@ In addition to Telegram, outbound messages can be dispatched to Discord and Slac
151
151
  - Mini App artifacts are chat-scoped: a Telegram user sees only artifacts produced in their own chat; allowlisted admins see all
152
152
  - Mini App sign-in is hardened: `auth_date` required, 1h acceptance window, one-time `query_id` (replays rejected); `initData` in URL query is not accepted — the Mini App must send it via the `Authorization: tma` header
153
153
  - HITL approvals are fail-closed by default: in `mutations` sensitivity every tool that is not recognizably read-only (including network, send and MCP tools) asks for confirmation; a chat without a target refuses unconfirmed actions
154
+ - Delivered updates are deduplicated across polling and webhook transports and the poll offset survives restarts — commands never execute twice
155
+ - Artifact SVG entering the dashboard store is sanitized (script blocks, event handlers and `javascript:` URLs stripped)
156
+ - Pending interactive asks are bounded (200 concurrent) and the ask timeout is clamped to 10 minutes
154
157
  - `telegram.apiRoot` with plain `http` is only accepted for loopback hosts (a local Bot API server); the webhook URL is logged without its secret path
155
158
  - `webhooks.enabled: false` removes the messenger write routes entirely; GET `/update` and `/messenger` require trusted callers
156
159
  - `webAppEnabled: false` takes the whole Mini App surface down (dashboard and every `/api/*` answer 404) and clears the menu button
File without changes
@@ -1,4 +1,8 @@
1
1
  import { readFile } from 'node:fs/promises'
2
+ import { existsSync, readFileSync } from 'node:fs'
3
+ import { join } from 'node:path'
4
+ import { homedir } from 'node:os'
5
+ import { writeJsonAtomicSync } from '../storage-atomic.js'
2
6
  import {
3
7
  cacheName,
4
8
  extOf, saveToCache, safeName, TELEGRAM_MAX_DOC_BYTES,
@@ -66,11 +70,14 @@ export class TelegramAdapter {
66
70
  this.pollErrorCount = 0
67
71
  this.webhookUrl = String(opts.webhookUrl || '').trim()
68
72
  this.webhookSecret = String(opts.webhookSecret || '').trim()
69
- this.offset = 0
70
73
  this.stopped = false
71
74
  this.mediaGroups = new Map()
72
75
  this.mediaGroupDebounceMs = Number(opts.mediaGroupDebounceMs) || 650
73
76
  this.pollTimer = undefined
77
+ // Offset persistence (#208): the last update_id survives restarts so a
78
+ // fresh poll does not replay the tail of the previous session.
79
+ this.offsetPath = opts.offsetPath || join(process.env.DSH_HOME || join(homedir(), '.dsh'), 'storages', 'telegram-offset.json')
80
+ this.offset = this.loadOffset()
74
81
  this.botId = 0
75
82
  this.botUsername = ''
76
83
  this.apiRoot = normalizeApiRoot(opts.apiRoot)
@@ -93,6 +100,26 @@ export class TelegramAdapter {
93
100
  this.allowedUserIds = (ids || []).map(Number).filter((n) => Number.isFinite(n))
94
101
  }
95
102
 
103
+ loadOffset() {
104
+ try {
105
+ if (existsSync(this.offsetPath)) {
106
+ const raw = JSON.parse(readFileSync(this.offsetPath, 'utf8'))
107
+ return Number(raw.offset) || 0
108
+ }
109
+ } catch (e) {
110
+ this.logger?.warn?.(`telegram offset load: ${e.message}`)
111
+ }
112
+ return 0
113
+ }
114
+
115
+ persistOffset() {
116
+ try {
117
+ writeJsonAtomicSync(this.offsetPath, { offset: this.offset, updatedAt: Date.now() })
118
+ } catch (e) {
119
+ this.logger?.debug?.(`telegram offset persist: ${e.message}`)
120
+ }
121
+ }
122
+
96
123
  async call(method, params = {}, externalSignal) {
97
124
  const timeoutMs = (this.timeoutSeconds * 1000) + 15000
98
125
  // A long poll holds its request open for `timeoutSeconds`, so stopping the
@@ -295,15 +322,15 @@ export class TelegramAdapter {
295
322
  }, signal)
296
323
  this.pollingConflict = false
297
324
  this.pollErrorCount = 0
325
+ let maxSeen = this.offset
298
326
  for (const update of updates || []) {
299
- this.offset = Math.max(this.offset, update.update_id + 1)
300
- if (processedUpdateIds.has(update.update_id)) {
301
- this.logger?.debug?.(`telegram: skipping already handled update ${update.update_id}`)
302
- continue
303
- }
304
- rememberUpdate(update.update_id)
327
+ maxSeen = Math.max(maxSeen, update.update_id + 1)
305
328
  await this.dispatchUpdate(update)
306
329
  }
330
+ if (maxSeen !== this.offset) {
331
+ this.offset = maxSeen
332
+ this.persistOffset()
333
+ }
307
334
  } catch (e) {
308
335
  if (!this.stopped) {
309
336
  this.pollErrorCount = (this.pollErrorCount || 0) + 1
@@ -321,6 +348,17 @@ export class TelegramAdapter {
321
348
  }
322
349
 
323
350
  async dispatchUpdate(update) {
351
+ // Shared dedup for BOTH transports (#208): Telegram redelivers updates on
352
+ // timeout or 5xx, and the webhook path has no offset bookkeeping. Updates
353
+ // without an id (synthetic/internal dispatches) bypass the dedup.
354
+ const uid = update.update_id
355
+ if (uid !== undefined) {
356
+ if (processedUpdateIds.has(uid)) {
357
+ this.logger?.debug?.(`telegram: skipping already handled update ${uid}`)
358
+ return
359
+ }
360
+ rememberUpdate(uid)
361
+ }
324
362
  if (update.callback_query) {
325
363
  const fromId = update.callback_query.from?.id
326
364
  if (fromId && !this.allowed(fromId)) {
package/lib/ask.js CHANGED
@@ -154,9 +154,24 @@ export function rejectPendingAsk(pending, err) {
154
154
 
155
155
  export const REMOVE_KEYBOARD = { inline_keyboard: [] }
156
156
 
157
+ // Bounds for pending asks (#209): unbounded concurrent asks pin memory and
158
+ // callback slots; a runaway caller must fail fast instead of exhausting the
159
+ // process. The timeout is clamped well below the previous 1h maximum.
160
+ const MAX_PENDING_ASKS = 200
161
+ const MAX_ASK_TIMEOUT_MS = 600_000
162
+ const DEFAULT_ASK_TIMEOUT_MS = 300_000
163
+
164
+ /** Clamp the caller-supplied ask timeout to a sane maximum (#209). */
165
+ export function clampAskTimeout(ms) {
166
+ return Math.min(Number(ms) || DEFAULT_ASK_TIMEOUT_MS, MAX_ASK_TIMEOUT_MS)
167
+ }
168
+
157
169
  export async function executeMessengerAsk(gw, target, payload, timeoutMs = 300_000) {
158
170
  const adapter = gw.getAdapter(target.platform)
159
171
  if (!adapter) throw new Error(`adapter ${target.platform} unavailable`)
172
+ if (gw.pendingAsks.size >= MAX_PENDING_ASKS) {
173
+ throw new Error(`too many pending asks (${gw.pendingAsks.size} >= ${MAX_PENDING_ASKS}); refusing new ask`)
174
+ }
160
175
  const token = makeAskToken()
161
176
  const isMulti = payload.mode === 'multi' || (Array.isArray(payload.options) && payload.options.length > 0)
162
177
  let replyMarkup
@@ -193,7 +208,7 @@ export async function executeMessengerAsk(gw, target, payload, timeoutMs = 300_0
193
208
  gw.pendingAsks.delete(token)
194
209
  releaseCallbacks(gw.callbackIndex, callbackKeys)
195
210
  reject(new Error('messenger.ask timed out'))
196
- }, timeoutMs)
211
+ }, clampAskTimeout(timeoutMs))
197
212
  timer.unref?.()
198
213
  gw.pendingAsks.set(token, {
199
214
  resolve, reject, timer, target, callbackKeys, isMulti,
package/lib/documents.js CHANGED
@@ -70,17 +70,25 @@ export function extractPdfText(buffer) {
70
70
  }
71
71
  }
72
72
 
73
- // Also check plain text streams if no BT/ET blocks were matched
73
+ // Also check plain text streams if no BT/ET blocks were matched. Linear
74
+ // indexOf scan (#209): no lazy quantifiers running over the whole document,
75
+ // so a crafted PDF cannot drive catastrophic backtracking.
74
76
  if (!textBlocks.length) {
75
- const streamRegex = /stream[\r\n]+([\s\S]*?)[\r\n]+endstream/g
76
- let sMatch
77
- while ((sMatch = streamRegex.exec(content)) !== null) {
78
- const stream = sMatch[1]
77
+ let cursor = 0
78
+ while (cursor < content.length) {
79
+ const start = content.indexOf('stream', cursor)
80
+ if (start === -1) break
81
+ let bodyStart = start + 'stream'.length
82
+ while (bodyStart < content.length && (content[bodyStart] === '\r' || content[bodyStart] === '\n')) bodyStart++
83
+ const end = content.indexOf('endstream', bodyStart)
84
+ if (end === -1) break
85
+ const stream = content.slice(bodyStart, end)
79
86
  const strRegex = /\(([\w\s.,;:!?-]{4,})\)/g
80
87
  let strMatch
81
88
  while ((strMatch = strRegex.exec(stream)) !== null) {
82
89
  textBlocks.push(strMatch[1])
83
90
  }
91
+ cursor = end + 'endstream'.length
84
92
  }
85
93
  }
86
94
 
@@ -1,6 +1,6 @@
1
1
  import { escapeHtml } from './telegram-format.js'
2
2
  import { realpathSync } from 'node:fs'
3
- import { readdir, stat, readFile } from 'node:fs/promises'
3
+ import { readdir, stat, open } from 'node:fs/promises'
4
4
  import { resolve, normalize, relative, join, extname, isAbsolute, sep, dirname } from 'node:path'
5
5
 
6
6
  export function formatFileSize(bytes) {
@@ -210,7 +210,16 @@ export async function getFileForDownload(baseCwd, relativePath, maxBytes = 50 *
210
210
  }
211
211
  }
212
212
 
213
- const bytes = await readFile(target)
213
+ // Read through a file descriptor opened AFTER the boundary check: swapping
214
+ // the path for a symlink between check and read cannot redirect the read
215
+ // anymore — the fd pins the already-verified inode (#209).
216
+ const handle = await open(target, 'r')
217
+ let bytes
218
+ try {
219
+ bytes = await handle.readFile()
220
+ } finally {
221
+ await handle.close()
222
+ }
214
223
  const ext = extname(safeRel).toLowerCase()
215
224
 
216
225
  const mimeMap = {
@@ -19,10 +19,6 @@ import { rememberUserFact, listUserMemories, forgetUserMemory } from './memory-b
19
19
  import { formatSessionCostReport } from './cost-tracker.js'
20
20
 
21
21
 
22
- export function releaseChatTurn(chat) {
23
- chat.busy = Promise.resolve()
24
- }
25
-
26
22
  export async function handleGatewayCommand(gw, key, text, input) {
27
23
  const parts = text.split(/\s+/)
28
24
  const cmd = parts[0].toLowerCase().split('@')[0]
@@ -655,8 +651,10 @@ export async function handleGatewayCommand(gw, key, text, input) {
655
651
  const chat = gw.chats.get(key)
656
652
  if (chat?.turnActive || chat?.abort) {
657
653
  try { chat.abort?.abort() } catch { /* safe best-effort stop abort */ }
658
- releaseChatTurn(chat)
659
654
  chat.turnActive = false
655
+ // busy deliberately KEEPS the interrupted run's chain (#213): the next
656
+ // turn dispatches as chat.busy.then(...) and therefore starts only
657
+ // after the aborted run has fully drained — no parallel runTurn.
660
658
  return reply(t('msg.turn_stopped', {}, locale))
661
659
  }
662
660
  return reply('Nothing to stop.')
@@ -240,6 +240,10 @@ export async function runGatewayTurn(gw, chat, input, signal) {
240
240
  const { reply, typing, startStream, startProgress, react, inboundWasVoice, userId } = input
241
241
  const turnStartTime = Date.now()
242
242
  chat.turnActive = true
243
+ // Ownership token (#211): the dispatch site stores the AbortController on
244
+ // chat.turnOwner; the finally block below clears chat state only when this
245
+ // turn still owns it, so a stale turn never strips a newer turn's abort.
246
+ const turnOwner = chat.turnOwner
243
247
  const sessionId = chat.agent.session.id
244
248
  const tg = gw.tg()
245
249
  const streaming = tg.streaming === true && typeof startStream === 'function'
@@ -453,8 +457,12 @@ export async function runGatewayTurn(gw, chat, input, signal) {
453
457
  stopTyping()
454
458
  if (progress) try { await progress.remove() } catch (err) { gw.logger?.debug?.('progress.remove failed in finally:', err?.message || err) }
455
459
  if (typeof react === 'function') react('').catch?.((e) => { gw.logger?.debug?.('react clear error in finally:', e?.message || e) })
456
- chat.turnActive = false
457
- chat.abort = undefined
458
- gw.pending.delete(sessionId)
460
+ // Ownership-scoped cleanup (#211): if a newer turn already took over the
461
+ // chat state, this stale turn must leave it alone.
462
+ if (chat.turnOwner === turnOwner) {
463
+ chat.turnActive = false
464
+ chat.abort = undefined
465
+ gw.pending.delete(sessionId)
466
+ }
459
467
  }
460
468
  }
package/lib/gateway.js CHANGED
@@ -46,6 +46,8 @@ export class Gateway {
46
46
  this.config = config
47
47
  this.hooks = hooks
48
48
  this.chats = new Map()
49
+ // Single-flight chat creation (#215): key -> in-flight createChat promise
50
+ this.chatCreating = new Map()
49
51
  this.sessionToChat = new Map()
50
52
  this.sessionToThread = new Map()
51
53
  this.threadToSession = new Map()
@@ -174,14 +176,20 @@ export class Gateway {
174
176
  const turnInput = { ...input, attachments: [], inboundWasVoice: false }
175
177
  chat.turnActive = true
176
178
  try { chat.abort?.abort?.() } catch { /* safe best-effort abort */ }
177
- chat.abort = new AbortController()
178
- const run = chat.busy.then(() => this.runTurn(chat, turnInput, chat.abort.signal))
179
+ const ac = new AbortController()
180
+ // Per-turn ownership token (#211): state cleanup is ownership-scoped, so a
181
+ // stale turn can never clear a newer turn's abort/state.
182
+ chat.turnOwner = ac
183
+ chat.abort = ac
184
+ const run = chat.busy.then(() => this.runTurn(chat, turnInput, ac.signal))
179
185
  chat.busy = run.catch((e) => { void e })
180
186
  run.catch((err) => {
181
187
  const msg = err instanceof Error ? err.message : String(err)
182
188
  this.ctx.logger?.warn?.(`dsh-messenger-gateway: cron turn: ${msg}`)
183
- chat.turnActive = false
184
- chat.abort = undefined
189
+ if (chat.turnOwner === ac) {
190
+ chat.turnActive = false
191
+ chat.abort = undefined
192
+ }
185
193
  })
186
194
  return run
187
195
  }
@@ -556,8 +564,13 @@ export class Gateway {
556
564
  // Mark busy BEFORE yielding to the poll loop, otherwise steer/stop never see an active turn.
557
565
  chat.turnActive = true
558
566
  try { chat.abort?.abort?.() } catch { /* safe best-effort abort */ }
559
- chat.abort = new AbortController()
560
- const run = chat.busy.then(() => this.runTurn(chat, turnInput, chat.abort.signal))
567
+ const ac = new AbortController()
568
+ // Per-turn ownership token (#211): state cleanup is ownership-scoped, so
569
+ // a stale turn's finally/error handler can never clear a newer turn's
570
+ // abort/state.
571
+ chat.turnOwner = ac
572
+ chat.abort = ac
573
+ const run = chat.busy.then(() => this.runTurn(chat, turnInput, ac.signal))
561
574
  chat.busy = run.catch((e) => { void e })
562
575
  // Do NOT await: Telegram poll is sequential; awaiting blocked steer and /stop until the turn finished.
563
576
  run.catch((err) => {
@@ -570,8 +583,10 @@ export class Gateway {
570
583
  chatId: input.chatId,
571
584
  threadId: input.threadId,
572
585
  }).catch((e) => this.recordApiFailure('alert.turn', e))
573
- chat.turnActive = false
574
- chat.abort = undefined
586
+ if (chat.turnOwner === ac) {
587
+ chat.turnActive = false
588
+ chat.abort = undefined
589
+ }
575
590
  })
576
591
  } catch (err) {
577
592
  const msg = err instanceof Error ? err.message : String(err)
@@ -617,10 +632,21 @@ export class Gateway {
617
632
  async getOrCreateChat(key, input) {
618
633
  let chat = this.chats.get(key)
619
634
  if (!chat) {
620
- const threadKey = `${input.chatId}:${input.threadId || 0}`
621
- const existingSessionId = this.threadToSession.get(threadKey)
622
- chat = await this.createChat(key, input, existingSessionId)
623
- this.chats.set(key, chat)
635
+ // Single-flight (#215): webhook updates and media-group flushes can hit
636
+ // the same key concurrently; without a shared in-flight promise each
637
+ // caller creates its own session and the loser is never disposed.
638
+ let flight = this.chatCreating.get(key)
639
+ if (!flight) {
640
+ flight = (async () => {
641
+ const threadKey = `${input.chatId}:${input.threadId || 0}`
642
+ const existingSessionId = this.threadToSession.get(threadKey)
643
+ const created = await this.createChat(key, input, existingSessionId)
644
+ this.chats.set(key, created)
645
+ return created
646
+ })()
647
+ this.chatCreating.set(key, flight)
648
+ }
649
+ chat = await flight
624
650
  }
625
651
  chat.lastUsed = Date.now()
626
652
  chat.target = { platform: input.platform, chatId: input.chatId, threadId: input.threadId || 0 }
package/lib/index.js CHANGED
File without changes
File without changes
package/lib/proxy.js CHANGED
File without changes
package/lib/updater.js CHANGED
File without changes
package/lib/webapp.js CHANGED
@@ -94,6 +94,22 @@ export function validateTelegramWebAppData(rawInitData, botToken, maxAgeSec = 36
94
94
  }
95
95
  }
96
96
 
97
+ // Defense-in-depth for the dashboard innerHTML path (#210): SVG markup is
98
+ // normally generated by our own renderer with escaped text nodes, but anything
99
+ // entering the store is stripped of script blocks, event-handler attributes
100
+ // and javascript: URLs so a future non-escaping source cannot produce stored
101
+ // XSS in the privileged Mini App context.
102
+
103
+ /** @param {any} svg */
104
+ function sanitizeSvgMarkup(svg) {
105
+ let s = String(svg || '')
106
+ s = s.replace(/<script[\s\S]*?<\/script\s*>/gi, '')
107
+ s = s.replace(/<script[^>]*>/gi, '')
108
+ s = s.replace(/\son[a-z]+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi, '')
109
+ s = s.replace(/(href|xlink:href)\s*=\s*(?:"[^"]*javascript:[^"]*"|'[^']*javascript:[^']*'|[^\s>]*javascript:[^\s>]*)/gi, '$1="#"')
110
+ return s
111
+ }
112
+
97
113
  /**
98
114
  * Creates in-memory ring buffer store for artifacts and diagrams
99
115
  * @param {number} [maxItems=100]
@@ -109,7 +125,7 @@ export function createArtifactStore(maxItems = 100) {
109
125
  title: String(artifact.title || 'Artifact'),
110
126
  type: String(artifact.type || 'diagram'),
111
127
  code: String(artifact.code || ''),
112
- svg: String(artifact.svg || ''),
128
+ svg: sanitizeSvgMarkup(artifact.svg),
113
129
  createdAt: Number(artifact.createdAt) || Date.now(),
114
130
  chatId: artifact.chatId != null ? String(artifact.chatId) : null,
115
131
  }
@@ -479,7 +495,12 @@ export function createWebAppHtml(options = {}) {
479
495
  renderArtifacts(currentArtifacts)
480
496
  checkDeepLink()
481
497
  } catch (e) {
482
- document.getElementById('artifacts-list').innerHTML = '<div class="empty-state">Failed to load artifacts: ' + e.message + '</div>'
498
+ const el = document.getElementById('artifacts-list')
499
+ el.innerHTML = ''
500
+ const div = document.createElement('div')
501
+ div.className = 'empty-state'
502
+ div.textContent = 'Failed to load artifacts: ' + e.message
503
+ el.appendChild(div)
483
504
  }
484
505
  }
485
506
 
@@ -565,9 +586,9 @@ export function createWebAppHtml(options = {}) {
565
586
  document.getElementById('stat-sent').textContent = s.stats && s.stats.sent || 0
566
587
  document.getElementById('stat-chats').textContent = s.activeChats || 0
567
588
  document.getElementById('stat-outbox').textContent = s.outbox && s.outbox.pending || 0
568
- document.getElementById('tg-details').innerHTML = 'Bot: @' + (s.botUsername || 'none') + ' | Transport: ' + (s.config && s.config.telegram && s.config.telegram.transport || 'poll')
569
- document.getElementById('adapters-list').innerHTML = (s.adapters || []).map(a => '• <b>' + a + '</b>: active').join('<br>') || 'None'
570
- document.getElementById('model-details').innerHTML = 'Model: <b>' + (s.config && s.config.agent && s.config.agent.model || 'default') + '</b><br>CWD: <code>' + (s.config && s.config.agent && s.config.agent.effectiveCwd || 'standard') + '</code>'
589
+ document.getElementById('tg-details').textContent = 'Bot: @' + (s.botUsername || 'none') + ' | Transport: ' + (s.config && s.config.telegram && s.config.telegram.transport || 'poll')
590
+ document.getElementById('adapters-list').textContent = (s.adapters || []).map(a => '• ' + a + ': active').join('\n') || 'None'
591
+ document.getElementById('model-details').textContent = 'Model: ' + (s.config && s.config.agent && s.config.agent.model || 'default') + ' | CWD: ' + (s.config && s.config.agent && s.config.agent.effectiveCwd || 'standard')
571
592
  } catch (_e) {
572
593
  // ignore status fetch error
573
594
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goodandready/dsh-messenger-gateway",
3
- "version": "0.4.39",
3
+ "version": "0.4.41",
4
4
  "description": "Telegram messenger bridge for DeepSeek Harness: sessions, steer, homes, inline asks, notify bridge, and optional TTS voice notes.",
5
5
  "license": "MIT",
6
6
  "type": "module",