@goodandready/dsh-messenger-gateway 0.4.17 → 0.4.19
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/lib/client.js +1 -1
- package/lib/http.js +178 -24
- package/lib/index.js +8 -7
- package/lib/updater.js +0 -1
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -612,7 +612,7 @@ window.__ModuleLoader__.load({
|
|
|
612
612
|
|
|
613
613
|
const loadStatus = React.useCallback(async () => {
|
|
614
614
|
try {
|
|
615
|
-
const res = await fetch(`${ROUTE_PREFIX}/status`, { cache: 'no-store', signal: timeoutSignal(10000) })
|
|
615
|
+
const res = await fetch(`${ROUTE_PREFIX}/status`, { credentials: 'same-origin', cache: 'no-store', signal: timeoutSignal(10000) })
|
|
616
616
|
const data = await res.json().catch(() => ({}))
|
|
617
617
|
if (res.ok && data.ok) {
|
|
618
618
|
setStatus(data)
|
package/lib/http.js
CHANGED
|
@@ -36,14 +36,68 @@ export function isLoopback(value) {
|
|
|
36
36
|
|| address.startsWith('::ffff:127.')
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
function isPrivateIPv4(address) {
|
|
40
|
+
const parts = address.split('.')
|
|
41
|
+
if (parts.length !== 4) return false
|
|
42
|
+
for (const p of parts) {
|
|
43
|
+
if (!/^\d{1,3}$/.test(p)) return false
|
|
44
|
+
const n = Number(p)
|
|
45
|
+
if (n < 0 || n > 255) return false
|
|
46
|
+
}
|
|
47
|
+
const o0 = Number(parts[0])
|
|
48
|
+
const o1 = Number(parts[1])
|
|
49
|
+
if (o0 === 10) return true
|
|
50
|
+
if (o0 === 192 && o1 === 168) return true
|
|
51
|
+
if (o0 === 172 && o1 >= 16 && o1 <= 31) return true
|
|
52
|
+
if (o0 === 169 && o1 === 254) return true
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function isPrivateAddress(value) {
|
|
57
|
+
let address = String(value || '').toLowerCase().replace(/^\[|\]$/g, '')
|
|
58
|
+
if (address.startsWith('::ffff:')) {
|
|
59
|
+
address = address.slice('::ffff:'.length)
|
|
60
|
+
}
|
|
61
|
+
if (isLoopback(address)) return true
|
|
62
|
+
if (isPrivateIPv4(address)) return true
|
|
63
|
+
|
|
64
|
+
if (address.includes(':')) {
|
|
65
|
+
const firstHextet = address.split(':')[0]
|
|
66
|
+
const num = Number.parseInt(firstHextet, 16)
|
|
67
|
+
if (!Number.isNaN(num)) {
|
|
68
|
+
if ((num & 0xfe00) === 0xfc00) return true // fc00::/7 ULA (RFC 4193)
|
|
69
|
+
if ((num & 0xffc0) === 0xfe80) return true // fe80::/10 Link-Local (RFC 4291)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return false
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const isLoopbackOrLan = isPrivateAddress
|
|
76
|
+
|
|
77
|
+
export function getConnection(ctx) {
|
|
78
|
+
try {
|
|
79
|
+
const fromCtx = ctx?.reflect?.get
|
|
80
|
+
? (ctx.reflect.get('connection') || ctx?.connection)
|
|
81
|
+
: (ctx?.get ? (ctx.get('connection') || ctx?.connection) : Reflect.get(ctx || {}, 'connection'))
|
|
82
|
+
if (fromCtx) return fromCtx
|
|
83
|
+
if (ctx?.root) {
|
|
84
|
+
return getConnection(ctx.root)
|
|
85
|
+
}
|
|
86
|
+
return undefined
|
|
87
|
+
} catch {
|
|
88
|
+
return undefined
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function isTrustedSettingsRequest(req, expectedSecret, ctx = null) {
|
|
40
93
|
const remote = req?.socket?.remoteAddress
|
|
41
|
-
const host = String(req?.headers?.host || '')
|
|
94
|
+
const host = String(req?.headers?.host || req?.headers?.['x-forwarded-host'] || '')
|
|
42
95
|
if (!host) return false
|
|
43
96
|
|
|
44
97
|
const site = String(req?.headers?.['sec-fetch-site'] || '').toLowerCase()
|
|
45
98
|
if (site === 'cross-site') return false
|
|
46
99
|
|
|
100
|
+
// 1. Explicit secret authentication (Bearer token or x-webhook-secret header)
|
|
47
101
|
const secret = String(expectedSecret || '').trim()
|
|
48
102
|
if (secret) {
|
|
49
103
|
const authHeader = req?.headers?.authorization || ''
|
|
@@ -55,46 +109,112 @@ export function isTrustedSettingsRequest(req, expectedSecret) {
|
|
|
55
109
|
}
|
|
56
110
|
}
|
|
57
111
|
|
|
58
|
-
|
|
112
|
+
const hostName = host.split(':')[0].toLowerCase().replace(/^\[|\]$/g, '')
|
|
113
|
+
|
|
114
|
+
// If host is loopback (localhost/127.0.0.1/::1), remote address must also be loopback
|
|
115
|
+
if (isLoopback(hostName) && remote !== undefined && !isLoopback(remote)) {
|
|
59
116
|
return false
|
|
60
117
|
}
|
|
61
118
|
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
119
|
+
// 2. Local loopback remote address (CLI, tests, local host processes)
|
|
120
|
+
if (remote !== undefined && isLoopback(remote)) {
|
|
121
|
+
const origin = String(req?.headers?.origin || '')
|
|
122
|
+
if (origin) {
|
|
123
|
+
try {
|
|
124
|
+
const url = new URL(origin)
|
|
125
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
|
|
126
|
+
if (url.host.toLowerCase() !== host.toLowerCase() && !isLoopback(url.hostname)) return false
|
|
127
|
+
} catch {
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const referer = String(req?.headers?.referer || '')
|
|
132
|
+
if (referer) {
|
|
133
|
+
try {
|
|
134
|
+
const url = new URL(referer)
|
|
135
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
|
|
136
|
+
if (url.host.toLowerCase() !== host.toLowerCase() && !isLoopback(url.hostname)) return false
|
|
137
|
+
} catch {
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return true
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 3. Mock/unit-test requests without socket where host is loopback and browser origin/referer/site is verified
|
|
145
|
+
if (remote === undefined && isLoopback(hostName)) {
|
|
146
|
+
const origin = String(req?.headers?.origin || '')
|
|
147
|
+
if (origin) {
|
|
148
|
+
try {
|
|
149
|
+
const url = new URL(origin)
|
|
150
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
|
|
151
|
+
return url.host.toLowerCase() === host.toLowerCase() || isLoopback(url.hostname)
|
|
152
|
+
} catch {
|
|
153
|
+
return false
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const referer = String(req?.headers?.referer || '')
|
|
157
|
+
if (referer) {
|
|
158
|
+
try {
|
|
159
|
+
const url = new URL(referer)
|
|
160
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
|
|
161
|
+
return url.host.toLowerCase() === host.toLowerCase() || isLoopback(url.hostname)
|
|
162
|
+
} catch {
|
|
163
|
+
return false
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (site === 'same-origin') {
|
|
167
|
+
return true
|
|
70
168
|
}
|
|
169
|
+
return false
|
|
71
170
|
}
|
|
72
171
|
|
|
73
|
-
|
|
74
|
-
|
|
172
|
+
// 4. DSH Connection Service session authentication (DSH Web UI browser session) (#157)
|
|
173
|
+
const conn = getConnection(ctx) || req?.connectionService || req?.connection
|
|
174
|
+
if (conn && typeof conn.requestRejection === 'function') {
|
|
175
|
+
let rejection
|
|
75
176
|
try {
|
|
76
|
-
|
|
77
|
-
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
|
|
78
|
-
return url.host.toLowerCase() === host.toLowerCase()
|
|
177
|
+
rejection = conn.requestRejection(req)
|
|
79
178
|
} catch {
|
|
80
179
|
return false
|
|
81
180
|
}
|
|
181
|
+
if (rejection === undefined) {
|
|
182
|
+
// DSH peer session is authenticated! Enforce same-origin CSRF protection
|
|
183
|
+
const origin = String(req?.headers?.origin || '')
|
|
184
|
+
if (origin) {
|
|
185
|
+
try {
|
|
186
|
+
const url = new URL(origin)
|
|
187
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
|
|
188
|
+
if (url.host.toLowerCase() !== host.toLowerCase()) return false
|
|
189
|
+
} catch {
|
|
190
|
+
return false
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const referer = String(req?.headers?.referer || '')
|
|
194
|
+
if (referer) {
|
|
195
|
+
try {
|
|
196
|
+
const url = new URL(referer)
|
|
197
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
|
|
198
|
+
if (url.host.toLowerCase() !== host.toLowerCase()) return false
|
|
199
|
+
} catch {
|
|
200
|
+
return false
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return true
|
|
204
|
+
}
|
|
205
|
+
return false
|
|
82
206
|
}
|
|
83
207
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return isLoopback(remote)
|
|
208
|
+
// 5. Remote caller (LAN RFC 1918 or WAN) without secret and without DSH connection session:
|
|
209
|
+
// LAN is NOT authentication! Fail-closed (#157).
|
|
210
|
+
return false
|
|
89
211
|
}
|
|
90
212
|
|
|
91
|
-
export function isAuthorizedMessengerRequest(req, webhooksConfig) {
|
|
213
|
+
export function isAuthorizedMessengerRequest(req, webhooksConfig, ctx = null) {
|
|
92
214
|
const site = String(req?.headers?.['sec-fetch-site'] || '').toLowerCase()
|
|
93
215
|
if (site === 'cross-site') return false
|
|
94
216
|
|
|
95
217
|
const secret = String(webhooksConfig?.secret || '').trim()
|
|
96
|
-
if (isTrustedSettingsRequest(req, secret)) return true
|
|
97
|
-
if (isLoopback(req?.socket?.remoteAddress)) return true
|
|
98
218
|
if (secret) {
|
|
99
219
|
const authHeader = req?.headers?.authorization || ''
|
|
100
220
|
const bearer = authHeader.startsWith('Bearer ') ? authHeader.slice(7).trim() : ''
|
|
@@ -105,6 +225,40 @@ export function isAuthorizedMessengerRequest(req, webhooksConfig) {
|
|
|
105
225
|
}
|
|
106
226
|
}
|
|
107
227
|
|
|
228
|
+
const host = String(req?.headers?.host || req?.headers?.['x-forwarded-host'] || '')
|
|
229
|
+
const hostName = host.split(':')[0].toLowerCase().replace(/^\[|\]$/g, '')
|
|
230
|
+
const remote = req?.socket?.remoteAddress
|
|
231
|
+
|
|
232
|
+
if (isLoopback(hostName) && remote !== undefined && !isLoopback(remote)) {
|
|
233
|
+
return false
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (remote !== undefined && isLoopback(remote)) {
|
|
237
|
+
return true
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (remote === undefined && isLoopback(hostName)) {
|
|
241
|
+
const origin = String(req?.headers?.origin || '')
|
|
242
|
+
if (origin) {
|
|
243
|
+
try {
|
|
244
|
+
const url = new URL(origin)
|
|
245
|
+
return url.host.toLowerCase() === host.toLowerCase() || isLoopback(url.hostname)
|
|
246
|
+
} catch {
|
|
247
|
+
return false
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return false
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const conn = getConnection(ctx) || req?.connectionService || req?.connection
|
|
254
|
+
if (conn && typeof conn.requestRejection === 'function') {
|
|
255
|
+
try {
|
|
256
|
+
if (conn.requestRejection(req) === undefined) return true
|
|
257
|
+
} catch {
|
|
258
|
+
return false
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
108
262
|
return false
|
|
109
263
|
}
|
|
110
264
|
|
package/lib/index.js
CHANGED
|
@@ -124,7 +124,7 @@ function registerMessengerRoute(ctx, getGw, path, action, source) {
|
|
|
124
124
|
kind: 'exact', path,
|
|
125
125
|
handler: async (req, res) => {
|
|
126
126
|
if (req.method !== 'POST') return writeJson(res, 405, { ok: false, error: 'POST only' })
|
|
127
|
-
if (!isAuthorizedMessengerRequest(req, source?.()?.webhooks)) {
|
|
127
|
+
if (!isAuthorizedMessengerRequest(req, source?.()?.webhooks, ctx)) {
|
|
128
128
|
return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
129
129
|
}
|
|
130
130
|
let payload
|
|
@@ -458,6 +458,7 @@ export function apply(ctx, config) {
|
|
|
458
458
|
kind: 'exact', path: '/dsh-messenger-gateway/status',
|
|
459
459
|
handler: async (req, res) => {
|
|
460
460
|
if (req.method !== 'GET') return writeJson(res, 405, { ok: false, error: 'GET only' })
|
|
461
|
+
if (!isTrustedSettingsRequest(req, source().webhooks?.secret, ctx)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
461
462
|
const gw = getGw()
|
|
462
463
|
const botInfo = gw?.getBotInfo?.() || {}
|
|
463
464
|
const uptimeSec = gw?.stats?.startedAt ? Math.max(0, Math.round((Date.now() - gw.stats.startedAt) / 1000)) : 0
|
|
@@ -496,7 +497,7 @@ export function apply(ctx, config) {
|
|
|
496
497
|
kind: 'exact', path: '/dsh-messenger-gateway/smoke',
|
|
497
498
|
handler: async (req, res) => {
|
|
498
499
|
if (req.method !== 'POST') return writeJson(res, 405, { ok: false, error: 'POST only' })
|
|
499
|
-
if (!isTrustedSettingsRequest(req, source().webhooks?.secret)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
500
|
+
if (!isTrustedSettingsRequest(req, source().webhooks?.secret, ctx)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
500
501
|
const gw = getGw()
|
|
501
502
|
if (!gw) return writeJson(res, 503, { ok: false, error: 'gateway not running' })
|
|
502
503
|
const parsed = await readJsonSafe(req, res, 65536)
|
|
@@ -571,9 +572,9 @@ export function apply(ctx, config) {
|
|
|
571
572
|
ctx.effect(() => getWebServer().register({
|
|
572
573
|
kind: 'exact', path: '/dsh-messenger-gateway/config',
|
|
573
574
|
handler: async (req, res) => {
|
|
575
|
+
if (req.method !== 'GET' && req.method !== 'PUT') return writeJson(res, 405, { ok: false, error: 'GET or PUT' })
|
|
576
|
+
if (!isTrustedSettingsRequest(req, source().webhooks?.secret, ctx)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
574
577
|
if (req.method === 'GET') return writeJson(res, 200, { ok: true, config: publicConfig(source(), getGw(), ctx) })
|
|
575
|
-
if (req.method !== 'PUT') return writeJson(res, 405, { ok: false, error: 'GET or PUT' })
|
|
576
|
-
if (!isTrustedSettingsRequest(req, source().webhooks?.secret)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
577
578
|
if (!settingsApi?.isReady?.()) return writeJson(res, 503, { ok: false, error: 'settings not ready' })
|
|
578
579
|
const parsedConfig = await readJsonSafe(req, res, 1_048_576)
|
|
579
580
|
if (!parsedConfig.ok) return
|
|
@@ -615,7 +616,7 @@ export function apply(ctx, config) {
|
|
|
615
616
|
kind: 'exact', path: '/dsh-messenger-gateway/pairing',
|
|
616
617
|
handler: async (req, res) => {
|
|
617
618
|
if (req.method !== 'GET') return writeJson(res, 405, { ok: false, error: 'GET only' })
|
|
618
|
-
if (!isTrustedSettingsRequest(req, source().webhooks?.secret)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
619
|
+
if (!isTrustedSettingsRequest(req, source().webhooks?.secret, ctx)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
619
620
|
const gw = getGw()
|
|
620
621
|
if (!gw) return writeJson(res, 503, { ok: false, error: 'gateway not running' })
|
|
621
622
|
writeJson(res, 200, {
|
|
@@ -630,7 +631,7 @@ export function apply(ctx, config) {
|
|
|
630
631
|
kind: 'exact', path: '/dsh-messenger-gateway/pairing/approve',
|
|
631
632
|
handler: async (req, res) => {
|
|
632
633
|
if (req.method !== 'POST') return writeJson(res, 405, { ok: false, error: 'POST only' })
|
|
633
|
-
if (!isTrustedSettingsRequest(req, source().webhooks?.secret)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
634
|
+
if (!isTrustedSettingsRequest(req, source().webhooks?.secret, ctx)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
634
635
|
const gw = getGw()
|
|
635
636
|
if (!gw) return writeJson(res, 503, { ok: false, error: 'gateway not running' })
|
|
636
637
|
const parsed = await readJsonSafe(req, res, 65536)
|
|
@@ -644,7 +645,7 @@ export function apply(ctx, config) {
|
|
|
644
645
|
kind: 'exact', path: '/dsh-messenger-gateway/pairing/reject',
|
|
645
646
|
handler: async (req, res) => {
|
|
646
647
|
if (req.method !== 'POST') return writeJson(res, 405, { ok: false, error: 'POST only' })
|
|
647
|
-
if (!isTrustedSettingsRequest(req, source().webhooks?.secret)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
648
|
+
if (!isTrustedSettingsRequest(req, source().webhooks?.secret, ctx)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
648
649
|
const gw = getGw()
|
|
649
650
|
if (!gw) return writeJson(res, 503, { ok: false, error: 'gateway not running' })
|
|
650
651
|
const parsed = await readJsonSafe(req, res, 65536)
|
package/lib/updater.js
CHANGED
|
@@ -4,7 +4,6 @@ import { spawn } from 'node:child_process'
|
|
|
4
4
|
import { homedir } from 'node:os'
|
|
5
5
|
import { basename, dirname, isAbsolute, resolve } from 'node:path'
|
|
6
6
|
import { fileURLToPath } from 'node:url'
|
|
7
|
-
import { isTrustedSettingsRequest } from './http.js'
|
|
8
7
|
|
|
9
8
|
const UPDATE_HEADER = 'x-dsh-plugin-update'
|
|
10
9
|
const UPDATE_TIMEOUT_MS = 180_000
|
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.19",
|
|
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",
|