@goodandready/dsh-messenger-gateway 0.4.18 → 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/http.js CHANGED
@@ -74,14 +74,30 @@ export function isPrivateAddress(value) {
74
74
 
75
75
  export const isLoopbackOrLan = isPrivateAddress
76
76
 
77
- export function isTrustedSettingsRequest(req, expectedSecret) {
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) {
78
93
  const remote = req?.socket?.remoteAddress
79
- const host = String(req?.headers?.host || '')
94
+ const host = String(req?.headers?.host || req?.headers?.['x-forwarded-host'] || '')
80
95
  if (!host) return false
81
96
 
82
97
  const site = String(req?.headers?.['sec-fetch-site'] || '').toLowerCase()
83
98
  if (site === 'cross-site') return false
84
99
 
100
+ // 1. Explicit secret authentication (Bearer token or x-webhook-secret header)
85
101
  const secret = String(expectedSecret || '').trim()
86
102
  if (secret) {
87
103
  const authHeader = req?.headers?.authorization || ''
@@ -93,49 +109,108 @@ export function isTrustedSettingsRequest(req, expectedSecret) {
93
109
  }
94
110
  }
95
111
 
96
- // Reject non-private / WAN requests without secret
97
- if (remote !== undefined && !isPrivateAddress(remote)) {
98
- return false
99
- }
112
+ const hostName = host.split(':')[0].toLowerCase().replace(/^\[|\]$/g, '')
100
113
 
101
114
  // If host is loopback (localhost/127.0.0.1/::1), remote address must also be loopback
102
- const hostName = host.split(':')[0].toLowerCase().replace(/^\[|\]$/g, '')
103
115
  if (isLoopback(hostName) && remote !== undefined && !isLoopback(remote)) {
104
116
  return false
105
117
  }
106
118
 
107
- const origin = String(req?.headers?.origin || '')
108
- if (origin) {
109
- try {
110
- const url = new URL(origin)
111
- if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
112
- return url.host.toLowerCase() === host.toLowerCase()
113
- } catch {
114
- return false
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
+ }
115
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
168
+ }
169
+ return false
116
170
  }
117
171
 
118
- const referer = String(req?.headers?.referer || '')
119
- if (referer) {
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
120
176
  try {
121
- const url = new URL(referer)
122
- if (url.protocol !== 'http:' && url.protocol !== 'https:') return false
123
- return url.host.toLowerCase() === host.toLowerCase()
177
+ rejection = conn.requestRejection(req)
124
178
  } catch {
125
179
  return false
126
180
  }
127
- }
128
-
129
- if (site === 'same-origin') {
130
- if (remote === undefined || isLoopback(remote)) {
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
+ }
131
203
  return true
132
204
  }
205
+ return false
133
206
  }
134
207
 
135
- 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
136
211
  }
137
212
 
138
- export function isAuthorizedMessengerRequest(req, webhooksConfig) {
213
+ export function isAuthorizedMessengerRequest(req, webhooksConfig, ctx = null) {
139
214
  const site = String(req?.headers?.['sec-fetch-site'] || '').toLowerCase()
140
215
  if (site === 'cross-site') return false
141
216
 
@@ -150,12 +225,41 @@ export function isAuthorizedMessengerRequest(req, webhooksConfig) {
150
225
  }
151
226
  }
152
227
 
228
+ const host = String(req?.headers?.host || req?.headers?.['x-forwarded-host'] || '')
229
+ const hostName = host.split(':')[0].toLowerCase().replace(/^\[|\]$/g, '')
153
230
  const remote = req?.socket?.remoteAddress
154
- if (remote !== undefined && !isLoopback(remote)) {
231
+
232
+ if (isLoopback(hostName) && remote !== undefined && !isLoopback(remote)) {
155
233
  return false
156
234
  }
157
235
 
158
- return isTrustedSettingsRequest(req, secret) || isLoopback(remote)
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
+
262
+ return false
159
263
  }
160
264
 
161
265
  export function timingSafeCompare(a, b) {
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,7 +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)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
461
+ if (!isTrustedSettingsRequest(req, source().webhooks?.secret, ctx)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
462
462
  const gw = getGw()
463
463
  const botInfo = gw?.getBotInfo?.() || {}
464
464
  const uptimeSec = gw?.stats?.startedAt ? Math.max(0, Math.round((Date.now() - gw.stats.startedAt) / 1000)) : 0
@@ -497,7 +497,7 @@ export function apply(ctx, config) {
497
497
  kind: 'exact', path: '/dsh-messenger-gateway/smoke',
498
498
  handler: async (req, res) => {
499
499
  if (req.method !== 'POST') return writeJson(res, 405, { ok: false, error: 'POST only' })
500
- 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' })
501
501
  const gw = getGw()
502
502
  if (!gw) return writeJson(res, 503, { ok: false, error: 'gateway not running' })
503
503
  const parsed = await readJsonSafe(req, res, 65536)
@@ -573,7 +573,7 @@ export function apply(ctx, config) {
573
573
  kind: 'exact', path: '/dsh-messenger-gateway/config',
574
574
  handler: async (req, res) => {
575
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)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
576
+ if (!isTrustedSettingsRequest(req, source().webhooks?.secret, ctx)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
577
577
  if (req.method === 'GET') return writeJson(res, 200, { ok: true, config: publicConfig(source(), getGw(), ctx) })
578
578
  if (!settingsApi?.isReady?.()) return writeJson(res, 503, { ok: false, error: 'settings not ready' })
579
579
  const parsedConfig = await readJsonSafe(req, res, 1_048_576)
@@ -616,7 +616,7 @@ export function apply(ctx, config) {
616
616
  kind: 'exact', path: '/dsh-messenger-gateway/pairing',
617
617
  handler: async (req, res) => {
618
618
  if (req.method !== 'GET') return writeJson(res, 405, { ok: false, error: 'GET only' })
619
- 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' })
620
620
  const gw = getGw()
621
621
  if (!gw) return writeJson(res, 503, { ok: false, error: 'gateway not running' })
622
622
  writeJson(res, 200, {
@@ -631,7 +631,7 @@ export function apply(ctx, config) {
631
631
  kind: 'exact', path: '/dsh-messenger-gateway/pairing/approve',
632
632
  handler: async (req, res) => {
633
633
  if (req.method !== 'POST') return writeJson(res, 405, { ok: false, error: 'POST only' })
634
- 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' })
635
635
  const gw = getGw()
636
636
  if (!gw) return writeJson(res, 503, { ok: false, error: 'gateway not running' })
637
637
  const parsed = await readJsonSafe(req, res, 65536)
@@ -645,7 +645,7 @@ export function apply(ctx, config) {
645
645
  kind: 'exact', path: '/dsh-messenger-gateway/pairing/reject',
646
646
  handler: async (req, res) => {
647
647
  if (req.method !== 'POST') return writeJson(res, 405, { ok: false, error: 'POST only' })
648
- 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' })
649
649
  const gw = getGw()
650
650
  if (!gw) return writeJson(res, 503, { ok: false, error: 'gateway not running' })
651
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.18",
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",