@goodandready/dsh-messenger-gateway 0.4.36 → 0.4.38
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 +3 -0
- package/lib/client.js +0 -0
- package/lib/config.js +0 -0
- package/lib/documents.js +0 -0
- package/lib/gateway-turn.js +27 -6
- package/lib/index.js +0 -0
- package/lib/pairing.js +2 -2
- package/lib/storage-atomic.js +11 -5
- package/lib/voice-prefs.js +2 -2
- package/lib/webapp.js +32 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -149,6 +149,9 @@ In addition to Telegram, outbound messages can be dispatched to Discord and Slac
|
|
|
149
149
|
- `telegram.proxyUrl` is a secret: the settings API returns only a masked value and an empty field keeps the configured proxy
|
|
150
150
|
- Inbound archives are extraction-capped (per-entry and per-archive budget verified against actual bytes — a lying ZIP header cannot balloon the heap)
|
|
151
151
|
- Mini App artifacts are chat-scoped: a Telegram user sees only artifacts produced in their own chat; allowlisted admins see all
|
|
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
|
+
- 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
|
+
- All state files (pairing, outbox, voice prefs) are written with `0600` permissions inside `0700` directories
|
|
152
155
|
- `sessionList`: `true` (default) — enables `/sessions` and `/resume` commands in chat
|
|
153
156
|
- `sessionTitleFromChat`: `false` (default) — name each new session after its originating chat (`TG · @user`, `TG · Group › topic N`) instead of generic generated titles
|
|
154
157
|
- `voiceMode`: `mirror` / `always` / `off` — when to speak replies (also `/voice`)
|
package/lib/client.js
CHANGED
|
File without changes
|
package/lib/config.js
CHANGED
|
File without changes
|
package/lib/documents.js
CHANGED
|
File without changes
|
package/lib/gateway-turn.js
CHANGED
|
@@ -85,23 +85,44 @@ export function formatApprovalCard(req, locale = "en") {
|
|
|
85
85
|
return t("ask.confirm_title", { tool: escapeHtml(tool), reason: reason + detail }, locale, { escape: false })
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
// Read-only allowlist for the "mutations" sensitivity (#203): everything NOT
|
|
89
|
+
// matching this allowlist (unknown tools, MCP tools, network and send tools)
|
|
90
|
+
// requires explicit user confirmation. Anything that mutates, sends, or
|
|
91
|
+
// executes can never be in the safe set regardless of its name.
|
|
92
|
+
const SAFE_READ_ONLY_TOOLS = new Set([
|
|
93
|
+
"read_file", "read_many_files", "list_files", "list_dir",
|
|
94
|
+
"glob", "grep", "doc_search", "view", "show",
|
|
95
|
+
])
|
|
96
|
+
const SAFE_READ_ONLY_PREFIX = /^(read|list|get|view|show|describe)[_-]/i
|
|
97
|
+
const UNSAFE_HINT = /(write|replace|edit|delete|remove|create|patch|send|post|put|exec|run|deploy|install|apply|curl|wget|fetch|http|schedule|email)/i
|
|
98
|
+
|
|
99
|
+
/** True when a tool is recognizably read-only and safe to run unconfirmed. */
|
|
100
|
+
export function isSafeReadOnlyTool(toolName) {
|
|
101
|
+
if (!toolName) return false
|
|
102
|
+
const t = String(toolName)
|
|
103
|
+
if (UNSAFE_HINT.test(t)) return false
|
|
104
|
+
return SAFE_READ_ONLY_TOOLS.has(t) || SAFE_READ_ONLY_PREFIX.test(t)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function answerApproval(gw, chatKeyValue, req, _next) {
|
|
89
108
|
try {
|
|
90
109
|
const tg = gw.tg ? gw.tg() : (gw.config?.telegram || {})
|
|
91
110
|
if (tg.approvalsEnabled === false || tg.approvalsSensitivity === "disabled") {
|
|
92
111
|
return "allowed-once"
|
|
93
112
|
}
|
|
94
113
|
const tool = req.toolName || "tool"
|
|
95
|
-
const sensitivity = tg.approvalsSensitivity || "mutations"
|
|
96
|
-
if (sensitivity === "mutations" && !isSensitiveTool(tool)) {
|
|
97
|
-
return "allowed-once"
|
|
98
|
-
}
|
|
99
114
|
|
|
115
|
+
// Fail-closed (#203): without a messenger target we cannot ask the user,
|
|
116
|
+
// so an unconfirmed action is refused instead of silently executed.
|
|
100
117
|
const chat = gw.chats.get(chatKeyValue)
|
|
101
|
-
if (!chat?.target) return
|
|
118
|
+
if (!chat?.target) return "rejected"
|
|
102
119
|
if (chat.sessionAllowlist?.has(tool)) {
|
|
103
120
|
return "allowed-once"
|
|
104
121
|
}
|
|
122
|
+
const sensitivity = tg.approvalsSensitivity || "mutations"
|
|
123
|
+
if (sensitivity === "mutations" && isSafeReadOnlyTool(tool)) {
|
|
124
|
+
return "allowed-once"
|
|
125
|
+
}
|
|
105
126
|
const locale = gw.resolveLocale ? gw.resolveLocale(chat.target) : "en"
|
|
106
127
|
const text = formatApprovalCard(req, locale)
|
|
107
128
|
const buttons = [
|
package/lib/index.js
CHANGED
|
File without changes
|
package/lib/pairing.js
CHANGED
|
@@ -13,10 +13,10 @@ export function generatePairingCode(length = 8) {
|
|
|
13
13
|
|
|
14
14
|
function writeJsonAtomicSync(filePath, data) {
|
|
15
15
|
const dir = dirname(filePath)
|
|
16
|
-
mkdirSync(dir, { recursive: true })
|
|
16
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 })
|
|
17
17
|
const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
|
|
18
18
|
try {
|
|
19
|
-
writeFileSync(tmpPath, JSON.stringify(data, null, 2), 'utf8')
|
|
19
|
+
writeFileSync(tmpPath, JSON.stringify(data, null, 2), { encoding: 'utf8', mode: 0o600 })
|
|
20
20
|
renameSync(tmpPath, filePath)
|
|
21
21
|
} catch (err) {
|
|
22
22
|
try { unlinkSync(tmpPath) } catch { /* safe best-effort temp file cleanup */ }
|
package/lib/storage-atomic.js
CHANGED
|
@@ -3,17 +3,23 @@ import { writeFileSync, renameSync, mkdirSync, unlinkSync } from 'node:fs'
|
|
|
3
3
|
import { dirname } from 'node:path'
|
|
4
4
|
import { randomUUID } from 'node:crypto'
|
|
5
5
|
|
|
6
|
+
// State files carry message texts, approved user ids and pairing data: they
|
|
7
|
+
// must not be world-readable (#204). Directories are created 0o700 so the
|
|
8
|
+
// contents stay private even when the parent is shared.
|
|
9
|
+
const FILE_MODE = 0o600
|
|
10
|
+
const DIR_MODE = 0o700
|
|
11
|
+
|
|
6
12
|
/**
|
|
7
13
|
* Write JSON atomically via temporary file and rename.
|
|
8
14
|
* Guarantees that readers never observe partially written files.
|
|
9
15
|
*/
|
|
10
16
|
export async function writeJsonAtomic(filePath, data) {
|
|
11
17
|
const dir = dirname(filePath)
|
|
12
|
-
await mkdir(dir, { recursive: true })
|
|
18
|
+
await mkdir(dir, { recursive: true, mode: DIR_MODE })
|
|
13
19
|
const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
|
|
14
20
|
const serialized = JSON.stringify(data, null, 2)
|
|
15
21
|
try {
|
|
16
|
-
await writeFile(tmpPath, serialized, 'utf8')
|
|
22
|
+
await writeFile(tmpPath, serialized, { encoding: 'utf8', mode: FILE_MODE })
|
|
17
23
|
await rename(tmpPath, filePath)
|
|
18
24
|
} catch (err) {
|
|
19
25
|
await unlink(tmpPath).catch((e) => { void e })
|
|
@@ -26,13 +32,13 @@ export async function writeJsonAtomic(filePath, data) {
|
|
|
26
32
|
*/
|
|
27
33
|
export function writeJsonAtomicSync(filePath, data) {
|
|
28
34
|
const dir = dirname(filePath)
|
|
29
|
-
mkdirSync(dir, { recursive: true })
|
|
35
|
+
mkdirSync(dir, { recursive: true, mode: DIR_MODE })
|
|
30
36
|
const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
|
|
31
37
|
try {
|
|
32
|
-
writeFileSync(tmpPath, JSON.stringify(data, null, 2), 'utf8')
|
|
38
|
+
writeFileSync(tmpPath, JSON.stringify(data, null, 2), { encoding: 'utf8', mode: FILE_MODE })
|
|
33
39
|
renameSync(tmpPath, filePath)
|
|
34
40
|
} catch (err) {
|
|
35
41
|
try { unlinkSync(tmpPath) } catch { /* ignore */ }
|
|
36
42
|
throw err
|
|
37
43
|
}
|
|
38
|
-
}
|
|
44
|
+
}
|
package/lib/voice-prefs.js
CHANGED
|
@@ -4,10 +4,10 @@ import { randomUUID } from "node:crypto"
|
|
|
4
4
|
|
|
5
5
|
function writeJsonAtomicSync(filePath, data) {
|
|
6
6
|
const dir = dirname(filePath)
|
|
7
|
-
mkdirSync(dir, { recursive: true })
|
|
7
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 })
|
|
8
8
|
const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
|
|
9
9
|
try {
|
|
10
|
-
writeFileSync(tmpPath, JSON.stringify(data, null, 2), "utf8")
|
|
10
|
+
writeFileSync(tmpPath, JSON.stringify(data, null, 2), { encoding: "utf8", mode: 0o600 })
|
|
11
11
|
renameSync(tmpPath, filePath)
|
|
12
12
|
} catch (err) {
|
|
13
13
|
try { unlinkSync(tmpPath) } catch { /* safe best-effort temp file cleanup */ }
|
package/lib/webapp.js
CHANGED
|
@@ -1,17 +1,34 @@
|
|
|
1
1
|
import { createHmac } from 'node:crypto'
|
|
2
2
|
import { timingSafeCompare, isTrustedSettingsRequest, readBody, writeJson } from './http.js'
|
|
3
3
|
|
|
4
|
+
// Replay guard (#202): a query_id can be redeemed only once. Telegram issues a
|
|
5
|
+
// fresh query_id on every Mini App opening, so legitimate re-openings are not
|
|
6
|
+
// affected; a stolen initData string becomes useless after the first use.
|
|
7
|
+
const seenQueryIds = new Map()
|
|
8
|
+
function checkAndRememberQueryId(queryId) {
|
|
9
|
+
if (!queryId) return null
|
|
10
|
+
const now = Date.now()
|
|
11
|
+
for (const [k, ts] of seenQueryIds) {
|
|
12
|
+
if (now - ts > 26 * 3600_000) seenQueryIds.delete(k)
|
|
13
|
+
}
|
|
14
|
+
if (seenQueryIds.has(queryId)) return 'replayed_query_id'
|
|
15
|
+
seenQueryIds.set(queryId, now)
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
|
|
4
19
|
/**
|
|
5
20
|
* Validates Telegram WebApp initData string using HMAC-SHA256 according to Telegram specification:
|
|
6
21
|
* https://core.telegram.org/bots/webapps#validating-data-received-via-the-mini-app
|
|
7
22
|
*
|
|
23
|
+
* Hardened (#202): auth_date is REQUIRED, the default acceptance window is
|
|
24
|
+
* 1 hour, and a query_id can be redeemed only once.
|
|
25
|
+
*
|
|
8
26
|
* @param {string} rawInitData - URL-encoded query string received from Telegram.WebApp.initData
|
|
9
27
|
* @param {string} botToken - Telegram Bot Token
|
|
10
|
-
* @param {number} [maxAgeSec=
|
|
11
|
-
* @returns {{ ok: boolean, user?: object, authDate?: number, queryId?: string, chatType?: string, error?: string }}
|
|
28
|
+
* @param {number} [maxAgeSec=3600] - Maximum allowed age of initData in seconds (default 1h)
|
|
29
|
+
* @returns {{ ok: boolean, user?: object, authDate?: number, queryId?: string, chatType?: string, chatInstance?: string, data?: Record<string, string>, error?: string }}
|
|
12
30
|
*/
|
|
13
|
-
|
|
14
|
-
export function validateTelegramWebAppData(rawInitData, botToken, maxAgeSec = 86400) {
|
|
31
|
+
export function validateTelegramWebAppData(rawInitData, botToken, maxAgeSec = 3600) {
|
|
15
32
|
if (!rawInitData || typeof rawInitData !== 'string') {
|
|
16
33
|
return { ok: false, error: 'empty_init_data' }
|
|
17
34
|
}
|
|
@@ -38,13 +55,21 @@ export function validateTelegramWebAppData(rawInitData, botToken, maxAgeSec = 86
|
|
|
38
55
|
|
|
39
56
|
const authDateStr = params.get('auth_date')
|
|
40
57
|
const authDate = Number(authDateStr) || 0
|
|
41
|
-
if (authDate
|
|
58
|
+
if (!authDate) {
|
|
59
|
+
return { ok: false, error: 'missing_auth_date' }
|
|
60
|
+
}
|
|
61
|
+
if (maxAgeSec > 0) {
|
|
42
62
|
const nowSec = Math.floor(Date.now() / 1000)
|
|
43
63
|
if (Math.abs(nowSec - authDate) > maxAgeSec) {
|
|
44
64
|
return { ok: false, error: 'expired', authDate }
|
|
45
65
|
}
|
|
46
66
|
}
|
|
47
67
|
|
|
68
|
+
const replayError = checkAndRememberQueryId(params.get('query_id'))
|
|
69
|
+
if (replayError) {
|
|
70
|
+
return { ok: false, error: replayError }
|
|
71
|
+
}
|
|
72
|
+
|
|
48
73
|
let user = null
|
|
49
74
|
const userStr = params.get('user')
|
|
50
75
|
if (userStr) {
|
|
@@ -614,9 +639,9 @@ export function createWebAppHandler(options = {}) {
|
|
|
614
639
|
initData = authHeader.slice(4).trim()
|
|
615
640
|
} else if (req.headers['x-telegram-init-data']) {
|
|
616
641
|
initData = String(req.headers['x-telegram-init-data']).trim()
|
|
617
|
-
} else if (url.searchParams.get('initData')) {
|
|
618
|
-
initData = url.searchParams.get('initData')
|
|
619
642
|
}
|
|
643
|
+
// initData in the URL query is no longer accepted (#202): it leaks into
|
|
644
|
+
// browser history, Referer headers and access logs.
|
|
620
645
|
|
|
621
646
|
if (initData) {
|
|
622
647
|
const val = validateTelegramWebAppData(initData, botToken)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodandready/dsh-messenger-gateway",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.38",
|
|
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",
|