@goodandready/dsh-messenger-gateway 0.4.39 → 0.4.40

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 = {
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.40",
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",